From 8d8f78241d303381abf6879cc007ca9cbc2a12e2 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Tue, 10 Sep 2019 21:12:38 -0700 Subject: Add native lazy loading for images and iframes This feature is available only in Chrome >= 76 for now. See https://web.dev/native-lazy-loading --- reader/sanitizer/sanitizer.go | 4 +++- reader/sanitizer/sanitizer_test.go | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'reader') diff --git a/reader/sanitizer/sanitizer.go b/reader/sanitizer/sanitizer.go index 1065e5b..642fedf 100644 --- a/reader/sanitizer/sanitizer.go +++ b/reader/sanitizer/sanitizer.go @@ -137,7 +137,9 @@ func getExtraAttributes(tagName string) ([]string, []string) { case "video", "audio": return []string{"controls"}, []string{"controls"} case "iframe": - return []string{"sandbox"}, []string{`sandbox="allow-scripts allow-same-origin allow-popups"`} + return []string{"sandbox", "loading"}, []string{`sandbox="allow-scripts allow-same-origin allow-popups"`, `loading="lazy"`} + case "img": + return []string{"loading"}, []string{`loading="lazy"`} default: return nil, nil } diff --git a/reader/sanitizer/sanitizer_test.go b/reader/sanitizer/sanitizer_test.go index 57769dd..649b7f0 100644 --- a/reader/sanitizer/sanitizer_test.go +++ b/reader/sanitizer/sanitizer_test.go @@ -7,7 +7,7 @@ package sanitizer // import "miniflux.app/reader/sanitizer" import "testing" func TestValidInput(t *testing.T) { - input := `

This is a text with an image: Test.

` + input := `

This is a text with an image: Test.

` output := Sanitize("http://example.org/", input) if input != output { @@ -16,7 +16,7 @@ func TestValidInput(t *testing.T) { } func TestSelfClosingTags(t *testing.T) { - input := `

This
is a text
with an image: Test.

` + input := `

This
is a text
with an image: Test.

` output := Sanitize("http://example.org/", input) if input != output { @@ -35,7 +35,7 @@ func TestTable(t *testing.T) { func TestRelativeURL(t *testing.T) { input := `This link is relative and this image: ` - expected := `This link is relative and this image: ` + expected := `This link is relative and this image: ` output := Sanitize("http://example.org/", input) if expected != output { @@ -165,7 +165,7 @@ func TestEspaceAttributes(t *testing.T) { func TestReplaceYoutubeURL(t *testing.T) { input := `` - expected := `` + expected := `` output := Sanitize("http://example.org/", input) if expected != output { @@ -175,7 +175,7 @@ func TestReplaceYoutubeURL(t *testing.T) { func TestReplaceSecureYoutubeURL(t *testing.T) { input := `` - expected := `` + expected := `` output := Sanitize("http://example.org/", input) if expected != output { @@ -185,7 +185,7 @@ func TestReplaceSecureYoutubeURL(t *testing.T) { func TestReplaceSecureYoutubeURLWithParameters(t *testing.T) { input := `` - expected := `` + expected := `` output := Sanitize("http://example.org/", input) if expected != output { @@ -195,7 +195,7 @@ func TestReplaceSecureYoutubeURLWithParameters(t *testing.T) { func TestReplaceYoutubeURLAlreadyReplaced(t *testing.T) { input := `` - expected := `` + expected := `` output := Sanitize("http://example.org/", input) if expected != output { @@ -205,7 +205,7 @@ func TestReplaceYoutubeURLAlreadyReplaced(t *testing.T) { func TestReplaceProtocolRelativeYoutubeURL(t *testing.T) { input := `` - expected := `` + expected := `` output := Sanitize("http://example.org/", input) if expected != output { @@ -215,7 +215,7 @@ func TestReplaceProtocolRelativeYoutubeURL(t *testing.T) { func TestReplaceIframeURL(t *testing.T) { input := `` - expected := `` + expected := `` output := Sanitize("http://example.org/", input) if expected != output { @@ -224,7 +224,7 @@ func TestReplaceIframeURL(t *testing.T) { } func TestReplaceNoScript(t *testing.T) { - input := `

Before paragraph.

After paragraph.

` + input := `

Before paragraph.

After paragraph.

` expected := `

Before paragraph.

After paragraph.

` output := Sanitize("http://example.org/", input) -- cgit v1.2.3