Commit 7142f4e
authored
fix(math): split multi-char m:r text per character to match Word (SD-2632) (#2875)
* fix(math): split multi-char m:r text per character to match Word (SD-2632)
Word's OMML2MML.XSL classifies each character in an m:r run individually —
digits group into a single <mn> (with one optional decimal point between
digits), operator characters each become their own <mo>, and everything
else becomes its own <mi>. SuperDoc was emitting the entire run text as
one element, so runs like "→∞" or "x+1" rendered as a single <mi>, losing
operator spacing and semantics.
tokenizeMathText implements the per-character classification. convertMathRun
returns a single Element for one-atom runs and a DocumentFragment when
multiple atoms are emitted, so siblings flow directly into the parent's
<mrow> without an extra wrapper.
m:fName is the documented exception — Word keeps multi-letter function
names like "sin" or "lim" as one <mi> inside the function-name slot.
convertFunction routes m:r children through convertMathRunWhole (no
splitting), and a new collapseFunctionNameBases pass re-merges the base
slot of any structural MathML element (munder/mover/msub/…) that Word
nested inside m:fName — without this, "lim" inside m:limLow would
incorrectly split to three <mi>.
Also drops U+221E (∞) from OPERATOR_CHARS — it's a mathematical constant
per Word's XSL, not an operator.
MathObjectConverter's return type widens from Element|null to Node|null
so convertMathRun can return a DocumentFragment. All other converters
already return Element, which is assignable to Node — no other changes.
Verified against real Word-native fixtures: `→∞` in the limit-tests
fixture case 1 now renders as <mi>n</mi><mo>→</mo><mi>∞</mi> (matches
Word OMML2MML.XSL byte-for-byte), and nested limits keep their function
names intact.
Ref ECMA-376 §22.1.2.116, Annex L.6.1.13, §22.1.2.58.
* fix(math): group letters inside m:fName mixed runs + cover mmultiscripts (SD-2632)
Follow-up to /review feedback backed by fresh Word OMML2MML.XSL evidence:
- `convertMathRunAsFunctionName` (renamed from `convertMathRunWhole` since
"whole" no longer fits) now groups consecutive non-digit / non-operator
characters into one <mi> while still splitting digits and operators. Word's
XSL for `<m:fName><m:r>log_2</m:r></m:fName>` produces
`<mi>log</mi><mo>_</mo><mn>2</mn>` — not `<mi>l</mi><mi>o</mi><mi>g</mi>…`.
- `BASE_BEARING_ELEMENTS` gains `mmultiscripts` — Word emits it when an
`m:sPre` sits inside `m:fName`; our base-collapse pass needs to know to
merge the first-child <mi> run.
- CONTRIBUTING.md now documents the widened `Node | null` return type.
Tests added:
- Direct `tokenizeMathText` edge cases: `.5` / `5.` / `1.2.3` / `2x+1` /
consecutive operators / empty / standalone ∞.
- m:fName mixed-content: `log_2` stays `<mi>log</mi><mo>_</mo><mn>2</mn>`.
- Base collapse inside nested `m:sSub` under `m:fName`.
- Base collapse inside nested `m:sPre` (mmultiscripts) under `m:fName`.
- Behavior test tightened to pin the full 3-atom sequence for `n→∞`.
Disputed during review and deferred with evidence:
- Opus claim that standalone `m:limLow` with "lim" base regresses to italic:
Word XSL itself splits "lim" per-char in that shape (with or without
m:sty=p), so our output matches Word.
- Codex claim that Arabic-Indic digits should be `<mn>`: Word XSL also
classifies them as `<mi>`, so our behavior matches.
- Non-BMP surrogate-pair support: edge case in extreme mathematical
alphanumerics; Word XSL itself errored on U+1D465. Separate ticket worth.
* fix(math): iterate tokenizer by code point to preserve surrogate pairs
Addresses Codex bot review on PR #2875. Astral-plane mathematical
alphanumerics (e.g. U+1D465 mathematical italic x, U+1D7D9
mathematical double-struck 1) are UTF-16 surrogate pairs. Walking
text by code unit split them into two half-pair <mi> atoms with
invalid content. `codePointUnitLength` returns 2 when the current
position starts a surrogate pair so tokenizeMathText and
tokenizeFunctionNameText step across the full code point.1 parent f3bfc71 commit 7142f4e
6 files changed
Lines changed: 576 additions & 39 deletions
File tree
- packages/layout-engine/painters/dom/src/features/math
- converters
- tests/behavior/tests/importing
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
Lines changed: 64 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
40 | 91 | | |
41 | 92 | | |
42 | 93 | | |
| |||
59 | 110 | | |
60 | 111 | | |
61 | 112 | | |
62 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
63 | 126 | | |
64 | 127 | | |
65 | 128 | | |
| |||
Lines changed: 179 additions & 33 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
| 49 | + | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
| |||
65 | 64 | | |
66 | 65 | | |
67 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
68 | 73 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
73 | 77 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
78 | 131 | | |
79 | 132 | | |
80 | 133 | | |
| |||
115 | 168 | | |
116 | 169 | | |
117 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
118 | 183 | | |
119 | | - | |
| 184 | + | |
120 | 185 | | |
121 | 186 | | |
122 | 187 | | |
123 | 188 | | |
124 | 189 | | |
125 | 190 | | |
126 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
127 | 200 | | |
128 | 201 | | |
129 | | - | |
| 202 | + | |
| 203 | + | |
130 | 204 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
139 | 254 | | |
| 255 | + | |
140 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
141 | 273 | | |
142 | 274 | | |
| 275 | + | |
| 276 | + | |
143 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
144 | 289 | | |
145 | 290 | | |
146 | | - | |
| 291 | + | |
147 | 292 | | |
148 | | - | |
| 293 | + | |
149 | 294 | | |
150 | | - | |
151 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
152 | 301 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
| 302 | + | |
159 | 303 | | |
160 | | - | |
161 | | - | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
0 commit comments