-
Notifications
You must be signed in to change notification settings - Fork 128
feat(math): implement m:d delimiter converter (SD-2380) #2710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
caio-pizzol
merged 9 commits into
superdoc-dev:main
from
andrewsrigom:feat/math-delimiter-converter
Apr 9, 2026
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
804c4d3
feat(math): implement m:d delimiter converter (SD-2380)
andrewsrigom 13d498b
Merge branch 'main' into feat/math-delimiter-converter
andrewsrigom a43afb5
fix(math): address delimiter review and sync converters
andrewsrigom 6e61742
Merge remote-tracking branch 'origin/main' into feat/math-delimiter-c…
andrewsrigom a0a0d00
fix(math): address delimiter review feedback
andrewsrigom e3f4d6e
docs(math): clarify delimiter separator spec
andrewsrigom 96b3b0c
Merge branch 'main' into feat/math-delimiter-converter
andrewsrigom 6784fed
test(math): add behavior tests for m:d delimiter rendering
caio-pizzol 408dad9
refactor(math): inline createExpressionGroup into single loop
caio-pizzol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
packages/layout-engine/painters/dom/src/features/math/converters/delimiter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import type { MathObjectConverter, OmmlJsonNode } from '../types.js'; | ||
|
|
||
| const MATHML_NS = 'http://www.w3.org/1998/Math/MathML'; | ||
|
|
||
| const DEFAULT_BEGIN_DELIMITER = '('; | ||
| const DEFAULT_END_DELIMITER = ')'; | ||
| const DEFAULT_SEPARATOR_DELIMITER = '|'; | ||
|
|
||
| function getDelimiterValue(properties: OmmlJsonNode | undefined, name: string, fallback: string): string { | ||
| const property = properties?.elements?.find((element) => element.name === name); | ||
| return property?.attributes?.['m:val'] ?? fallback; | ||
|
andrewsrigom marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| function createExpressionGroup( | ||
| expression: OmmlJsonNode | undefined, | ||
| doc: Document, | ||
| convertChildren: (children: OmmlJsonNode[]) => DocumentFragment, | ||
| ): Element | null { | ||
| const fragment = convertChildren(expression?.elements ?? []); | ||
| if (fragment.childNodes.length === 0) return null; | ||
|
|
||
| const group = doc.createElementNS(MATHML_NS, 'mrow'); | ||
| group.appendChild(fragment); | ||
| return group; | ||
| } | ||
|
|
||
| /** | ||
| * Convert m:d (delimiter) to MathML. | ||
| * | ||
| * OMML structure: | ||
| * m:d → m:dPr (optional: begChr, endChr, sepChr) + one or more m:e expressions | ||
| * | ||
| * MathML output: | ||
| * <mrow><mo>(</mo> ...content... <mo>)</mo></mrow> | ||
| * | ||
| * @spec ECMA-376 §22.1.2.24 | ||
| */ | ||
| export const convertDelimiter: MathObjectConverter = (node, doc, convertChildren) => { | ||
| const elements = node.elements ?? []; | ||
| const delimiterProps = elements.find((element) => element.name === 'm:dPr'); | ||
| const expressions = elements.filter((element) => element.name === 'm:e'); | ||
|
|
||
| const beginDelimiter = getDelimiterValue(delimiterProps, 'm:begChr', DEFAULT_BEGIN_DELIMITER); | ||
| const endDelimiter = getDelimiterValue(delimiterProps, 'm:endChr', DEFAULT_END_DELIMITER); | ||
| const separatorDelimiter = getDelimiterValue(delimiterProps, 'm:sepChr', DEFAULT_SEPARATOR_DELIMITER); | ||
|
|
||
| const wrapper = doc.createElementNS(MATHML_NS, 'mrow'); | ||
|
|
||
| const begin = doc.createElementNS(MATHML_NS, 'mo'); | ||
| begin.textContent = beginDelimiter; | ||
| wrapper.appendChild(begin); | ||
|
|
||
| const expressionGroups = expressions | ||
| .map((expression) => createExpressionGroup(expression, doc, convertChildren)) | ||
| .filter((expressionGroup): expressionGroup is Element => expressionGroup !== null); | ||
|
|
||
| expressionGroups.forEach((expressionGroup, index) => { | ||
| if (index > 0) { | ||
| const separator = doc.createElementNS(MATHML_NS, 'mo'); | ||
| separator.textContent = separatorDelimiter; | ||
| wrapper.appendChild(separator); | ||
| } | ||
|
|
||
| wrapper.appendChild(expressionGroup); | ||
| }); | ||
|
|
||
| const end = doc.createElementNS(MATHML_NS, 'mo'); | ||
| end.textContent = endDelimiter; | ||
| wrapper.appendChild(end); | ||
|
|
||
| return wrapper; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.