Skip to content

Commit 27a7b22

Browse files
Support RegExp in the list of valid property values
1 parent ee07ff2 commit 27a7b22

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

lib/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* Possible property values
1010
* @typedef {string|number|boolean} PrimitivePropertyValue
1111
* Possible primitive HTML attribute values
12-
* @typedef {string|[string, ...Array<PrimitivePropertyValue>]} AttributeValue
13-
* @typedef {Record<string, Array<PrimitivePropertyValue>>} AttributeMap
12+
* @typedef {string|[string, ...Array<PrimitivePropertyValue|RegExp>]} AttributeValue
13+
* @typedef {Record<string, Array<PrimitivePropertyValue|RegExp>>} AttributeMap
1414
*
1515
* @typedef Schema Sanitization configuration
1616
* @property {Record<string, Array<AttributeValue>>} [attributes]
@@ -389,7 +389,10 @@ function handlePropertyValue(schema, value, prop, definition) {
389389
typeof value === 'number' ||
390390
typeof value === 'string') &&
391391
safeProtocol(schema, value, prop) &&
392-
(definition.length === 0 || definition.includes(value))
392+
(definition.length === 0 ||
393+
definition.some((allowed) =>
394+
allowed instanceof RegExp ? allowed.test(value) : allowed === value
395+
))
393396
) {
394397
return schema.clobberPrefix &&
395398
schema.clobber &&

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ attributes: {
192192

193193
Instead of a single string (such as `type`), which allows any [*property
194194
value*][value] of that [*property name*][name], it’s also possible to provide
195-
an array (such as `['type', 'checkbox']`), where the first entry is the
196-
*property name*, and all other entries allowed *property values*.
195+
an array (such as `['type', 'checkbox']` or `['ClassName', /^hljs-/]`),
196+
where the first entry is the *property name*, and all other entries are
197+
*property values* allowed (or *RegEx*).
197198
This is how the default GitHub schema allows only disabled checkbox inputs:
198199

199200
```js

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,30 @@ test('sanitize()', (t) => {
610610
'should support a list of valid values on new attributes'
611611
)
612612

613+
t.deepEqual(
614+
sanitize(
615+
h('div', [
616+
h('span', {className: 'a-one'}),
617+
h('span', {className: 'a-two'}),
618+
h('span', {className: 'b-one'}),
619+
h('span', {className: 'b-two'}),
620+
h('span', {className: 'a-one a-two b-one b-two'})
621+
]),
622+
deepmerge(defaultSchema, {
623+
tagNames: ['span'],
624+
attributes: {span: [['className', /^a-/, 'b-one']]}
625+
})
626+
),
627+
h('div', [
628+
h('span', {className: 'a-one'}),
629+
h('span', {className: 'a-two'}),
630+
h('span', {className: 'b-one'}),
631+
h('span', {className: []}),
632+
h('span', {className: 'a-one a-two b-one'})
633+
]),
634+
'should support RegExp in the list of valid values'
635+
)
636+
613637
t.deepEqual(
614638
sanitize(
615639
h('div', [

0 commit comments

Comments
 (0)