
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 = '
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, ) } }