33 * @import {PhrasingParents} from '../types.js'
44 */
55
6+ import { encodeCharacterReference } from './encode-character-reference.js'
7+
68/**
79 * Serialize the children of a parent that contains phrasing children.
810 *
@@ -24,6 +26,8 @@ export function containerPhrasing(parent, state, info) {
2426 const results = [ ]
2527 let index = - 1
2628 let before = info . before
29+ /** @type {string | undefined } */
30+ let encodeAfter
2731
2832 indexStack . push ( - 1 )
2933 let tracker = state . createTracker ( info )
@@ -75,17 +79,43 @@ export function containerPhrasing(parent, state, info) {
7579 tracker . move ( results . join ( '' ) )
7680 }
7781
78- results . push (
79- tracker . move (
80- state . handle ( child , parent , state , {
81- ...tracker . current ( ) ,
82- before,
83- after
84- } )
85- )
86- )
82+ let value = state . handle ( child , parent , state , {
83+ ...tracker . current ( ) ,
84+ after,
85+ before
86+ } )
87+
88+ // If we had to encode the first character after the previous node and it’s
89+ // still the same character,
90+ // encode it.
91+ if ( encodeAfter && encodeAfter === value . slice ( 0 , 1 ) ) {
92+ value =
93+ encodeCharacterReference ( encodeAfter . charCodeAt ( 0 ) ) + value . slice ( 1 )
94+ }
95+
96+ const encodingInfo = state . attentionEncodeSurroundingInfo
97+ state . attentionEncodeSurroundingInfo = undefined
98+ encodeAfter = undefined
99+
100+ // If we have to encode the first character before the current node and
101+ // it’s still the same character,
102+ // encode it.
103+ if ( encodingInfo ) {
104+ if (
105+ encodingInfo . before &&
106+ before === results [ results . length - 1 ] . slice ( - 1 )
107+ ) {
108+ results [ results . length - 1 ] =
109+ results [ results . length - 1 ] . slice ( 0 , - 1 ) +
110+ encodeCharacterReference ( before . charCodeAt ( 0 ) )
111+ }
112+
113+ if ( encodingInfo . after ) encodeAfter = after
114+ }
87115
88- before = results [ results . length - 1 ] . slice ( - 1 )
116+ tracker . move ( value )
117+ results . push ( value )
118+ before = value . slice ( - 1 )
89119 }
90120
91121 indexStack . pop ( )
0 commit comments