Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to filter attribute values #160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const patternsConfig = require('./patterns.js');
* @property {!string} leftDelimiter left delimiter, default is `{`(left curly bracket)
* @property {!string} rightDelimiter right delimiter, default is `}`(right curly bracket)
* @property {AllowedAttribute[]} allowedAttributes empty means no limit
* @property {AllowedAttribute[]} allowedAttributeValues empty means no limit
*
* @typedef {string|RegExp} AllowedAttribute rule of allowed attribute
*
Expand Down Expand Up @@ -55,7 +56,8 @@ const patternsConfig = require('./patterns.js');
const defaultOptions = {
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: []
allowedAttributes: [],
allowedAttributeValues: []
};

/**
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ function describeTestsWithOptions(options, postText) {
assert.deepEqual(res, expected);
});

it(replaceDelimiters('should omit values {.class ..css-module #id key=val .class.with.dot}', options), () => {
const src = '{.good-class .keep-me .ignore-class .good.class.with.dot .ignore.class.with.dot ..good ..ignore #good #ignore-id key=key-to-ignore key=good-key}';
const expected = [['class', 'good-class'], ['class', 'keep-me'], ['class', 'good.class.with.dot'], ['css-module', 'good'], ['id', 'good'], ['key', 'good-key']];
const newOptions = Object.assign({}, options, { allowedAttributeValues: [/^good/, 'keep-me'] });
const res = utils.getAttrs(replaceDelimiters(src, options), 0, newOptions);
assert.deepEqual(res, expected);
});

it(replaceDelimiters('should parse attributes with = {attr=/id=1}', options), () => {
const src = '{link=/some/page/in/app/id=1}';
const expected = [['link', '/some/page/in/app/id=1']];
Expand Down
30 changes: 23 additions & 7 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,25 @@ exports.getAttrs = function (str, start, options) {
value += char_;
}

if (options.allowedAttributes && options.allowedAttributes.length) {
const allowedAttributes = options.allowedAttributes;
const needsFilterAttributes = options.allowedAttributes && options.allowedAttributes.length;
const needsFilterAttributeValues = options.allowedAttributeValues && options.allowedAttributeValues.length;

if (needsFilterAttributes || needsFilterAttributeValues) {
const allowedAttributes = options.allowedAttributes;
const allowedAttributeValues = options.allowedAttributeValues;
return attrs.filter(function (attrPair) {
const attr = attrPair[0];

const attrValue = attrPair[1];
let attrPassed = !needsFilterAttributes;
let attrValuePassed = !needsFilterAttributeValues;
/**
* @param {AllowedAttribute} allowedAttributeValue
*/
function isAllowedAttributeValue (allowedAttributeValue) {
return (attrValue === allowedAttributeValue
|| (allowedAttributeValue instanceof RegExp && allowedAttributeValue.test(attrValue))
);
}
/**
* @param {AllowedAttribute} allowedAttribute
*/
Expand All @@ -111,13 +124,16 @@ exports.getAttrs = function (str, start, options) {
|| (allowedAttribute instanceof RegExp && allowedAttribute.test(attr))
);
}

return allowedAttributes.some(isAllowedAttribute);
if (needsFilterAttributes) {
attrPassed = allowedAttributes.some(isAllowedAttribute);
}
if (needsFilterAttributeValues) {
attrValuePassed = allowedAttributeValues.some(isAllowedAttributeValue);
}
return attrPassed && attrValuePassed;
});

}
return attrs;

};

/**
Expand Down