Skip to content

Commit 7df9d8b

Browse files
authored
Merge pull request #623 from KevinSJ/main
Bug Fix: allow false in allowedClasses
2 parents 170269b + 94a79b6 commit 7df9d8b

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## UNRELEASED
44

5+
- Fix to allow `false` in `allowedClasses` attributes
56
- Upgrade mocha version
67
- Apply small linter fixes in tests
78
- Add `.idea` temp files to `.gitignore`

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ allowedClasses: {
320320
}
321321
```
322322

323+
If `allowedClasses` for a certain tag is `false`, all the classes for this tag will be allowed.
324+
323325
> Note: It is advised that your regular expressions always begin with `^` so that you are requiring a known prefix. A regular expression with neither `^` nor `$` just requires that something appear in the middle.
324326
325327
### Allowed CSS Styles

index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,24 @@ function sanitizeHtml(html, options, _recursing) {
170170
allowedAttributesMap[tag].push('class');
171171
}
172172

173-
allowedClassesMap[tag] = [];
174-
allowedClassesRegexMap[tag] = [];
175-
const globRegex = [];
176-
classes.forEach(function(obj) {
177-
if (typeof obj === 'string' && obj.indexOf('*') >= 0) {
178-
globRegex.push(escapeStringRegexp(obj).replace(/\\\*/g, '.*'));
179-
} else if (obj instanceof RegExp) {
180-
allowedClassesRegexMap[tag].push(obj);
181-
} else {
182-
allowedClassesMap[tag].push(obj);
173+
allowedClassesMap[tag] = classes;
174+
175+
if (Array.isArray(classes)) {
176+
const globRegex = [];
177+
allowedClassesMap[tag] = [];
178+
allowedClassesRegexMap[tag] = [];
179+
classes.forEach(function(obj) {
180+
if (typeof obj === 'string' && obj.indexOf('*') >= 0) {
181+
globRegex.push(escapeStringRegexp(obj).replace(/\\\*/g, '.*'));
182+
} else if (obj instanceof RegExp) {
183+
allowedClassesRegexMap[tag].push(obj);
184+
} else {
185+
allowedClassesMap[tag].push(obj);
186+
}
187+
});
188+
if (globRegex.length) {
189+
allowedClassesGlobMap[tag] = new RegExp('^(' + globRegex.join('|') + ')$');
183190
}
184-
});
185-
if (globRegex.length) {
186-
allowedClassesGlobMap[tag] = new RegExp('^(' + globRegex.join('|') + ')$');
187191
}
188192
});
189193

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,20 @@ describe('sanitizeHtml', function() {
482482
'<p class="nifty simple dippy">whee</p>'
483483
);
484484
});
485+
it('should allow all classes for a single tag if `allowedClasses` for the tag is false', function() {
486+
assert.equal(
487+
sanitizeHtml(
488+
'<p class="nifty simple dippy">whee</p>',
489+
{
490+
allowedTags: [ 'p' ],
491+
allowedClasses: {
492+
p: false
493+
}
494+
}
495+
),
496+
'<p class="nifty simple dippy">whee</p>'
497+
);
498+
});
485499
it('should allow only classes that matches `allowedClasses` regex', function() {
486500
assert.equal(
487501
sanitizeHtml(

0 commit comments

Comments
 (0)