replacing invalid tag names by div (#22)

This commit is contained in:
Filip Slatinac
2019-11-25 08:28:20 -05:00
committed by yz-yu
parent a0dc9481b2
commit acb53f2ef1
4 changed files with 55 additions and 3 deletions

View File

@@ -8,11 +8,25 @@ import {
} from './types';
let _id = 1;
const symbolAndNumberRegex = RegExp('[^a-z]');
function genId(): number {
return _id++;
}
function getValidTagName(tagName: string): string {
const processedTagName = tagName.toLowerCase().trim();
if (symbolAndNumberRegex.test(processedTagName)) {
// if the tag name is odd and we cannot extract
// anything from the string, then we return a
// generic div
return 'div';
}
return processedTagName;
}
function getCssRulesString(s: CSSStyleSheet): string | null {
try {
const rules = s.rules || s.cssRules;
@@ -174,7 +188,7 @@ function serializeNode(
}
});
}
const tagName = (n as HTMLElement).tagName.toLowerCase();
const tagName = getValidTagName((n as HTMLElement).tagName);
let attributes: attributes = {};
for (const { name, value } of Array.from((n as HTMLElement).attributes)) {
attributes[name] = transformAttribute(doc, name, value);
@@ -205,8 +219,9 @@ function serializeNode(
''
).trim().length
) {
const cssText = getCssRulesString((n as HTMLStyleElement)
.sheet as CSSStyleSheet);
const cssText = getCssRulesString(
(n as HTMLStyleElement).sheet as CSSStyleSheet,
);
if (cssText) {
attributes._cssText = absoluteToStylesheet(cssText, location.href);
}

View File

@@ -172,6 +172,20 @@ exports[`[html file]: invalid-attribute.html 1`] = `
</body></html>"
`;
exports[`[html file]: invalid-tagname.html 1`] = `
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
<meta charset=\\"UTF-8\\" />
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
<title>Document</title>
</head>
<body>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div></div></body></html>"
`;
exports[`[html file]: picture.html 1`] = `
"<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><head></head><body>
<picture>
@@ -190,6 +204,10 @@ exports[`[html file]: with-relative-res.html 1`] = `
</head>
<body>
<a href=\\"http://localhost:3030/basic.html\\"></a>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div></div>
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"\\" />
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"http://localhost:3030/a.jpg\\" />
<img src=\\"http://localhost:3030/a.jpg\\" alt=\\"\\" srcset=\\"http://exmple.com/a.jpg\\" />

View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<alt="">Hello</alt="">
<alt34>Hello</alt34>
<d123-_+!@#$%^&*()>Hello</d123-_+!@#$%^&*()>
<ale#></ale#>
</body>
</html>

View File

@@ -8,6 +8,10 @@
</head>
<body>
<a href="./basic.html"></a>
<alt="">Hello</alt="">
<alt34>Hello</alt34>
<d123-_+!@#$%^&*()>Hello</d123-_+!@#$%^&*()>
<ale#></ale#>
<img src="./a.jpg" alt="" srcset="">
<img src="./a.jpg" alt="" srcset="/a.jpg">
<img src="./a.jpg" alt="" srcset="http://exmple.com/a.jpg ">