Skip to content

Commit

Permalink
adds attribute exact selector type with test and also adds basic stru…
Browse files Browse the repository at this point in the history
…cture to add rest of attribute psuedo selectors #2
  • Loading branch information
GGAlanSmithee committed Oct 22, 2015
1 parent 3c75ec4 commit 58f53e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/core/selector-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ export const SelectorType = {
Typeid : 4,
Class : 5,
Id : 6,
Attribute : 7,
DescendantCombinator : 8,
ChildCombinator : 9,
AdjacentSiblingCombinator : 10,
GeneralSiblingCombinator : 11
Attribute : 7, // E[attr]
AttributeExact : 8, // E[attr=value]
AttributeExactHyphen : 9, // E[attr|=value]
AttributeList : 10, // E[attr~=value]
AttributePrefix : 11, // E[attr^=value]
AttributeSuffix : 12, // E[attr$=value]
AttributeContains : 13, // E[attr*=value]
DescendantCombinator : 14,
ChildCombinator : 15,
AdjacentSiblingCombinator : 16,
GeneralSiblingCombinator : 17
};

/**
Expand All @@ -45,7 +51,13 @@ const SelectorTypeRegex = {
TypeId : /^[A-Za-z]+\#[A-Za-z]+$/,
Class : /^\.[A-Za-z]+$/,
Id : /^\#[A-Za-z]+$/,
Attribute : /^[A-Za-z]+\[[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]
};

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

if (SelectorCategoryRegex.Attribute.test(selector)) {
return SelectorTypeRegex.Attribute.test(selector) ? SelectorType.Attribute :
return 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 @@ -16,6 +16,10 @@ describe('getType(selector)', function() {
expect(getType('div[test]')).to.equal(SelectorType.Attribute);
});

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

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

0 comments on commit 58f53e0

Please sign in to comment.