td-headers-attr-evaluate.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { tokenList } from '../../core/utils';
  2. function tdHeadersAttrEvaluate(node) {
  3. const cells = [];
  4. const reviewCells = [];
  5. const badCells = [];
  6. for (let rowIndex = 0; rowIndex < node.rows.length; rowIndex++) {
  7. const row = node.rows[rowIndex];
  8. for (let cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
  9. cells.push(row.cells[cellIndex]);
  10. }
  11. }
  12. const ids = cells.reduce((ids, cell) => {
  13. if (cell.getAttribute('id')) {
  14. ids.push(cell.getAttribute('id'));
  15. }
  16. return ids;
  17. }, []);
  18. cells.forEach(cell => {
  19. let isSelf = false;
  20. let notOfTable = false;
  21. if (!cell.hasAttribute('headers')) {
  22. return;
  23. }
  24. const headersAttr = cell.getAttribute('headers').trim();
  25. if (!headersAttr) {
  26. return reviewCells.push(cell);
  27. }
  28. // Get a list all the values of the headers attribute
  29. const headers = tokenList(headersAttr);
  30. if (headers.length !== 0) {
  31. // Check if the cell's id is in this list
  32. if (cell.getAttribute('id')) {
  33. isSelf = headers.indexOf(cell.getAttribute('id').trim()) !== -1;
  34. }
  35. // Check if the headers are of cells inside the table
  36. notOfTable = headers.some(header => !ids.includes(header));
  37. if (isSelf || notOfTable) {
  38. badCells.push(cell);
  39. }
  40. }
  41. });
  42. if (badCells.length > 0) {
  43. this.relatedNodes(badCells);
  44. return false;
  45. }
  46. if (reviewCells.length) {
  47. this.relatedNodes(reviewCells);
  48. return undefined;
  49. }
  50. return true;
  51. }
  52. export default tdHeadersAttrEvaluate;