4
0

content_rewrite_test.go 63 KB

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