Skip to content

Commit 73753e7

Browse files
authored
Merge pull request #4333 from omnivore-app/fix/readability-clean-event-handlers
Remove javascript event handlers from elements
2 parents be7d172 + ad19903 commit 73753e7

File tree

3 files changed

+346
-267
lines changed

3 files changed

+346
-267
lines changed

packages/readabilityjs/Readability.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Readability.prototype = {
303303

304304
if (!this._keepClasses) {
305305
// Remove classes.
306-
this._cleanClasses(articleContent);
306+
this._cleanElement(articleContent);
307307
}
308308
},
309309

@@ -456,7 +456,7 @@ Readability.prototype = {
456456
* @param Element
457457
* @return void
458458
*/
459-
_cleanClasses: function (node) {
459+
_cleanElement: function (node) {
460460
if (node.className && node.className.startsWith && node.className.startsWith('_omnivore')) {
461461
return;
462462
}
@@ -483,8 +483,10 @@ Readability.prototype = {
483483
node.removeAttribute("class");
484484
}
485485

486+
this._removeAllEventHandlers(node)
487+
486488
for (node = node.firstElementChild; node; node = node.nextElementSibling) {
487-
this._cleanClasses(node);
489+
this._cleanElement(node);
488490
}
489491
},
490492

@@ -546,7 +548,6 @@ Readability.prototype = {
546548
this._forEachNode(medias, function (media) {
547549
var src = media.getAttribute("src");
548550
var poster = media.getAttribute("poster");
549-
var srcset = media.getAttribute("srcset");
550551

551552
if (src) {
552553
media.setAttribute("src", this.toAbsoluteURI(src));
@@ -558,6 +559,20 @@ Readability.prototype = {
558559
});
559560
},
560561

562+
// removes all the javascript event handlers from the supplied element
563+
_removeAllEventHandlers(element) {
564+
const attributes = element.attributes;
565+
566+
// Iterate in reverse because removing attributes changes the length
567+
for (let i = attributes.length - 1; i >= 0; i--) {
568+
const attribute = attributes[i];
569+
// Check if the attribute starts with "on" (like "onload", "onerror", etc.)
570+
if (attribute.name.startsWith('on')) {
571+
element.removeAttribute(attribute.name);
572+
}
573+
}
574+
},
575+
561576
/** Creates imageproxy links for all article images with href source */
562577
_createImageProxyLinks: function (articleContent) {
563578
if (this.createImageProxyUrl !== undefined) {

packages/readabilityjs/test/test-pages/caixin/expected.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h2> 途虎养车港交所挂牌 腾讯为最大外部股东 <img src="https://f
1919
<p> 国内汽车服务市场高度分散,2022年,途虎养车取得汽车服务收入115亿元,市场份额0.9% </p>
2020
</div>
2121
<div id="Main_Content_Val">
22-
<p>   <b>【财新网】</b>9月26日,汽车服务平台<a href="https://s.ccxe.com.cn/entities/companies/202035144" target="_blank">途虎养车</a>正式在港交所主板挂牌上市。途虎养车( <a onclick="return false" href="http://fakehost/test/09690.HKM">09690.HK</a> )上市发行价为28港元/股,此前公司披露的发行价区间为28港元/股至31港元/股,即实际发行价为区间下限。当日,途虎养车收报29.5港元/股,较发行价涨5.36%,市值为239.6亿港元。 </p>
22+
<p>   <b>【财新网】</b>9月26日,汽车服务平台<a href="https://s.ccxe.com.cn/entities/companies/202035144" target="_blank">途虎养车</a>正式在港交所主板挂牌上市。途虎养车( <a href="http://fakehost/test/09690.HKM">09690.HK</a> )上市发行价为28港元/股,此前公司披露的发行价区间为28港元/股至31港元/股,即实际发行价为区间下限。当日,途虎养车收报29.5港元/股,较发行价涨5.36%,市值为239.6亿港元。 </p>
2323
<p>   途虎养车上市不易。途虎养车2022年1月即在港交所递表,2022年8月、2023年3月两次重新递交上市申请材料,终于在2023年8月23日通过聆讯。 </p>
2424
</div>
2525
<!--杂志购买 begin-->

0 commit comments

Comments
 (0)