content_rewrite_test.go 48 KB

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