1+ // @flow
12/**
23 * This file provides support to buildMathML.js and buildHTML.js
34 * for stretchy wide elements rendered from SVG files
@@ -9,7 +10,11 @@ import buildCommon from "./buildCommon";
910import mathMLTree from "./mathMLTree" ;
1011import utils from "./utils" ;
1112
12- const stretchyCodePoint = {
13+ import type Options from "./Options" ;
14+ import type ParseNode from "./ParseNode" ;
15+ import type { span } from "./domTree" ;
16+
17+ const stretchyCodePoint : { [ string ] : string } = {
1318 widehat : "^" ,
1419 widetilde : "~" ,
1520 undertilde : "~" ,
@@ -45,7 +50,7 @@ const stretchyCodePoint = {
4550 xtofrom : "\u21c4" ,
4651} ;
4752
48- const mathMLnode = function ( label ) {
53+ const mathMLnode = function ( label : string ) : mathMLTree . MathNode {
4954 const node = new mathMLTree . MathNode (
5055 "mo" , [ new mathMLTree . TextNode ( stretchyCodePoint [ label . substr ( 1 ) ] ) ] ) ;
5156 node . setAttribute ( "stretchy" , "true" ) ;
@@ -98,7 +103,9 @@ const mathMLnode = function(label) {
98103// That is, inside the font, that arrowhead is 522 units tall, which
99104// corresponds to 0.522 em inside the document.
100105
101- const katexImagesData = {
106+ const katexImagesData : {
107+ [ string ] : ( [ string [ ] , number , number ] | [ string [ ] , number , number , string ] )
108+ } = {
102109 // path(s), minWidth, height, align
103110 overrightarrow : [ [ "rightarrow" ] , 0.888 , 522 , "xMaxYMin" ] ,
104111 overleftarrow : [ [ "leftarrow" ] , 0.888 , 522 , "xMinYMin" ] ,
@@ -139,15 +146,15 @@ const katexImagesData = {
139146 xtofrom : [ [ "leftToFrom" , "rightToFrom" ] , 1.75 , 528 ] ,
140147} ;
141148
142- const groupLength = function ( arg ) {
149+ const groupLength = function ( arg : ParseNode ) : number {
143150 if ( arg . type === "ordgroup" ) {
144151 return arg . value . length ;
145152 } else {
146153 return 1 ;
147154 }
148155} ;
149156
150- const svgSpan = function ( group , options ) {
157+ const svgSpan = function ( group : ParseNode , options : Options ) : span {
151158 // Create a span with inline SVG for the element.
152159 const label = group . value . label . substr ( 1 ) ;
153160 let attributes = [ ] ;
@@ -203,6 +210,11 @@ const svgSpan = function(group, options) {
203210
204211 [ paths , minWidth , viewBoxHeight , align ] = katexImagesData [ label ] ;
205212 const numSvgChildren = paths . length ;
213+ if ( 1 > numSvgChildren || numSvgChildren > 3 ) {
214+ throw new Error (
215+ `Correct katexImagesData or update code below to support
216+ ${ numSvgChildren } children.` ) ;
217+ }
206218 height = viewBoxHeight / 1000 ;
207219
208220 for ( let i = 0 ; i < numSvgChildren ; i ++ ) {
@@ -223,17 +235,16 @@ const svgSpan = function(group, options) {
223235 svgNode = new domTree . svgNode ( [ path ] , attributes ) ;
224236
225237 if ( numSvgChildren === 1 ) {
226- span = buildCommon . makeSpan ( [ "hide-tail" ] , [ svgNode ] , options ) ;
238+ spans . push ( buildCommon . makeSpan ( [ "hide-tail" ] , [ svgNode ] , options ) ) ;
227239 } else {
228- span = buildCommon . makeSpan ( [ widthClass ] , [ svgNode ] , options ) ;
240+ const span = buildCommon . makeSpan ( [ widthClass ] , [ svgNode ] , options ) ;
229241 span . style . height = height + "em" ;
230242 spans . push ( span ) ;
231243 }
232244 }
233245
234- if ( numSvgChildren > 1 ) {
235- span = buildCommon . makeSpan ( [ "stretchy" ] , spans , options ) ;
236- }
246+ span = numSvgChildren === 1 ? spans [ 0 ] :
247+ buildCommon . makeSpan ( [ "stretchy" ] , spans , options ) ;
237248 }
238249
239250 // Note that we are returning span.depth = 0.
@@ -247,7 +258,12 @@ const svgSpan = function(group, options) {
247258 return span ;
248259} ;
249260
250- const encloseSpan = function ( inner , label , pad , options ) {
261+ const encloseSpan = function (
262+ inner : span ,
263+ label : string ,
264+ pad : number ,
265+ options : Options ,
266+ ) : span {
251267 // Return an image span for \cancel, \bcancel, \xcancel, or \fbox
252268 let img ;
253269 const totalHeight = inner . height + inner . depth + 2 * pad ;
0 commit comments