Skip to content

Commit 24fc81f

Browse files
committed
Add support for allowing data properties through data*
This new feature allows passing `data*` in `attributes`, on any element or on all elements (`'*'`), which results in all data properties being allowed in HAST and not sanitised away. Closes remarkjs/remark-vdom#1.
1 parent ed7d19f commit 24fc81f

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

lib/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ function handleProperties(schema, properties, node, stack) {
173173
for (prop in props) {
174174
value = props[prop];
175175

176-
if (allowed.indexOf(prop) === -1) {
176+
if (
177+
allowed.indexOf(prop) === -1 &&
178+
!(data(prop) && allowed.indexOf('data*') !== -1)
179+
) {
177180
continue;
178181
}
179182

@@ -360,3 +363,8 @@ function handleValue(schema, value) {
360363
function allow(schema, value) {
361364
return value;
362365
}
366+
367+
/* Check if `prop` is a data property. */
368+
function data(prop) {
369+
return prop.length > 4 && prop.slice(0, 4).toLowerCase() === 'data';
370+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"devDependencies": {
2626
"browserify": "^13.0.1",
27+
"deepmerge": "^0.2.10",
2728
"esmangle": "^1.0.1",
2829
"hast-util-to-html": "^2.1.0",
2930
"hastscript": "^3.0.0",
@@ -56,7 +57,10 @@
5657
"no-negated-condition": "off",
5758
"guard-for-in": "off",
5859
"no-eq-null": "off",
59-
"eqeqeq": [2, "allow-null"],
60+
"eqeqeq": [
61+
2,
62+
"allow-null"
63+
],
6064
"max-lines": "off"
6165
},
6266
"ignores": [

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ Map of tag-names to allowed attributes (`Object.<Array.<string>>`).
111111

112112
The special `'*'` key sets attributes allowed on all elements.
113113

114+
One special value, namely `'data*'`, can be used to allow all `data`
115+
properties.
116+
114117
```js
115118
"attributes": {
116119
"a": [

test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var test = require('tape');
1313
var html = require('hast-util-to-html');
1414
var h = require('hastscript');
1515
var u = require('unist-builder');
16+
var merge = require('deepmerge');
17+
var gh = require('./lib/github');
1618
var sanitize = require('./index.js');
1719

1820
/* eslint-disable no-script-url, max-params */
@@ -257,6 +259,27 @@ test('sanitize()', function (t) {
257259
'should ignore mismatched specific properties'
258260
);
259261

262+
st.deepEqual(
263+
sanitize(h('div', {dataFoo: 'bar'})),
264+
h('div'),
265+
'should ignore unspecified properties'
266+
);
267+
268+
st.deepEqual(
269+
sanitize(h('div', {dataFoo: 'bar'})),
270+
h('div'),
271+
'should ignore unspecified properties'
272+
);
273+
274+
st.deepEqual(
275+
sanitize(
276+
h('div', {dataFoo: 'bar'}),
277+
merge(gh, {attributes: {'*': ['data*']}})
278+
),
279+
h('div', {dataFoo: 'bar'}),
280+
'should allow `data*`'
281+
);
282+
260283
st.deepEqual(
261284
sanitize(h('img', {alt: 'hello'})),
262285
h('img', {alt: 'hello'}),

0 commit comments

Comments
 (0)