mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
refactor(strip_html): inline block kinds to match file style
Drop the module-level STRIP_BLOCKS table; the rest of the file keeps each filter self-contained (only escapeMap/unescapeMap are top-level maps shared across filters). Two openers don't justify a table. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
+11
-15
@@ -42,32 +42,28 @@ export function newline_to_br (this: FilterImpl, v: string) {
|
||||
return str.replace(/\r?\n/gm, '<br />\n')
|
||||
}
|
||||
|
||||
// Linear-time replacement for the previous backtracking regex
|
||||
// Linear-time strip via indexOf scan. A regex equivalent to the old
|
||||
// /<script[\s\S]*?<\/script>|<style[\s\S]*?<\/style>|<[\s\S]*?>|<!--[\s\S]*?-->/g
|
||||
// which is O(n^2) in V8 on inputs with many unclosed openers. JS regex has no atomic
|
||||
// groups / possessive quantifiers, so unrolled-loop patterns don't help asymptotically.
|
||||
// We scan forward with indexOf and cache the next known closer per kind so unclosed
|
||||
// openers don't re-scan the tail. Each character is visited O(1) times.
|
||||
const STRIP_BLOCKS = [['<script', '</script>'], ['<style', '</style>']] as const
|
||||
|
||||
// is O(n^2) on unclosed openers (JS regex has no atomic groups / memoization),
|
||||
// so we cache "next known </script>/</style> position" across openers.
|
||||
export function strip_html (this: FilterImpl, v: string) {
|
||||
const str = stringify(v)
|
||||
this.context.memoryLimit.use(str.length)
|
||||
const closers = [0, 0]
|
||||
let out = ''
|
||||
let i = 0
|
||||
let scriptEnd = 0
|
||||
let styleEnd = 0
|
||||
while (i < str.length) {
|
||||
const lt = str.indexOf('<', i)
|
||||
if (lt < 0) return out + str.slice(i)
|
||||
out += str.slice(i, lt)
|
||||
let end = -1
|
||||
for (let k = 0; k < STRIP_BLOCKS.length; k++) {
|
||||
const [opener, closer] = STRIP_BLOCKS[k]
|
||||
if (!str.startsWith(opener, lt)) continue
|
||||
const from = lt + opener.length
|
||||
if (closers[k] !== -1 && closers[k] < from) closers[k] = str.indexOf(closer, from)
|
||||
if (closers[k] >= 0) end = closers[k] + closer.length
|
||||
break
|
||||
if (str.startsWith('<script', lt)) {
|
||||
if (scriptEnd >= 0 && scriptEnd < lt + 7) scriptEnd = str.indexOf('</script>', lt + 7)
|
||||
if (scriptEnd >= 0) end = scriptEnd + 9
|
||||
} else if (str.startsWith('<style', lt)) {
|
||||
if (styleEnd >= 0 && styleEnd < lt + 6) styleEnd = str.indexOf('</style>', lt + 6)
|
||||
if (styleEnd >= 0) end = styleEnd + 8
|
||||
}
|
||||
if (end < 0) end = str.indexOf('>', lt + 1) + 1
|
||||
if (end <= 0) return out + str.slice(lt)
|
||||
|
||||
Reference in New Issue
Block a user