Skip to content

Commit 5f0f6fc

Browse files
authored
Merge pull request #39 from FoxMoss/main
Inject a doctype to be rewriten for quirky html
2 parents d0fa0d9 + 9d1c7be commit 5f0f6fc

File tree

1 file changed

+71
-69
lines changed

1 file changed

+71
-69
lines changed

lib/global/rewrite/html/html.ts

+71-69
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,68 @@ export default class html {
1212
generateHead: Function = generateHead;
1313

1414
config: Array<Object> = [
15-
{
16-
"elements": "all",
17-
"tags": ['style'],
18-
"action": "css"
19-
},
20-
{
21-
"elements": ['script', 'iframe', 'embed', 'input', 'track', 'media', 'source', 'img', 'a', 'link', 'area', 'form', 'object'],
22-
"tags": ['src', 'href', 'action', 'data'],
23-
"action": "url"
24-
},
25-
{
26-
"elements": ['source', 'img'],
27-
"tags": ['srcset'],
28-
"action": "srcset"
29-
},
30-
/*{
31-
"elements": ['a', 'link', 'area'],
32-
"tags": ['href'],
33-
"action": "url"
34-
},
35-
{
36-
"elements": ['form'],
37-
"tags": ['action'],
38-
"action": "url"
39-
},
40-
{
41-
"elements": ['object'],
42-
"tags": ['data'],
43-
"action": "url",
44-
},*/
45-
{
46-
"elements": ['script', 'link'],
47-
"tags": ['integrity'],
48-
"action": "rewrite",
49-
"new": "nointegrity",
50-
},
51-
{
52-
"elements": ['script', 'link'],
53-
"tags": ['nonce'],
54-
"action": "rewrite",
55-
"new": "nononce",
56-
},
57-
{
58-
"elements": ['meta'],
59-
"tags": ['http-equiv'],
60-
"action": "http-equiv",
61-
},
62-
{
63-
"elements": ['iframe'],
64-
"tags": ['srcdoc'],
65-
"action": "html",
66-
},
67-
{
68-
"elements": ['link'],
69-
"tags": ["imagesrcset"],
70-
"action": "srcset",
71-
},
72-
{
73-
"elements": 'all',
74-
"tags": ['onclick'],
75-
"action": "js",
76-
}
15+
{
16+
"elements": "all",
17+
"tags": ['style'],
18+
"action": "css"
19+
},
20+
{
21+
"elements": ['script', 'iframe', 'embed', 'input', 'track', 'media', 'source', 'img', 'a', 'link', 'area', 'form', 'object'],
22+
"tags": ['src', 'href', 'action', 'data'],
23+
"action": "url"
24+
},
25+
{
26+
"elements": ['source', 'img'],
27+
"tags": ['srcset'],
28+
"action": "srcset"
29+
},
30+
/*{
31+
"elements": ['a', 'link', 'area'],
32+
"tags": ['href'],
33+
"action": "url"
34+
},
35+
{
36+
"elements": ['form'],
37+
"tags": ['action'],
38+
"action": "url"
39+
},
40+
{
41+
"elements": ['object'],
42+
"tags": ['data'],
43+
"action": "url",
44+
},*/
45+
{
46+
"elements": ['script', 'link'],
47+
"tags": ['integrity'],
48+
"action": "rewrite",
49+
"new": "nointegrity",
50+
},
51+
{
52+
"elements": ['script', 'link'],
53+
"tags": ['nonce'],
54+
"action": "rewrite",
55+
"new": "nononce",
56+
},
57+
{
58+
"elements": ['meta'],
59+
"tags": ['http-equiv'],
60+
"action": "http-equiv",
61+
},
62+
{
63+
"elements": ['iframe'],
64+
"tags": ['srcdoc'],
65+
"action": "html",
66+
},
67+
{
68+
"elements": ['link'],
69+
"tags": ["imagesrcset"],
70+
"action": "srcset",
71+
},
72+
{
73+
"elements": 'all',
74+
"tags": ['onclick'],
75+
"action": "js",
76+
}
7777
];
7878

7979
constructor(ctx: DynamicRewrites) {
@@ -93,27 +93,29 @@ The document has moved
9393

9494
iterate(_dom: Object, cb: Function) {
9595
function it(dom: Object | any = _dom) {
96-
for (var i = 0; i<dom.childNodes.length; i++) {
96+
for (var i = 0; i < dom.childNodes.length; i++) {
9797
cb(dom.childNodes[i]);
98-
98+
9999
if (dom.childNodes[i].childNodes) if (dom.childNodes[i].childNodes.length) {
100100
it(dom.childNodes[i]);
101101
};
102102
}
103103
}
104-
104+
105105
it(_dom);
106106
}
107107

108108
rewrite(src: string, meta: MetaURL, head: Array<string | Object> = []) {
109109
if (Array.isArray(src)) src = src[0];
110-
110+
111111
if (!src) return src;
112112

113113
src = src.toString();
114114

115-
if (!src.match(/<(html|script|style)[^>]*>/g) && src.match(/<\!DOCTYPE[^>]*>/gi)) return src;
115+
if (!src.match(/<\!DOCTYPE[^>]*>/gi)) {
116+
src = "<!DOCTYPE html>" + src
117+
}
116118

117-
return src.replace(/(<!DOCTYPE html>|<html(.*?)>)/im, `$1${head.join(``)}\n`).replace(/<(script|link)\b[^>]*>/g,(e,n)=>e.replace(/\snonce\s*=\s*"[^"]*"/,e=>e.replace("nonce","nononce")).replace(/\sintegrity\s*=\s*"[^"]*"/,e=>e.replace("integrity","nointegrity")));
119+
return src.replace(/(<!DOCTYPE html>|<html(.*?)>)/im, `$1${head.join(``)}\n`).replace(/<(script|link)\b[^>]*>/g, (e, n) => e.replace(/\snonce\s*=\s*"[^"]*"/, e => e.replace("nonce", "nononce")).replace(/\sintegrity\s*=\s*"[^"]*"/, e => e.replace("integrity", "nointegrity")));
118120
}
119-
}
121+
}

0 commit comments

Comments
 (0)