-
Notifications
You must be signed in to change notification settings - Fork 369
feat: ability to specify if selfClosing element is a void element #670
base: main
Are you sure you want to change the base?
Changes from 5 commits
c54eeb1
d705545
96d3b63
85f6340
344c408
c884bd4
9b0e176
e8a6bab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ node_modules | |
| .idea | ||
| # We do not commit CSS, only LESS | ||
| public/css/*.css | ||
| .vscode | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,7 +117,15 @@ function sanitizeHtml(html, options, _recursing) { | |
| options = Object.assign({}, sanitizeHtml.defaults, options); | ||
| options.parser = Object.assign({}, htmlParserDefaults, options.parser); | ||
|
|
||
| const tagAllowed = function (name) { | ||
| const { selfClosing } = options; | ||
| if (Array.isArray(selfClosing)) { | ||
| options.selfClosing = selfClosing.reduce((before, tagName) => ({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be more performant to use a for loop here, avoiding the need to make a new object on every pass.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i replaced the reduce with a for-of loop |
||
| ...before, | ||
| [tagName]: true | ||
| }), {}); | ||
| } | ||
|
|
||
| const tagAllowed = function(name) { | ||
| return options.allowedTags === false || (options.allowedTags || []).indexOf(name) > -1; | ||
| }; | ||
|
|
||
|
|
@@ -484,15 +492,19 @@ function sanitizeHtml(html, options, _recursing) { | |
| } | ||
| }); | ||
| } | ||
| if (options.selfClosing.indexOf(name) !== -1) { | ||
| result += ' />'; | ||
|
|
||
| if (options.selfClosing[name]) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| result += options.selfClosing[name]?.voidElement === true | ||
| ? '>' | ||
| : ' />'; | ||
| } else { | ||
| result += '>'; | ||
| if (frame.innerText && !hasText && !options.textFilter) { | ||
| result += escapeHtml(frame.innerText); | ||
| addedText = true; | ||
| } | ||
| } | ||
|
|
||
| if (skip) { | ||
| result = tempResult + escapeHtml(result); | ||
| tempResult = ''; | ||
|
|
@@ -584,7 +596,7 @@ function sanitizeHtml(html, options, _recursing) { | |
|
|
||
| if ( | ||
| // Already output /> | ||
| options.selfClosing.indexOf(name) !== -1 || | ||
| options.selfClosing[name] || | ||
| // Escaped tag, closing tag is implied | ||
| (isImplied && !tagAllowed(name) && [ 'escape', 'recursiveEscape' ].indexOf(options.disallowedTagsMode) >= 0) | ||
| ) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Localized and then never used in a localized away again because of the reassignment for the array case. So I think it would be clearer if we didn't do this at all and just referred to
options.selfClosingthroughout this bit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The README must be updated to cover this new syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay on this one!