Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion extractor/readability.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,13 @@ func extractWithSelector(body, selector string, excludes []string) (string, erro
})
excluded.Remove()

// each match keeps its own tags, so a heading stays a heading and a paragraph block stays a block
var parts []string
kept.Each(func(_ int, s *goquery.Selection) {
if h, err := s.Html(); err == nil && h != "" {
if inner, err := s.Html(); err != nil || inner == "" {
return
}
if h, err := goquery.OuterHtml(s); err == nil {
parts = append(parts, h)
}
})
Expand Down
27 changes: 19 additions & 8 deletions extractor/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestExtractWithSelectorExcludes(t *testing.T) {
excludes []string
want string
}{
{name: "no excludes", want: "<p>first</p><div class=\"t-meta\">by someone</div><footer>share</footer>\n<p>second</p><footer>share again</footer>"},
{name: "no excludes", want: "<article><p>first</p><div class=\"t-meta\">by someone</div><footer>share</footer></article>\n<article><p>second</p><footer>share again</footer></article>"},
{name: "blank excludes change nothing", excludes: []string{"", " "},
want: "<p>first</p><div class=\"t-meta\">by someone</div><footer>share</footer>\n<p>second</p><footer>share again</footer>"},
{name: "one exclude", excludes: []string{".t-meta"}, want: "<p>first</p><footer>share</footer>\n<p>second</p><footer>share again</footer>"},
{name: "every match loses its excludes", excludes: []string{".t-meta", "footer", ""}, want: "<p>first</p>\n<p>second</p>"},
want: "<article><p>first</p><div class=\"t-meta\">by someone</div><footer>share</footer></article>\n<article><p>second</p><footer>share again</footer></article>"},
{name: "one exclude", excludes: []string{".t-meta"}, want: "<article><p>first</p><footer>share</footer></article>\n<article><p>second</p><footer>share again</footer></article>"},
{name: "every match loses its excludes", excludes: []string{".t-meta", "footer", ""}, want: "<article><p>first</p></article>\n<article><p>second</p></article>"},
{name: "exclude padded with spaces the selector parser keeps", excludes: []string{"\u00a0footer\v"},
want: "<p>first</p><div class=\"t-meta\">by someone</div>\n<p>second</p>"},
want: "<article><p>first</p><div class=\"t-meta\">by someone</div></article>\n<article><p>second</p></article>"},
{name: "excludes match the page as it is", excludes: []string{".t-meta", "p:first-child + footer"},
want: "<p>first</p><footer>share</footer>\n<p>second</p>"}, // removing .t-meta first would also expose the first footer
want: "<article><p>first</p><footer>share</footer></article>\n<article><p>second</p></article>"}, // removing .t-meta first would also expose the first footer
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -44,7 +44,7 @@ func TestExtractWithSelectorNestedMatchInExcluded(t *testing.T) {
`</div></body></html>`
got, err := extractWithSelector(page, ".post", []string{".related"})
require.NoError(t, err)
assert.Equal(t, "<p>Main</p>", got)
assert.Equal(t, `<div class="post"><p>Main</p></div>`, got)
}

func TestGetContentCustomExcludes(t *testing.T) {
Expand All @@ -54,7 +54,18 @@ func TestGetContentCustomExcludes(t *testing.T) {
rule := &datastore.Rule{Content: ".post", Excludes: []string{".wp-embedded-content"}}

content, rich := lr.getContent(context.Background(), page, "https://techcrunch.com/post", rule)
assert.Equal(t, "<p>The article text.</p>", rich)
assert.Equal(t, `<div class="post"><p>The article text.</p></div>`, rich)
assert.Equal(t, rich, content)
assert.Equal(t, "The article text.", lr.getText(content, "Post"))
}

func TestExtractWithSelectorKeepsTags(t *testing.T) {
// each match keeps its own tags: a heading stays a heading and a paragraph block stays a block
const page = `<html><body><article><div class="pagebody-copy">First paragraph.</div><h2 class="pagebody-header">` +
`<strong>A section</strong></h2><div class="pagebody-copy">Second paragraph.</div><div class="pagebody-copy"></div>` +
`</article></body></html>`
got, err := extractWithSelector(page, ".pagebody-header, .pagebody-copy", nil)
require.NoError(t, err)
assert.Equal(t, `<div class="pagebody-copy">First paragraph.</div>`+"\n"+`<h2 class="pagebody-header"><strong>A section</strong></h2>`+
"\n"+`<div class="pagebody-copy">Second paragraph.</div>`, got)
}