Skip to content

Commit c626dd3

Browse files
committed
Fix raw ending in text, followed by more raw
Closes GH-16.
1 parent 64e1a66 commit c626dd3

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

Diff for: lib/index.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ export const raw =
213213

214214
one(tree)
215215

216+
resetTokenizer()
217+
216218
parser._adoptNodes(mock.childNodes[0], doc)
217219

218220
return doc
@@ -235,6 +237,8 @@ export const raw =
235237

236238
one(tree)
237239

240+
resetTokenizer()
241+
238242
return doc
239243
}
240244

@@ -336,7 +340,7 @@ export const raw =
336340
if (!locationTracker) throw new Error('Expected `locationTracker`')
337341

338342
// Reset preprocessor:
339-
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/preprocessor.js>.
343+
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/preprocessor.js#L17>.
340344
preprocessor.html = undefined
341345
preprocessor.pos = -1
342346
preprocessor.lastGapPos = -1
@@ -377,18 +381,6 @@ export const raw =
377381
preprocessor.lastChunkWritten = true
378382
tokenizer[tokenizer.state](tokenizer._consume())
379383
}
380-
381-
// Process final characters if they’re still there after hibernating.
382-
// Similar to:
383-
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/extensions/location-info/tokenizer-mixin.js#L95>.
384-
const token = tokenizer.currentCharacterToken
385-
386-
if (token) {
387-
token.location.endLine = posTracker.line
388-
token.location.endCol = posTracker.col + 1
389-
token.location.endOffset = posTracker.offset + 1
390-
parser._processToken(token)
391-
}
392384
}
393385

394386
/**
@@ -423,8 +415,21 @@ export const raw =
423415
}
424416

425417
function resetTokenizer() {
426-
/* c8 ignore next */
418+
/* c8 ignore next 2 */
427419
if (!tokenizer) throw new Error('Expected `tokenizer`')
420+
if (!posTracker) throw new Error('Expected `posTracker`')
421+
422+
// Process final characters if they’re still there after hibernating.
423+
// Similar to:
424+
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/extensions/location-info/tokenizer-mixin.js#L95>.
425+
const token = tokenizer.currentCharacterToken
426+
427+
if (token) {
428+
token.location.endLine = posTracker.line
429+
token.location.endCol = posTracker.col + 1
430+
token.location.endOffset = posTracker.offset + 1
431+
parser._processToken(token)
432+
}
428433

429434
// Reset tokenizer:
430435
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/index.js#L218-L234>.

Diff for: test.js

+6
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ test('raw', (t) => {
270270
'should support passing through nodes w/ broken raw children (4)'
271271
)
272272

273+
t.deepEqual(
274+
raw(u('root', [u('raw', 'aaa'), u('raw', '<x>')])),
275+
u('root', {data: {quirksMode: false}}, [u('text', 'aaa'), h('x')]),
276+
'should support raw text and then another raw node'
277+
)
278+
273279
t.deepEqual(
274280
raw(u('root', [u('raw', '<script>alert(1)</script>')])),
275281
u('root', {data: {quirksMode: false}}, [

0 commit comments

Comments
 (0)