Skip to content

Commit

Permalink
adds selector categories #2
Browse files Browse the repository at this point in the history
  • Loading branch information
GGAlanSmithee committed Oct 20, 2015
1 parent 15a81bb commit c115676
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/core/selector-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]+$/,

};

/**
Expand All @@ -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;
}
Empty file added test.css
Empty file.
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

import objectify from './src/objectify';

0 comments on commit c115676

Please sign in to comment.