From 794f4886a32755edf3ce18fd4e333656d3335d8c Mon Sep 17 00:00:00 2001 From: Tyler Reardon Date: Wed, 4 Dec 2024 13:25:56 -0800 Subject: [PATCH 1/2] add ability to filter values --- index.js | 1 + test.js | 8 ++++++++ utils.js | 30 +++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 9a07e22..0b68ef8 100644 --- a/index.js +++ b/index.js @@ -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 * diff --git a/test.js b/test.js index 8ae477a..0887d60 100644 --- a/test.js +++ b/test.js @@ -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']]; diff --git a/utils.js b/utils.js index dc4e68a..18cde2d 100644 --- a/utils.js +++ b/utils.js @@ -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 */ @@ -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; - }; /** From c6cba473f0568a82e1c6e2ef217da256a697acf8 Mon Sep 17 00:00:00 2001 From: Tyler Reardon Date: Wed, 4 Dec 2024 14:49:34 -0800 Subject: [PATCH 2/2] add default for new option --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0b68ef8..80f5aa5 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,8 @@ const patternsConfig = require('./patterns.js'); const defaultOptions = { leftDelimiter: '{', rightDelimiter: '}', - allowedAttributes: [] + allowedAttributes: [], + allowedAttributeValues: [] }; /**