Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 80 additions & 8 deletions Readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,29 @@ Readability.prototype = {

this._forEachNode(articleContent.children, function (topCandidate) {
this._cleanMatchedNodes(topCandidate, function (node, matchString) {
return (
var shouldRemove =
this.REGEXPS.shareElements.test(matchString) &&
node.textContent.length < shareElementThreshold
);
node.textContent.length < shareElementThreshold;

// Fix for issue #986: Don't remove divs that contain h2 headings with meaningful content,
// even if they match share elements criteria. This prevents Wikipedia h2 headings from
// being removed along with their parent divs.
if (shouldRemove && node.tagName === "DIV") {
var h2Elements = this._getAllNodesWithTag(node, ["h2"]);
for (var i = 0; i < h2Elements.length; i++) {
var h2Text = this._getInnerText(h2Elements[i], false).trim();
if (h2Text.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to check if the h2 is non-empty?

this.log(
"Preserving div with h2 heading despite share elements criteria:",
node,
h2Text
);
return false;
}
}
}

return shouldRemove;
});
});

Expand Down Expand Up @@ -2513,6 +2532,23 @@ Readability.prototype = {
var contentScore = 0;

if (weight + contentScore < 0) {
// Fix for issue #986: Don't remove divs that contain h2 headings with meaningful content,
// even if the div has negative class weight (e.g., contains "share" in class name).
// This prevents Wikipedia h2 headings from being removed along with their parent divs.
if (tag === "DIV") {
var h2Elements = this._getAllNodesWithTag(node, ["h2"]);
for (var i = 0; i < h2Elements.length; i++) {
var h2Text = this._getInnerText(h2Elements[i], false).trim();
if (h2Text.length) {
this.log(
"Preserving div with h2 heading despite negative class weight:",
node,
h2Text
);
return false;
}
}
}
return true;
}

Expand Down Expand Up @@ -2540,18 +2576,18 @@ Readability.prototype = {
"iframe",
]);

for (var i = 0; i < embeds.length; i++) {
for (var k = 0; k < embeds.length; k++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated, can you revert it?

// If this embed has attribute that matches video regex, don't delete it.
for (var j = 0; j < embeds[i].attributes.length; j++) {
if (this._allowedVideoRegex.test(embeds[i].attributes[j].value)) {
for (var j = 0; j < embeds[k].attributes.length; j++) {
if (this._allowedVideoRegex.test(embeds[k].attributes[j].value)) {
return false;
}
}

// For embed with <object> tag, check inner HTML as well.
if (
embeds[i].tagName === "object" &&
this._allowedVideoRegex.test(embeds[i].innerHTML)
embeds[k].tagName === "object" &&
this._allowedVideoRegex.test(embeds[k].innerHTML)
) {
return false;
}
Expand Down Expand Up @@ -2636,6 +2672,24 @@ Readability.prototype = {

var haveToRemove = shouldRemoveNode();

// Fix for issue #986: Don't remove divs that contain h2 headings with meaningful content,
// even if they fail other cleaning criteria. This prevents Wikipedia h2 headings from
// being removed along with their parent divs.
if (haveToRemove && tag === "DIV") {
var h2Elements2 = this._getAllNodesWithTag(node, ["h2"]);
for (var m = 0; m < h2Elements2.length; m++) {
var h2Text2 = this._getInnerText(h2Elements2[m], false).trim();
if (h2Text2.length) {
this.log(
"Preserving div with h2 heading despite cleaning criteria:",
node,
h2Text2
);
return false;
}
}
}

// Allow simple lists of images to remain in pages
if (isList && haveToRemove) {
for (var x = 0; x < node.children.length; x++) {
Expand Down Expand Up @@ -2686,6 +2740,24 @@ Readability.prototype = {
let headingNodes = this._getAllNodesWithTag(e, ["h1", "h2"]);
this._removeNodes(headingNodes, function (node) {
let shouldRemove = this._getClassWeight(node) < 0;

// Fix for issue #986: Preserve h2 headings that contain meaningful content,
// even if their parent div contains extra links like "edit" or "talk".
// This prevents Wikipedia h2 headings from being incorrectly removed.
if (shouldRemove && node.tagName === "H2") {
// Check if the h2 has meaningful text content (more than just whitespace)
let headingText = this._getInnerText(node, false).trim();
if (headingText.length) {
// Preserve h2 headings with actual content, regardless of parent container class weights
shouldRemove = false;
this.log(
"Preserving h2 heading with content despite negative class weight:",
node,
headingText
);
}
}

if (shouldRemove) {
this.log("Removing header with low class weight:", node);
}
Expand Down
116 changes: 116 additions & 0 deletions test/wikipedia-h2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* eslint-env node, mocha */

/**
* Test case for issue #986: Wikipedia h2 headings being skipped when parent div contains extra links
*
* This test verifies that h2 headings are preserved even when their parent div contains
* links like "edit" or "talk" that might trigger negative class weight filtering.
*/

const { JSDOM } = require("jsdom");
const chai = require("chai");
const expect = chai.expect;
const Readability = require("../index").Readability;

// Sample HTML that mimics Wikipedia structure with h2 headings in divs containing edit/talk links
const testHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Test Wikipedia Article</title>
</head>
<body>
<div id="content">
<h1>Main Article Title</h1>

<p>This is the introduction paragraph.</p>

<!-- This h2 should be preserved despite the parent div having edit/talk links -->
<div class="mw-headline" id="History">
<h2>History</h2>
<span class="mw-editsection">
<span class="mw-editsection-bracket">[</span>
<a href="/w/index.php?title=Test&action=edit&section=1" title="Edit section: History">edit</a>
<span class="mw-editsection-bracket">]</span>
</span>
</div>

<p>This is content under the History section.</p>

<!-- Another h2 that should be preserved -->
<div class="mw-headline" id="Development">
<h2>Development</h2>
<span class="mw-editsection">
<span class="mw-editsection-bracket">[</span>
<a href="/w/index.php?title=Test&action=edit&section=2" title="Edit section: Development">edit</a>
<span class="mw-editsection-bracket">]</span>
</span>
</div>

<p>This is content under the Development section.</p>

<!-- h2 with negative class that should still be preserved if it has content -->
<div class="edit-talk-share">
<h2>Important Section</h2>
<a href="/talk">talk</a>
<a href="/share">share</a>
</div>

<p>This is content under the Important Section.</p>

<!-- Empty h2 that should be removed -->
<div class="edit-talk-share">
<h2></h2>
<a href="/talk">talk</a>
</div>

<p>This paragraph should remain.</p>
</div>
</body>
</html>
`;

describe("Wikipedia H2 Heading Preservation", function () {
it("should preserve h2 headings even when parent div contains edit/talk links", function () {
const dom = new JSDOM(testHTML, {
url: "https://example.com",
contentType: "text/html",
});

const reader = new Readability(dom.window.document, {
debug: false,
});

const article = reader.parse();

expect(article).to.not.be.null;
expect(article.content).to.not.be.undefined;

// Check that h2 headings are preserved
const h2Count = (article.content.match(/<h2>/g) || []).length;

// We expect 4 h2 headings: "Main Article Title", "History", "Development", and "Important Section"
// The empty h2 should be removed
expect(h2Count).to.equal(4, `Expected 4 h2 headings, but found ${h2Count}`);

// Check for specific headings
expect(article.content).to.include(
"<h2>History</h2>",
"History h2 heading not found"
);
expect(article.content).to.include(
"<h2>Development</h2>",
"Development h2 heading not found"
);
expect(article.content).to.include(
"<h2>Important Section</h2>",
"Important Section h2 heading not found"
);

// Check that empty h2 was removed
expect(article.content).to.not.include(
"<h2></h2>",
"Empty h2 heading should have been removed"
);
});
});