1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @fileoverview Utility functions for mathml and mathjax rule store.
9 goog
.provide('cvox.MathmlStoreUtil');
11 goog
.require('cvox.MathUtil');
12 goog
.require('cvox.TraverseMath');
16 * Retrieves MathML sub element with same id as MathJax node.
17 * @param {!Node} inner A node internal to a MathJax node.
18 * @return {Node} The internal MathML node corresponding to the MathJax node.
20 cvox
.MathmlStoreUtil
.matchMathjaxToMathml = function(inner
) {
21 var mml
= cvox
.TraverseMath
.getInstance().activeMathmlHost
;
22 return mml
.querySelector('#' + inner
.id
);
27 * Retrieve an extender symbol for a given node.
28 * @param {!Node} jax The MathJax node.
29 * @return {Array<Node>} The resulting node list.
31 cvox
.MathmlStoreUtil
.retrieveMathjaxExtender = function(jax
) {
32 var ext
= cvox
.MathmlStoreUtil
.matchMathjaxToMathml(jax
);
41 * Retrieve an extender symbol for a given node.
42 * @param {!Node} jax The MathJax node.
43 * @return {Array<Node>} The resulting node list.
45 cvox
.MathmlStoreUtil
.retrieveMathjaxLeaf = function(jax
) {
46 var leaf
= cvox
.MathmlStoreUtil
.matchMathjaxToMathml(jax
);
55 * For a given MathJax node it returns the equivalent MathML node,
56 * if it is of the right tag.
57 * @param {!Node} jax The Mathjax node.
58 * @param {!string} tag The required tag.
59 * @return {Array<Node>} The resulting node list.
61 cvox
.MathmlStoreUtil
.checkMathjaxTag = function(jax
, tag
) {
62 var node
= cvox
.MathmlStoreUtil
.matchMathjaxToMathml(jax
);
63 if (node
&& node
.tagName
.toUpperCase() == tag
) {
71 * Returns MathML node if MathJax is munder.
72 * @param {!Node} jax The Mathjax node.
73 * @return {Array<Node>} The resulting node list.
75 cvox
.MathmlStoreUtil
.checkMathjaxMunder = function(jax
) {
76 return cvox
.MathmlStoreUtil
.checkMathjaxTag(jax
, 'MUNDER');
81 * Returns MathML node if MathJax is mover.
82 * @param {!Node} jax The Mathjax node.
83 * @return {Array<Node>} The resulting node list.
85 cvox
.MathmlStoreUtil
.checkMathjaxMover = function(jax
) {
86 return cvox
.MathmlStoreUtil
.checkMathjaxTag(jax
, 'MOVER');
91 * Returns MathML node if MathJax is msub.
92 * @param {!Node} jax The Mathjax node.
93 * @return {Array<Node>} The resulting node list.
95 cvox
.MathmlStoreUtil
.checkMathjaxMsub = function(jax
) {
96 return cvox
.MathmlStoreUtil
.checkMathjaxTag(jax
, 'MSUB');
101 * Returns MathML node if MathJax is msup.
102 * @param {!Node} jax The Mathjax node.
103 * @return {Array<Node>} The resulting node list.
105 cvox
.MathmlStoreUtil
.checkMathjaxMsup = function(jax
) {
106 return cvox
.MathmlStoreUtil
.checkMathjaxTag(jax
, 'MSUP');
111 * Constructs a closure that returns separators for an MathML mfenced
113 * Separators in MathML are represented by a list and used up one by one
114 * until the final element is used as the default.
115 * Example: a b c d e and separators [+,-,*]
116 * would result in a + b - c * d * e.
117 * @param {string} separators String representing a list of mfenced separators.
118 * @return {function(): string|null} A closure that returns the next separator
119 * for an mfenced expression starting with the first node in nodes.
121 cvox
.MathmlStoreUtil
.nextSeparatorFunction = function(separators
) {
123 // Mathjax does not expand empty separators.
124 if (separators
.match(/^\s+$/)) {
127 var sepList
= separators
.replace(/\s/g, '')
129 .filter(function(x
) {return x
;});
132 // When no separator is given MathML uses comma as default.
137 if (sepList
.length
> 1) {
138 return sepList
.shift();
146 * Computes the correct separators for each node.
147 * @param {Array<Node>} nodes A node array.
148 * @param {string} context A context string.
149 * @return {function(): string} A closure that returns the next separator for an
150 * mfenced expression starting with the first node in nodes.
152 cvox
.MathmlStoreUtil
.mfencedSeparators = function(nodes
, context
) {
153 var nextSeparator
= cvox
.MathmlStoreUtil
.nextSeparatorFunction(context
);
155 return nextSeparator
? nextSeparator() : '';
161 * Iterates over the list of content nodes of the parent of the given nodes.
162 * @param {Array<Node>} nodes A node array.
163 * @param {string} context A context string.
164 * @return {function(): string} A closure that returns the content of the next
165 * content node. Returns only context string if list is exhausted.
167 cvox
.MathmlStoreUtil
.contentIterator = function(nodes
, context
) {
168 if (nodes
.length
> 0) {
169 var contentNodes
= cvox
.XpathUtil
.evalXPath('../../content/*', nodes
[0]);
171 var contentNodes
= [];
174 var content
= contentNodes
.shift();
175 return context
+ (content
? content
.textContent
: '');