From c115676dd62c880c30857bb63014940e191458aa Mon Sep 17 00:00:00 2001 From: Alan Smithee Date: Tue, 20 Oct 2015 11:15:05 +0000 Subject: [PATCH] adds selector categories #2 --- src/core/selector-type.js | 34 ++++++++++++++++++++++++---------- test.css | 0 test.js | 4 ++++ 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 test.css create mode 100644 test.js diff --git a/src/core/selector-type.js b/src/core/selector-type.js index a8a30a1..315371f 100644 --- a/src/core/selector-type.js +++ b/src/core/selector-type.js @@ -21,20 +21,30 @@ export const SelectorType = { }; /** - * Regex used to identify what type a CSS selector is (Element/class/id/psuedo etc) + * Regex used to identify what category a CSS selector is (Element/class/id/psuedo etc) * * @author Alan Smithee */ -const Regex = { +const SelectorCategoryRegex = { + Type : /^[A-Za-z]+/, + Attribute : /^[A-Za-z]+\[*\]$/, +}; + +/** + * Regex used to identify what type a CSS selector is (Element.Class/Element[foo0] etc) + * + * @author Alan Smithee + */ +const SelectorTypeRegex = { None : '', Invalid : '', Universal : /\*/, - TypeCategory : /^[A-Za-z]+/, Type : /^[A-Za-z]+$/, TypeClass : /^[A-Za-z]+\.[A-Za-z]+$/, TypeId : /^[A-Za-z]+\#[A-Za-z]+$/, Class : /^\.[A-Za-z]+$/, - Id : /^\#[A-Za-z]+$/ + Id : /^\#[A-Za-z]+$/, + }; /** @@ -53,14 +63,18 @@ export default function getType(selector) { return SelectorType.Universal; } - if (Regex.TypeCategory.test(selector)) { - return Regex.TypeClass.test(selector) ? SelectorType.TypeClass : - Regex.TypeId.test(selector) ? SelectorType.TypeId : - Regex.Type.test(selector) ? SelectorType.Type : + if (SelectorCategoryRegex.Type.test(selector)) { + return SelectorTypeRegex.TypeClass.test(selector) ? SelectorType.TypeClass : + SelectorTypeRegex.TypeId.test(selector) ? SelectorType.TypeId : + SelectorTypeRegex.Type.test(selector) ? SelectorType.Type : SelectorType.Invalid; } - return Regex.Class.test(selector) ? SelectorType.Class : - Regex.Id.test(selector) ? SelectorType.Id : + if (SelectorCategoryRegex.Attribute.test(selector)) { + return SelectorType.Invalid; + } + + return SelectorTypeRegex.Class.test(selector) ? SelectorType.Class : + SelectorTypeRegex.Id.test(selector) ? SelectorType.Id : SelectorType.None; } \ No newline at end of file diff --git a/test.css b/test.css new file mode 100644 index 0000000..e69de29 diff --git a/test.js b/test.js new file mode 100644 index 0000000..99ab4a0 --- /dev/null +++ b/test.js @@ -0,0 +1,4 @@ +"use strict"; + +import objectify from './src/objectify'; +