Skip to content

Commit

Permalink
changes selectors to take id and class selectors in to account in add…
Browse files Browse the repository at this point in the history
…ition to regular type selectors and updates tests #2
  • Loading branch information
GGAlanSmithee committed Nov 4, 2015
1 parent 8c41872 commit f97e348
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/core/selector-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const SelectorType = {
*/
const SelectorCategoryRegex = {
Type : /^[A-Za-z]+/,
Attribute : /^[A-Za-z]+\[.+?\]$/,
Attribute : /^(\#|\.)?[A-Za-z]+\[.+?\]$/,
};

/**
Expand All @@ -46,20 +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]
Root : /(^|\s)[a-zA-Z]*:root(\s|$)/, // E:root, :root
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 : /^(\#|\.)?[a-zA-Z]*:root/, // E:root, :root
};

/**
Expand Down
52 changes: 52 additions & 0 deletions test/get-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ describe('getType(selector)', function() {
expect(getType('div span')).to.equal(SelectorType.Invalid);
});

it('Invalid selector (E, E)', () => {
expect(getType('div, span')).to.equal(SelectorType.Invalid);
});

it('Invalid selector (E.class.class)', () => {
expect(getType('div.class1.class2')).to.equal(SelectorType.Invalid);
});
Expand All @@ -40,22 +44,62 @@ describe('getType(selector)', function() {
expect(getType('div[test]')).to.equal(SelectorType.Attribute);
});

it('Attribute selector (.class[foo])', () => {
expect(getType('.myClass[test]')).to.equal(SelectorType.Attribute);
});

it('Attribute selector (#id[foo])', () => {
expect(getType('#myId[test]')).to.equal(SelectorType.Attribute);
});

it('Attribute exact selector (E[foo="bar"])', () => {
expect(getType('a[href="a"]')).to.equal(SelectorType.AttributeExact);
});

it('Attribute exact selector (.class[foo="bar"])', () => {
expect(getType('.myClass[href="a"]')).to.equal(SelectorType.AttributeExact);
});

it('Attribute exact selector (#id[foo="bar"])', () => {
expect(getType('#myId[href="a"]')).to.equal(SelectorType.AttributeExact);
});

it('Attribute prefix selector (E[foo^="bar"])', () => {
expect(getType('a[href^="a"]')).to.equal(SelectorType.AttributePrefix);
});

it('Attribute prefix selector (.class[foo^="bar"])', () => {
expect(getType('.myClass[href^="a"]')).to.equal(SelectorType.AttributePrefix);
});

it('Attribute prefix selector (#id[foo^="bar"])', () => {
expect(getType('#myId[href^="a"]')).to.equal(SelectorType.AttributePrefix);
});

it('Attribute suffix selector (E[foo$="bar"])', () => {
expect(getType('a[href$="a"]')).to.equal(SelectorType.AttributeSuffix);
});

it('Attribute suffix selector (.class[foo$="bar"])', () => {
expect(getType('.myClass[href$="a"]')).to.equal(SelectorType.AttributeSuffix);
});

it('Attribute suffix selector (#id[foo$="bar"])', () => {
expect(getType('#myId[href$="a"]')).to.equal(SelectorType.AttributeSuffix);
});

it('Attribute contains selector (E[foo*="bar"])', () => {
expect(getType('a[href*="a"]')).to.equal(SelectorType.AttributeContains);
});

it('Attribute contains selector (.class[foo*="bar"])', () => {
expect(getType('.myClass[href*="a"]')).to.equal(SelectorType.AttributeContains);
});

it('Attribute contains selector (#id[foo*="bar"])', () => {
expect(getType('#myId[href*="a"]')).to.equal(SelectorType.AttributeContains);
});

it('Class selector (.class)', () => {
expect(getType('.class')).to.equal(SelectorType.Class);
});
Expand All @@ -79,5 +123,13 @@ describe('getType(selector)', function() {
it('Root selector (E:root)', () => {
expect(getType('div:root')).to.equal(SelectorType.Root);
});

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

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

0 comments on commit f97e348

Please sign in to comment.