Skip to content

Commit

Permalink
refactor: Move js script checks in the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Apr 5, 2024
1 parent e145d83 commit a61acc9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
18 changes: 16 additions & 2 deletions src/detectors/transpilers/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});

Expand Down
15 changes: 2 additions & 13 deletions src/linter/html/linter.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -9,18 +9,7 @@ import type {ReadStream} from "node:fs";
export async function lintHtml(resourceName: string, contentStream: ReadStream): Promise<TranspileResult> {
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();
Expand Down

0 comments on commit a61acc9

Please sign in to comment.