@@ -32,6 +32,8 @@ import * as bitem from './BaseItems.js';
3232import { AbstractTags } from '../Tags.js' ;
3333import './BaseMappings.js' ;
3434import { getRange } from '../../../core/MmlTree/OperatorDictionary.js' ;
35+ import { MmlNode } from '../../../core/MmlTree/MmlNode.js' ;
36+ import ParseOptions from '../ParseOptions.js' ;
3537
3638/**
3739 * Remapping some ASCII characters to their Unicode operator equivalent.
@@ -89,6 +91,45 @@ function envUndefined(_parser: TexParser, env: string) {
8991 throw new TexError ( 'UnknownEnv' , 'Unknown environment \'%1\'' , env ) ;
9092}
9193
94+ /**
95+ * Filter for removing spacing following \nonscript
96+ * @param {ParseOptions } data The active tex parser.
97+ */
98+ function filterNonscript ( { data} : { data : ParseOptions } ) {
99+ for ( const mml of data . getList ( 'nonscript' ) ) {
100+ //
101+ // This is the list of mspace elements or mrow > mstyle > mspace
102+ // that followed \nonscript macros to be tested for removal.
103+ //
104+ if ( mml . attributes . get ( 'scriptlevel' ) > 0 ) {
105+ //
106+ // The mspace needs to be removed, since we are in a script style.
107+ // Remove it from the DOM and from the list of mspace elements.
108+ //
109+ const parent = mml . parent ;
110+ parent . childNodes . splice ( parent . childIndex ( mml ) , 1 ) ;
111+ data . removeFromList ( mml . kind , [ mml ] ) ;
112+ //
113+ // If it is an mrow > mstyle > mspace, then we have just
114+ // removed the mrow from its list, and must remove
115+ // the mstyle and mspace from their lists as well.
116+ //
117+ if ( mml . isKind ( 'mrow' ) ) {
118+ const mstyle = mml . childNodes [ 0 ] as MmlNode ;
119+ data . removeFromList ( 'mstyle' , [ mstyle ] ) ;
120+ data . removeFromList ( 'mspace' , mstyle . childNodes [ 0 ] . childNodes as MmlNode [ ] ) ;
121+ }
122+ } else if ( mml . isKind ( 'mrow' ) ) {
123+ //
124+ // This is an mrow > mstyle > mspace but we're not in a script
125+ // style, so remove the mrow that we had added in the NonscriptItem.
126+ //
127+ mml . parent . replaceChild ( mml . childNodes [ 0 ] , mml ) ;
128+ data . removeFromList ( 'mrow' , [ mml ] ) ;
129+ }
130+ }
131+ }
132+
92133
93134/**
94135 * @constructor
@@ -135,19 +176,21 @@ export const BaseConfiguration: Configuration = Configuration.create(
135176 [ bitem . MmlItem . prototype . kind ] : bitem . MmlItem ,
136177 [ bitem . FnItem . prototype . kind ] : bitem . FnItem ,
137178 [ bitem . NotItem . prototype . kind ] : bitem . NotItem ,
179+ [ bitem . NonscriptItem . prototype . kind ] : bitem . NonscriptItem ,
138180 [ bitem . DotsItem . prototype . kind ] : bitem . DotsItem ,
139181 [ bitem . ArrayItem . prototype . kind ] : bitem . ArrayItem ,
140182 [ bitem . EqnArrayItem . prototype . kind ] : bitem . EqnArrayItem ,
141183 [ bitem . EquationItem . prototype . kind ] : bitem . EquationItem
142- } ,
143- options : {
144- maxMacros : 1000 ,
145- baseURL : ( typeof ( document ) === 'undefined' ||
146- document . getElementsByTagName ( 'base' ) . length === 0 ) ?
147- '' : String ( document . location ) . replace ( / # .* $ / , '' )
148- } ,
149- tags : {
150- base : BaseTags
151- }
184+ } ,
185+ options : {
186+ maxMacros : 1000 ,
187+ baseURL : ( typeof ( document ) === 'undefined' ||
188+ document . getElementsByTagName ( 'base' ) . length === 0 ) ?
189+ '' : String ( document . location ) . replace ( / # .* $ / , '' )
190+ } ,
191+ tags : {
192+ base : BaseTags
193+ } ,
194+ postprocessors : [ [ filterNonscript , - 4 ] ]
152195 }
153196) ;
0 commit comments