content_rewrite_test.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rewrite // import "miniflux.app/v2/internal/reader/rewrite"
  4. import (
  5. "os"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/model"
  11. )
  12. func TestParseRules(t *testing.T) {
  13. rulesText := `add_dynamic_image,replace("article/(.*).svg"|"article/$1.png"),remove(".spam, .ads:not(.keep)")`
  14. expected := []rule{
  15. {name: "add_dynamic_image"},
  16. {name: "replace", args: []string{"article/(.*).svg", "article/$1.png"}},
  17. {name: "remove", args: []string{".spam, .ads:not(.keep)"}},
  18. }
  19. actual := parseRules(rulesText)
  20. if !reflect.DeepEqual(expected, actual) {
  21. t.Errorf(`Parsed rules do not match expected rules: got %v instead of %v`, actual, expected)
  22. }
  23. }
  24. func TestReplaceTextLinks(t *testing.T) {
  25. scenarios := map[string]string{
  26. `This is a link to example.org`: `This is a link to example.org`,
  27. `This is a link to ftp://example.org`: `This is a link to ftp://example.org`,
  28. `This is a link to www.example.org`: `This is a link to www.example.org`,
  29. `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`,
  30. `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.`,
  31. `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`,
  32. `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>`,
  33. `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>.`,
  34. }
  35. for input, expected := range scenarios {
  36. actual := replaceTextLinks(input)
  37. if actual != expected {
  38. t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
  39. }
  40. }
  41. }
  42. func TestRewriteWithNoMatchingRule(t *testing.T) {
  43. controlEntry := &model.Entry{
  44. URL: "https://example.org/article",
  45. Title: `A title`,
  46. Content: `Some text.`,
  47. }
  48. testEntry := &model.Entry{
  49. URL: "https://example.org/article",
  50. Title: `A title`,
  51. Content: `Some text.`,
  52. }
  53. ApplyContentRewriteRules(testEntry, ``)
  54. if !reflect.DeepEqual(testEntry, controlEntry) {
  55. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  56. }
  57. }
  58. func TestRewriteYoutubeVideoLink(t *testing.T) {
  59. config.Opts = config.NewConfigOptions()
  60. controlEntry := &model.Entry{
  61. URL: "https://www.youtube.com/watch?v=1234",
  62. Title: `A title`,
  63. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description`,
  64. }
  65. testEntry := &model.Entry{
  66. URL: "https://www.youtube.com/watch?v=1234",
  67. Title: `A title`,
  68. Content: `Video Description`,
  69. }
  70. ApplyContentRewriteRules(testEntry, ``)
  71. if !reflect.DeepEqual(testEntry, controlEntry) {
  72. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  73. }
  74. }
  75. func TestRewriteYoutubeShortLink(t *testing.T) {
  76. config.Opts = config.NewConfigOptions()
  77. controlEntry := &model.Entry{
  78. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  79. Title: `A title`,
  80. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1LUWKWZkPjo" allowfullscreen></iframe><br>Video Description`,
  81. }
  82. testEntry := &model.Entry{
  83. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  84. Title: `A title`,
  85. Content: `Video Description`,
  86. }
  87. ApplyContentRewriteRules(testEntry, ``)
  88. if !reflect.DeepEqual(testEntry, controlEntry) {
  89. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  90. }
  91. }
  92. func TestRewriteIncorrectYoutubeLink(t *testing.T) {
  93. config.Opts = config.NewConfigOptions()
  94. controlEntry := &model.Entry{
  95. URL: "https://www.youtube.com/some-page",
  96. Title: `A title`,
  97. Content: `Video Description`,
  98. }
  99. testEntry := &model.Entry{
  100. URL: "https://www.youtube.com/some-page",
  101. Title: `A title`,
  102. Content: `Video Description`,
  103. }
  104. ApplyContentRewriteRules(testEntry, ``)
  105. if !reflect.DeepEqual(testEntry, controlEntry) {
  106. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  107. }
  108. }
  109. func TestRewriteYoutubeLinkAndCustomEmbedURL(t *testing.T) {
  110. os.Clearenv()
  111. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  112. var err error
  113. parser := config.NewConfigParser()
  114. config.Opts, err = parser.ParseEnvironmentVariables()
  115. if err != nil {
  116. t.Fatalf(`Parsing failure: %v`, err)
  117. }
  118. controlEntry := &model.Entry{
  119. URL: "https://www.youtube.com/watch?v=1234",
  120. Title: `A title`,
  121. Content: `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/1234" allowfullscreen></iframe><br>Video Description`,
  122. }
  123. testEntry := &model.Entry{
  124. URL: "https://www.youtube.com/watch?v=1234",
  125. Title: `A title`,
  126. Content: `Video Description`,
  127. }
  128. ApplyContentRewriteRules(testEntry, ``)
  129. if !reflect.DeepEqual(testEntry, controlEntry) {
  130. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  131. }
  132. }
  133. func TestRewriteYoutubeVideoLinkUsingInvidious(t *testing.T) {
  134. config.Opts = config.NewConfigOptions()
  135. controlEntry := &model.Entry{
  136. URL: "https://www.youtube.com/watch?v=1234",
  137. Title: `A title`,
  138. Content: `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/1234" allowfullscreen></iframe><br>Video Description`,
  139. }
  140. testEntry := &model.Entry{
  141. URL: "https://www.youtube.com/watch?v=1234",
  142. Title: `A title`,
  143. Content: `Video Description`,
  144. }
  145. ApplyContentRewriteRules(testEntry, `add_youtube_video_using_invidious_player`)
  146. if !reflect.DeepEqual(testEntry, controlEntry) {
  147. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  148. }
  149. }
  150. func TestRewriteYoutubeShortLinkUsingInvidious(t *testing.T) {
  151. config.Opts = config.NewConfigOptions()
  152. controlEntry := &model.Entry{
  153. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  154. Title: `A title`,
  155. Content: `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/1LUWKWZkPjo" allowfullscreen></iframe><br>Video Description`,
  156. }
  157. testEntry := &model.Entry{
  158. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  159. Title: `A title`,
  160. Content: `Video Description`,
  161. }
  162. ApplyContentRewriteRules(testEntry, `add_youtube_video_using_invidious_player`)
  163. if !reflect.DeepEqual(testEntry, controlEntry) {
  164. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  165. }
  166. }
  167. func TestAddYoutubeVideoFromId(t *testing.T) {
  168. config.Opts = config.NewConfigOptions()
  169. scenarios := map[string]string{
  170. // Test with single YouTube ID
  171. `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>`,
  172. // Test with multiple YouTube IDs
  173. `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"`,
  174. // Test with YouTube ID using equals sign
  175. `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"`,
  176. // Test with spaces around delimiters
  177. `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"`,
  178. // Test with YouTube ID without quotes (regex requires quotes)
  179. `Some content with youtube_id: dQw4w9WgXcQ and more`: `Some content with youtube_id: dQw4w9WgXcQ and more`,
  180. // Test with no YouTube ID
  181. `Some regular content without any video ID`: `Some regular content without any video ID`,
  182. // Test with invalid YouTube ID (wrong length)
  183. `Some content with youtube_id: "invalid"`: `Some content with youtube_id: "invalid"`,
  184. // Test with empty content
  185. ``: ``,
  186. }
  187. for input, expected := range scenarios {
  188. actual := addYoutubeVideoFromId(input)
  189. if actual != expected {
  190. t.Errorf(`addYoutubeVideoFromId test failed for input "%s"`, input)
  191. t.Errorf(`Expected: "%s"`, expected)
  192. t.Errorf(`Actual: "%s"`, actual)
  193. }
  194. }
  195. }
  196. func TestAddYoutubeVideoFromIdWithCustomEmbedURL(t *testing.T) {
  197. os.Clearenv()
  198. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  199. var err error
  200. parser := config.NewConfigParser()
  201. config.Opts, err = parser.ParseEnvironmentVariables()
  202. if err != nil {
  203. t.Fatalf(`Parsing failure: %v`, err)
  204. }
  205. input := `Some content with youtube_id: "dQw4w9WgXcQ"`
  206. expected := `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id: "dQw4w9WgXcQ"`
  207. actual := addYoutubeVideoFromId(input)
  208. if actual != expected {
  209. t.Errorf(`addYoutubeVideoFromId with custom embed URL failed`)
  210. t.Errorf(`Expected: "%s"`, expected)
  211. t.Errorf(`Actual: "%s"`, actual)
  212. }
  213. }
  214. func TestAddInvidiousVideo(t *testing.T) {
  215. scenarios := map[string][]string{
  216. // Test with various Invidious instances
  217. "https://invidious.io/watch?v=dQw4w9WgXcQ": {
  218. "Some video content",
  219. `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some video content`,
  220. },
  221. "https://yewtu.be/watch?v=jNQXAC9IVRw": {
  222. "Another video description",
  223. `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/jNQXAC9IVRw" allowfullscreen></iframe><br>Another video description`,
  224. },
  225. "http://invidious.snopyta.org/watch?v=dQw4w9WgXcQ": {
  226. "HTTP instance test",
  227. `<iframe width="650" height="350" frameborder="0" src="https://invidious.snopyta.org/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>HTTP instance test`,
  228. },
  229. "https://youtube.com/watch?v=dQw4w9WgXcQ": {
  230. "YouTube URL (also matches regex)",
  231. `<iframe width="650" height="350" frameborder="0" src="https://youtube.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>YouTube URL (also matches regex)`,
  232. },
  233. "https://example.org/watch?v=dQw4w9WgXcQ": {
  234. "Any domain with watch pattern",
  235. `<iframe width="650" height="350" frameborder="0" src="https://example.org/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Any domain with watch pattern`,
  236. },
  237. // Test with query parameters
  238. "https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s": {
  239. "Video with timestamp",
  240. `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?t=30s" allowfullscreen></iframe><br>Video with timestamp`,
  241. },
  242. // Test with more complex query parameters
  243. "https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s&autoplay=1": {
  244. "Video with multiple parameters",
  245. `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?autoplay=1&t=30s" allowfullscreen></iframe><br>Video with multiple parameters`,
  246. },
  247. // Test with non-matching URLs (should return content unchanged)
  248. "https://invidious.io/": {
  249. "Invidious homepage",
  250. "Invidious homepage",
  251. },
  252. "https://invidious.io/some-other-page": {
  253. "Other page",
  254. "Other page",
  255. },
  256. "https://invidious.io/search?q=test": {
  257. "Search page",
  258. "Search page",
  259. },
  260. // Test with empty content
  261. "https://empty.invidious.io/watch?v=dQw4w9WgXcQ": {
  262. "",
  263. `<iframe width="650" height="350" frameborder="0" src="https://empty.invidious.io/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>`,
  264. },
  265. }
  266. for entryURL, testData := range scenarios {
  267. entryContent := testData[0]
  268. expected := testData[1]
  269. actual := addInvidiousVideo(entryURL, entryContent)
  270. if actual != expected {
  271. t.Errorf(`addInvidiousVideo test failed for URL "%s" and content "%s"`, entryURL, entryContent)
  272. t.Errorf(`Expected: "%s"`, expected)
  273. t.Errorf(`Actual: "%s"`, actual)
  274. }
  275. }
  276. }
  277. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  278. controlEntry := &model.Entry{
  279. URL: "https://www.youtube.com/watch?v=1234",
  280. Title: `A title`,
  281. Content: `Video Description`,
  282. }
  283. testEntry := &model.Entry{
  284. URL: "https://www.youtube.com/watch?v=1234",
  285. Title: `A title`,
  286. Content: `Video Description`,
  287. }
  288. ApplyContentRewriteRules(testEntry, `some rule`)
  289. if !reflect.DeepEqual(testEntry, controlEntry) {
  290. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  291. }
  292. }
  293. func TestRewriteWithXkcdLink(t *testing.T) {
  294. controlEntry := &model.Entry{
  295. URL: "https://xkcd.com/1912/",
  296. Title: `A title`,
  297. 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>`,
  298. }
  299. testEntry := &model.Entry{
  300. URL: "https://xkcd.com/1912/",
  301. Title: `A title`,
  302. 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." />`,
  303. }
  304. ApplyContentRewriteRules(testEntry, ``)
  305. if !reflect.DeepEqual(testEntry, controlEntry) {
  306. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  307. }
  308. }
  309. func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) {
  310. controlEntry := &model.Entry{
  311. URL: "https://xkcd.com/1912/",
  312. Title: `A title`,
  313. Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="&lt;foo&gt;"/><figcaption><p>&lt;foo&gt;</p></figcaption></figure>`,
  314. }
  315. testEntry := &model.Entry{
  316. URL: "https://xkcd.com/1912/",
  317. Title: `A title`,
  318. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" />`,
  319. }
  320. ApplyContentRewriteRules(testEntry, ``)
  321. if !reflect.DeepEqual(testEntry, controlEntry) {
  322. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  323. }
  324. }
  325. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  326. controlEntry := &model.Entry{
  327. URL: "https://xkcd.com/1912/",
  328. Title: `A title`,
  329. 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." />`,
  330. }
  331. testEntry := &model.Entry{
  332. URL: "https://xkcd.com/1912/",
  333. Title: `A title`,
  334. 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." />`,
  335. }
  336. ApplyContentRewriteRules(testEntry, ``)
  337. if !reflect.DeepEqual(testEntry, controlEntry) {
  338. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  339. }
  340. }
  341. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  342. controlEntry := &model.Entry{
  343. URL: "https://xkcd.com/1912/",
  344. Title: `A title`,
  345. Content: `test`,
  346. }
  347. testEntry := &model.Entry{
  348. URL: "https://xkcd.com/1912/",
  349. Title: `A title`,
  350. Content: `test`,
  351. }
  352. ApplyContentRewriteRules(testEntry, ``)
  353. if !reflect.DeepEqual(testEntry, controlEntry) {
  354. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  355. }
  356. }
  357. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  358. controlEntry := &model.Entry{
  359. URL: "https://xkcd.com/1912/",
  360. Title: `A title`,
  361. Content: `test`,
  362. }
  363. testEntry := &model.Entry{
  364. URL: "https://xkcd.com/1912/",
  365. Title: `A title`,
  366. Content: `test`,
  367. }
  368. ApplyContentRewriteRules(testEntry, ``)
  369. if !reflect.DeepEqual(testEntry, controlEntry) {
  370. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  371. }
  372. }
  373. func TestRewriteMailtoLink(t *testing.T) {
  374. controlEntry := &model.Entry{
  375. URL: "https://www.qwantz.com/",
  376. Title: `A title`,
  377. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a>`,
  378. }
  379. testEntry := &model.Entry{
  380. URL: "https://www.qwantz.com/",
  381. Title: `A title`,
  382. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a>`,
  383. }
  384. ApplyContentRewriteRules(testEntry, ``)
  385. if !reflect.DeepEqual(testEntry, controlEntry) {
  386. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  387. }
  388. }
  389. func TestRewriteWithPDFLink(t *testing.T) {
  390. controlEntry := &model.Entry{
  391. URL: "https://example.org/document.pdf",
  392. Title: `A title`,
  393. Content: `<a href="https://example.org/document.pdf">PDF</a><br>test`,
  394. }
  395. testEntry := &model.Entry{
  396. URL: "https://example.org/document.pdf",
  397. Title: `A title`,
  398. Content: `test`,
  399. }
  400. ApplyContentRewriteRules(testEntry, ``)
  401. if !reflect.DeepEqual(testEntry, controlEntry) {
  402. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  403. }
  404. }
  405. func TestRewriteWithNoLazyImage(t *testing.T) {
  406. controlEntry := &model.Entry{
  407. URL: "https://example.org/article",
  408. Title: `A title`,
  409. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  410. }
  411. testEntry := &model.Entry{
  412. URL: "https://example.org/article",
  413. Title: `A title`,
  414. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  415. }
  416. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  417. if !reflect.DeepEqual(testEntry, controlEntry) {
  418. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  419. }
  420. }
  421. func TestRewriteWithLazyImage(t *testing.T) {
  422. controlEntry := &model.Entry{
  423. URL: "https://example.org/article",
  424. Title: `A title`,
  425. 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>`,
  426. }
  427. testEntry := &model.Entry{
  428. URL: "https://example.org/article",
  429. Title: `A title`,
  430. Content: `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  431. }
  432. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  433. if !reflect.DeepEqual(testEntry, controlEntry) {
  434. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  435. }
  436. }
  437. func TestRewriteWithLazyDivImage(t *testing.T) {
  438. controlEntry := &model.Entry{
  439. URL: "https://example.org/article",
  440. Title: `A title`,
  441. Content: `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"/></noscript>`,
  442. }
  443. testEntry := &model.Entry{
  444. URL: "https://example.org/article",
  445. Title: `A title`,
  446. Content: `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  447. }
  448. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  449. if !reflect.DeepEqual(testEntry, controlEntry) {
  450. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  451. }
  452. }
  453. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  454. controlEntry := &model.Entry{
  455. URL: "https://example.org/article",
  456. Title: `A title`,
  457. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`,
  458. }
  459. testEntry := &model.Entry{
  460. URL: "https://example.org/article",
  461. Title: `A title`,
  462. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  463. }
  464. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  465. if !reflect.DeepEqual(testEntry, controlEntry) {
  466. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  467. }
  468. }
  469. func TestRewriteWithLazySrcset(t *testing.T) {
  470. controlEntry := &model.Entry{
  471. URL: "https://example.org/article",
  472. Title: `A title`,
  473. Content: `<img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  474. }
  475. testEntry := &model.Entry{
  476. URL: "https://example.org/article",
  477. Title: `A title`,
  478. Content: `<img srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  479. }
  480. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  481. if !reflect.DeepEqual(testEntry, controlEntry) {
  482. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  483. }
  484. }
  485. func TestRewriteWithImageAndLazySrcset(t *testing.T) {
  486. controlEntry := &model.Entry{
  487. URL: "https://example.org/article",
  488. Title: `A title`,
  489. Content: `<img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  490. }
  491. testEntry := &model.Entry{
  492. URL: "https://example.org/article",
  493. Title: `A title`,
  494. Content: `<img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  495. }
  496. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  497. if !reflect.DeepEqual(testEntry, controlEntry) {
  498. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  499. }
  500. }
  501. func TestRewriteWithNoLazyIframe(t *testing.T) {
  502. controlEntry := &model.Entry{
  503. URL: "https://example.org/article",
  504. Title: `A title`,
  505. Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
  506. }
  507. testEntry := &model.Entry{
  508. URL: "https://example.org/article",
  509. Title: `A title`,
  510. Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
  511. }
  512. ApplyContentRewriteRules(testEntry, "add_dynamic_iframe")
  513. if !reflect.DeepEqual(testEntry, controlEntry) {
  514. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  515. }
  516. }
  517. func TestRewriteWithLazyIframe(t *testing.T) {
  518. controlEntry := &model.Entry{
  519. URL: "https://example.org/article",
  520. Title: `A title`,
  521. Content: `<iframe data-src="https://example.org/embed" allowfullscreen="" src="https://example.org/embed"></iframe>`,
  522. }
  523. testEntry := &model.Entry{
  524. URL: "https://example.org/article",
  525. Title: `A title`,
  526. Content: `<iframe data-src="https://example.org/embed" allowfullscreen></iframe>`,
  527. }
  528. ApplyContentRewriteRules(testEntry, "add_dynamic_iframe")
  529. if !reflect.DeepEqual(testEntry, controlEntry) {
  530. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  531. }
  532. }
  533. func TestRewriteWithLazyIframeAndSrc(t *testing.T) {
  534. controlEntry := &model.Entry{
  535. URL: "https://example.org/article",
  536. Title: `A title`,
  537. Content: `<iframe src="https://example.org/embed" data-src="https://example.org/embed" allowfullscreen=""></iframe>`,
  538. }
  539. testEntry := &model.Entry{
  540. URL: "https://example.org/article",
  541. Title: `A title`,
  542. Content: `<iframe src="about:blank" data-src="https://example.org/embed" allowfullscreen></iframe>`,
  543. }
  544. ApplyContentRewriteRules(testEntry, "add_dynamic_iframe")
  545. if !reflect.DeepEqual(testEntry, controlEntry) {
  546. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  547. }
  548. }
  549. func TestNewLineRewriteRule(t *testing.T) {
  550. controlEntry := &model.Entry{
  551. URL: "https://example.org/article",
  552. Title: `A title`,
  553. Content: `A<br>B<br>C`,
  554. }
  555. testEntry := &model.Entry{
  556. URL: "https://example.org/article",
  557. Title: `A title`,
  558. Content: "A\nB\nC",
  559. }
  560. ApplyContentRewriteRules(testEntry, "nl2br")
  561. if !reflect.DeepEqual(testEntry, controlEntry) {
  562. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  563. }
  564. }
  565. func TestConvertTextLinkRewriteRule(t *testing.T) {
  566. controlEntry := &model.Entry{
  567. URL: "https://example.org/article",
  568. Title: `A title`,
  569. Content: `Test: <a href="http://example.org/a/b">http://example.org/a/b</a>`,
  570. }
  571. testEntry := &model.Entry{
  572. URL: "https://example.org/article",
  573. Title: `A title`,
  574. Content: `Test: http://example.org/a/b`,
  575. }
  576. ApplyContentRewriteRules(testEntry, "convert_text_link")
  577. if !reflect.DeepEqual(testEntry, controlEntry) {
  578. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  579. }
  580. }
  581. func TestMediumImage(t *testing.T) {
  582. controlEntry := &model.Entry{
  583. URL: "https://example.org/article",
  584. Title: `A title`,
  585. 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"/>`,
  586. }
  587. testEntry := &model.Entry{
  588. URL: "https://example.org/article",
  589. Title: `A title`,
  590. Content: `
  591. <figure class="ht hu hv hw hx hy cy cz paragraph-image">
  592. <div class="hz ia ib ic aj">
  593. <div class="cy cz hs">
  594. <div class="ii s ib ij">
  595. <div class="ik il s">
  596. <div class="id ie t u v if aj bk ig ih">
  597. <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"/>
  598. </div>
  599. <img alt="Image for post" class="id ie t u v if aj c" width="1280" height="720"/>
  600. <noscript>
  601. <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"/>
  602. </noscript>
  603. </div>
  604. </div>
  605. </div>
  606. </div>
  607. </figure>
  608. `,
  609. }
  610. ApplyContentRewriteRules(testEntry, "fix_medium_images")
  611. testEntry.Content = strings.TrimSpace(testEntry.Content)
  612. if !reflect.DeepEqual(testEntry, controlEntry) {
  613. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  614. }
  615. }
  616. func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) {
  617. controlEntry := &model.Entry{
  618. URL: "https://example.org/article",
  619. Title: `A title`,
  620. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>`,
  621. }
  622. testEntry := &model.Entry{
  623. URL: "https://example.org/article",
  624. Title: `A title`,
  625. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>`,
  626. }
  627. ApplyContentRewriteRules(testEntry, "use_noscript_figure_images")
  628. testEntry.Content = strings.TrimSpace(testEntry.Content)
  629. if !reflect.DeepEqual(testEntry, controlEntry) {
  630. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  631. }
  632. }
  633. func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) {
  634. controlEntry := &model.Entry{
  635. URL: "https://example.org/article",
  636. Title: `A title`,
  637. Content: `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>`,
  638. }
  639. testEntry := &model.Entry{
  640. URL: "https://example.org/article",
  641. Title: `A title`,
  642. 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>`,
  643. }
  644. ApplyContentRewriteRules(testEntry, "use_noscript_figure_images")
  645. testEntry.Content = strings.TrimSpace(testEntry.Content)
  646. if !reflect.DeepEqual(testEntry, controlEntry) {
  647. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  648. }
  649. }
  650. func TestRewriteReplaceCustom(t *testing.T) {
  651. controlEntry := &model.Entry{
  652. URL: "https://example.org/article",
  653. Title: `A title`,
  654. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png">`,
  655. }
  656. testEntry := &model.Entry{
  657. URL: "https://example.org/article",
  658. Title: `A title`,
  659. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg">`,
  660. }
  661. ApplyContentRewriteRules(testEntry, `replace("article/(.*).svg"|"article/$1.png")`)
  662. if !reflect.DeepEqual(testEntry, controlEntry) {
  663. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  664. }
  665. }
  666. func TestRewriteReplaceTitleCustom(t *testing.T) {
  667. controlEntry := &model.Entry{
  668. URL: "https://example.org/article",
  669. Title: `Ouch, a thistle`,
  670. Content: `The replace_title rewrite rule should not modify the content.`,
  671. }
  672. testEntry := &model.Entry{
  673. URL: "https://example.org/article",
  674. Title: `A title`,
  675. Content: `The replace_title rewrite rule should not modify the content.`,
  676. }
  677. ApplyContentRewriteRules(testEntry, `replace_title("(?i)^a\\s*ti"|"Ouch, a this")`)
  678. if !reflect.DeepEqual(testEntry, controlEntry) {
  679. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  680. }
  681. }
  682. func TestRewriteRemoveCustom(t *testing.T) {
  683. controlEntry := &model.Entry{
  684. URL: "https://example.org/article",
  685. Title: `A title`,
  686. Content: `<div>Lorem Ipsum <span class="ads keep">Super important info</span></div>`,
  687. }
  688. testEntry := &model.Entry{
  689. URL: "https://example.org/article",
  690. Title: `A title`,
  691. Content: `<div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div>`,
  692. }
  693. ApplyContentRewriteRules(testEntry, `remove(".spam, .ads:not(.keep)")`)
  694. if !reflect.DeepEqual(testEntry, controlEntry) {
  695. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  696. }
  697. }
  698. func TestRewriteRemoveQuotedSelector(t *testing.T) {
  699. controlEntry := &model.Entry{
  700. URL: "https://example.org/article",
  701. Title: `A title`,
  702. Content: `<div>Lorem Ipsum</div>`,
  703. }
  704. testEntry := &model.Entry{
  705. URL: "https://example.org/article",
  706. Title: `A title`,
  707. Content: `<div>Lorem Ipsum<img alt="LINUX KERNEL" src="/assets/categories/linuxkernel.webp" width="100" height="100"></div>`,
  708. }
  709. ApplyContentRewriteRules(testEntry, `remove("img[src^='/assets/categories/']")`)
  710. if !reflect.DeepEqual(testEntry, controlEntry) {
  711. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  712. }
  713. }
  714. func TestRewriteAddCastopodEpisode(t *testing.T) {
  715. controlEntry := &model.Entry{
  716. URL: "https://podcast.demo/@demo/episodes/test",
  717. Title: `A title`,
  718. Content: `<iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description`,
  719. }
  720. testEntry := &model.Entry{
  721. URL: "https://podcast.demo/@demo/episodes/test",
  722. Title: `A title`,
  723. Content: `Episode Description`,
  724. }
  725. ApplyContentRewriteRules(testEntry, `add_castopod_episode`)
  726. if !reflect.DeepEqual(testEntry, controlEntry) {
  727. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  728. }
  729. }
  730. func TestRewriteBase64Decode(t *testing.T) {
  731. controlEntry := &model.Entry{
  732. URL: "https://example.org/article",
  733. Title: `A title`,
  734. Content: `This is some base64 encoded content`,
  735. }
  736. testEntry := &model.Entry{
  737. URL: "https://example.org/article",
  738. Title: `A title`,
  739. Content: `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=`,
  740. }
  741. ApplyContentRewriteRules(testEntry, `base64_decode`)
  742. if !reflect.DeepEqual(testEntry, controlEntry) {
  743. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  744. }
  745. }
  746. func TestRewriteBase64DecodeInHTML(t *testing.T) {
  747. controlEntry := &model.Entry{
  748. URL: "https://example.org/article",
  749. Title: `A title`,
  750. Content: `<div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div>`,
  751. }
  752. testEntry := &model.Entry{
  753. URL: "https://example.org/article",
  754. Title: `A title`,
  755. Content: `<div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  756. }
  757. ApplyContentRewriteRules(testEntry, `base64_decode`)
  758. if !reflect.DeepEqual(testEntry, controlEntry) {
  759. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  760. }
  761. }
  762. func TestRewriteBase64DecodeArgs(t *testing.T) {
  763. controlEntry := &model.Entry{
  764. URL: "https://example.org/article",
  765. Title: `A title`,
  766. Content: `<div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div>`,
  767. }
  768. testEntry := &model.Entry{
  769. URL: "https://example.org/article",
  770. Title: `A title`,
  771. Content: `<div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  772. }
  773. ApplyContentRewriteRules(testEntry, `base64_decode(".base64")`)
  774. if !reflect.DeepEqual(testEntry, controlEntry) {
  775. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  776. }
  777. }
  778. func TestRewriteRemoveTables(t *testing.T) {
  779. controlEntry := &model.Entry{
  780. URL: "https://example.org/article",
  781. Title: `A title`,
  782. Content: `<p>Test</p><p>Hello World!</p><p>Test</p>`,
  783. }
  784. testEntry := &model.Entry{
  785. URL: "https://example.org/article",
  786. Title: `A title`,
  787. 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>`,
  788. }
  789. ApplyContentRewriteRules(testEntry, `remove_tables`)
  790. if !reflect.DeepEqual(testEntry, controlEntry) {
  791. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  792. }
  793. }
  794. func TestRemoveClickbait(t *testing.T) {
  795. controlEntry := &model.Entry{
  796. URL: "https://example.org/article",
  797. Title: `This Is Amazing`,
  798. Content: `Some description`,
  799. }
  800. testEntry := &model.Entry{
  801. URL: "https://example.org/article",
  802. Title: `THIS IS AMAZING`,
  803. Content: `Some description`,
  804. }
  805. ApplyContentRewriteRules(testEntry, `remove_clickbait`)
  806. if !reflect.DeepEqual(testEntry, controlEntry) {
  807. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  808. }
  809. }
  810. func TestAddHackerNewsLinksUsingHack(t *testing.T) {
  811. testEntry := &model.Entry{
  812. URL: "https://example.org/article",
  813. Title: `A title`,
  814. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  815. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  816. <p>Points: 23</p>
  817. <p># Comments: 38</p>`,
  818. }
  819. controlEntry := &model.Entry{
  820. URL: "https://example.org/article",
  821. Title: `A title`,
  822. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  823. <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>
  824. <p>Points: 23</p>
  825. <p># Comments: 38</p>`,
  826. }
  827. ApplyContentRewriteRules(testEntry, `add_hn_links_using_hack`)
  828. if !reflect.DeepEqual(testEntry, controlEntry) {
  829. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  830. }
  831. }
  832. func TestAddHackerNewsLinksUsingOpener(t *testing.T) {
  833. testEntry := &model.Entry{
  834. URL: "https://example.org/article",
  835. Title: `A title`,
  836. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  837. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  838. <p>Points: 23</p>
  839. <p># Comments: 38</p>`,
  840. }
  841. controlEntry := &model.Entry{
  842. URL: "https://example.org/article",
  843. Title: `A title`,
  844. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  845. <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>
  846. <p>Points: 23</p>
  847. <p># Comments: 38</p>`,
  848. }
  849. ApplyContentRewriteRules(testEntry, `add_hn_links_using_opener`)
  850. if !reflect.DeepEqual(testEntry, controlEntry) {
  851. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  852. }
  853. }
  854. func TestAddImageTitle(t *testing.T) {
  855. testEntry := &model.Entry{
  856. URL: "https://example.org/article",
  857. Title: `A title`,
  858. Content: `
  859. <img src="pif" title="pouf">
  860. <img src="pif" title="pouf" alt='"onerror=alert(1) a="'>
  861. <img src="pif" title="pouf" alt='&quot;onerror=alert(1) a=&quot'>
  862. <img src="pif" title="pouf" alt=';&amp;quot;onerror=alert(1) a=;&amp;quot;'>
  863. <img src="pif" alt="pouf" title='"onerror=alert(1) a="'>
  864. <img src="pif" alt="pouf" title='&quot;onerror=alert(1) a=&quot'>
  865. <img src="pif" alt="pouf" title=';&amp;quot;onerror=alert(1) a=;&amp;quot;'>
  866. `,
  867. }
  868. controlEntry := &model.Entry{
  869. URL: "https://example.org/article",
  870. Title: `A title`,
  871. Content: `<figure><img src="pif" alt=""/><figcaption><p>pouf</p></figcaption></figure>
  872. <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
  873. <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
  874. <figure><img src="pif" alt=";&#34;onerror=alert(1) a=;&#34;"/><figcaption><p>pouf</p></figcaption></figure>
  875. <figure><img src="pif" alt="pouf"/><figcaption><p>&#34;onerror=alert(1) a=&#34;</p></figcaption></figure>
  876. <figure><img src="pif" alt="pouf"/><figcaption><p>&#34;onerror=alert(1) a=&#34;</p></figcaption></figure>
  877. <figure><img src="pif" alt="pouf"/><figcaption><p>;&amp;quot;onerror=alert(1) a=;&amp;quot;</p></figcaption></figure>
  878. `,
  879. }
  880. ApplyContentRewriteRules(testEntry, `add_image_title`)
  881. if !reflect.DeepEqual(testEntry, controlEntry) {
  882. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  883. }
  884. }
  885. func TestFixGhostCard(t *testing.T) {
  886. testEntry := &model.Entry{
  887. URL: "https://example.org/article",
  888. Title: `A title`,
  889. Content: `<figure class="kg-card kg-bookmark-card">
  890. <a class="kg-bookmark-container" href="https://example.org/article">
  891. <div class="kg-bookmark-content">
  892. <div class="kg-bookmark-title">Example Article</div>
  893. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  894. <div class="kg-bookmark-metadata">
  895. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  896. <span class="kg-bookmark-author">Example</span>
  897. <span class="kg-bookmark-publisher">Test Author</span>
  898. </div>
  899. </div>
  900. <div class="kg-bookmark-thumbnail">
  901. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  902. </div>
  903. </a>
  904. </figure>`,
  905. }
  906. controlEntry := &model.Entry{
  907. URL: "https://example.org/article",
  908. Title: `A title`,
  909. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  910. }
  911. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  912. if !reflect.DeepEqual(testEntry, controlEntry) {
  913. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  914. }
  915. }
  916. func TestFixGhostCardNoCard(t *testing.T) {
  917. testEntry := &model.Entry{
  918. URL: "https://example.org/article",
  919. Title: `A title`,
  920. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  921. }
  922. controlEntry := &model.Entry{
  923. URL: "https://example.org/article",
  924. Title: `A title`,
  925. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  926. }
  927. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  928. if !reflect.DeepEqual(testEntry, controlEntry) {
  929. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  930. }
  931. }
  932. func TestFixGhostCardInvalidCard(t *testing.T) {
  933. testEntry := &model.Entry{
  934. URL: "https://example.org/article",
  935. Title: `A title`,
  936. Content: `<figure class="kg-card kg-bookmark-card">
  937. <a href="https://example.org/article">This card does not have the required fields</a>
  938. </figure>`,
  939. }
  940. controlEntry := &model.Entry{
  941. URL: "https://example.org/article",
  942. Title: `A title`,
  943. Content: `<figure class="kg-card kg-bookmark-card">
  944. <a href="https://example.org/article">This card does not have the required fields</a>
  945. </figure>`,
  946. }
  947. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  948. if !reflect.DeepEqual(testEntry, controlEntry) {
  949. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  950. }
  951. }
  952. func TestFixGhostCardMissingAuthor(t *testing.T) {
  953. testEntry := &model.Entry{
  954. URL: "https://example.org/article",
  955. Title: `A title`,
  956. Content: `<figure class="kg-card kg-bookmark-card">
  957. <a class="kg-bookmark-container" href="https://example.org/article">
  958. <div class="kg-bookmark-content">
  959. <div class="kg-bookmark-title">Example Article</div>
  960. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  961. </div>
  962. <div class="kg-bookmark-thumbnail">
  963. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  964. </div>
  965. </a>
  966. </figure>`,
  967. }
  968. controlEntry := &model.Entry{
  969. URL: "https://example.org/article",
  970. Title: `A title`,
  971. Content: `<a href="https://example.org/article">Example Article</a>`,
  972. }
  973. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  974. if !reflect.DeepEqual(testEntry, controlEntry) {
  975. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  976. }
  977. }
  978. func TestFixGhostCardDuplicatedAuthor(t *testing.T) {
  979. testEntry := &model.Entry{
  980. URL: "https://example.org/article",
  981. Title: `A title`,
  982. Content: `<figure class="kg-card kg-bookmark-card">
  983. <a class="kg-bookmark-container" href="https://example.org/article">
  984. <div class="kg-bookmark-content">
  985. <div class="kg-bookmark-title">Example Article - Example</div>
  986. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  987. <div class="kg-bookmark-metadata">
  988. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  989. <span class="kg-bookmark-author">Example</span>
  990. <span class="kg-bookmark-publisher">Test Author</span>
  991. </div>
  992. </div>
  993. <div class="kg-bookmark-thumbnail">
  994. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  995. </div>
  996. </a>
  997. </figure>`,
  998. }
  999. controlEntry := &model.Entry{
  1000. URL: "https://example.org/article",
  1001. Title: `A title`,
  1002. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  1003. }
  1004. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1005. if !reflect.DeepEqual(testEntry, controlEntry) {
  1006. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1007. }
  1008. }
  1009. func TestFixGhostCardMultiple(t *testing.T) {
  1010. testEntry := &model.Entry{
  1011. URL: "https://example.org/article",
  1012. Title: `A title`,
  1013. Content: `<figure class="kg-card kg-bookmark-card">
  1014. <a class="kg-bookmark-container" href="https://example.org/article1">
  1015. <div class="kg-bookmark-content">
  1016. <div class="kg-bookmark-title">Example Article 1 - Example</div>
  1017. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1018. <div class="kg-bookmark-metadata">
  1019. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1020. <span class="kg-bookmark-author">Example</span>
  1021. <span class="kg-bookmark-publisher">Test Author</span>
  1022. </div>
  1023. </div>
  1024. <div class="kg-bookmark-thumbnail">
  1025. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1026. </div>
  1027. </a>
  1028. </figure>
  1029. <figure class="kg-card kg-bookmark-card">
  1030. <a class="kg-bookmark-container" href="https://example.org/article2">
  1031. <div class="kg-bookmark-content">
  1032. <div class="kg-bookmark-title">Example Article 2 - Example</div>
  1033. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1034. <div class="kg-bookmark-metadata">
  1035. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1036. <span class="kg-bookmark-author">Example</span>
  1037. <span class="kg-bookmark-publisher">Test Author</span>
  1038. </div>
  1039. </div>
  1040. <div class="kg-bookmark-thumbnail">
  1041. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1042. </div>
  1043. </a>
  1044. </figure>`,
  1045. }
  1046. controlEntry := &model.Entry{
  1047. URL: "https://example.org/article",
  1048. Title: `A title`,
  1049. 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>`,
  1050. }
  1051. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1052. if !reflect.DeepEqual(testEntry, controlEntry) {
  1053. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1054. }
  1055. }
  1056. func TestFixGhostCardMultipleSplit(t *testing.T) {
  1057. testEntry := &model.Entry{
  1058. URL: "https://example.org/article",
  1059. Title: `A title`,
  1060. Content: `<figure class="kg-card kg-bookmark-card">
  1061. <a class="kg-bookmark-container" href="https://example.org/article1">
  1062. <div class="kg-bookmark-content">
  1063. <div class="kg-bookmark-title">Example Article 1 - Example</div>
  1064. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1065. <div class="kg-bookmark-metadata">
  1066. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1067. <span class="kg-bookmark-author">Example</span>
  1068. <span class="kg-bookmark-publisher">Test Author</span>
  1069. </div>
  1070. </div>
  1071. <div class="kg-bookmark-thumbnail">
  1072. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1073. </div>
  1074. </a>
  1075. </figure>
  1076. <p>This separates the two cards</p>
  1077. <figure class="kg-card kg-bookmark-card">
  1078. <a class="kg-bookmark-container" href="https://example.org/article2">
  1079. <div class="kg-bookmark-content">
  1080. <div class="kg-bookmark-title">Example Article 2 - Example</div>
  1081. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1082. <div class="kg-bookmark-metadata">
  1083. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1084. <span class="kg-bookmark-author">Example</span>
  1085. <span class="kg-bookmark-publisher">Test Author</span>
  1086. </div>
  1087. </div>
  1088. <div class="kg-bookmark-thumbnail">
  1089. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1090. </div>
  1091. </a>
  1092. </figure>`,
  1093. }
  1094. controlEntry := &model.Entry{
  1095. URL: "https://example.org/article",
  1096. Title: `A title`,
  1097. Content: `<a href="https://example.org/article1">Example Article 1 - Example</a>
  1098. <p>This separates the two cards</p>
  1099. <a href="https://example.org/article2">Example Article 2 - Example</a>`,
  1100. }
  1101. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1102. if !reflect.DeepEqual(testEntry, controlEntry) {
  1103. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1104. }
  1105. }
  1106. func TestStripImageQueryParams(t *testing.T) {
  1107. testEntry := &model.Entry{
  1108. URL: "https://example.org/article",
  1109. Title: `News Article Title`,
  1110. Content: `
  1111. <article>
  1112. <p>Article content with images having query parameters:</p>
  1113. <img src="https://example.org/images/image1.jpg?width=200&height=113&q=80&blur=90" alt="Image with params">
  1114. <img src="https://example.org/images/image2.jpg?width=800&height=600&q=85" alt="Another image with params">
  1115. <p>More images with various query parameters:</p>
  1116. <img src="https://example.org/image123.jpg?blur=50&size=small&format=webp" alt="Complex query params">
  1117. <img src="https://example.org/image123.jpg?size=large&quality=95&cache=123" alt="Different params">
  1118. <p>Image without query parameters:</p>
  1119. <img src="https://example.org/single-image.jpg" alt="Clean image">
  1120. <p>Images with various other params:</p>
  1121. <img src="https://example.org/normal1.jpg?width=300&format=jpg" alt="Normal 1">
  1122. <img src="https://example.org/normal1.jpg?width=600&quality=high" alt="Normal 2">
  1123. </article>`,
  1124. }
  1125. controlEntry := &model.Entry{
  1126. URL: "https://example.org/article",
  1127. Title: `News Article Title`,
  1128. Content: `<article>
  1129. <p>Article content with images having query parameters:</p>
  1130. <img src="https://example.org/images/image1.jpg" alt="Image with params"/>
  1131. <img src="https://example.org/images/image2.jpg?width=800&amp;height=600&amp;q=85" alt="Another image with params"/>
  1132. <p>More images with various query parameters:</p>
  1133. <img src="https://example.org/image123.jpg" alt="Complex query params"/>
  1134. <img src="https://example.org/image123.jpg?size=large&amp;quality=95&amp;cache=123" alt="Different params"/>
  1135. <p>Image without query parameters:</p>
  1136. <img src="https://example.org/single-image.jpg" alt="Clean image"/>
  1137. <p>Images with various other params:</p>
  1138. <img src="https://example.org/normal1.jpg?width=300&amp;format=jpg" alt="Normal 1"/>
  1139. <img src="https://example.org/normal1.jpg?width=600&amp;quality=high" alt="Normal 2"/>
  1140. </article>`,
  1141. }
  1142. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1143. if !reflect.DeepEqual(testEntry, controlEntry) {
  1144. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1145. }
  1146. }
  1147. func TestStripImageQueryParamsNoChanges(t *testing.T) {
  1148. testEntry := &model.Entry{
  1149. URL: "https://example.org/article",
  1150. Title: `Article Without Images`,
  1151. Content: `<p>No images here:</p>
  1152. <div>Just some text content</div>
  1153. <a href="https://example.org">A link</a>`,
  1154. }
  1155. controlEntry := &model.Entry{
  1156. URL: "https://example.org/article",
  1157. Title: `Article Without Images`,
  1158. Content: `<p>No images here:</p>
  1159. <div>Just some text content</div>
  1160. <a href="https://example.org">A link</a>`,
  1161. }
  1162. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1163. if !reflect.DeepEqual(testEntry, controlEntry) {
  1164. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1165. }
  1166. }
  1167. func TestStripImageQueryParamsEdgeCases(t *testing.T) {
  1168. testEntry := &model.Entry{
  1169. URL: "https://example.org/article",
  1170. Title: `Edge Cases`,
  1171. Content: `
  1172. <p>Edge cases for image query parameter stripping:</p>
  1173. <!-- Various query parameters -->
  1174. <img src="https://example.org/image1.jpg?blur=80&width=300" alt="Multiple params">
  1175. <!-- Complex query parameters -->
  1176. <img src="https://example.org/image2.jpg?BLUR=60&format=webp&cache=123" alt="Complex params">
  1177. <img src="https://example.org/image3.jpg?quality=high&version=2" alt="Other params">
  1178. <!-- Query params in middle of string -->
  1179. <img src="https://example.org/image4.jpg?size=large&blur=30&format=webp&quality=90" alt="Middle params">
  1180. <!-- Image without query params -->
  1181. <img src="https://example.org/clean.jpg" alt="Clean image">
  1182. `,
  1183. }
  1184. controlEntry := &model.Entry{
  1185. URL: "https://example.org/article",
  1186. Title: `Edge Cases`,
  1187. Content: `<p>Edge cases for image query parameter stripping:</p>
  1188. <!-- Various query parameters -->
  1189. <img src="https://example.org/image1.jpg" alt="Multiple params"/>
  1190. <!-- Complex query parameters -->
  1191. <img src="https://example.org/image2.jpg?BLUR=60&amp;format=webp&amp;cache=123" alt="Complex params"/>
  1192. <img src="https://example.org/image3.jpg?quality=high&amp;version=2" alt="Other params"/>
  1193. <!-- Query params in middle of string -->
  1194. <img src="https://example.org/image4.jpg" alt="Middle params"/>
  1195. <!-- Image without query params -->
  1196. <img src="https://example.org/clean.jpg" alt="Clean image"/>
  1197. `,
  1198. }
  1199. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1200. if !reflect.DeepEqual(testEntry, controlEntry) {
  1201. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1202. }
  1203. }
  1204. func TestStripImageQueryParamsSimple(t *testing.T) {
  1205. testEntry := &model.Entry{
  1206. URL: "https://example.org/article",
  1207. Title: `Simple Test`,
  1208. Content: `
  1209. <p>Testing query parameter stripping:</p>
  1210. <!-- Images with various query parameters -->
  1211. <img src="https://example.org/test1.jpg?blur=0&width=300" alt="With blur zero">
  1212. <img src="https://example.org/test2.jpg?blur=50&width=300&format=webp" alt="With blur fifty">
  1213. <img src="https://example.org/test3.jpg?width=800&quality=high" alt="No blur param">
  1214. <img src="https://example.org/test4.jpg" alt="No params at all">
  1215. `,
  1216. }
  1217. controlEntry := &model.Entry{
  1218. URL: "https://example.org/article",
  1219. Title: `Simple Test`,
  1220. Content: `<p>Testing query parameter stripping:</p>
  1221. <!-- Images with various query parameters -->
  1222. <img src="https://example.org/test1.jpg?blur=0&amp;width=300" alt="With blur zero"/>
  1223. <img src="https://example.org/test2.jpg" alt="With blur fifty"/>
  1224. <img src="https://example.org/test3.jpg?width=800&amp;quality=high" alt="No blur param"/>
  1225. <img src="https://example.org/test4.jpg" alt="No params at all"/>
  1226. `,
  1227. }
  1228. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1229. if !reflect.DeepEqual(testEntry, controlEntry) {
  1230. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1231. }
  1232. }