Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/core/src/lint/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ export function findHtmlTag(source: string): OpenTag | null {
};
}

// Same-length whitespace keeps downstream offsets aligned.
function maskRanges(src: string, pattern: RegExp): string {
const p = new RegExp(
pattern.source,
pattern.flags.includes("g") ? pattern.flags : pattern.flags + "g",
);
let out = src;
let m: RegExpExecArray | null;
while ((m = p.exec(out)) !== null) {
out = out.slice(0, m.index) + " ".repeat(m[0].length) + out.slice(m.index + m[0].length);
}
return out;
}

export function findRootTag(source: string): OpenTag | null {
const bodyOpenMatch = /<body\b[^>]*>/i.exec(source);
const bodyCloseMatch = /<\/body>/i.exec(source);
Expand All @@ -88,7 +102,12 @@ export function findRootTag(source: string): OpenTag | null {
? bodyCloseMatch.index
: source.length;
const bodyContent = bodyOpenMatch ? source.slice(bodyStart, bodyEnd) : source;
const bodyTags = extractOpenTags(bodyContent);
// Mask comments / <style> / <script> so a `<video>` token inside any of them isn't picked as root.
const masked = maskRanges(
maskRanges(maskRanges(bodyContent, /<!--[\s\S]*?-->/g), STYLE_BLOCK_PATTERN),
SCRIPT_BLOCK_PATTERN,
);
const bodyTags = extractOpenTags(masked);
for (const tag of bodyTags) {
if (["script", "style", "meta", "link", "title"].includes(tag.name)) continue;
return { ...tag, index: tag.index + bodyStart };
Expand Down
Loading