|
| 1 | +package readability |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/PuerkitoBio/goquery" |
| 8 | +) |
| 9 | + |
| 10 | +// TestPreservableMediaCountAudioWithSrc covers the <audio src=…> branch: |
| 11 | +// an audio element with a source URL counts as preservable media even |
| 12 | +// when no other attribute matches the video allow-list. |
| 13 | +func TestPreservableMediaCountAudioWithSrc(t *testing.T) { |
| 14 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 15 | + `<div><audio src="https://example.com/podcast.mp3"></audio></div>`)) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + cfg := defaultParserConfig() |
| 20 | + got := preservableMediaCount(doc.Find("div"), cfg) |
| 21 | + if got != 1 { |
| 22 | + t.Fatalf("preservableMediaCount = %d, want 1 (audio with src)", got) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// TestPreservableMediaCountAudioWithoutSrc verifies the audio branch |
| 27 | +// requires a non-empty src; bare <audio> tags do not count. |
| 28 | +func TestPreservableMediaCountAudioWithoutSrc(t *testing.T) { |
| 29 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader(`<div><audio></audio></div>`)) |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + cfg := defaultParserConfig() |
| 34 | + got := preservableMediaCount(doc.Find("div"), cfg) |
| 35 | + if got != 0 { |
| 36 | + t.Fatalf("preservableMediaCount = %d, want 0 (audio without src)", got) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// TestPreservableMediaCountVideoBlobSrc covers the video blob: branch: |
| 41 | +// player elements often surface their bound stream as a blob: URL, which |
| 42 | +// must be treated as legitimate media even though the URL itself is |
| 43 | +// useless to readers. |
| 44 | +func TestPreservableMediaCountVideoBlobSrc(t *testing.T) { |
| 45 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 46 | + `<div><video src="blob:https://example.com/abc-123"></video></div>`)) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + cfg := defaultParserConfig() |
| 51 | + got := preservableMediaCount(doc.Find("div"), cfg) |
| 52 | + if got != 1 { |
| 53 | + t.Fatalf("preservableMediaCount = %d, want 1 (video blob:)", got) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestPreservableMediaCountVideoDataVideoID covers the data-video-id |
| 58 | +// branch: many CMS templates render an empty <video> shell that gets |
| 59 | +// hydrated by JS using data-video-id; this must still count as media. |
| 60 | +func TestPreservableMediaCountVideoDataVideoID(t *testing.T) { |
| 61 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 62 | + `<div><video data-video-id="abc123"></video></div>`)) |
| 63 | + if err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + } |
| 66 | + cfg := defaultParserConfig() |
| 67 | + got := preservableMediaCount(doc.Find("div"), cfg) |
| 68 | + if got != 1 { |
| 69 | + t.Fatalf("preservableMediaCount = %d, want 1 (video data-video-id)", got) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// TestPreservableMediaCountIframeAllowed covers the generic allow-list |
| 74 | +// branch: an iframe whose src matches cfg.videoAllowed counts as |
| 75 | +// preservable media. |
| 76 | +func TestPreservableMediaCountIframeAllowed(t *testing.T) { |
| 77 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 78 | + `<div><iframe src="https://www.youtube.com/embed/abc"></iframe></div>`)) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + cfg := defaultParserConfig() |
| 83 | + got := preservableMediaCount(doc.Find("div"), cfg) |
| 84 | + if got != 1 { |
| 85 | + t.Fatalf("preservableMediaCount = %d, want 1 (iframe in allow-list)", got) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestPreservableMediaCountIframeUnrecognized verifies that iframes |
| 90 | +// whose attributes do NOT match the allow-list are not counted as |
| 91 | +// preservable media — they fall through to removableEmbedCount instead. |
| 92 | +func TestPreservableMediaCountIframeUnrecognized(t *testing.T) { |
| 93 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 94 | + `<div><iframe src="https://ads.example.com/banner"></iframe></div>`)) |
| 95 | + if err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + cfg := defaultParserConfig() |
| 99 | + got := preservableMediaCount(doc.Find("div"), cfg) |
| 100 | + if got != 0 { |
| 101 | + t.Fatalf("preservableMediaCount = %d, want 0 (unrecognized iframe)", got) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// TestRemovableEmbedCountUnrecognized confirms that an iframe outside |
| 106 | +// the allow-list bumps removable embed count, which conditional cleanup |
| 107 | +// uses to push the candidate toward removal. |
| 108 | +func TestRemovableEmbedCountUnrecognized(t *testing.T) { |
| 109 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 110 | + `<div><iframe src="https://ads.example.com/banner"></iframe></div>`)) |
| 111 | + if err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + cfg := defaultParserConfig() |
| 115 | + got := removableEmbedCount(doc.Find("div"), cfg) |
| 116 | + if got != 1 { |
| 117 | + t.Fatalf("removableEmbedCount = %d, want 1 (unrecognized iframe)", got) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// TestRemovableEmbedCountAllowedAttribute confirms that an iframe whose |
| 122 | +// attribute matches the allow-list is NOT counted — preservableMediaCount |
| 123 | +// will pick it up instead. |
| 124 | +func TestRemovableEmbedCountAllowedAttribute(t *testing.T) { |
| 125 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 126 | + `<div><iframe src="https://www.youtube.com/embed/abc"></iframe></div>`)) |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + cfg := defaultParserConfig() |
| 131 | + got := removableEmbedCount(doc.Find("div"), cfg) |
| 132 | + if got != 0 { |
| 133 | + t.Fatalf("removableEmbedCount = %d, want 0 (iframe in allow-list)", got) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// TestRemovableEmbedCountAllowedInnerHTML covers the inner-HTML allow |
| 138 | +// path: an embed/object that holds the recognized URL inside a child |
| 139 | +// node rather than on its own attributes (e.g. <object><param value=…/> |
| 140 | +// </object>) must also be considered allowed. |
| 141 | +func TestRemovableEmbedCountAllowedInnerHTML(t *testing.T) { |
| 142 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 143 | + `<div><object><param name="movie" value="https://www.youtube.com/embed/abc"/></object></div>`)) |
| 144 | + if err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
| 147 | + cfg := defaultParserConfig() |
| 148 | + got := removableEmbedCount(doc.Find("div"), cfg) |
| 149 | + if got != 0 { |
| 150 | + t.Fatalf("removableEmbedCount = %d, want 0 (allowed via inner HTML)", got) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// TestRemovableEmbedCountObjectAndEmbed covers the multi-element path: |
| 155 | +// object and embed tags are inspected alongside iframe. |
| 156 | +func TestRemovableEmbedCountObjectAndEmbed(t *testing.T) { |
| 157 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader( |
| 158 | + `<div> |
| 159 | + <object data="https://ads.example.com/x"></object> |
| 160 | + <embed src="https://ads.example.com/y"> |
| 161 | + <iframe src="https://ads.example.com/z"></iframe> |
| 162 | + </div>`)) |
| 163 | + if err != nil { |
| 164 | + t.Fatal(err) |
| 165 | + } |
| 166 | + cfg := defaultParserConfig() |
| 167 | + got := removableEmbedCount(doc.Find("div"), cfg) |
| 168 | + if got != 3 { |
| 169 | + t.Fatalf("removableEmbedCount = %d, want 3 (three unrecognized embeds)", got) |
| 170 | + } |
| 171 | +} |
0 commit comments