Skip to content

Commit

Permalink
fix: refine script/style regex further
Browse files Browse the repository at this point in the history
take into account boolean attributes
#2204
  • Loading branch information
dummdidumm committed Nov 16, 2023
1 parent 27c56d8 commit dd9cbf4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/svelte2tsx/src/utils/htmlxparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ function parseAttributes(str: string, start: number) {
return attrs;
}

// Regex ensures that attributes with > characters in them still result in the content being matched correctly
const scriptRegex =
/(<!--[^]*?-->)|(<script((?:\s+[^=>'"\/]+=(?:"[^"]*"|'[^']*'|[^>\s])|\s+[^=>'"\/]+)*\s*)>)([\S\s]*?)<\/script>/g;
const styleRegex =
/(<!--[^]*?-->)|(<style((?:\s+[^=>'"\/]+=(?:"[^"]*"|'[^']*'|[^>\s])|\s+[^=>'"\/]+)*\s*)>)([\S\s]*?)<\/style>/g;

function extractTag(htmlx: string, tag: 'script' | 'style') {
const exp = new RegExp(
// Regex ensures that attributes with > characters in them still result in the content being matched correctly
`(<!--[^]*?-->)|(<${tag}((?:\\s+[^=>]+=(?:"[^"]*"|'[^']*'|[^>\\s]))*\\s*[^>]*?)>)([\\S\\s]*?)<\\/${tag}>`,
'g'
);
const exp = tag === 'script' ? scriptRegex : styleRegex;
const matches: Node[] = [];

let match: RegExpExecArray | null = null;
Expand Down

0 comments on commit dd9cbf4

Please sign in to comment.