@@ -4,7 +4,6 @@ import { fileURLToPath } from "node:url";
44
55import { initializeCore , options } from "@triliumnext/core" ;
66import schemaSql from "@triliumnext/core/src/assets/schema.sql?raw" ;
7- import HappyDomHtmlParser from "happy-dom/lib/html-parser/HTMLParser.js" ;
87import serverEnTranslations from "../../server/src/assets/translations/en/server.json" ;
98import { beforeAll } from "vitest" ;
109
@@ -71,23 +70,17 @@ WebAssembly.instantiateStreaming = (async (source, importObject) => {
7170// Per HTML5 parsing spec, a single U+000A LINE FEED immediately after a <pre>,
7271// <listing>, or <textarea> start tag must be ignored ("newlines at the start
7372// of pre blocks are ignored as an authoring convenience"). Real browsers and
74- // domino (which the server runtime uses via turnish) both implement this;
75- // happy-dom (as of 20.8.9) does not — it keeps the LF as a text node.
76- //
77- // That difference makes turnish's markdown export produce different output
78- // under happy-dom vs. production, breaking markdown.spec.ts > "exports jQuery
79- // code in table properly". Patch HTMLParser.parse to pre-process the string.
73+ // domino (which turnish uses in Node) both implement this; happy-dom does not.
74+ // Patch at the DOMParser boundary since turnish prefers DOMParser when it's
75+ // available — patching via module-level HTMLParser import hits a different
76+ // happy-dom copy than the vitest env loaded.
8077const LEADING_LF_IN_PRE_RE = / ( < (?: p r e | l i s t i n g | t e x t a r e a ) \b [ ^ > ] * > ) ( \r \n | \r | \n ) / gi;
81- const originalHtmlParserParse = ( HappyDomHtmlParser as unknown as {
82- prototype : { parse ( html : string , rootNode ?: unknown ) : unknown } ;
83- } ) . prototype . parse ;
84- ( HappyDomHtmlParser as unknown as {
85- prototype : { parse ( html : string , rootNode ?: unknown ) : unknown } ;
86- } ) . prototype . parse = function ( html : string , rootNode ?: unknown ) {
87- const patched = typeof html === "string"
88- ? html . replace ( LEADING_LF_IN_PRE_RE , "$1" )
89- : html ;
90- return originalHtmlParserParse . call ( this , patched , rootNode ) ;
78+ const originalParseFromString = DOMParser . prototype . parseFromString ;
79+ DOMParser . prototype . parseFromString = function ( source : string , type : DOMParserSupportedType ) {
80+ const patched = typeof source === "string"
81+ ? source . replace ( LEADING_LF_IN_PRE_RE , "$1" )
82+ : source ;
83+ return originalParseFromString . call ( this , patched , type ) ;
9184} ;
9285
9386// =============================================================================
0 commit comments