Skip to content

Commit

Permalink
adds implementation and test for attribute contains selector #2
Browse files Browse the repository at this point in the history
  • Loading branch information
GGAlanSmithee committed Oct 23, 2015
1 parent e0a5852 commit 615c19b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 14 additions & 13 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]+$/, // 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]
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]
};

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

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

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

0 comments on commit 615c19b

Please sign in to comment.