| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package rewrite // import "miniflux.app/v2/internal/reader/rewrite"
- import (
- "os"
- "reflect"
- "strings"
- "testing"
- "miniflux.app/v2/internal/config"
- "miniflux.app/v2/internal/model"
- )
- func TestParseRules(t *testing.T) {
- rulesText := `add_dynamic_image,replace("article/(.*).svg"|"article/$1.png"),remove(".spam, .ads:not(.keep)")`
- expected := []rule{
- {name: "add_dynamic_image"},
- {name: "replace", args: []string{"article/(.*).svg", "article/$1.png"}},
- {name: "remove", args: []string{".spam, .ads:not(.keep)"}},
- }
- actual, errs := parseRules(rulesText)
- if len(errs) != 0 {
- t.Errorf(`Unexpected errors while parsing rules: %v`, errs)
- }
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf(`Parsed rules do not match expected rules: got %v instead of %v`, actual, expected)
- }
- }
- func TestReplaceTextLinks(t *testing.T) {
- scenarios := map[string]string{
- `This is a link to example.org`: `This is a link to example.org`,
- `This is a link to ftp://example.org`: `This is a link to ftp://example.org`,
- `This is a link to www.example.org`: `This is a link to www.example.org`,
- `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`,
- `This is a link to http://example.org, end of sentence.`: `This is a link to <a href="http://example.org">http://example.org</a>, end of sentence.`,
- `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`,
- `This is a link to https://www.example.org/path/to?q=s`: `This is a link to <a href="https://www.example.org/path/to?q=s">https://www.example.org/path/to?q=s</a>`,
- `This is a link to https://example.org/index#hash-tag, http://example.org/.`: `This is a link to <a href="https://example.org/index#hash-tag">https://example.org/index#hash-tag</a>, <a href="http://example.org/">http://example.org/</a>.`,
- }
- for input, expected := range scenarios {
- actual := replaceTextLinks(input)
- if actual != expected {
- t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
- }
- }
- }
- func TestRewriteWithNoMatchingRule(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Some text.`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Some text.`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteYoutubeVideoLink(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteYoutubeShortLink(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
- Title: `A title`,
- Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1LUWKWZkPjo" allowfullscreen></iframe><br>Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteIncorrectYoutubeLink(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/some-page",
- Title: `A title`,
- Content: `Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/some-page",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteYoutubeLinkRejectsLookalikeDomain(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- controlEntry := &model.Entry{
- URL: "https://notyoutube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://notyoutube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, `add_youtube_video`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`A domain that merely ends in "youtube.com" must not be treated as YouTube: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteYoutubeLinkAndCustomEmbedURL(t *testing.T) {
- os.Clearenv()
- os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
- var err error
- parser := config.NewConfigParser()
- config.Opts, err = parser.ParseEnvironmentVariables()
- if err != nil {
- t.Fatalf(`Parsing failure: %v`, err)
- }
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/1234" allowfullscreen></iframe><br>Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteYoutubeVideoLinkUsingInvidious(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/1234" allowfullscreen></iframe><br>Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, `add_youtube_video_using_invidious_player`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteYoutubeShortLinkUsingInvidious(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
- Title: `A title`,
- Content: `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/1LUWKWZkPjo" allowfullscreen></iframe><br>Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, `add_youtube_video_using_invidious_player`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestAddYoutubeVideoFromId(t *testing.T) {
- config.Opts = config.NewConfigOptions()
- scenarios := map[string]string{
- // Test with single YouTube ID
- `Some content with youtube ID <script type="text/javascript" data-reactid="6">window.__APOLLO_STATE__ = {youtube_id: "9uASADiYe_8"}</script>`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/9uASADiYe_8" allowfullscreen></iframe><br>Some content with youtube ID <script type="text/javascript" data-reactid="6">window.__APOLLO_STATE__ = {youtube_id: "9uASADiYe_8"}</script>`,
- // Test with multiple YouTube IDs
- `Content with youtube_id: "dQw4w9WgXcQ" and youtube_id: "jNQXAC9IVRw"`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br><iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/jNQXAC9IVRw" allowfullscreen></iframe><br>Content with youtube_id: "dQw4w9WgXcQ" and youtube_id: "jNQXAC9IVRw"`,
- // Test with YouTube ID using equals sign
- `Some content with youtube_id = "dQw4w9WgXcQ"`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id = "dQw4w9WgXcQ"`,
- // Test with spaces around delimiters
- `Some content with youtube_id : "dQw4w9WgXcQ"`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id : "dQw4w9WgXcQ"`,
- // Test with YouTube ID without quotes (regex requires quotes)
- `Some content with youtube_id: dQw4w9WgXcQ and more`: `Some content with youtube_id: dQw4w9WgXcQ and more`,
- // Test with no YouTube ID
- `Some regular content without any video ID`: `Some regular content without any video ID`,
- // Test with invalid YouTube ID (wrong length)
- `Some content with youtube_id: "invalid"`: `Some content with youtube_id: "invalid"`,
- // Test with empty content
- ``: ``,
- }
- for input, expected := range scenarios {
- actual := addYoutubeVideoFromId(input)
- if actual != expected {
- t.Errorf(`addYoutubeVideoFromId test failed for input "%s"`, input)
- t.Errorf(`Expected: "%s"`, expected)
- t.Errorf(`Actual: "%s"`, actual)
- }
- }
- }
- func TestAddYoutubeVideoFromIdWithCustomEmbedURL(t *testing.T) {
- os.Clearenv()
- os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
- var err error
- parser := config.NewConfigParser()
- config.Opts, err = parser.ParseEnvironmentVariables()
- if err != nil {
- t.Fatalf(`Parsing failure: %v`, err)
- }
- input := `Some content with youtube_id: "dQw4w9WgXcQ"`
- expected := `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id: "dQw4w9WgXcQ"`
- actual := addYoutubeVideoFromId(input)
- if actual != expected {
- t.Errorf(`addYoutubeVideoFromId with custom embed URL failed`)
- t.Errorf(`Expected: "%s"`, expected)
- t.Errorf(`Actual: "%s"`, actual)
- }
- }
- func TestAddInvidiousVideo(t *testing.T) {
- scenarios := map[string][]string{
- // Test with various Invidious instances
- "https://invidious.io/watch?v=dQw4w9WgXcQ": {
- "Some video content",
- `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some video content`,
- },
- "https://yewtu.be/watch?v=jNQXAC9IVRw": {
- "Another video description",
- `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/jNQXAC9IVRw" allowfullscreen></iframe><br>Another video description`,
- },
- "http://invidious.snopyta.org/watch?v=dQw4w9WgXcQ": {
- "HTTP instance test",
- `<iframe width="650" height="350" frameborder="0" src="https://invidious.snopyta.org/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>HTTP instance test`,
- },
- "https://youtube.com/watch?v=dQw4w9WgXcQ": {
- "YouTube URL (also matches regex)",
- `<iframe width="650" height="350" frameborder="0" src="https://youtube.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>YouTube URL (also matches regex)`,
- },
- "https://example.org/watch?v=dQw4w9WgXcQ": {
- "Any domain with watch pattern",
- `<iframe width="650" height="350" frameborder="0" src="https://example.org/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Any domain with watch pattern`,
- },
- // Test with query parameters
- "https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s": {
- "Video with timestamp",
- `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?t=30s" allowfullscreen></iframe><br>Video with timestamp`,
- },
- // Test with more complex query parameters
- "https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s&autoplay=1": {
- "Video with multiple parameters",
- `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?autoplay=1&t=30s" allowfullscreen></iframe><br>Video with multiple parameters`,
- },
- // Test with non-matching URLs (should return content unchanged)
- "https://invidious.io/": {
- "Invidious homepage",
- "Invidious homepage",
- },
- "https://invidious.io/some-other-page": {
- "Other page",
- "Other page",
- },
- "https://invidious.io/search?q=test": {
- "Search page",
- "Search page",
- },
- // Test with empty content
- "https://empty.invidious.io/watch?v=dQw4w9WgXcQ": {
- "",
- `<iframe width="650" height="350" frameborder="0" src="https://empty.invidious.io/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>`,
- },
- }
- for entryURL, testData := range scenarios {
- entryContent := testData[0]
- expected := testData[1]
- actual := addInvidiousVideo(entryURL, entryContent)
- if actual != expected {
- t.Errorf(`addInvidiousVideo test failed for URL "%s" and content "%s"`, entryURL, entryContent)
- t.Errorf(`Expected: "%s"`, expected)
- t.Errorf(`Actual: "%s"`, actual)
- }
- }
- }
- func TestRewriteWithInexistingCustomRule(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- testEntry := &model.Entry{
- URL: "https://www.youtube.com/watch?v=1234",
- Title: `A title`,
- Content: `Video Description`,
- }
- ApplyContentRewriteRules(testEntry, `some rule`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithXkcdLink(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you."/><figcaption><p>Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.</p></figcaption></figure>`,
- }
- testEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="<foo>"/><figcaption><p><foo></p></figcaption></figure>`,
- }
- testEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" />`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`,
- }
- testEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `test`,
- }
- testEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `test`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithXkcdAndNoImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `test`,
- }
- testEntry := &model.Entry{
- URL: "https://xkcd.com/1912/",
- Title: `A title`,
- Content: `test`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteMailtoLink(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://www.qwantz.com/",
- Title: `A title`,
- Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a>`,
- }
- testEntry := &model.Entry{
- URL: "https://www.qwantz.com/",
- Title: `A title`,
- Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a>`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithPDFLink(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/document.pdf",
- Title: `A title`,
- Content: `<a href="https://example.org/document.pdf">PDF</a><br>test`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/document.pdf",
- Title: `A title`,
- Content: `test`,
- }
- ApplyContentRewriteRules(testEntry, ``)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithNoLazyImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
- }
- ApplyContentRewriteRules(testEntry, "add_dynamic_image")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithLazyImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="https://example.org/image.jpg" data-url="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"/></noscript>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
- }
- ApplyContentRewriteRules(testEntry, "add_dynamic_image")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithLazyDivImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"/></noscript>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
- }
- ApplyContentRewriteRules(testEntry, "add_dynamic_image")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
- }
- ApplyContentRewriteRules(testEntry, "add_dynamic_image")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithLazySrcset(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
- }
- ApplyContentRewriteRules(testEntry, "add_dynamic_image")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithImageAndLazySrcset(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
- }
- ApplyContentRewriteRules(testEntry, "add_dynamic_image")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteWithNoLazyIframe(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
- }
- ApplyContentRewriteRules(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{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<iframe data-src="https://example.org/embed" allowfullscreen="" src="https://example.org/embed"></iframe>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<iframe data-src="https://example.org/embed" allowfullscreen></iframe>`,
- }
- ApplyContentRewriteRules(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{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<iframe src="https://example.org/embed" data-src="https://example.org/embed" allowfullscreen=""></iframe>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<iframe src="about:blank" data-src="https://example.org/embed" allowfullscreen></iframe>`,
- }
- ApplyContentRewriteRules(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{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `A<br>B<br>C`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: "A\nB\nC",
- }
- ApplyContentRewriteRules(testEntry, "nl2br")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestConvertTextLinkRewriteRule(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Test: <a href="http://example.org/a/b">http://example.org/a/b</a>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Test: http://example.org/a/b`,
- }
- ApplyContentRewriteRules(testEntry, "convert_text_link")
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestMediumImage(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img alt="Image for post" class="t u v if aj" src="https://miro.medium.com/max/2560/1*ephLSqSzQYLvb7faDwzRbw.jpeg" width="1280" height="720" srcset="https://miro.medium.com/max/552/1*ephLSqSzQYLvb7faDwzRbw.jpeg 276w, https://miro.medium.com/max/1104/1*ephLSqSzQYLvb7faDwzRbw.jpeg 552w, https://miro.medium.com/max/1280/1*ephLSqSzQYLvb7faDwzRbw.jpeg 640w, https://miro.medium.com/max/1400/1*ephLSqSzQYLvb7faDwzRbw.jpeg 700w" sizes="700px"/>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `
- <figure class="ht hu hv hw hx hy cy cz paragraph-image">
- <div class="hz ia ib ic aj">
- <div class="cy cz hs">
- <div class="ii s ib ij">
- <div class="ik il s">
- <div class="id ie t u v if aj bk ig ih">
- <img alt="Image for post" class="t u v if aj im in io" src="https://miro.medium.com/max/60/1*ephLSqSzQYLvb7faDwzRbw.jpeg?q=20" width="1280" height="720"/>
- </div>
- <img alt="Image for post" class="id ie t u v if aj c" width="1280" height="720"/>
- <noscript>
- <img alt="Image for post" class="t u v if aj" src="https://miro.medium.com/max/2560/1*ephLSqSzQYLvb7faDwzRbw.jpeg" width="1280" height="720" srcSet="https://miro.medium.com/max/552/1*ephLSqSzQYLvb7faDwzRbw.jpeg 276w, https://miro.medium.com/max/1104/1*ephLSqSzQYLvb7faDwzRbw.jpeg 552w, https://miro.medium.com/max/1280/1*ephLSqSzQYLvb7faDwzRbw.jpeg 640w, https://miro.medium.com/max/1400/1*ephLSqSzQYLvb7faDwzRbw.jpeg 700w" sizes="700px"/>
- </noscript>
- </div>
- </div>
- </div>
- </div>
- </figure>
- `,
- }
- ApplyContentRewriteRules(testEntry, "fix_medium_images")
- testEntry.Content = strings.TrimSpace(testEntry.Content)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>`,
- }
- ApplyContentRewriteRules(testEntry, "use_noscript_figure_images")
- testEntry.Content = strings.TrimSpace(testEntry.Content)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><noscript><img src="http://example.org/logo.svg"></noscript><figcaption>MDN Logo</figcaption></figure>`,
- }
- ApplyContentRewriteRules(testEntry, "use_noscript_figure_images")
- testEntry.Content = strings.TrimSpace(testEntry.Content)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteReplaceCustom(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png">`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg">`,
- }
- ApplyContentRewriteRules(testEntry, `replace("article/(.*).svg"|"article/$1.png")`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteReplaceTitleCustom(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Ouch, a thistle`,
- Content: `The replace_title rewrite rule should not modify the content.`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `The replace_title rewrite rule should not modify the content.`,
- }
- ApplyContentRewriteRules(testEntry, `replace_title("(?i)^a\\s*ti"|"Ouch, a this")`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteRemoveCustom(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum <span class="ads keep">Super important info</span></div>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div>`,
- }
- ApplyContentRewriteRules(testEntry, `remove(".spam, .ads:not(.keep)")`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteRemoveQuotedSelector(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum</div>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum<img alt="LINUX KERNEL" src="/assets/categories/linuxkernel.webp" width="100" height="100"></div>`,
- }
- ApplyContentRewriteRules(testEntry, `remove("img[src^='/assets/categories/']")`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteAddCastopodEpisode(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://podcast.demo/@demo/episodes/test",
- Title: `A title`,
- Content: `<iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description`,
- }
- testEntry := &model.Entry{
- URL: "https://podcast.demo/@demo/episodes/test",
- Title: `A title`,
- Content: `Episode Description`,
- }
- ApplyContentRewriteRules(testEntry, `add_castopod_episode`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteAddEnclosureLinks(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Article Content<hr/><ul>` +
- `<li><a href="https://example.org/episode.mp3?token=a&b=c">https://example.org/episode.mp3?token=a&b=c</a></li>` +
- `<li><a href="https://example.org/video.mp4">https://example.org/video.mp4</a></li></ul>`,
- Enclosures: model.EnclosureList{
- {URL: "https://example.org/episode.mp3?token=a&b=c", MimeType: "audio/mpeg"},
- {URL: "https://example.org/video.mp4", MimeType: "video/mp4"},
- {URL: "", MimeType: "application/pdf"},
- },
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Article Content`,
- Enclosures: model.EnclosureList{
- {URL: "https://example.org/episode.mp3?token=a&b=c", MimeType: "audio/mpeg"},
- {URL: "https://example.org/video.mp4", MimeType: "video/mp4"},
- {URL: "", MimeType: "application/pdf"},
- },
- }
- ApplyContentRewriteRules(testEntry, `add_enclosure_links`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteAddEnclosureLinksWithoutEnclosureURL(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Article Content`,
- Enclosures: model.EnclosureList{
- {URL: "", MimeType: "audio/mpeg"},
- },
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `Article Content`,
- Enclosures: model.EnclosureList{
- {URL: "", MimeType: "audio/mpeg"},
- },
- }
- ApplyContentRewriteRules(testEntry, `add_enclosure_links`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteBase64Decode(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `This is some base64 encoded content`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=`,
- }
- ApplyContentRewriteRules(testEntry, `base64_decode`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteBase64DecodeInHTML(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
- }
- ApplyContentRewriteRules(testEntry, `base64_decode`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteBase64DecodeArgs(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
- }
- ApplyContentRewriteRules(testEntry, `base64_decode(".base64")`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestRewriteRemoveTables(t *testing.T) {
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<p>Test</p><p>Hello World!</p><p>Test</p>`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<table class="container"><tbody><tr><td><p>Test</p><table class="row"><tbody><tr><td><p>Hello World!</p></td><td><p>Test</p></td></tr></tbody></table></td></tr></tbody></table>`,
- }
- ApplyContentRewriteRules(testEntry, `remove_tables`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- 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",
- Title: `This Is Amazing`,
- Content: `Some description`,
- }
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `THIS IS AMAZING`,
- Content: `Some description`,
- }
- ApplyContentRewriteRules(testEntry, `remove_clickbait`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestAddHackerNewsLinksUsingHack(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
- <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
- <p>Points: 23</p>
- <p># Comments: 38</p>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
- <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a> <a href="hack://item?id=37620043">Open with HACK</a></p>
- <p>Points: 23</p>
- <p># Comments: 38</p>`,
- }
- ApplyContentRewriteRules(testEntry, `add_hn_links_using_hack`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestAddHackerNewsLinksUsingOpener(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
- <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
- <p>Points: 23</p>
- <p># Comments: 38</p>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
- <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a> <a href="opener://x-callback-url/show-options?url=https%3A%2F%2Fnews.ycombinator.com%2Fitem%3Fid%3D37620043">Open with Opener</a></p>
- <p>Points: 23</p>
- <p># Comments: 38</p>`,
- }
- ApplyContentRewriteRules(testEntry, `add_hn_links_using_opener`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestAddImageTitle(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `
- <img src="pif" title="pouf">
- <img src="pif" title="pouf" alt='"onerror=alert(1) a="'>
- <img src="pif" title="pouf" alt='"onerror=alert(1) a="'>
- <img src="pif" title="pouf" alt=';&quot;onerror=alert(1) a=;&quot;'>
- <img src="pif" alt="pouf" title='"onerror=alert(1) a="'>
- <img src="pif" alt="pouf" title='"onerror=alert(1) a="'>
- <img src="pif" alt="pouf" title=';&quot;onerror=alert(1) a=;&quot;'>
- `,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure><img src="pif" alt=""/><figcaption><p>pouf</p></figcaption></figure>
- <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
- <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
- <figure><img src="pif" alt=";"onerror=alert(1) a=;""/><figcaption><p>pouf</p></figcaption></figure>
- <figure><img src="pif" alt="pouf"/><figcaption><p>"onerror=alert(1) a="</p></figcaption></figure>
- <figure><img src="pif" alt="pouf"/><figcaption><p>"onerror=alert(1) a="</p></figcaption></figure>
- <figure><img src="pif" alt="pouf"/><figcaption><p>;&quot;onerror=alert(1) a=;&quot;</p></figcaption></figure>
- `,
- }
- ApplyContentRewriteRules(testEntry, `add_image_title`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCard(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- <div class="kg-bookmark-metadata">
- <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
- <span class="kg-bookmark-author">Example</span>
- <span class="kg-bookmark-publisher">Test Author</span>
- </div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<a href="https://example.org/article">Example Article - Example</a>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCardNoCard(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<a href="https://example.org/article">Example Article - Example</a>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<a href="https://example.org/article">Example Article - Example</a>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCardInvalidCard(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a href="https://example.org/article">This card does not have the required fields</a>
- </figure>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a href="https://example.org/article">This card does not have the required fields</a>
- </figure>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCardMissingAuthor(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<a href="https://example.org/article">Example Article</a>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCardDuplicatedAuthor(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article - Example</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- <div class="kg-bookmark-metadata">
- <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
- <span class="kg-bookmark-author">Example</span>
- <span class="kg-bookmark-publisher">Test Author</span>
- </div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<a href="https://example.org/article">Example Article - Example</a>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCardMultiple(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article1">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article 1 - Example</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- <div class="kg-bookmark-metadata">
- <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
- <span class="kg-bookmark-author">Example</span>
- <span class="kg-bookmark-publisher">Test Author</span>
- </div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>
- <figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article2">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article 2 - Example</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- <div class="kg-bookmark-metadata">
- <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
- <span class="kg-bookmark-author">Example</span>
- <span class="kg-bookmark-publisher">Test Author</span>
- </div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<ul><li><a href="https://example.org/article1">Example Article 1 - Example</a></li><li><a href="https://example.org/article2">Example Article 2 - Example</a></li></ul>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestFixGhostCardMultipleSplit(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article1">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article 1 - Example</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- <div class="kg-bookmark-metadata">
- <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
- <span class="kg-bookmark-author">Example</span>
- <span class="kg-bookmark-publisher">Test Author</span>
- </div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>
- <p>This separates the two cards</p>
- <figure class="kg-card kg-bookmark-card">
- <a class="kg-bookmark-container" href="https://example.org/article2">
- <div class="kg-bookmark-content">
- <div class="kg-bookmark-title">Example Article 2 - Example</div>
- <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
- <div class="kg-bookmark-metadata">
- <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
- <span class="kg-bookmark-author">Example</span>
- <span class="kg-bookmark-publisher">Test Author</span>
- </div>
- </div>
- <div class="kg-bookmark-thumbnail">
- <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
- </div>
- </a>
- </figure>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `A title`,
- Content: `<a href="https://example.org/article1">Example Article 1 - Example</a>
- <p>This separates the two cards</p>
- <a href="https://example.org/article2">Example Article 2 - Example</a>`,
- }
- ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestStripImageQueryParams(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `News Article Title`,
- Content: `
- <article>
- <p>Article content with images having query parameters:</p>
- <img src="https://example.org/images/image1.jpg?width=200&height=113&q=80&blur=90" alt="Image with params">
- <img src="https://example.org/images/image2.jpg?width=800&height=600&q=85" alt="Another image with params">
- <p>More images with various query parameters:</p>
- <img src="https://example.org/image123.jpg?blur=50&size=small&format=webp" alt="Complex query params">
- <img src="https://example.org/image123.jpg?size=large&quality=95&cache=123" alt="Different params">
- <p>Image without query parameters:</p>
- <img src="https://example.org/single-image.jpg" alt="Clean image">
- <p>Images with various other params:</p>
- <img src="https://example.org/normal1.jpg?width=300&format=jpg" alt="Normal 1">
- <img src="https://example.org/normal1.jpg?width=600&quality=high" alt="Normal 2">
- </article>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `News Article Title`,
- Content: `<article>
- <p>Article content with images having query parameters:</p>
- <img src="https://example.org/images/image1.jpg" alt="Image with params"/>
- <img src="https://example.org/images/image2.jpg?width=800&height=600&q=85" alt="Another image with params"/>
- <p>More images with various query parameters:</p>
- <img src="https://example.org/image123.jpg" alt="Complex query params"/>
- <img src="https://example.org/image123.jpg?size=large&quality=95&cache=123" alt="Different params"/>
- <p>Image without query parameters:</p>
- <img src="https://example.org/single-image.jpg" alt="Clean image"/>
- <p>Images with various other params:</p>
- <img src="https://example.org/normal1.jpg?width=300&format=jpg" alt="Normal 1"/>
- <img src="https://example.org/normal1.jpg?width=600&quality=high" alt="Normal 2"/>
- </article>`,
- }
- ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestStripImageQueryParamsNoChanges(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Article Without Images`,
- Content: `<p>No images here:</p>
- <div>Just some text content</div>
- <a href="https://example.org">A link</a>`,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Article Without Images`,
- Content: `<p>No images here:</p>
- <div>Just some text content</div>
- <a href="https://example.org">A link</a>`,
- }
- ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestStripImageQueryParamsEdgeCases(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Edge Cases`,
- Content: `
- <p>Edge cases for image query parameter stripping:</p>
- <!-- Various query parameters -->
- <img src="https://example.org/image1.jpg?blur=80&width=300" alt="Multiple params">
- <!-- Complex query parameters -->
- <img src="https://example.org/image2.jpg?BLUR=60&format=webp&cache=123" alt="Complex params">
- <img src="https://example.org/image3.jpg?quality=high&version=2" alt="Other params">
- <!-- Query params in middle of string -->
- <img src="https://example.org/image4.jpg?size=large&blur=30&format=webp&quality=90" alt="Middle params">
- <!-- Image without query params -->
- <img src="https://example.org/clean.jpg" alt="Clean image">
- `,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Edge Cases`,
- Content: `<p>Edge cases for image query parameter stripping:</p>
- <!-- Various query parameters -->
- <img src="https://example.org/image1.jpg" alt="Multiple params"/>
- <!-- Complex query parameters -->
- <img src="https://example.org/image2.jpg?BLUR=60&format=webp&cache=123" alt="Complex params"/>
- <img src="https://example.org/image3.jpg?quality=high&version=2" alt="Other params"/>
- <!-- Query params in middle of string -->
- <img src="https://example.org/image4.jpg" alt="Middle params"/>
- <!-- Image without query params -->
- <img src="https://example.org/clean.jpg" alt="Clean image"/>
- `,
- }
- ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
- func TestStripImageQueryParamsSimple(t *testing.T) {
- testEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Simple Test`,
- Content: `
- <p>Testing query parameter stripping:</p>
- <!-- Images with various query parameters -->
- <img src="https://example.org/test1.jpg?blur=0&width=300" alt="With blur zero">
- <img src="https://example.org/test2.jpg?blur=50&width=300&format=webp" alt="With blur fifty">
- <img src="https://example.org/test3.jpg?width=800&quality=high" alt="No blur param">
- <img src="https://example.org/test4.jpg" alt="No params at all">
- `,
- }
- controlEntry := &model.Entry{
- URL: "https://example.org/article",
- Title: `Simple Test`,
- Content: `<p>Testing query parameter stripping:</p>
- <!-- Images with various query parameters -->
- <img src="https://example.org/test1.jpg?blur=0&width=300" alt="With blur zero"/>
- <img src="https://example.org/test2.jpg" alt="With blur fifty"/>
- <img src="https://example.org/test3.jpg?width=800&quality=high" alt="No blur param"/>
- <img src="https://example.org/test4.jpg" alt="No params at all"/>
- `,
- }
- ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
- if !reflect.DeepEqual(testEntry, controlEntry) {
- t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
- }
- }
|