Skip to content

Commit

Permalink
Feat: add equal attribute selector (ref #26) (#38)
Browse files Browse the repository at this point in the history
* Feat: add equal attribute selector

* Test: add test case for !exec
  • Loading branch information
JPeer264 authored Apr 26, 2018
1 parent 5ac4625 commit 6918a3b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/replace/regex.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
selectors: /(#|\.)[^\s:.{)[>+,\s]+/g, // matches all selectors beginning with . or # - e.g. .matching#alsomatch .matchmetoo
strings: /"\s*[\S\s]*?\s*"|'\s*[\S\s]*?\s*'/g, // matches strings such as: 'hello' or "hello"
attributeSelectors: /\[\s*(class|id)\s*([*^$])=\s*("[\s\S]*?"|'[\s\S]*?'|[\s\S]*)[\s\S]*?\]/g, // matches attribute selectors of html just with class or id in it with `$=`, `^=` or `*=`, e.g.: [class*="selector"]. 3 group matches, first `class` or `id`, second regex operator, third the string
attributeSelectors: /\[\s*(class|id)\s*([*^$]?)=\s*("[\s\S]*?"|'[\s\S]*?'|[\s\S]*)[\s\S]*?\]/g, // matches attribute selectors of html just with class or id in it with `$=`, `^=` or `*=`, e.g.: [class*="selector"]. 3 group matches, first `class` or `id`, second regex operator, third the string
keyframes: /@(-[a-z]*-)*keyframes\s+([a-zA-Z0-9_-]+)/g, // matches keyframes and just the first group the first matched non-whitespace characters - e.g. matches: `@keyframes my-KeyFra_me`
};
23 changes: 21 additions & 2 deletions lib/selectorLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,22 +339,33 @@ class SelectorLibrary {

const re = new RegExp(replace.regex.attributeSelectors);
const exec = re.exec(attributeSelector);

if (!exec) {
return;
}

const typeChar = exec[1] === 'class' ? '.' : '#';
const term = exec[3];

let attributeSelectorType = exec[2];
let selector = term;

// it is empty if the attribute selector is following: [class=test]
if (attributeSelectorType === '') {
attributeSelectorType = '=';
}

if (term.charAt(0).match(/"|'/)) {
selector = term.slice(1, exec[3].length - 1);
}

const attributeSelectorKey = typeChar + exec[2] + selector;
const attributeSelectorKey = typeChar + attributeSelectorType + selector;

this.attributeSelectors[attributeSelectorKey] = {
type: exec[1],
typeChar,
originalString: attributeSelector,
regexType: exec[2],
regexType: attributeSelectorType,
nameGeneratorCounter: new NameGenerator(),
};
} // /setAttributeSelector
Expand All @@ -374,6 +385,14 @@ class SelectorLibrary {
return;
}

if (attributeSelector.charAt(1) === '=') {
const match = slicedSelector.match(attributeString);

if (match && slicedSelector === match[0]) {
result = firstChar + match;
}
}

if (attributeSelector.charAt(1) === '*') {
const match = slicedSelector.match(attributeString);

Expand Down
82 changes: 82 additions & 0 deletions test/selectorLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ test('setAttributeSelector | should set attribute selectors correctly', (t) => {
t.is(rcs.selectorLibrary.attributeSelectors['#^header'].originalString, '[id^="header"]');
});

test('setAttributeSelector | should do nothing', (t) => {
rcs.selectorLibrary.setAttributeSelector([
'ewe weo',
]);

t.is(Object.keys(rcs.selectorLibrary.attributeSelectors).length, 0);
});


test('setAttributeSelector | should set attribute selectors correctly without quotes', (t) => {
rcs.selectorLibrary.setAttributeSelector('[class*=test]');
rcs.selectorLibrary.setAttributeSelector([
Expand All @@ -437,3 +446,76 @@ test('setAttributeSelector | should set attribute selectors correctly without qu
t.is(typeof rcs.selectorLibrary.attributeSelectors['#^header'], 'object');
t.is(rcs.selectorLibrary.attributeSelectors['#^header'].originalString, '[id^=header]');
});

test('setAttributeSelector | should set attribute all attribute selectors', (t) => {
rcs.selectorLibrary.setAttributeSelector([
'[id^=header]',
'[id=test]',
'[id="hello"]',
]);

t.is(typeof rcs.selectorLibrary.attributeSelectors['#^header'], 'object');
t.is(rcs.selectorLibrary.attributeSelectors['#^header'].originalString, '[id^=header]');
t.is(typeof rcs.selectorLibrary.attributeSelectors['#=test'], 'object');
t.is(rcs.selectorLibrary.attributeSelectors['#=test'].originalString, '[id=test]');
t.is(typeof rcs.selectorLibrary.attributeSelectors['#=hello'], 'object');
t.is(rcs.selectorLibrary.attributeSelectors['#=hello'].originalString, '[id="hello"]');
});

/* ************************ *
* REPLACEATTRIBUTESELECTOR *
* ************************ */

test('replaceAttributeSelector | should return the correct begin selector', (t) => {
rcs.selectorLibrary.setAttributeSelector('[id^=begin]');

t.is(rcs.selectorLibrary.replaceAttributeSelector('#testbegin'), false);
t.is(rcs.selectorLibrary.replaceAttributeSelector('#begintest'), '#begint');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#begin'), '#beginn');
});

test('replaceAttributeSelector | should return the correct equal selector', (t) => {
rcs.selectorLibrary.setAttributeSelector('[id=equal]');

t.is(rcs.selectorLibrary.replaceAttributeSelector('#equal'), '#equal');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#equalq'), false);
t.is(rcs.selectorLibrary.replaceAttributeSelector('#lequal'), false);
});

test('replaceAttributeSelector | should return the correct end selector', (t) => {
rcs.selectorLibrary.setAttributeSelector('[id$=end]');

t.is(rcs.selectorLibrary.replaceAttributeSelector('#end'), '#tend');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#endquark'), false);
t.is(rcs.selectorLibrary.replaceAttributeSelector('#quarkend'), '#nend');
});

test('replaceAttributeSelector | should return the correct multi selector', (t) => {
rcs.selectorLibrary.setAttributeSelector('[id*=multi]');

t.is(rcs.selectorLibrary.replaceAttributeSelector('#multi'), '#tmultin');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#multiquark'), '#rmultii');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#quarkmulti'), '#smultio');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#quarkmultiafter'), '#umultia');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#nix'), false);
});

test('replaceAttributeSelector | combination', (t) => {
rcs.selectorLibrary.setAttributeSelector([
'[class=equal]',
'[id$=end]',
'[id*=multi]',
]);

t.is(rcs.selectorLibrary.replaceAttributeSelector('.equal'), '.equal');
t.is(rcs.selectorLibrary.replaceAttributeSelector('.equalq'), false);
t.is(rcs.selectorLibrary.replaceAttributeSelector('.lequal'), false);
t.is(rcs.selectorLibrary.replaceAttributeSelector('#end'), '#tend');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#endquark'), false);
t.is(rcs.selectorLibrary.replaceAttributeSelector('#quarkend'), '#nend');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#multi'), '#tmultin');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#multiquark'), '#rmultii');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#quarkmulti'), '#smultio');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#quarkmultiafter'), '#umultia');
t.is(rcs.selectorLibrary.replaceAttributeSelector('#nix'), false);
});

0 comments on commit 6918a3b

Please sign in to comment.