Skip to content

Commit

Permalink
adds regex and tests for :root selector #2
Browse files Browse the repository at this point in the history
  • Loading branch information
GGAlanSmithee committed Nov 4, 2015
1 parent 2c407e1 commit 8c41872
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/core/selector-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const SelectorType = {
DescendantCombinator : 14,
ChildCombinator : 15,
AdjacentSiblingCombinator : 16,
GeneralSiblingCombinator : 17
GeneralSiblingCombinator : 17,
Root : 18, // E:root, :root
};

/**
Expand All @@ -45,19 +46,20 @@ const SelectorCategoryRegex = {
const SelectorTypeRegex = {
None : '',
Invalid : '',
Universal : /\*/, // *
Type : /^[A-Za-z]+$/, // E
TypeClass : /^[A-Za-z]+\.[A-Za-z]+$/, // E.Class
TypeId : /^[A-Za-z]+\#[A-Za-z]+$/, // E#Id
Class : /^\.[A-Za-z]+$/, // .Class
Id : /^\#[A-Za-z]+$/, // #Id
Attribute : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr]
AttributeExact : /^[A-Za-z]+\[[A-Za-z]+\=\"[A-Za-z]+\"\]$/, // E[attr=value]
AttributeExactHyphen : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr|=value]
AttributeList : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr~=value]
AttributePrefix : /^[A-Za-z]+\[[A-Za-z]+\^\=\"[A-Za-z]+\"\]$/, // E[attr^=value]
AttributeSuffix : /^[A-Za-z]+\[[A-Za-z]+\$\=\"[A-Za-z]+\"\]$/, // E[attr$=value]
AttributeContains : /^[A-Za-z]+\[[A-Za-z]+\*\=\"[A-Za-z]+\"\]$/ // E[attr*=value]
Universal : /\*/, // *
Type : /^[A-Za-z]+$/, // E
TypeClass : /^[A-Za-z]+\.[A-Za-z]+$/, // E.Class
TypeId : /^[A-Za-z]+\#[A-Za-z]+$/, // E#Id
Class : /^\.[A-Za-z]+$/, // .Class
Id : /^\#[A-Za-z]+$/, // #Id
Attribute : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr]
AttributeExact : /^[A-Za-z]+\[[A-Za-z]+\=\"[A-Za-z]+\"\]$/, // E[attr=value]
AttributeExactHyphen : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr|=value]
AttributeList : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr~=value]
AttributePrefix : /^[A-Za-z]+\[[A-Za-z]+\^\=\"[A-Za-z]+\"\]$/, // E[attr^=value]
AttributeSuffix : /^[A-Za-z]+\[[A-Za-z]+\$\=\"[A-Za-z]+\"\]$/, // E[attr$=value]
AttributeContains : /^[A-Za-z]+\[[A-Za-z]+\*\=\"[A-Za-z]+\"\]$/, // E[attr*=value]
Root : /(^|\s)[a-zA-Z]*:root(\s|$)/, // E:root, :root
};

/**
Expand All @@ -76,6 +78,10 @@ export default function getType(selector) {
return SelectorType.Universal;
}

if (SelectorTypeRegex.Root.test(selector)) {
return SelectorType.Root;
}

if (SelectorCategoryRegex.Attribute.test(selector)) {
return SelectorTypeRegex.AttributePrefix.test(selector) ? SelectorType.AttributePrefix :
SelectorTypeRegex.AttributeSuffix.test(selector) ? SelectorType.AttributeSuffix :
Expand Down
8 changes: 8 additions & 0 deletions test/get-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,13 @@ describe('getType(selector)', function() {
it('Element#Id selector (E#id)', () => {
expect(getType('E#div')).to.equal(SelectorType.TypeId);
});

it('Root selector (:root)', () => {
expect(getType(':root')).to.equal(SelectorType.Root);
});

it('Root selector (E:root)', () => {
expect(getType('div:root')).to.equal(SelectorType.Root);
});
});
});

0 comments on commit 8c41872

Please sign in to comment.