rewriter_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. Title: `A title`,
  45. Content: `Some text.`,
  46. }
  47. testEntry := &model.Entry{
  48. Title: `A title`,
  49. Content: `Some text.`,
  50. }
  51. Rewriter("https://example.org/article", testEntry, ``)
  52. if !reflect.DeepEqual(testEntry, controlEntry) {
  53. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  54. }
  55. }
  56. func TestRewriteWithYoutubeLink(t *testing.T) {
  57. config.Opts = config.NewOptions()
  58. controlEntry := &model.Entry{
  59. Title: `A title`,
  60. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description`,
  61. }
  62. testEntry := &model.Entry{
  63. Title: `A title`,
  64. Content: `Video Description`,
  65. }
  66. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, ``)
  67. if !reflect.DeepEqual(testEntry, controlEntry) {
  68. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  69. }
  70. }
  71. func TestRewriteWithYoutubeLinkAndCustomEmbedURL(t *testing.T) {
  72. os.Clearenv()
  73. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  74. var err error
  75. parser := config.NewParser()
  76. config.Opts, err = parser.ParseEnvironmentVariables()
  77. if err != nil {
  78. t.Fatalf(`Parsing failure: %v`, err)
  79. }
  80. controlEntry := &model.Entry{
  81. Title: `A title`,
  82. Content: `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/1234" allowfullscreen></iframe><br>Video Description`,
  83. }
  84. testEntry := &model.Entry{
  85. Title: `A title`,
  86. Content: `Video Description`,
  87. }
  88. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, ``)
  89. if !reflect.DeepEqual(testEntry, controlEntry) {
  90. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  91. }
  92. }
  93. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  94. controlEntry := &model.Entry{
  95. Title: `A title`,
  96. Content: `Video Description`,
  97. }
  98. testEntry := &model.Entry{
  99. Title: `A title`,
  100. Content: `Video Description`,
  101. }
  102. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, `some rule`)
  103. if !reflect.DeepEqual(testEntry, controlEntry) {
  104. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  105. }
  106. }
  107. func TestRewriteWithXkcdLink(t *testing.T) {
  108. controlEntry := &model.Entry{
  109. Title: `A title`,
  110. 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>`,
  111. }
  112. testEntry := &model.Entry{
  113. Title: `A title`,
  114. 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." />`,
  115. }
  116. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  117. if !reflect.DeepEqual(testEntry, controlEntry) {
  118. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  119. }
  120. }
  121. func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) {
  122. controlEntry := &model.Entry{
  123. Title: `A title`,
  124. Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="&lt;foo&gt;"/><figcaption><p>&lt;foo&gt;</p></figcaption></figure>`,
  125. }
  126. testEntry := &model.Entry{
  127. Title: `A title`,
  128. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" />`,
  129. }
  130. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  131. if !reflect.DeepEqual(testEntry, controlEntry) {
  132. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  133. }
  134. }
  135. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  136. controlEntry := &model.Entry{
  137. Title: `A title`,
  138. 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." />`,
  139. }
  140. testEntry := &model.Entry{
  141. Title: `A title`,
  142. 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." />`,
  143. }
  144. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  145. if !reflect.DeepEqual(testEntry, controlEntry) {
  146. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  147. }
  148. }
  149. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  150. controlEntry := &model.Entry{
  151. Title: `A title`,
  152. Content: `test`,
  153. }
  154. testEntry := &model.Entry{
  155. Title: `A title`,
  156. Content: `test`,
  157. }
  158. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  159. if !reflect.DeepEqual(testEntry, controlEntry) {
  160. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  161. }
  162. }
  163. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  164. controlEntry := &model.Entry{
  165. Title: `A title`,
  166. Content: `test`,
  167. }
  168. testEntry := &model.Entry{
  169. Title: `A title`,
  170. Content: `test`,
  171. }
  172. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  173. if !reflect.DeepEqual(testEntry, controlEntry) {
  174. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  175. }
  176. }
  177. func TestRewriteMailtoLink(t *testing.T) {
  178. controlEntry := &model.Entry{
  179. Title: `A title`,
  180. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a>`,
  181. }
  182. testEntry := &model.Entry{
  183. Title: `A title`,
  184. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a>`,
  185. }
  186. Rewriter("https://www.qwantz.com/", testEntry, ``)
  187. if !reflect.DeepEqual(testEntry, controlEntry) {
  188. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  189. }
  190. }
  191. func TestRewriteWithPDFLink(t *testing.T) {
  192. controlEntry := &model.Entry{
  193. Title: `A title`,
  194. Content: `<a href="https://example.org/document.pdf">PDF</a><br>test`,
  195. }
  196. testEntry := &model.Entry{
  197. Title: `A title`,
  198. Content: `test`,
  199. }
  200. Rewriter("https://example.org/document.pdf", testEntry, ``)
  201. if !reflect.DeepEqual(testEntry, controlEntry) {
  202. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  203. }
  204. }
  205. func TestRewriteWithNoLazyImage(t *testing.T) {
  206. controlEntry := &model.Entry{
  207. Title: `A title`,
  208. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  209. }
  210. testEntry := &model.Entry{
  211. Title: `A title`,
  212. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  213. }
  214. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  215. if !reflect.DeepEqual(testEntry, controlEntry) {
  216. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  217. }
  218. }
  219. func TestRewriteWithLazyImage(t *testing.T) {
  220. controlEntry := &model.Entry{
  221. Title: `A title`,
  222. 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>`,
  223. }
  224. testEntry := &model.Entry{
  225. Title: `A title`,
  226. Content: `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  227. }
  228. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  229. if !reflect.DeepEqual(testEntry, controlEntry) {
  230. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  231. }
  232. }
  233. func TestRewriteWithLazyDivImage(t *testing.T) {
  234. controlEntry := &model.Entry{
  235. Title: `A title`,
  236. Content: `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  237. }
  238. testEntry := &model.Entry{
  239. Title: `A title`,
  240. Content: `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  241. }
  242. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  243. if !reflect.DeepEqual(testEntry, controlEntry) {
  244. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  245. }
  246. }
  247. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  248. controlEntry := &model.Entry{
  249. Title: `A title`,
  250. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`,
  251. }
  252. testEntry := &model.Entry{
  253. Title: `A title`,
  254. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  255. }
  256. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  257. if !reflect.DeepEqual(testEntry, controlEntry) {
  258. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  259. }
  260. }
  261. func TestRewriteWithLazySrcset(t *testing.T) {
  262. controlEntry := &model.Entry{
  263. Title: `A title`,
  264. Content: `<img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  265. }
  266. testEntry := &model.Entry{
  267. Title: `A title`,
  268. Content: `<img srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  269. }
  270. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  271. if !reflect.DeepEqual(testEntry, controlEntry) {
  272. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  273. }
  274. }
  275. func TestRewriteWithImageAndLazySrcset(t *testing.T) {
  276. controlEntry := &model.Entry{
  277. Title: `A title`,
  278. Content: `<img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  279. }
  280. testEntry := &model.Entry{
  281. Title: `A title`,
  282. Content: `<img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  283. }
  284. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  285. if !reflect.DeepEqual(testEntry, controlEntry) {
  286. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  287. }
  288. }
  289. func TestRewriteWithNoLazyIframe(t *testing.T) {
  290. controlEntry := &model.Entry{
  291. Title: `A title`,
  292. Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
  293. }
  294. testEntry := &model.Entry{
  295. Title: `A title`,
  296. Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
  297. }
  298. Rewriter("https://example.org/article", testEntry, "add_dynamic_iframe")
  299. if !reflect.DeepEqual(testEntry, controlEntry) {
  300. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  301. }
  302. }
  303. func TestRewriteWithLazyIframe(t *testing.T) {
  304. controlEntry := &model.Entry{
  305. Title: `A title`,
  306. Content: `<iframe data-src="https://example.org/embed" allowfullscreen="" src="https://example.org/embed"></iframe>`,
  307. }
  308. testEntry := &model.Entry{
  309. Title: `A title`,
  310. Content: `<iframe data-src="https://example.org/embed" allowfullscreen></iframe>`,
  311. }
  312. Rewriter("https://example.org/article", testEntry, "add_dynamic_iframe")
  313. if !reflect.DeepEqual(testEntry, controlEntry) {
  314. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  315. }
  316. }
  317. func TestRewriteWithLazyIframeAndSrc(t *testing.T) {
  318. controlEntry := &model.Entry{
  319. Title: `A title`,
  320. Content: `<iframe src="https://example.org/embed" data-src="https://example.org/embed" allowfullscreen=""></iframe>`,
  321. }
  322. testEntry := &model.Entry{
  323. Title: `A title`,
  324. Content: `<iframe src="about:blank" data-src="https://example.org/embed" allowfullscreen></iframe>`,
  325. }
  326. Rewriter("https://example.org/article", testEntry, "add_dynamic_iframe")
  327. if !reflect.DeepEqual(testEntry, controlEntry) {
  328. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  329. }
  330. }
  331. func TestNewLineRewriteRule(t *testing.T) {
  332. controlEntry := &model.Entry{
  333. Title: `A title`,
  334. Content: `A<br>B<br>C`,
  335. }
  336. testEntry := &model.Entry{
  337. Title: `A title`,
  338. Content: "A\nB\nC",
  339. }
  340. Rewriter("https://example.org/article", testEntry, "nl2br")
  341. if !reflect.DeepEqual(testEntry, controlEntry) {
  342. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  343. }
  344. }
  345. func TestConvertTextLinkRewriteRule(t *testing.T) {
  346. controlEntry := &model.Entry{
  347. Title: `A title`,
  348. Content: `Test: <a href="http://example.org/a/b">http://example.org/a/b</a>`,
  349. }
  350. testEntry := &model.Entry{
  351. Title: `A title`,
  352. Content: `Test: http://example.org/a/b`,
  353. }
  354. Rewriter("https://example.org/article", testEntry, "convert_text_link")
  355. if !reflect.DeepEqual(testEntry, controlEntry) {
  356. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  357. }
  358. }
  359. func TestMediumImage(t *testing.T) {
  360. controlEntry := &model.Entry{
  361. Title: `A title`,
  362. 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"/>`,
  363. }
  364. testEntry := &model.Entry{
  365. Title: `A title`,
  366. Content: `
  367. <figure class="ht hu hv hw hx hy cy cz paragraph-image">
  368. <div class="hz ia ib ic aj">
  369. <div class="cy cz hs">
  370. <div class="ii s ib ij">
  371. <div class="ik il s">
  372. <div class="id ie t u v if aj bk ig ih">
  373. <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"/>
  374. </div>
  375. <img alt="Image for post" class="id ie t u v if aj c" width="1280" height="720"/>
  376. <noscript>
  377. <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"/>
  378. </noscript>
  379. </div>
  380. </div>
  381. </div>
  382. </div>
  383. </figure>
  384. `,
  385. }
  386. Rewriter("https://example.org/article", testEntry, "fix_medium_images")
  387. testEntry.Content = strings.TrimSpace(testEntry.Content)
  388. if !reflect.DeepEqual(testEntry, controlEntry) {
  389. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  390. }
  391. }
  392. func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) {
  393. controlEntry := &model.Entry{
  394. Title: `A title`,
  395. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>`,
  396. }
  397. testEntry := &model.Entry{
  398. Title: `A title`,
  399. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>`,
  400. }
  401. Rewriter("https://example.org/article", testEntry, "use_noscript_figure_images")
  402. testEntry.Content = strings.TrimSpace(testEntry.Content)
  403. if !reflect.DeepEqual(testEntry, controlEntry) {
  404. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  405. }
  406. }
  407. func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) {
  408. controlEntry := &model.Entry{
  409. Title: `A title`,
  410. Content: `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>`,
  411. }
  412. testEntry := &model.Entry{
  413. Title: `A title`,
  414. 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>`,
  415. }
  416. Rewriter("https://example.org/article", testEntry, "use_noscript_figure_images")
  417. testEntry.Content = strings.TrimSpace(testEntry.Content)
  418. if !reflect.DeepEqual(testEntry, controlEntry) {
  419. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  420. }
  421. }
  422. func TestRewriteReplaceCustom(t *testing.T) {
  423. controlEntry := &model.Entry{
  424. Title: `A title`,
  425. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png">`,
  426. }
  427. testEntry := &model.Entry{
  428. Title: `A title`,
  429. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg">`,
  430. }
  431. Rewriter("https://example.org/article", testEntry, `replace("article/(.*).svg"|"article/$1.png")`)
  432. if !reflect.DeepEqual(testEntry, controlEntry) {
  433. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  434. }
  435. }
  436. func TestRewriteReplaceTitleCustom(t *testing.T) {
  437. controlEntry := &model.Entry{
  438. Title: `Ouch, a thistle`,
  439. Content: `The replace_title rewrite rule should not modify the content.`,
  440. }
  441. testEntry := &model.Entry{
  442. Title: `A title`,
  443. Content: `The replace_title rewrite rule should not modify the content.`,
  444. }
  445. Rewriter("https://example.org/article", testEntry, `replace_title("(?i)^a\\s*ti"|"Ouch, a this")`)
  446. if !reflect.DeepEqual(testEntry, controlEntry) {
  447. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  448. }
  449. }
  450. func TestRewriteRemoveCustom(t *testing.T) {
  451. controlEntry := &model.Entry{
  452. Title: `A title`,
  453. Content: `<div>Lorem Ipsum <span class="ads keep">Super important info</span></div>`,
  454. }
  455. testEntry := &model.Entry{
  456. Title: `A title`,
  457. Content: `<div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div>`,
  458. }
  459. Rewriter("https://example.org/article", testEntry, `remove(".spam, .ads:not(.keep)")`)
  460. if !reflect.DeepEqual(testEntry, controlEntry) {
  461. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  462. }
  463. }
  464. func TestRewriteAddCastopodEpisode(t *testing.T) {
  465. controlEntry := &model.Entry{
  466. Title: `A title`,
  467. Content: `<iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description`,
  468. }
  469. testEntry := &model.Entry{
  470. Title: `A title`,
  471. Content: `Episode Description`,
  472. }
  473. Rewriter("https://podcast.demo/@demo/episodes/test", testEntry, `add_castopod_episode`)
  474. if !reflect.DeepEqual(testEntry, controlEntry) {
  475. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  476. }
  477. }
  478. func TestRewriteBase64Decode(t *testing.T) {
  479. controlEntry := &model.Entry{
  480. Title: `A title`,
  481. Content: `This is some base64 encoded content`,
  482. }
  483. testEntry := &model.Entry{
  484. Title: `A title`,
  485. Content: `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=`,
  486. }
  487. Rewriter("https://example.org/article", testEntry, `base64_decode`)
  488. if !reflect.DeepEqual(testEntry, controlEntry) {
  489. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  490. }
  491. }
  492. func TestRewriteBase64DecodeInHTML(t *testing.T) {
  493. controlEntry := &model.Entry{
  494. Title: `A title`,
  495. Content: `<div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div>`,
  496. }
  497. testEntry := &model.Entry{
  498. Title: `A title`,
  499. Content: `<div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  500. }
  501. Rewriter("https://example.org/article", testEntry, `base64_decode`)
  502. if !reflect.DeepEqual(testEntry, controlEntry) {
  503. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  504. }
  505. }
  506. func TestRewriteBase64DecodeArgs(t *testing.T) {
  507. controlEntry := &model.Entry{
  508. Title: `A title`,
  509. Content: `<div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div>`,
  510. }
  511. testEntry := &model.Entry{
  512. Title: `A title`,
  513. Content: `<div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  514. }
  515. Rewriter("https://example.org/article", testEntry, `base64_decode(".base64")`)
  516. if !reflect.DeepEqual(testEntry, controlEntry) {
  517. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  518. }
  519. }
  520. func TestRewriteRemoveTables(t *testing.T) {
  521. controlEntry := &model.Entry{
  522. Title: `A title`,
  523. Content: `<p>Test</p><p>Hello World!</p><p>Test</p>`,
  524. }
  525. testEntry := &model.Entry{
  526. Title: `A title`,
  527. 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>`,
  528. }
  529. Rewriter("https://example.org/article", testEntry, `remove_tables`)
  530. if !reflect.DeepEqual(testEntry, controlEntry) {
  531. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  532. }
  533. }
  534. func TestRemoveClickbait(t *testing.T) {
  535. controlEntry := &model.Entry{
  536. Title: `This Is Amazing`,
  537. Content: `Some description`,
  538. }
  539. testEntry := &model.Entry{
  540. Title: `THIS IS AMAZING`,
  541. Content: `Some description`,
  542. }
  543. Rewriter("https://example.org/article", testEntry, `remove_clickbait`)
  544. if !reflect.DeepEqual(testEntry, controlEntry) {
  545. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  546. }
  547. }
  548. func TestAddHackerNewsLinksUsingHack(t *testing.T) {
  549. testEntry := &model.Entry{
  550. Title: `A title`,
  551. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  552. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  553. <p>Points: 23</p>
  554. <p># Comments: 38</p>`,
  555. }
  556. controlEntry := &model.Entry{
  557. Title: `A title`,
  558. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  559. <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>
  560. <p>Points: 23</p>
  561. <p># Comments: 38</p>`,
  562. }
  563. Rewriter("https://example.org/article", testEntry, `add_hn_links_using_hack`)
  564. if !reflect.DeepEqual(testEntry, controlEntry) {
  565. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  566. }
  567. }
  568. func TestAddHackerNewsLinksUsingOpener(t *testing.T) {
  569. testEntry := &model.Entry{
  570. Title: `A title`,
  571. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  572. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  573. <p>Points: 23</p>
  574. <p># Comments: 38</p>`,
  575. }
  576. controlEntry := &model.Entry{
  577. Title: `A title`,
  578. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  579. <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>
  580. <p>Points: 23</p>
  581. <p># Comments: 38</p>`,
  582. }
  583. Rewriter("https://example.org/article", testEntry, `add_hn_links_using_opener`)
  584. if !reflect.DeepEqual(testEntry, controlEntry) {
  585. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  586. }
  587. }
  588. func TestAddImageTitle(t *testing.T) {
  589. testEntry := &model.Entry{
  590. Title: `A title`,
  591. Content: `
  592. <img src="pif" title="pouf">
  593. <img src="pif" title="pouf" alt='"onerror=alert(1) a="'>
  594. <img src="pif" title="pouf" alt='&quot;onerror=alert(1) a=&quot'>
  595. <img src="pif" title="pouf" alt=';&amp;quot;onerror=alert(1) a=;&amp;quot;'>
  596. <img src="pif" alt="pouf" title='"onerror=alert(1) a="'>
  597. <img src="pif" alt="pouf" title='&quot;onerror=alert(1) a=&quot'>
  598. <img src="pif" alt="pouf" title=';&amp;quot;onerror=alert(1) a=;&amp;quot;'>
  599. `,
  600. }
  601. controlEntry := &model.Entry{
  602. Title: `A title`,
  603. Content: `<figure><img src="pif" alt=""/><figcaption><p>pouf</p></figcaption></figure>
  604. <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
  605. <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
  606. <figure><img src="pif" alt=";&#34;onerror=alert(1) a=;&#34;"/><figcaption><p>pouf</p></figcaption></figure>
  607. <figure><img src="pif" alt="pouf"/><figcaption><p>&#34;onerror=alert(1) a=&#34;</p></figcaption></figure>
  608. <figure><img src="pif" alt="pouf"/><figcaption><p>&#34;onerror=alert(1) a=&#34;</p></figcaption></figure>
  609. <figure><img src="pif" alt="pouf"/><figcaption><p>;&amp;quot;onerror=alert(1) a=;&amp;quot;</p></figcaption></figure>
  610. `,
  611. }
  612. Rewriter("https://example.org/article", testEntry, `add_image_title`)
  613. if !reflect.DeepEqual(testEntry, controlEntry) {
  614. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  615. }
  616. }