@@ -140,11 +140,15 @@ function readKeyval(text: string, l3keys: boolean = false): EnvList {
140140 * @return {string } The cleaned string.
141141 */
142142function removeBraces ( text : string , count : number ) : string {
143+ if ( count === 0 ) {
144+ return text . replace ( / ^ \s + / , '' )
145+ . replace ( / ( [ ^ \\ \s ] | ^ ) ( (?: \\ \\ ) * (?: \\ \s ) ? ) ? \s + $ / , '$1$2' ) ;
146+ }
143147 while ( count > 0 ) {
144148 text = text . trim ( ) . slice ( 1 , - 1 ) ;
145149 count -- ;
146150 }
147- return text . trim ( ) ;
151+ return text ;
148152}
149153
150154
@@ -165,56 +169,46 @@ function readValue(text: string, end: string[],
165169 let value = '' ;
166170 let index = 0 ;
167171 let start = 0 ; // Counter for the starting left braces.
168- let startCount = true ; // Flag for counting starting left braces.
169- let stopCount = false ; // If true right braces are found directly
172+ let countBraces = true ; // Flag for counting starting left braces.
170173 // after starting braces, but no other char yet.
171174 while ( index < length ) {
172175 let c = text [ index ++ ] ;
173176 switch ( c ) {
174177 case '\\' : // Handle control sequences (in particular, \{ and \})
175- value += c + text [ index ++ ] ;
176- startCount = stopCount = false ;
178+ value += c + ( text [ index ++ ] || '' ) ;
179+ countBraces = false ;
177180 continue ;
178181 case ' ' : // Ignore spaces.
179182 break ;
180183 case '{' :
181- if ( startCount ) { // Count start left braces at start.
184+ if ( countBraces ) { // Count open left braces at start.
182185 start ++ ;
183- } else {
184- stopCount = false ;
185186 }
186187 braces ++ ;
187188 break ;
188189 case '}' :
189- if ( braces ) { // Closing braces.
190- braces -- ;
191- }
192- if ( startCount || stopCount ) { // Closing braces at the start.
193- start -- ;
194- stopCount = true ; // Continue to close braces.
190+ if ( ! braces ) { // Closing braces.
191+ throw new TexError ( 'ExtraCloseMissingOpen' , 'Extra close brace or missing open brace' ) ;
195192 }
196- startCount = false ; // Stop counting start left braces.
193+ braces -- ;
194+ countBraces = false ; // Stop counting start left braces.
197195 break ;
198196 default :
199197 if ( ! braces && end . indexOf ( c ) !== - 1 ) { // End character reached.
200- return [ stopCount ? 'true' : // If Stop count is true we
201- // have balanced braces, only.
202- removeBraces ( value , l3keys ? Math . min ( 1 , start ) : start ) , c , text . slice ( index ) ] ;
198+ return [ removeBraces ( value , l3keys ? Math . min ( 1 , start ) : start ) , c , text . slice ( index ) ] ;
203199 }
204200 if ( start > braces ) { // Some start left braces have been closed.
205201 start = braces ;
206202 }
207- startCount = false ;
208- stopCount = false ;
203+ countBraces = false ;
209204 }
210205 value += c ;
211206 }
212207 if ( braces ) {
213- throw new TexError ( 'ExtraOpenMissingClose' ,
214- 'Extra open brace or missing close brace' ) ;
208+ throw new TexError ( 'ExtraOpenMissingClose' , 'Extra open brace or missing close brace' ) ;
215209 }
216- return ( dropBrace && ! stopCount && start ) ? [ '' , '' , removeBraces ( value , 1 ) ] :
217- [ stopCount ? 'true' : removeBraces ( value , l3keys ? Math . min ( 1 , start ) : start ) , '' , text . slice ( index ) ] ;
210+ return ( dropBrace && start ) ? [ '' , '' , removeBraces ( value , 1 ) ] :
211+ [ removeBraces ( value , l3keys ? Math . min ( 1 , start ) : start ) , '' , text . slice ( index ) ] ;
218212}
219213
220214export const ParseUtil = {
0 commit comments