Skip to content

Commit

Permalink
fix: whitelist takes precedence over blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
fczbkk committed Jan 9, 2019
1 parent e8c5e01 commit a3b5dcd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/utilities-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export function wildcardToRegExp (input) {
* @return {RegExp}
*/
export function convertMatchListToRegExp (list = []) {
if (list.length === 0) {
return new RegExp('.^');
}
const combined_re = list
.map((item) => {
return (typeof item === 'string')
Expand Down
9 changes: 1 addition & 8 deletions src/utilities-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,5 @@ import {DEFAULT_OPTIONS} from './constants';
* @return {css_selector_generator_options}
*/
export function sanitizeOptions (custom_options = {}) {
const options = Object.assign({}, DEFAULT_OPTIONS, custom_options);

// add never-matching item to blacklist so that it will always fail if empty
if (options.blacklist.length === 0) {
options.blacklist.push('.^');
}

return options;
return Object.assign({}, DEFAULT_OPTIONS, custom_options);
}
11 changes: 8 additions & 3 deletions src/utilities-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ export function getSelectorsByType (element, selector_type) {
* Remove blacklisted selectors from list.
* @param {Array.<string>} list
* @param {RegExp} blacklist_re
* @param {RegExp} whitelist_re
* @return {Array.<string>}
*/
export function filterSelectors (list = [], blacklist_re) {
return list.filter((item) => !blacklist_re.test(item));
export function filterSelectors (list = [], blacklist_re, whitelist_re) {
return list.filter((item) => (
whitelist_re.test(item)
|| !blacklist_re.test(item)
));
}

/**
Expand Down Expand Up @@ -126,7 +130,8 @@ export function getSelectorsList (element, options) {

const reducer = (data, selector_type) => {
const selectors_by_type = getSelectorsByType(element, selector_type);
const filtered_selectors = filterSelectors(selectors_by_type, blacklist_re);
const filtered_selectors =
filterSelectors(selectors_by_type, blacklist_re, whitelist_re);
const found_selectors = orderSelectors(filtered_selectors, whitelist_re);

data[selector_type] = combineWithinSelector
Expand Down
9 changes: 9 additions & 0 deletions test/options-whitelist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ describe('options: whitelist', function () {
assert.equal(result, '.bbb');
});

it('should include whitelisted even if matched by blacklist', function () {
root.innerHTML = '<div class="aaa"></div>';
const result = getCssSelector(root.firstChild, {
whitelist: ['.aaa'],
blacklist: ['.aaa']
});
assert.equal(result, '.aaa');
});

// TODO
it.skip('should prioritise regardless of selector type', function () {
root.innerHTML = '<p class="aaa"></p>';
Expand Down

0 comments on commit a3b5dcd

Please sign in to comment.