Skip to content

Commit

Permalink
Add addDynamicIframe rewrite function.
Browse files Browse the repository at this point in the history
Add unit tests for `add_dynamic_iframe` rewrite.
  • Loading branch information
Dave authored and fguillot committed Jan 24, 2024
1 parent 5034175 commit 1159dd6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/reader/rewrite/rewrite_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ func addDynamicImage(entryURL, entryContent string) string {
return entryContent
}

func addDynamicIframe(entryURL, entryContent string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
if err != nil {
return entryContent
}

// Ordered most preferred to least preferred.
candidateAttrs := []string{
"data-src",
"data-original",
"data-orig",
"data-url",
"data-lazy-src",
}

changed := false

doc.Find("iframe").Each(func(i int, iframe *goquery.Selection) {
for _, candidateAttr := range candidateAttrs {
if srcAttr, found := iframe.Attr(candidateAttr); found {
changed = true

iframe.SetAttr("src", srcAttr)

break
}
}
})

if changed {
output, _ := doc.Find("body").First().Html()
return output
}

return entryContent
}

func fixMediumImages(entryURL, entryContent string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/reader/rewrite/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func applyRule(entryURL string, entry *model.Entry, rule rule) {
entry.Content = addMailtoSubject(entryURL, entry.Content)
case "add_dynamic_image":
entry.Content = addDynamicImage(entryURL, entry.Content)
case "add_dynamic_iframe":
entry.Content = addDynamicIframe(entryURL, entry.Content)
case "add_youtube_video":
entry.Content = addYoutubeVideo(entryURL, entry.Content)
case "add_invidious_video":
Expand Down
48 changes: 48 additions & 0 deletions internal/reader/rewrite/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,54 @@ func TestRewriteWithImageAndLazySrcset(t *testing.T) {
}
}

func TestRewriteWithNoLazyIframe(t *testing.T) {
controlEntry := &model.Entry{
Title: `A title`,
Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
}
testEntry := &model.Entry{
Title: `A title`,
Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
}
Rewriter("https://example.org/article", testEntry, "add_dynamic_iframe")

if !reflect.DeepEqual(testEntry, controlEntry) {
t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
}
}

func TestRewriteWithLazyIframe(t *testing.T) {
controlEntry := &model.Entry{
Title: `A title`,
Content: `<iframe data-src="https://example.org/embed" allowfullscreen="" src="https://example.org/embed"></iframe>`,
}
testEntry := &model.Entry{
Title: `A title`,
Content: `<iframe data-src="https://example.org/embed" allowfullscreen></iframe>`,
}
Rewriter("https://example.org/article", testEntry, "add_dynamic_iframe")

if !reflect.DeepEqual(testEntry, controlEntry) {
t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
}
}

func TestRewriteWithLazyIframeAndSrc(t *testing.T) {
controlEntry := &model.Entry{
Title: `A title`,
Content: `<iframe src="https://example.org/embed" data-src="https://example.org/embed" allowfullscreen=""></iframe>`,
}
testEntry := &model.Entry{
Title: `A title`,
Content: `<iframe src="about:blank" data-src="https://example.org/embed" allowfullscreen></iframe>`,
}
Rewriter("https://example.org/article", testEntry, "add_dynamic_iframe")

if !reflect.DeepEqual(testEntry, controlEntry) {
t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
}
}

func TestNewLineRewriteRule(t *testing.T) {
controlEntry := &model.Entry{
Title: `A title`,
Expand Down

0 comments on commit 1159dd6

Please sign in to comment.