Skip to content

Commit a1a6b23

Browse files
committed
Merge branch 'sanitize-html'
2 parents 42d19e4 + faf833e commit a1a6b23

File tree

7 files changed

+77
-40
lines changed

7 files changed

+77
-40
lines changed

package-lock.json

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import filterRules from './util/filterRules'
22
import convLinkToText from './util/convLinkToText'
3-
import convUrlToAbs from './util/convUrlToAbs'
43
import convTextToRules from './util/convTextToRules'
54
import postTideCss from './util/postTideCss'
65
import generateRulesAll from './util/generateRulesAll'
6+
import cleanHTML from './util/cleanHTML'
77

88
type cssNodeObj = Awaited<ReturnType<typeof convTextToRules>>
99

@@ -116,25 +116,7 @@ async function getC($0: HTMLElement) {
116116
chrome.runtime.sendMessage({
117117
action: 'celebrate',
118118
css: postTideCss(data),
119-
html: $0.outerHTML
120-
.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, '')
121-
.replace(/<style[\s\S]*?>[\s\S]*?<\/style>/gi, '')
122-
.replace(/<link[\s\S]*?>/gi, '')
123-
.replace(/(<img[^>]+src=(['"]))(.*?)(\2.*?>)/g, function () {
124-
var src = convUrlToAbs(doc.location.href, arguments[3])
125-
return arguments[1] + src + arguments[4]
126-
})
127-
.replace(/(<img[^>]+srcset=(['"]))(.*?)(\2.*?>)/g, function () {
128-
var srcset = arguments[3].split(/,\s*/)
129-
srcset.forEach(function (ele, index) {
130-
var src = ele.replace(/([^ ]*)(.*)/, function () {
131-
var _src = convUrlToAbs(doc.location.href, arguments[1])
132-
return _src + ' ' + arguments[2]
133-
})
134-
srcset[index] = src
135-
})
136-
return arguments[1] + srcset.join(',') + arguments[4]
137-
}),
119+
html: cleanHTML($0.outerHTML, doc),
138120
})
139121
})
140122
}

src/util/cleanHTML.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import convUrlToAbs from './convUrlToAbs'
2+
3+
function makeTagReg(tagName:string){
4+
return new RegExp('<'+tagName+'[\\s\\S]*?>[\\s\\S]*?<\/'+tagName+'>','gi')
5+
}
6+
7+
export default function (dirty: string, doc: Document): string {
8+
return dirty
9+
.replace(makeTagReg('script'), '')
10+
.replace(makeTagReg('style'), '')
11+
.replace(/<link[\s\S]*?>/gi, '')
12+
.replace(/ on\w+=".*?"/gi, "")
13+
.replace(/(<img[^>]+src=(['"]))(.*?)(\2.*?>)/g, function () {
14+
var src = convUrlToAbs(doc.location.href, arguments[3])
15+
return arguments[1] + src + arguments[4]
16+
})
17+
.replace(/(<img[^>]+srcset=(['"]))(.*?)(\2.*?>)/g, function () {
18+
var srcset = arguments[3].split(/,\s*/)
19+
srcset.forEach(function (ele, index) {
20+
var src = ele.replace(/([^ ]*)(.*)/, function () {
21+
var _src = convUrlToAbs(doc.location.href, arguments[1])
22+
return _src + ' ' + arguments[2]
23+
})
24+
srcset[index] = src
25+
})
26+
return arguments[1] + srcset.join(',') + arguments[4]
27+
})
28+
}

src/util/filterRules.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function filterRules($0: HTMLElement, objCss, taskTimerRecord) {
7575
.replace(REG1, ' * ')
7676
.replace(REG2, '(*)')
7777
.replace(REG3, '')
78+
.replace(/:not\(\*\)/ig, '')
7879

7980
try {
8081
if (

src/util/generateRulesAll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function generateRulesAll(
4949
html = html.replace(
5050
/url\((['"]?)(.*?)\1\)/g,
5151
function (_a, _p1, p2) {
52-
return "url(" + convUrlToAbs(doc.location.href, p2) + ")";
52+
return "url(\"" + convUrlToAbs(doc.location.href, p2) + "\")";
5353
}
5454
);
5555
// the next operation is asynchronous

test/css/index.css

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
background: #38c;
99
color: #fff;
1010
font-size: 12px;
11-
opacity: 0.1;
11+
opacity: 0;
12+
pointer-events: none;
1213
}
1314

1415
html {
@@ -80,7 +81,27 @@ body {
8081
.font-style {
8182
font-family: 'Cutive Mono', icon-font, Inter, Avenir, Helvetica , Arial, sans-serif ;
8283
}
83-
84+
.svg-bg {
85+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath fill='rgba(0,0,0,.54)' d='M20.49 19l-5.73-5.73C15.53 12.2 16 10.91 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.41 0 2.7-.47 3.77-1.24L19 20.49 20.49 19zM5 9.5C5 7.01 7.01 5 9.5 5S14 7.01 14 9.5 11.99 14 9.5 14 5 11.99 5 9.5z'/%3E%3C/svg%3E");
86+
background-position: center;
87+
background-repeat: no-repeat;
88+
background-size: 20px;
89+
height: 20px;
90+
padding: 10px;
91+
width: 20px;
92+
font-size:0;
93+
}
94+
.not-focus{
95+
width: 50px;
96+
height: 50px;
97+
border: 1px solid #000;
98+
}
99+
.not-focus:not(:focus){
100+
background: rgb(10, 149, 86);
101+
}
102+
.not-focus:focus{
103+
background: rgb(158, 51, 204);
104+
}
84105
@media print {
85106
i {
86107
font-size: 10pt;

test/index.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
font-family: icon-font;
1212
}
1313
</style>
14+
<style>
15+
.svg-bg-inpage {
16+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath fill='rgba(0,0,0,.54)' d='M20.49 19l-5.73-5.73C15.53 12.2 16 10.91 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.41 0 2.7-.47 3.77-1.24L19 20.49 20.49 19zM5 9.5C5 7.01 7.01 5 9.5 5S14 7.01 14 9.5 11.99 14 9.5 14 5 11.99 5 9.5z'/%3E%3C/svg%3E");
17+
background-position: center;
18+
background-repeat: no-repeat;
19+
background-size: 20px;
20+
height: 20px;
21+
padding: 10px;
22+
width: 20px;
23+
font-size:0;
24+
}
25+
</style>
1426
</head>
1527

1628
<body>
@@ -25,6 +37,9 @@
2537
</div>
2638
<div class="font-style">FontFamily</div>
2739
<p class="S_spetxt">.S_spetxt</p>
40+
<div class="svg-bg">svg-bg</div>
41+
<div class="svg-bg-inpage">svg-bg-inpage</div>
42+
<button class="not-focus">focus</button>
2843
<iframe src="./iframe/index.html" frameborder="0"></iframe>
2944
<p>
3045
Compare text below with result in devtools, with &lt;html&gt; tag
@@ -58,7 +73,7 @@
5873
body{color:#333;text-decoration:none;}
5974
a{color:#76a513;}
6075
/*! end @import */
61-
:AFTER{content:'after';background:#38c;color:#fff;font-size:12px;opacity:0.1;}
76+
:AFTER{content:'after';background:#38c;color:#fff;font-size:12px;opacity:0;pointer-events:none;}
6277
html{height:100%;}
6378
A{background:url(${prefix}img/logo.jpg);background-size:100%;animation:teatnamekey;}
6479
@media screen and (min-width:500px){
@@ -71,6 +86,10 @@
7186
}
7287
body{font-family:"Cutive Mono";}
7388
.font-style{font-family:'Cutive Mono', icon-font, Inter, Avenir, Helvetica , Arial, sans-serif;}
89+
.svg-bg{background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath fill='rgba(0,0,0,.54)' d='M20.49 19l-5.73-5.73C15.53 12.2 16 10.91 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.41 0 2.7-.47 3.77-1.24L19 20.49 20.49 19zM5 9.5C5 7.01 7.01 5 9.5 5S14 7.01 14 9.5 11.99 14 9.5 14 5 11.99 5 9.5z'/%3E%3C/svg%3E");background-position:center;background-repeat:no-repeat;background-size:20px;height:20px;padding:10px;width:20px;font-size:0;}
90+
.not-focus{width:50px;height:50px;border:1px solid #000;}
91+
.not-focus:not(:focus){background:rgb(10, 149, 86);}
92+
.not-focus:focus{background:rgb(158, 51, 204);}
7493
@media print{
7594
i{font-size:10pt;}
7695
}
@@ -84,6 +103,8 @@
84103
@media print{
85104
i{color:#789;font-family:icon-font;}
86105
}
106+
/*! CSS Used from: Embedded */
107+
.svg-bg-inpage{background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath fill='rgba(0,0,0,.54)' d='M20.49 19l-5.73-5.73C15.53 12.2 16 10.91 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.41 0 2.7-.47 3.77-1.24L19 20.49 20.49 19zM5 9.5C5 7.01 7.01 5 9.5 5S14 7.01 14 9.5 11.99 14 9.5 14 5 11.99 5 9.5z'/%3E%3C/svg%3E");background-position:center;background-repeat:no-repeat;background-size:20px;height:20px;padding:10px;width:20px;font-size:0;}
87108
/*! CSS Used keyframes */
88109
@-webkit-keyframes teatnamekey{0%{width:10px;}100%{height:10px;}}
89110
@keyframes teatnamekey{0%{width:10px;}100%{height:10px;}}

0 commit comments

Comments
 (0)