Skip to content

Commit

Permalink
adds attribute exact, prefix and suffix regex / types as well as test…
Browse files Browse the repository at this point in the history
…s for them #2
  • Loading branch information
GGAlanSmithee committed Oct 23, 2015
1 parent 58f53e0 commit e0a5852
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/core/selector-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ const SelectorCategoryRegex = {
const SelectorTypeRegex = {
None : '',
Invalid : '',
Universal : /\*/,
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]+$/,
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]+\]$/, // E[attr^=value]
AttributeSuffix : /^[A-Za-z]+\[[A-Za-z]+\]$/, // E[attr$=value]
AttributeContains : /^[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]+\]$/ // E[attr*=value]
};

/**
Expand All @@ -77,7 +77,9 @@ export default function getType(selector) {
}

if (SelectorCategoryRegex.Attribute.test(selector)) {
return SelectorTypeRegex.AttributeExact.test(selector) ? SelectorType.AttributeExact :
return SelectorTypeRegex.AttributePrefix.test(selector) ? SelectorType.AttributePrefix :
SelectorTypeRegex.AttributeSuffix.test(selector) ? SelectorType.AttributeSuffix :
SelectorTypeRegex.AttributeExact.test(selector) ? SelectorType.AttributeExact :
SelectorTypeRegex.Attribute.test(selector) ? SelectorType.Attribute :
SelectorType.Invalid;
}
Expand Down
8 changes: 8 additions & 0 deletions test/get-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ describe('getType(selector)', function() {
expect(getType('a[href="a"]')).to.equal(SelectorType.AttributeExact);
});

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

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

it('Class selector (.class)', () => {
expect(getType('.class')).to.equal(SelectorType.Class);
});
Expand Down

0 comments on commit e0a5852

Please sign in to comment.