-
Notifications
You must be signed in to change notification settings - Fork 690
Fix #986: Preserve Wikipedia <h2> headings #987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rabumaabraham
wants to merge
2
commits into
mozilla:main
Choose a base branch
from
rabumaabraham:issue-986-preserve-wikipedia-h2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
| this.log( | ||
| "Preserving div with h2 heading despite share elements criteria:", | ||
| node, | ||
| h2Text | ||
| ); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return shouldRemove; | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
@@ -2540,18 +2576,18 @@ Readability.prototype = { | |
| "iframe", | ||
| ]); | ||
|
|
||
| for (var i = 0; i < embeds.length; i++) { | ||
| for (var k = 0; k < embeds.length; k++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
@@ -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++) { | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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§ion=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§ion=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" | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?