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 DOM utility functions to aid in table navigation. | 6 * @fileoverview DOM utility functions to aid in table navigation. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.TableUtil'); | 9 goog.provide('cvox.TableUtil'); |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 * | 30 * |
31 * Please Note: | 31 * Please Note: |
32 * The HTML5 spec specifies that only header <TH> elements can be headers | 32 * The HTML5 spec specifies that only header <TH> elements can be headers |
33 * ( http://dev.w3.org/html5/spec/tabular-data.html#row-header ) but the | 33 * ( http://dev.w3.org/html5/spec/tabular-data.html#row-header ) but the |
34 * HTML4 spec says that <TD> elements can act as headers if they have a | 34 * HTML4 spec says that <TD> elements can act as headers if they have a |
35 * scope attribute defined | 35 * scope attribute defined |
36 * ( http://www.w3.org/TR/html401/struct/tables.html#h-11.2.6 ). In the | 36 * ( http://www.w3.org/TR/html401/struct/tables.html#h-11.2.6 ). In the |
37 * interest of providing meaningful header information for all tables, here | 37 * interest of providing meaningful header information for all tables, here |
38 * we take the position that <TD> elements can act as headers. | 38 * we take the position that <TD> elements can act as headers. |
39 */ | 39 */ |
40 return ((cell.tagName == 'TH') || | 40 return ( |
41 cell.hasAttribute('scope') || (cell.hasAttribute('role') && | 41 (cell.tagName == 'TH') || cell.hasAttribute('scope') || |
42 ((cell.getAttribute('role') == 'rowheader') || | 42 (cell.hasAttribute('role') && |
43 (cell.getAttribute('role') == 'columnheader')))); | 43 ((cell.getAttribute('role') == 'rowheader') || |
| 44 (cell.getAttribute('role') == 'columnheader')))); |
44 }; | 45 }; |
45 | 46 |
46 | 47 |
47 /** | 48 /** |
48 * Utility function to determine colgroup structure. Builds an array that | 49 * Utility function to determine colgroup structure. Builds an array that |
49 * associates a column number to a particular col group. | 50 * associates a column number to a particular col group. |
50 * @param {Array} colGroups An array of all the colgroup elements in a | 51 * @param {Array} colGroups An array of all the colgroup elements in a |
51 * particular table. | 52 * particular table. |
52 * @return {Array} An array that maps indexes representing table columns | 53 * @return {Array} An array that maps indexes representing table columns |
53 * to indexes into the colGroups array. | 54 * to indexes into the colGroups array. |
54 */ | 55 */ |
55 cvox.TableUtil.determineColGroups = function(colGroups) { | 56 cvox.TableUtil.determineColGroups = function(colGroups) { |
56 var colToColGroup = []; | 57 var colToColGroup = []; |
57 | 58 |
58 if (colGroups.length == 0) { | 59 if (colGroups.length == 0) { |
59 return colToColGroup; | 60 return colToColGroup; |
60 } | 61 } |
61 // A colgroup has either a series of col element children or a span | 62 // A colgroup has either a series of col element children or a span |
62 // attribute. If it has col children, ignore the span attribute | 63 // attribute. If it has col children, ignore the span attribute |
63 for (var colGroupCtr = 0; colGroupCtr < colGroups.length; | 64 for (var colGroupCtr = 0; colGroupCtr < colGroups.length; colGroupCtr++) { |
64 colGroupCtr++) { | |
65 | |
66 var currentColGroup = colGroups[colGroupCtr]; | 65 var currentColGroup = colGroups[colGroupCtr]; |
67 | 66 |
68 var childCols = cvox.TableUtil.getColNodes(currentColGroup); | 67 var childCols = cvox.TableUtil.getColNodes(currentColGroup); |
69 if (childCols.length > 0) { | 68 if (childCols.length > 0) { |
70 for (var colNodeCtr = 0; colNodeCtr < childCols.length; | 69 for (var colNodeCtr = 0; colNodeCtr < childCols.length; colNodeCtr++) { |
71 colNodeCtr++) { | |
72 var colElement = childCols[colNodeCtr]; | 70 var colElement = childCols[colNodeCtr]; |
73 | 71 |
74 if (colElement.hasAttribute('span')) { | 72 if (colElement.hasAttribute('span')) { |
75 var span = parseInt(colElement.getAttribute('span'), 10); | 73 var span = parseInt(colElement.getAttribute('span'), 10); |
76 | 74 |
77 for (var s = 0; s < span; s++) { | 75 for (var s = 0; s < span; s++) { |
78 colToColGroup.push(colGroupCtr); | 76 colToColGroup.push(colGroupCtr); |
79 } | 77 } |
80 } else { | 78 } else { |
81 colToColGroup.push(colGroupCtr); | 79 colToColGroup.push(colGroupCtr); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 }; | 111 }; |
114 | 112 |
115 | 113 |
116 /** | 114 /** |
117 * Returns a JavaScript array of all the non-nested rows in the given table. | 115 * Returns a JavaScript array of all the non-nested rows in the given table. |
118 * | 116 * |
119 * @param {Node} table A table node. | 117 * @param {Node} table A table node. |
120 * @return {Array} An array of all the child rows of the active table. | 118 * @return {Array} An array of all the child rows of the active table. |
121 */ | 119 */ |
122 cvox.TableUtil.getChildRows = function(table) { | 120 cvox.TableUtil.getChildRows = function(table) { |
123 return cvox.XpathUtil.evalXPath('child::tbody/tr | child::thead/tr | ' + | 121 return cvox.XpathUtil.evalXPath( |
124 'child::*[attribute::role="row"]', table); | 122 'child::tbody/tr | child::thead/tr | ' + |
| 123 'child::*[attribute::role="row"]', |
| 124 table); |
125 }; | 125 }; |
126 | 126 |
127 | 127 |
128 /** | 128 /** |
129 * Returns a JavaScript array of all the child cell <TD> or <TH> or | 129 * Returns a JavaScript array of all the child cell <TD> or <TH> or |
130 * role='gridcell' nodes of the given row. | 130 * role='gridcell' nodes of the given row. |
131 * | 131 * |
132 * @param {Node} rowNode The specified row node. | 132 * @param {Node} rowNode The specified row node. |
133 * @return {Array} An array of all the child cells of the given row node. | 133 * @return {Array} An array of all the child cells of the given row node. |
134 */ | 134 */ |
135 cvox.TableUtil.getChildCells = function(rowNode) { | 135 cvox.TableUtil.getChildCells = function(rowNode) { |
136 return cvox.XpathUtil.evalXPath('child::td | child::th | ' + | 136 return cvox.XpathUtil.evalXPath( |
137 'child::*[attribute::role="gridcell"] |' + | 137 'child::td | child::th | ' + |
138 'child::*[attribute::role="rowheader"] |' + | 138 'child::*[attribute::role="gridcell"] |' + |
139 'child::*[attribute::role="columnheader"]', rowNode); | 139 'child::*[attribute::role="rowheader"] |' + |
| 140 'child::*[attribute::role="columnheader"]', |
| 141 rowNode); |
140 }; | 142 }; |
141 | 143 |
142 | 144 |
143 /** | 145 /** |
144 * Returns a JavaScript array containing the cell in the active table | 146 * Returns a JavaScript array containing the cell in the active table |
145 * with the given ID. | 147 * with the given ID. |
146 * | 148 * |
147 * @param {Node} table A table node. | 149 * @param {Node} table A table node. |
148 * @param {string} cellID The specified ID. | 150 * @param {string} cellID The specified ID. |
149 * @return {Array} An array containing the cell with the specified ID. | 151 * @return {Array} An array containing the cell with the specified ID. |
(...skipping 19 matching lines...) Expand all Loading... |
169 * Returns a Javascript array containing the child col elements of the given | 171 * Returns a Javascript array containing the child col elements of the given |
170 * colgroup element. | 172 * colgroup element. |
171 * | 173 * |
172 * @param {Node} colGroupNode The specified <COLGROUP> element. | 174 * @param {Node} colGroupNode The specified <COLGROUP> element. |
173 * @return {Array} An array of all of the child col elements of the given | 175 * @return {Array} An array of all of the child col elements of the given |
174 * colgroup element. | 176 * colgroup element. |
175 */ | 177 */ |
176 cvox.TableUtil.getColNodes = function(colGroupNode) { | 178 cvox.TableUtil.getColNodes = function(colGroupNode) { |
177 return cvox.XpathUtil.evalXPath('child::col', colGroupNode); | 179 return cvox.XpathUtil.evalXPath('child::col', colGroupNode); |
178 }; | 180 }; |
179 | |
OLD | NEW |