From e0a5852207f3ada83e4da8c5d4d9110c5f218083 Mon Sep 17 00:00:00 2001 From: Alan Smithee Date: Fri, 23 Oct 2015 11:52:30 +0000 Subject: [PATCH] adds attribute exact, prefix and suffix regex / types as well as tests for them #2 --- src/core/selector-type.js | 30 ++++++++++++++++-------------- test/get-type.js | 8 ++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/core/selector-type.js b/src/core/selector-type.js index 7434df4..b1f3dd3 100644 --- a/src/core/selector-type.js +++ b/src/core/selector-type.js @@ -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] }; /** @@ -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; } diff --git a/test/get-type.js b/test/get-type.js index 603d89b..be54011 100644 --- a/test/get-type.js +++ b/test/get-type.js @@ -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); });