fix(html): preserve case-sensitive tag names (#5101)

master
Ika 2018-09-19 12:53:38 +08:00 committed by GitHub
parent 79f4251e26
commit 00ed49fad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 14 deletions

View File

@ -1,5 +1,6 @@
"use strict";
const htmlTagNames = require("html-tag-names");
const nonFragmentRegex = /^\s*(<!--[\s\S]*?-->\s*)*<(!doctype|html|head|body)[\s>]/i;
function parse(text /*, parsers, opts*/) {
@ -7,23 +8,18 @@ function parse(text /*, parsers, opts*/) {
const parse5 = require("parse5");
const htmlparser2TreeAdapter = require("parse5-htmlparser2-tree-adapter");
try {
const isFragment = !nonFragmentRegex.test(text);
const isFragment = !nonFragmentRegex.test(text);
const ast = (isFragment ? parse5.parseFragment : parse5.parse)(text, {
treeAdapter: htmlparser2TreeAdapter,
sourceCodeLocationInfo: true
});
const ast = (isFragment ? parse5.parseFragment : parse5.parse)(text, {
treeAdapter: htmlparser2TreeAdapter,
sourceCodeLocationInfo: true
});
return normalize(extendAst(ast));
} catch (error) {
throw error;
}
return normalize(extendAst(ast), text);
}
function normalize(ast) {
function normalize(ast, text) {
if (Array.isArray(ast)) {
return ast.map(normalize);
return ast.map(child => normalize(child, text));
}
if (!ast || typeof ast !== "object") {
@ -34,8 +30,20 @@ function normalize(ast) {
delete ast.next;
delete ast.prev;
// preserve case-sensitive tag names
if (
ast.type === "tag" &&
ast.sourceCodeLocation &&
htmlTagNames.indexOf(ast.name) === -1
) {
ast.name = text.slice(
ast.sourceCodeLocation.startOffset + 1, // <
ast.sourceCodeLocation.startOffset + 1 + ast.name.length
);
}
for (const key of Object.keys(ast)) {
ast[key] = normalize(ast[key]);
ast[key] = normalize(ast[key], text);
}
return ast;

View File

@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`case-sensitive.html - parse5-verify 1`] = `
<CaseSensitive>hello world</CaseSensitive>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<CaseSensitive>hello world</CaseSensitive>
`;
exports[`pre.html - parse5-verify 1`] = `
<pre>
--------------------------------------------------------------------------------

View File

@ -0,0 +1 @@
<CaseSensitive>hello world</CaseSensitive>