Skip to content

Commit 9934500

Browse files
benelliottbenelliottgsa
authored andcommitted
Tweak escape behaviour to preserve escaped attributes
Fixes apostrophecms#540
1 parent f47281e commit 9934500

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,17 @@ function sanitizeHtml(html, options, _recursing) {
287287
}
288288
}
289289

290-
if (!allowedAttributesMap || has(allowedAttributesMap, name) || allowedAttributesMap['*']) {
290+
const willEscape = skip;
291+
292+
if (willEscape || !allowedAttributesMap || has(allowedAttributesMap, name) || allowedAttributesMap['*']) {
291293
each(attribs, function(value, a) {
294+
if (willEscape) {
295+
result += ' ' + a;
296+
value = value || '';
297+
result += '="' + escapeHtml(value, true) + '"';
298+
return;
299+
}
300+
292301
if (!VALID_HTML_ATTRIBUTE_NAME.test(a)) {
293302
// This prevents part of an attribute name in the output from being
294303
// interpreted as the end of an attribute, or end of a tag.

test/test.js

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ describe('sanitizeHtml', function() {
1313
allowedAttributes: false
1414
}), 'before <img src="test.png" /> after');
1515
});
16+
it('should preserve all attributes in escaped tags', () => {
17+
assert.equal(sanitizeHtml('before <img src="test.png" foo="bar baz boo" style="color: red" /> after', {
18+
disallowedTagsMode: 'escape',
19+
allowedTags: []
20+
}), 'before &lt;img src="test.png" foo="bar baz boo" style="color: red" /&gt; after');
21+
});
22+
it('should preserve all attributes in unrecognised escaped tags', () => {
23+
assert.equal(sanitizeHtml('before <vimg src="test.png" foo="bar baz boo" style="color: red" /> after', {
24+
disallowedTagsMode: 'escape',
25+
allowedTags: []
26+
}), 'before &lt;vimg src="test.png" foo="bar baz boo" style="color: red"&gt; after');
27+
});
1628
it('should handle numbers as strings', () => {
1729
assert.equal(sanitizeHtml(5, {
1830
allowedTags: [ 'b', 'em', 'i', 's', 'small', 'strong', 'sub', 'sup', 'time', 'u' ],

0 commit comments

Comments
 (0)