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 semantic tree computations. | 6 * @fileoverview Utility functions for semantic tree computations. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.SemanticUtil'); | 9 goog.provide('cvox.SemanticUtil'); |
10 | 10 |
11 | 11 |
12 /** | 12 /** |
13 * @constructor | 13 * @constructor |
14 */ | 14 */ |
15 cvox.SemanticUtil = function() { }; | 15 cvox.SemanticUtil = function() { }; |
16 | 16 |
17 | 17 |
18 /** | 18 /** |
19 * Merges keys of objects into an array. | 19 * Merges keys of objects into an array. |
20 * @param {...Object.<string, string>} objects Optional objects. | 20 * @param {...Object<string, string>} objects Optional objects. |
21 * @return {Array.<string>} Array of all keys of the objects. | 21 * @return {Array<string>} Array of all keys of the objects. |
22 */ | 22 */ |
23 cvox.SemanticUtil.objectsToKeys = function(objects) { | 23 cvox.SemanticUtil.objectsToKeys = function(objects) { |
24 objects = Array.prototype.slice.call(arguments, 0); | 24 objects = Array.prototype.slice.call(arguments, 0); |
25 var keys = []; | 25 var keys = []; |
26 return keys.concat.apply(keys, objects.map(Object.keys)); | 26 return keys.concat.apply(keys, objects.map(Object.keys)); |
27 }; | 27 }; |
28 | 28 |
29 | 29 |
30 /** | 30 /** |
31 * Merges values of objects into an array. | 31 * Merges values of objects into an array. |
32 * @param {...Object.<string, string>} objects Optional objects. | 32 * @param {...Object<string, string>} objects Optional objects. |
33 * @return {Array.<string>} Array of all values of the objects. | 33 * @return {Array<string>} Array of all values of the objects. |
34 */ | 34 */ |
35 cvox.SemanticUtil.objectsToValues = function(objects) { | 35 cvox.SemanticUtil.objectsToValues = function(objects) { |
36 objects = Array.prototype.slice.call(arguments, 0); | 36 objects = Array.prototype.slice.call(arguments, 0); |
37 var result = []; | 37 var result = []; |
38 var collectValues = function(obj) { | 38 var collectValues = function(obj) { |
39 for (var key in obj) { | 39 for (var key in obj) { |
40 result.push(obj[key]); | 40 result.push(obj[key]); |
41 } | 41 } |
42 }; | 42 }; |
43 objects.forEach(collectValues); | 43 objects.forEach(collectValues); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 * @param {Element} node The node. | 89 * @param {Element} node The node. |
90 * @return {string} The node's tagname. | 90 * @return {string} The node's tagname. |
91 */ | 91 */ |
92 cvox.SemanticUtil.tagName = function(node) { | 92 cvox.SemanticUtil.tagName = function(node) { |
93 return node.tagName.toUpperCase(); | 93 return node.tagName.toUpperCase(); |
94 }; | 94 }; |
95 | 95 |
96 | 96 |
97 /** | 97 /** |
98 * List of MathML Tags that are to be ignored. | 98 * List of MathML Tags that are to be ignored. |
99 * @type {Array.<string>} | 99 * @type {Array<string>} |
100 * @const | 100 * @const |
101 */ | 101 */ |
102 cvox.SemanticUtil.IGNORETAGS = [ | 102 cvox.SemanticUtil.IGNORETAGS = [ |
103 'MERROR', 'MPHANTOM', 'MSPACE', 'MACTION', 'MALIGNGROUP', 'MALIGNMARK', | 103 'MERROR', 'MPHANTOM', 'MSPACE', 'MACTION', 'MALIGNGROUP', 'MALIGNMARK', |
104 'MACTION' | 104 'MACTION' |
105 ]; | 105 ]; |
106 | 106 |
107 | 107 |
108 /** | 108 /** |
109 * List of MathML Tags to be ignore if they have no children. | 109 * List of MathML Tags to be ignore if they have no children. |
110 * @type {Array.<string>} | 110 * @type {Array<string>} |
111 * @const | 111 * @const |
112 */ | 112 */ |
113 cvox.SemanticUtil.EMPTYTAGS = ['MATH', 'MROW', 'MPADDED', 'MSTYLE']; | 113 cvox.SemanticUtil.EMPTYTAGS = ['MATH', 'MROW', 'MPADDED', 'MSTYLE']; |
114 | 114 |
115 | 115 |
116 /** | 116 /** |
117 * Removes elements from a list of MathML nodes that are either to be ignored or | 117 * Removes elements from a list of MathML nodes that are either to be ignored or |
118 * ignored if they have empty children. | 118 * ignored if they have empty children. |
119 * Observe that this is currently not recursive, i.e. will not take care of | 119 * Observe that this is currently not recursive, i.e. will not take care of |
120 * pathological cases, where content is hidden in incorrectly used tags! | 120 * pathological cases, where content is hidden in incorrectly used tags! |
121 * @param {Array.<Element>} nodes The node list to be cleaned. | 121 * @param {Array<Element>} nodes The node list to be cleaned. |
122 * @return {Array.<Element>} The cleansed list. | 122 * @return {Array<Element>} The cleansed list. |
123 */ | 123 */ |
124 cvox.SemanticUtil.purgeNodes = function(nodes) { | 124 cvox.SemanticUtil.purgeNodes = function(nodes) { |
125 var nodeArray = []; | 125 var nodeArray = []; |
126 for (var i = 0, node; node = nodes[i]; i++) { | 126 for (var i = 0, node; node = nodes[i]; i++) { |
127 var tagName = cvox.SemanticUtil.tagName(node); | 127 var tagName = cvox.SemanticUtil.tagName(node); |
128 if (cvox.SemanticUtil.IGNORETAGS.indexOf(tagName) != -1) continue; | 128 if (cvox.SemanticUtil.IGNORETAGS.indexOf(tagName) != -1) continue; |
129 if (cvox.SemanticUtil.EMPTYTAGS.indexOf(tagName) != -1 && | 129 if (cvox.SemanticUtil.EMPTYTAGS.indexOf(tagName) != -1 && |
130 node.childNodes.length == 0) | 130 node.childNodes.length == 0) |
131 continue; | 131 continue; |
132 nodeArray.push(node); | 132 nodeArray.push(node); |
133 } | 133 } |
134 return nodeArray; | 134 return nodeArray; |
135 }; | 135 }; |
OLD | NEW |