OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Utility functions for mathml and mathjax rule store. | 6 * @fileoverview Utility functions for mathml and mathjax rule store. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.MathmlStoreUtil'); | 9 goog.provide('cvox.MathmlStoreUtil'); |
10 | 10 |
11 goog.require('cvox.MathUtil'); | 11 goog.require('cvox.MathUtil'); |
12 goog.require('cvox.TraverseMath'); | 12 goog.require('cvox.TraverseMath'); |
13 | 13 |
14 | 14 |
15 /** | 15 /** |
16 * Retrieves MathML sub element with same id as MathJax node. | 16 * Retrieves MathML sub element with same id as MathJax node. |
17 * @param {!Node} inner A node internal to a 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. | 18 * @return {Node} The internal MathML node corresponding to the MathJax node. |
19 */ | 19 */ |
20 cvox.MathmlStoreUtil.matchMathjaxToMathml = function(inner) { | 20 cvox.MathmlStoreUtil.matchMathjaxToMathml = function(inner) { |
21 var mml = cvox.TraverseMath.getInstance().activeMathmlHost; | 21 var mml = cvox.TraverseMath.getInstance().activeMathmlHost; |
22 return mml.querySelector('#' + inner.id); | 22 return mml.querySelector('#' + inner.id); |
23 }; | 23 }; |
24 | 24 |
25 | 25 |
26 /** | 26 /** |
27 * Retrieve an extender symbol for a given node. | 27 * Retrieve an extender symbol for a given node. |
28 * @param {!Node} jax The MathJax node. | 28 * @param {!Node} jax The MathJax node. |
29 * @return {Array.<Node>} The resulting node list. | 29 * @return {Array<Node>} The resulting node list. |
30 */ | 30 */ |
31 cvox.MathmlStoreUtil.retrieveMathjaxExtender = function(jax) { | 31 cvox.MathmlStoreUtil.retrieveMathjaxExtender = function(jax) { |
32 var ext = cvox.MathmlStoreUtil.matchMathjaxToMathml(jax); | 32 var ext = cvox.MathmlStoreUtil.matchMathjaxToMathml(jax); |
33 if (ext) { | 33 if (ext) { |
34 return [ext]; | 34 return [ext]; |
35 } | 35 } |
36 return []; | 36 return []; |
37 }; | 37 }; |
38 | 38 |
39 | 39 |
40 /** | 40 /** |
41 * Retrieve an extender symbol for a given node. | 41 * Retrieve an extender symbol for a given node. |
42 * @param {!Node} jax The MathJax node. | 42 * @param {!Node} jax The MathJax node. |
43 * @return {Array.<Node>} The resulting node list. | 43 * @return {Array<Node>} The resulting node list. |
44 */ | 44 */ |
45 cvox.MathmlStoreUtil.retrieveMathjaxLeaf = function(jax) { | 45 cvox.MathmlStoreUtil.retrieveMathjaxLeaf = function(jax) { |
46 var leaf = cvox.MathmlStoreUtil.matchMathjaxToMathml(jax); | 46 var leaf = cvox.MathmlStoreUtil.matchMathjaxToMathml(jax); |
47 if (leaf) { | 47 if (leaf) { |
48 return [leaf]; | 48 return [leaf]; |
49 } | 49 } |
50 return []; | 50 return []; |
51 }; | 51 }; |
52 | 52 |
53 | 53 |
54 /** | 54 /** |
55 * For a given MathJax node it returns the equivalent MathML node, | 55 * For a given MathJax node it returns the equivalent MathML node, |
56 * if it is of the right tag. | 56 * if it is of the right tag. |
57 * @param {!Node} jax The Mathjax node. | 57 * @param {!Node} jax The Mathjax node. |
58 * @param {!string} tag The required tag. | 58 * @param {!string} tag The required tag. |
59 * @return {Array.<Node>} The resulting node list. | 59 * @return {Array<Node>} The resulting node list. |
60 */ | 60 */ |
61 cvox.MathmlStoreUtil.checkMathjaxTag = function(jax, tag) { | 61 cvox.MathmlStoreUtil.checkMathjaxTag = function(jax, tag) { |
62 var node = cvox.MathmlStoreUtil.matchMathjaxToMathml(jax); | 62 var node = cvox.MathmlStoreUtil.matchMathjaxToMathml(jax); |
63 if (node && node.tagName.toUpperCase() == tag) { | 63 if (node && node.tagName.toUpperCase() == tag) { |
64 return [node]; | 64 return [node]; |
65 } | 65 } |
66 return []; | 66 return []; |
67 }; | 67 }; |
68 | 68 |
69 | 69 |
70 /** | 70 /** |
71 * Returns MathML node if MathJax is munder. | 71 * Returns MathML node if MathJax is munder. |
72 * @param {!Node} jax The Mathjax node. | 72 * @param {!Node} jax The Mathjax node. |
73 * @return {Array.<Node>} The resulting node list. | 73 * @return {Array<Node>} The resulting node list. |
74 */ | 74 */ |
75 cvox.MathmlStoreUtil.checkMathjaxMunder = function(jax) { | 75 cvox.MathmlStoreUtil.checkMathjaxMunder = function(jax) { |
76 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MUNDER'); | 76 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MUNDER'); |
77 }; | 77 }; |
78 | 78 |
79 | 79 |
80 /** | 80 /** |
81 * Returns MathML node if MathJax is mover. | 81 * Returns MathML node if MathJax is mover. |
82 * @param {!Node} jax The Mathjax node. | 82 * @param {!Node} jax The Mathjax node. |
83 * @return {Array.<Node>} The resulting node list. | 83 * @return {Array<Node>} The resulting node list. |
84 */ | 84 */ |
85 cvox.MathmlStoreUtil.checkMathjaxMover = function(jax) { | 85 cvox.MathmlStoreUtil.checkMathjaxMover = function(jax) { |
86 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MOVER'); | 86 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MOVER'); |
87 }; | 87 }; |
88 | 88 |
89 | 89 |
90 /** | 90 /** |
91 * Returns MathML node if MathJax is msub. | 91 * Returns MathML node if MathJax is msub. |
92 * @param {!Node} jax The Mathjax node. | 92 * @param {!Node} jax The Mathjax node. |
93 * @return {Array.<Node>} The resulting node list. | 93 * @return {Array<Node>} The resulting node list. |
94 */ | 94 */ |
95 cvox.MathmlStoreUtil.checkMathjaxMsub = function(jax) { | 95 cvox.MathmlStoreUtil.checkMathjaxMsub = function(jax) { |
96 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MSUB'); | 96 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MSUB'); |
97 }; | 97 }; |
98 | 98 |
99 | 99 |
100 /** | 100 /** |
101 * Returns MathML node if MathJax is msup. | 101 * Returns MathML node if MathJax is msup. |
102 * @param {!Node} jax The Mathjax node. | 102 * @param {!Node} jax The Mathjax node. |
103 * @return {Array.<Node>} The resulting node list. | 103 * @return {Array<Node>} The resulting node list. |
104 */ | 104 */ |
105 cvox.MathmlStoreUtil.checkMathjaxMsup = function(jax) { | 105 cvox.MathmlStoreUtil.checkMathjaxMsup = function(jax) { |
106 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MSUP'); | 106 return cvox.MathmlStoreUtil.checkMathjaxTag(jax, 'MSUP'); |
107 }; | 107 }; |
108 | 108 |
109 | 109 |
110 /** | 110 /** |
111 * Constructs a closure that returns separators for an MathML mfenced | 111 * Constructs a closure that returns separators for an MathML mfenced |
112 * expression. | 112 * expression. |
113 * Separators in MathML are represented by a list and used up one by one | 113 * Separators in MathML are represented by a list and used up one by one |
(...skipping 23 matching lines...) Expand all Loading... |
137 if (sepList.length > 1) { | 137 if (sepList.length > 1) { |
138 return sepList.shift(); | 138 return sepList.shift(); |
139 } | 139 } |
140 return sepList[0]; | 140 return sepList[0]; |
141 }; | 141 }; |
142 }; | 142 }; |
143 | 143 |
144 | 144 |
145 /** | 145 /** |
146 * Computes the correct separators for each node. | 146 * Computes the correct separators for each node. |
147 * @param {Array.<Node>} nodes A node array. | 147 * @param {Array<Node>} nodes A node array. |
148 * @param {string} context A context string. | 148 * @param {string} context A context string. |
149 * @return {function(): string} A closure that returns the next separator for an | 149 * @return {function(): string} A closure that returns the next separator for an |
150 * mfenced expression starting with the first node in nodes. | 150 * mfenced expression starting with the first node in nodes. |
151 */ | 151 */ |
152 cvox.MathmlStoreUtil.mfencedSeparators = function(nodes, context) { | 152 cvox.MathmlStoreUtil.mfencedSeparators = function(nodes, context) { |
153 var nextSeparator = cvox.MathmlStoreUtil.nextSeparatorFunction(context); | 153 var nextSeparator = cvox.MathmlStoreUtil.nextSeparatorFunction(context); |
154 return function() { | 154 return function() { |
155 return nextSeparator ? nextSeparator() : ''; | 155 return nextSeparator ? nextSeparator() : ''; |
156 }; | 156 }; |
157 }; | 157 }; |
158 | 158 |
159 | 159 |
160 /** | 160 /** |
161 * Iterates over the list of content nodes of the parent of the given nodes. | 161 * Iterates over the list of content nodes of the parent of the given nodes. |
162 * @param {Array.<Node>} nodes A node array. | 162 * @param {Array<Node>} nodes A node array. |
163 * @param {string} context A context string. | 163 * @param {string} context A context string. |
164 * @return {function(): string} A closure that returns the content of the next | 164 * @return {function(): string} A closure that returns the content of the next |
165 * content node. Returns only context string if list is exhausted. | 165 * content node. Returns only context string if list is exhausted. |
166 */ | 166 */ |
167 cvox.MathmlStoreUtil.contentIterator = function(nodes, context) { | 167 cvox.MathmlStoreUtil.contentIterator = function(nodes, context) { |
168 if (nodes.length > 0) { | 168 if (nodes.length > 0) { |
169 var contentNodes = cvox.XpathUtil.evalXPath('../../content/*', nodes[0]); | 169 var contentNodes = cvox.XpathUtil.evalXPath('../../content/*', nodes[0]); |
170 } else { | 170 } else { |
171 var contentNodes = []; | 171 var contentNodes = []; |
172 } | 172 } |
173 return function() { | 173 return function() { |
174 var content = contentNodes.shift(); | 174 var content = contentNodes.shift(); |
175 return context + (content ? content.textContent : ''); | 175 return context + (content ? content.textContent : ''); |
176 }; | 176 }; |
177 }; | 177 }; |
OLD | NEW |