From 931784b401011272eb244ef59386d0009666aa20 Mon Sep 17 00:00:00 2001 From: Maruthan G Date: Sat, 11 Apr 2026 14:18:44 +0530 Subject: [PATCH] fix: add isModule property to Node for script type=module detection (#133) --- src/htmlLanguageTypes.ts | 6 ++++++ src/parser/htmlParser.ts | 12 ++++++++++++ src/test/parser.test.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/htmlLanguageTypes.ts b/src/htmlLanguageTypes.ts index fd75c7e..e87e334 100644 --- a/src/htmlLanguageTypes.ts +++ b/src/htmlLanguageTypes.ts @@ -84,6 +84,12 @@ export interface Node { children: Node[]; parent?: Node; attributes?: { [name: string]: string | null } | undefined; + /** + * Returns true if this is a `'); + assert.strictEqual(doc1.roots[0].isModule, true); + + // Classic scripts share the global scope + const doc2 = parse(''); + assert.strictEqual(doc2.roots[0].isModule, false); + + // Single-quoted attribute value + const doc3 = parse(""); + assert.strictEqual(doc3.roots[0].isModule, true); + + // Other type values are not modules + const doc4 = parse(''); + assert.strictEqual(doc4.roots[0].isModule, false); + + // Non-script tags are not modules + const doc5 = parse('
'); + assert.strictEqual(doc5.roots[0].isModule, false); + + // Multiple scripts: module scripts have isolated scopes + const doc6 = parse(''); + assert.strictEqual(doc6.roots[0].isModule, true); + assert.strictEqual(doc6.roots[1].isModule, true); + + // Mixed: module and classic scripts + const doc7 = parse(''); + assert.strictEqual(doc7.roots[0].isModule, true); + assert.strictEqual(doc7.roots[1].isModule, false); + }); });