diff --git a/src/detectors/transpilers/html/parser.ts b/src/detectors/transpilers/html/parser.ts index 41e6faa8c..cc72550b7 100644 --- a/src/detectors/transpilers/html/parser.ts +++ b/src/detectors/transpilers/html/parser.ts @@ -45,13 +45,27 @@ async function parseHtml(contentStream: ReadStream, parseHandler: (type: SaxEven saxParser.end(); } -export async function extractScriptTags(contentStream: ReadStream) { +export async function extractJSScriptTags(contentStream: ReadStream) { const scriptTags: SaxTag[] = []; + await parseHtml(contentStream, (event, tag) => { if (tag instanceof SaxTag && event === SaxEventType.CloseTag && tag.value === "script") { - scriptTags.push(tag); + const isJSScriptTag = tag.attributes.every((attr) => { + // The "type" attribute of the script tag should be + // 1. not set (default), + // 2. an empty string, + // 3. or a JavaScript MIME type (text/javascript) + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type#attribute_is_not_set_default_an_empty_string_or_a_javascript_mime_type + return attr.name.value !== "type" || + (attr.name.value === "type" && + (attr.value.value === "" || attr.value.value === "text/javascript")); + }); + + if (isJSScriptTag) { + scriptTags.push(tag); + } } }); diff --git a/src/linter/html/linter.ts b/src/linter/html/linter.ts index 6ed3e38ed..6dd781aea 100644 --- a/src/linter/html/linter.ts +++ b/src/linter/html/linter.ts @@ -1,5 +1,5 @@ import {taskStart} from "../../detectors/util/perf.js"; -import {extractScriptTags} from "../../detectors/transpilers/html/parser.js"; +import {extractJSScriptTags} from "../../detectors/transpilers/html/parser.js"; import {LintMessageSeverity} from "../../detectors/AbstractDetector.js"; import HtmlReporter from "./HtmlReporter.js"; @@ -9,18 +9,7 @@ import type {ReadStream} from "node:fs"; export async function lintHtml(resourceName: string, contentStream: ReadStream): Promise { const taskLintEnd = taskStart("Linting HTML", resourceName); const report = new HtmlReporter(resourceName); - - const scriptTags = await extractScriptTags(contentStream); - const jsScriptTags = scriptTags.filter((tag) => tag.attributes.every((attr) => { - // The "type" attribute of the script tag should be - // 1. not set (default), - // 2. an empty string, - // 3. or a JavaScript MIME type (text/javascript) - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type#attribute_is_not_set_default_an_empty_string_or_a_javascript_mime_type - return attr.name.value !== "type" || - (attr.name.value === "type" && - (attr.value.value === "" || attr.value.value === "text/javascript")); - })); + const jsScriptTags = await extractJSScriptTags(contentStream); jsScriptTags.forEach((tag) => { const scriptContent = tag.textNodes?.map((tNode) => tNode.value).join("").trim();