Skip to content

Commit 5ea9edb

Browse files
authored
fix(html): handle offset magic-string slice error (#15435)
1 parent 49d21fe commit 5ea9edb

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

packages/vite/src/node/plugins/html.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
345345
const nodeStartWithLeadingWhitespace = (
346346
node: DefaultTreeAdapterMap['node'],
347347
) => {
348-
if (node.sourceCodeLocation!.startOffset === 0)
349-
return node.sourceCodeLocation!.startOffset
348+
const startOffset = node.sourceCodeLocation!.startOffset
349+
if (startOffset === 0) return 0
350350

351351
// Gets the offset for the start of the line including the
352352
// newline trailing the previous node
353353
const lineStartOffset =
354-
node.sourceCodeLocation!.startOffset -
355-
node.sourceCodeLocation!.startCol
356-
const line = s.slice(
357-
Math.max(0, lineStartOffset),
358-
node.sourceCodeLocation!.startOffset,
359-
)
354+
startOffset - node.sourceCodeLocation!.startCol
360355

361356
// <previous-line-node></previous-line-node>
362357
// <target-node></target-node>
@@ -369,9 +364,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
369364
//
370365
// However, if there is content between our target node start and the
371366
// previous newline, we cannot strip it out without risking content deletion.
372-
return line.trim()
373-
? node.sourceCodeLocation!.startOffset
374-
: lineStartOffset
367+
let isLineEmpty = false
368+
try {
369+
const line = s.slice(Math.max(0, lineStartOffset), startOffset)
370+
isLineEmpty = !line.trim()
371+
} catch {
372+
// magic-string may throw if there's some content removed in the sliced string,
373+
// which we ignore and assume the line is not empty
374+
}
375+
376+
return isLineEmpty ? lineStartOffset : startOffset
375377
}
376378

377379
// pre-transform

playground/html/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default defineConfig({
3939
serveFolder: resolve(__dirname, 'serve/folder/index.html'),
4040
serveBothFile: resolve(__dirname, 'serve/both.html'),
4141
serveBothFolder: resolve(__dirname, 'serve/both/index.html'),
42+
write: resolve(__dirname, 'write.html'),
4243
},
4344
},
4445
},

playground/html/write.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!-- prettier-ignore -->
2+
<html><head><style>div {}
3+
</style></head><body><script type="module" src="./shared.js"></script></body></html>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy