fix(rewrite): keep content order when remove_tables unwraps tables
The remove_tables rule unwrapped each table element by appending its inner HTML to the end of the parent node and then removing the element. Any content located after the table (and the table content itself) was therefore moved to the bottom of the entry instead of staying in place, which reordered the article. Replace the append-then-remove with an in-place ReplaceWithHtml so the unwrapped content keeps its original document order. The existing TestRewriteRemoveTables still passes because its content is fully nested in a single root table; a new test with content surrounding the table covers the regression. Fixes #3110 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -508,8 +508,11 @@ func removeTables(entryContent string) string {
|
||||
break
|
||||
}
|
||||
|
||||
loopElement.Parent().AppendHtml(innerHtml)
|
||||
loopElement.Remove()
|
||||
// Replace the element with its own content in place, so the
|
||||
// surrounding content keeps its original document order.
|
||||
// Appending to the parent would move the unwrapped content to
|
||||
// the end of the parent instead.
|
||||
loopElement.ReplaceWithHtml(innerHtml)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -958,6 +958,29 @@ func TestRewriteRemoveTables(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteRemoveTablesKeepsSurroundingContentOrder(t *testing.T) {
|
||||
// Regression test for https://github.com/miniflux/v2/issues/3110
|
||||
// When a table is unwrapped, its content and the content surrounding it
|
||||
// must keep their original document order. Previously the unwrapped
|
||||
// content was appended to the end of the parent, so anything after the
|
||||
// table (and the table content itself) ended up reordered.
|
||||
controlEntry := &model.Entry{
|
||||
URL: "https://example.org/article",
|
||||
Title: `A title`,
|
||||
Content: `<h1>Header</h1><p>Intro</p><img src="https://example.org/image.png"/><p>Outro</p>`,
|
||||
}
|
||||
testEntry := &model.Entry{
|
||||
URL: "https://example.org/article",
|
||||
Title: `A title`,
|
||||
Content: `<h1>Header</h1><table><tbody><tr><td><p>Intro</p><img src="https://example.org/image.png"/></td></tr></tbody></table><p>Outro</p>`,
|
||||
}
|
||||
ApplyContentRewriteRules(testEntry, `remove_tables`)
|
||||
|
||||
if !reflect.DeepEqual(testEntry, controlEntry) {
|
||||
t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveClickbait(t *testing.T) {
|
||||
controlEntry := &model.Entry{
|
||||
URL: "https://example.org/article",
|
||||
|
||||
Reference in New Issue
Block a user