Skip to content

Commit

Permalink
fix(html): preserve case-sensitive tag names (#5101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Sep 19, 2018
1 parent 79f4251 commit 00ed49f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/language-html/parser-parse5.js
@@ -1,29 +1,25 @@
"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*/) {
// Inline the require to avoid loading all the JS if we don't use it
const parse5 = require("parse5");
const htmlparser2TreeAdapter = require("parse5-htmlparser2-tree-adapter");

try {
const isFragment = !nonFragmentRegex.test(text);

const ast = (isFragment ? parse5.parseFragment : parse5.parse)(text, {
treeAdapter: htmlparser2TreeAdapter,
sourceCodeLocationInfo: true
});
const isFragment = !nonFragmentRegex.test(text);
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") {
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/html_tags/__snapshots__/jsfmt.spec.js.snap
@@ -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>
--------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions tests/html_tags/case-sensitive.html
@@ -0,0 +1 @@
<CaseSensitive>hello world</CaseSensitive>

0 comments on commit 00ed49f

Please sign in to comment.