tablewalker.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/tablewalker
  7. */
  8. /**
  9. * Table iterator class. It allows to iterate over a table cells. For each cell the iterator yields
  10. * {@link module:table/tablewalker~TableWalkerValue} with proper table cell attributes.
  11. */
  12. export default class TableWalker {
  13. /**
  14. * Creates a range iterator. All parameters are optional, but you have to specify either `boundaries` or `startPosition`.
  15. *
  16. * The most important values of iterator values are column & row of a cell.
  17. *
  18. * To iterate over given row:
  19. *
  20. * const tableWalker = new TableWalker( table, { startRow: 1, endRow: 2 } );
  21. *
  22. * for ( const cellInfo of tableWalker ) {
  23. * console.log( 'A cell at row ' + cellInfo.row + ' and column ' + cellInfo.column );
  24. * }
  25. *
  26. * For instance the above code for a table:
  27. *
  28. * +----+----+----+----+----+----+
  29. * | 00 | 02 | 03 | 05 |
  30. * | +----+----+----+----+
  31. * | | 12 | 14 | 15 |
  32. * | +----+----+----+----+
  33. * | | 22 |
  34. * |----+----+ +
  35. * | 31 | 32 | |
  36. * +----+----+----+----+----+----+
  37. *
  38. * will log in the console:
  39. *
  40. * 'A cell at row 1 and column 2'
  41. * 'A cell at row 1 and column 4'
  42. * 'A cell at row 1 and column 5'
  43. * 'A cell at row 2 and column 2'
  44. *
  45. * To iterate over spanned cells also:
  46. *
  47. * const tableWalker = new TableWalker( table, { startRow: 1, endRow: 1, includeSpanned: true } );
  48. *
  49. * for ( const cellInfo of tableWalker ) {
  50. * console.log( 'Cell at ' + cellInfo.row + ' x ' + cellInfo.column + ' : ' + ( cellInfo.cell ? 'has data' : 'is spanned' ) );
  51. * }
  52. *
  53. * will log in the console for the table from previous example:
  54. *
  55. * 'Cell at 1 x 0 : is spanned'
  56. * 'Cell at 1 x 1 : is spanned'
  57. * 'Cell at 1 x 2 : has data'
  58. * 'Cell at 1 x 3 : is spanned'
  59. * 'Cell at 1 x 4 : has data'
  60. * 'Cell at 1 x 5 : has data'
  61. *
  62. * @constructor
  63. * @param {module:engine/model/element~Element} table A table over which iterate.
  64. * @param {Object} [options={}] Object with configuration.
  65. * @param {Number} [options.column] A column index for which this iterator will output cells.
  66. * @param {Number} [options.startRow=0] A row index for which this iterator should start.
  67. * @param {Number} [options.endRow] A row index for which this iterator should end.
  68. * @param {Boolean} [options.includeSpanned] Also return values for spanned cells.
  69. */
  70. constructor( table, options = {} ) {
  71. /**
  72. * The walker's table element.
  73. *
  74. * @readonly
  75. * @member {module:engine/model/element~Element}
  76. */
  77. this.table = table;
  78. /**
  79. * A row index on which this iterator will start.
  80. *
  81. * @readonly
  82. * @member {Number}
  83. */
  84. this.startRow = options.startRow || 0;
  85. /**
  86. * A row index on which this iterator will end.
  87. *
  88. * @readonly
  89. * @member {Number}
  90. */
  91. this.endRow = typeof options.endRow == 'number' ? options.endRow : undefined;
  92. /**
  93. * Enables output of spanned cells that are normally not yielded.
  94. *
  95. * @type {Boolean}
  96. */
  97. this.includeSpanned = !!options.includeSpanned;
  98. this._ccc = typeof options.column == 'number' ? options.column : undefined;
  99. this._skipRows = new Set();
  100. /**
  101. * A current row index.
  102. *
  103. * @readonly
  104. * @member {Number}
  105. */
  106. this.row = 0;
  107. /**
  108. * A current cell index in a row.
  109. *
  110. * @readonly
  111. * @member {Number}
  112. */
  113. this.cell = 0;
  114. /**
  115. * A current column index.
  116. *
  117. * @readonly
  118. * @member {Number}
  119. */
  120. this.column = 0;
  121. /**
  122. * The previous cell in a row.
  123. *
  124. * @readonly
  125. * @member {module:engine/model/element~Element}
  126. * @private
  127. */
  128. this._previousCell = undefined;
  129. /**
  130. * Holds spanned cells info to be outputed when {@link #includeSpanned} is set to true.
  131. *
  132. * @type {Array.<module:table/tablewalker~TableWalkerValue>}
  133. * @private
  134. */
  135. this._spannedCells = [];
  136. /**
  137. * Cached table properties - returned for every yielded value.
  138. *
  139. * @readonly
  140. * @member {{headingRows: Number, headingColumns: Number}}
  141. * @private
  142. */
  143. this._tableData = {
  144. headingRows: parseInt( this.table.getAttribute( 'headingRows' ) || 0 ),
  145. headingColumns: parseInt( this.table.getAttribute( 'headingColumns' ) || 0 )
  146. };
  147. this._spans = new Map();
  148. }
  149. /**
  150. * Iterable interface.
  151. *
  152. * @returns {Iterable.<module:table/tablewalker~TableWalkerValue>}
  153. */
  154. [ Symbol.iterator ]() {
  155. return this;
  156. }
  157. /**
  158. * Gets the next table walker's value.
  159. *
  160. * @returns {module:table/tablewalker~TableWalkerValue} Next table walker's value.
  161. */
  162. next() {
  163. const row = this.table.getChild( this.row );
  164. if ( !row || ( this.endRow !== undefined && this.row > this.endRow ) ) {
  165. return { done: true };
  166. }
  167. if ( this._isSpanned( this.row, this.column ) ) {
  168. const column = this.column;
  169. const outValue = {
  170. row: this.row,
  171. column,
  172. rowspan: 1,
  173. colspan: 1,
  174. cellIndex: this.cell,
  175. cell: undefined,
  176. table: this._tableData
  177. };
  178. this.column++;
  179. if ( !this.includeSpanned || this.startRow > this.row || this._checkCCC( column, 1 ) || this._skipRows.has( this.row ) ) {
  180. return this.next();
  181. }
  182. return { done: false, value: outValue };
  183. }
  184. const cell = row.getChild( this.cell );
  185. if ( !cell ) {
  186. this.row++;
  187. this.column = 0;
  188. this.cell = 0;
  189. return this.next();
  190. }
  191. const colspan = parseInt( cell.getAttribute( 'colspan' ) || 1 );
  192. const rowspan = parseInt( cell.getAttribute( 'rowspan' ) || 1 );
  193. if ( colspan > 1 || rowspan > 1 ) {
  194. this._recordSpans( this.row, this.column, rowspan, colspan );
  195. }
  196. const column = this.column;
  197. const outValue = {
  198. cell,
  199. row: this.row,
  200. column,
  201. rowspan,
  202. colspan,
  203. cellIndex: this.cell,
  204. table: this._tableData
  205. };
  206. this.column++;
  207. this.cell++;
  208. if ( this.startRow > this.row || this._skipRows.has( this.row ) || this._checkCCC( column, colspan ) ) {
  209. return this.next();
  210. }
  211. return {
  212. done: false,
  213. value: outValue
  214. };
  215. }
  216. skipRow( row ) {
  217. this._skipRows.add( row );
  218. }
  219. _checkCCC( column, colspan ) {
  220. if ( this._ccc === undefined ) {
  221. return;
  222. }
  223. return !( column === this._ccc || ( column < this._ccc && column + colspan > this._ccc ) );
  224. }
  225. _isSpanned( row, column ) {
  226. if ( !this._spans.has( row ) ) {
  227. return false;
  228. }
  229. const rowSpans = this._spans.get( row );
  230. return rowSpans.has( column ) ? rowSpans.get( column ) : false;
  231. }
  232. _recordSpans( row, column, rowspan, colspan ) {
  233. // This will update all rows after columns
  234. for ( let columnToUpdate = column + 1; columnToUpdate <= column + colspan - 1; columnToUpdate++ ) {
  235. this._recordSpan( row, columnToUpdate );
  236. }
  237. // This will update all rows below up to row height with value of span width.
  238. for ( let rowToUpdate = row + 1; rowToUpdate < row + rowspan; rowToUpdate++ ) {
  239. for ( let columnToUpdate = column; columnToUpdate <= column + colspan - 1; columnToUpdate++ ) {
  240. this._recordSpan( rowToUpdate, columnToUpdate );
  241. }
  242. }
  243. }
  244. _recordSpan( row, column ) {
  245. if ( !this._spans.has( row ) ) {
  246. this._spans.set( row, new Map() );
  247. }
  248. const rowSpans = this._spans.get( row );
  249. rowSpans.set( column, 1 );
  250. }
  251. }
  252. /**
  253. * Object returned by {@link module:table/tablewalker~TableWalker} when traversing table cells.
  254. *
  255. * @typedef {Object} module:table/tablewalker~TableWalkerValue
  256. * @property {module:engine/model/element~Element} [cell] Current table cell. Might be empty if
  257. * {@link module:table/tablewalker~TableWalker#includeSpanned} is set to true.
  258. * @property {Number} row The row index of a cell.
  259. * @property {Number} column The column index of a cell. Column index is adjusted to widths & heights of previous cells.
  260. * @property {Number} [colspan] The colspan attribute of a cell - always defined even if model attribute is not present. Not set if
  261. * {@link module:table/tablewalker~TableWalker#includeSpanned} is set to true.
  262. * @property {Number} [rowspan] The rowspan attribute of a cell - always defined even if model attribute is not present. Not set if
  263. * {@link module:table/tablewalker~TableWalker#includeSpanned} is set to true.
  264. * @property {Number} cellIndex The index of a current cell in parent row. When using `includeSpanned` option it will indicate next child
  265. * index if #cell is empty (spanned cell).
  266. * @property {Object} table Table attributes
  267. * @property {Object} table.headingRows The heading rows attribute of a table - always defined even if model attribute is not present.
  268. * @property {Object} table.headingColumns The heading columns attribute of a table - always defined even if model attribute is not present.
  269. */