MCPcopy
hub / github.com/KaTeX/KaTeX / traverseNonSpaceNodes

Function traverseNonSpaceNodes

src/buildHTML.ts:141–197  ·  view source on GitHub ↗
(
    nodes: HtmlDomNode[],
    callback: (arg0: HtmlDomNode, arg1: HtmlDomNode) =>
        HtmlDomNode | null | void,
    prev: {
        node: HtmlDomNode,
        insertAfter?: (arg0: HtmlDomNode) => void,
    },
    next: HtmlDomNode | null | undefined,
    isRoot: boolean,
)

Source from the content-addressed store, hash-verified

139// function to insert after it. `next` is a node that will be added to the right.
140// Used for bin cancellation and inserting spacings.
141const traverseNonSpaceNodes = function(
142 nodes: HtmlDomNode[],
143 callback: (arg0: HtmlDomNode, arg1: HtmlDomNode) =>
144 HtmlDomNode | null | void,
145 prev: {
146 node: HtmlDomNode,
147 insertAfter?: (arg0: HtmlDomNode) => void,
148 },
149 next: HtmlDomNode | null | undefined,
150 isRoot: boolean,
151) {
152 if (next) { // temporarily append the right node, if exists
153 nodes.push(next);
154 }
155 let i = 0;
156 for (; i < nodes.length; i++) {
157 const node = nodes[i];
158 const partialGroup = checkPartialGroup(node);
159
160 if (partialGroup) { // Recursive DFS
161 // TODO(ts): partialGroup.children is ReadonlyArray but this
162 // function mutates the array (insertAfter splices into it).
163 traverseNonSpaceNodes(partialGroup.children as HtmlDomNode[],
164 callback, prev, null, isRoot);
165 continue;
166 }
167
168 // Ignore explicit spaces (e.g., \;, \,) when determining what implicit
169 // spacing should go between atoms of different classes
170 const nonspace = !node.hasClass("mspace");
171 if (nonspace) {
172 const result = callback(node, prev.node);
173 if (result) {
174 if (prev.insertAfter) {
175 prev.insertAfter(result);
176 } else { // insert at front
177 nodes.unshift(result);
178 i++;
179 }
180 }
181 }
182
183 if (nonspace) {
184 prev.node = node;
185 } else if (isRoot && node.hasClass("newline")) {
186 prev.node = makeSpan(["leftmost"]); // treat like beginning of line
187 }
188 prev.insertAfter = (index => n => {
189 nodes.splice(index + 1, 0, n);
190 i++;
191 })(i);
192 }
193
194 if (next) {
195 nodes.pop();
196 }
197};
198

Callers 1

buildExpressionFunction · 0.85

Calls 3

makeSpanFunction · 0.90
checkPartialGroupFunction · 0.85
hasClassMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…