package eu.friedcutlet.yokan.services.extensions.js import kotlin.test.Test import kotlin.test.assertEquals /** * Exercises the injected Jsoup-like HTML DOM ([HTML_DOM_JS]) inside the real * QuickJS runtime, proving element selection, attribute/text extraction, CSS * combinators/pseudo-classes, `abs:` URL resolution, dual property/method text * access, and base64 helpers behave correctly on the engine extensions run on. */ class HtmlDomRuntimeTest { private fun evalWithDom(script: String): String { val engine = QuickJsJavaScriptEngine(bridge = JavaScriptMessageBridge { _, _ -> "null" }) return try { engine.evaluate(HTML_DOM_JS) engine.evaluate(script) } finally { engine.close() } } @Test fun parsesListingsAttributesAndText() { val result = evalWithDom( """ var html = '
' + '
Naruto
' + '
' + '
Bleach
' + '
'; var doc = new Document(html, "https://site.tld/browse/page"); var out = doc.select("div.listupd article a.tip").map(function (a) { return { name: a.selectFirst("div.tt").text, link: a.attr("href"), img: a.selectFirst("img").attr("abs:data-src") }; }); JSON.stringify(out); """.trimIndent(), ) assertEquals( "[{\"name\":\"Naruto\",\"link\":\"/anime/naruto\",\"img\":\"https://site.tld/img/n.jpg\"}," + "{\"name\":\"Bleach\",\"link\":\"https://x.tld/anime/bleach\",\"img\":\"https://site.tld/img/b.jpg\"}]", result, ) } @Test fun supportsCombinatorsPseudoClassesAndIndexing() { val result = evalWithDom( """ var html = '' + '
' + '
abc
'; var doc = new Document(html); var eps = doc.select("ul.eplister > li > a").map(function (a) { return a.attr("href") + ":" + a.selectFirst("span.epl-num").text(); }); var next = doc.selectFirst("div.pg a:not(.prev)").text(); var second = doc.select("div.s span:nth-child(2)").first().text(); var last = doc.selectFirst("div.s span:last-child").text(); var count = doc.select("ul.eplister a").size(); JSON.stringify({ eps: eps, next: next, second: second, last: last, count: count }); """.trimIndent(), ) assertEquals( "{\"eps\":[\"/e/1:EP 1\",\"/e/2:EP 2\"],\"next\":\"Next\",\"second\":\"b\",\"last\":\"c\",\"count\":2}", result, ) } @Test fun decodesEntitiesScriptDataAndBase64() { val result = evalWithDom( """ var html = '

Hello World & more

' + '' + '

tail

'; var doc = new Document(html); var text = doc.selectFirst("#d p").text; var scriptHasFile = String(doc.selectFirst("script").data()).indexOf("master.m3u8") !== -1; var style = doc.selectFirst("style").data(); var after = doc.selectFirst("p.after").text(); var scripts = doc.select("script").size(); var round = atob(btoa("hello-world")); var typeofText = typeof doc.selectFirst("#d p").text; JSON.stringify({ text: text, scriptHasFile: scriptHasFile, style: style, after: after, scripts: scripts, round: round, typeofText: typeofText }); """.trimIndent(), ) assertEquals( "{\"text\":\"Hello World & more\",\"scriptHasFile\":true,\"style\":\"p > b { color: red }\",\"after\":\"tail\"," + "\"scripts\":2,\"round\":\"hello-world\",\"typeofText\":\"function\"}", result, ) } @Test fun querySelectorHelpersReturnElements() { val result = evalWithDom( """ var html = 'M1M2'; var all = querySelectorAll(html, "a[data-em]").map(function (a) { return a.attr("data-em"); }); var one = querySelector(html, "a[data-em]").text(); JSON.stringify({ all: all, one: one }); """.trimIndent(), ) assertEquals("{\"all\":[\"e1\",\"e2\"],\"one\":\"M1\"}", result) } @Test fun supportsMangayomiCollectionIterationAndAccessors() { val result = evalWithDom( """ var doc = new Document('
'); var links = []; for (var anchor of doc.select("a")) { links.push({ href: anchor.getHref, src: anchor.selectFirst("img").getSrc }); } JSON.stringify(links); """.trimIndent(), ) assertEquals( "[{\"href\":\"/one\",\"src\":\"/1.jpg\"},{\"href\":\"/two\",\"src\":\"/2.jpg\"}]", result, ) } }