mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 12:52:31 +02:00
fix(v2): escape HTML entities in user tags attributes (#4894)
This commit is contained in:
parent
0587d6d6e7
commit
1349ece883
3 changed files with 6 additions and 3 deletions
|
@ -69,6 +69,7 @@
|
||||||
"cssnano": "^5.0.4",
|
"cssnano": "^5.0.4",
|
||||||
"del": "^6.0.0",
|
"del": "^6.0.0",
|
||||||
"detect-port": "^1.3.0",
|
"detect-port": "^1.3.0",
|
||||||
|
"escape-html": "^1.0.3",
|
||||||
"eta": "^1.12.1",
|
"eta": "^1.12.1",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"file-loader": "^6.2.0",
|
"file-loader": "^6.2.0",
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
import htmlTagObjectToString from '../htmlTags';
|
import htmlTagObjectToString from '../htmlTags';
|
||||||
|
|
||||||
describe('htmlTagObjectToString', () => {
|
describe('htmlTagObjectToString', () => {
|
||||||
test('simple html tag', () => {
|
test('valid html tag', () => {
|
||||||
expect(
|
expect(
|
||||||
htmlTagObjectToString({
|
htmlTagObjectToString({
|
||||||
tagName: 'script',
|
tagName: 'script',
|
||||||
|
@ -17,10 +17,11 @@ describe('htmlTagObjectToString', () => {
|
||||||
src:
|
src:
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
|
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
|
||||||
async: true,
|
async: true,
|
||||||
|
'data-options': '{"prop":true}',
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
).toMatchInlineSnapshot(
|
).toMatchInlineSnapshot(
|
||||||
`"<script type=\\"text/javascript\\" src=\\"https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js\\" async></script>"`,
|
`"<script type=\\"text/javascript\\" src=\\"https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js\\" async data-options=\\"{"prop":true}\\"></script>"`,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
|
|
|
@ -9,6 +9,7 @@ import {isPlainObject} from 'lodash';
|
||||||
import {HtmlTagObject} from '@docusaurus/types';
|
import {HtmlTagObject} from '@docusaurus/types';
|
||||||
import htmlTags from 'html-tags';
|
import htmlTags from 'html-tags';
|
||||||
import voidHtmlTags from 'html-tags/void';
|
import voidHtmlTags from 'html-tags/void';
|
||||||
|
import escapeHTML from 'escape-html';
|
||||||
|
|
||||||
function assertIsHtmlTagObject(val: unknown): asserts val is HtmlTagObject {
|
function assertIsHtmlTagObject(val: unknown): asserts val is HtmlTagObject {
|
||||||
if (!isPlainObject(val)) {
|
if (!isPlainObject(val)) {
|
||||||
|
@ -41,7 +42,7 @@ export default function htmlTagObjectToString(tagDefinition: unknown): string {
|
||||||
if (tagAttributes[attributeName] === true) {
|
if (tagAttributes[attributeName] === true) {
|
||||||
return attributeName;
|
return attributeName;
|
||||||
}
|
}
|
||||||
return `${attributeName}="${tagAttributes[attributeName]}"`;
|
return `${attributeName}="${escapeHTML(tagAttributes[attributeName])}"`;
|
||||||
});
|
});
|
||||||
return `<${[tagDefinition.tagName].concat(attributes).join(' ')}>${
|
return `<${[tagDefinition.tagName].concat(attributes).join(' ')}>${
|
||||||
(!isVoidTag && tagDefinition.innerHTML) || ''
|
(!isVoidTag && tagDefinition.innerHTML) || ''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue