From 3707e10ef604625518613a96308de606341e8c23 Mon Sep 17 00:00:00 2001 From: Rob Hoelz Date: Thu, 8 Aug 2024 04:06:44 -0500 Subject: [PATCH] Add start/end properties for table row/cell elements (#8494) * Add test for existing table parsing functionality * Add expected start/end for tr elements in table wikitext test * Populate start/end properties for tr elements * Add expected start/end for th/td elements in table wikitext test * Populate start/end properties for th/td elements --- .../modules/parsers/wikiparser/rules/table.js | 9 +- .../tiddlers/tests/test-wikitext-parser.js | 94 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 59aa81e91..fbdbb4f9d 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -93,11 +93,12 @@ var processRow = function(prevColumns) { } // Check whether this is a heading cell var cell; + var start = this.parser.pos; if(chr === "!") { this.parser.pos++; - cell = {type: "element", tag: "th", children: []}; + cell = {type: "element", tag: "th", start: start, children: []}; } else { - cell = {type: "element", tag: "td", children: []}; + cell = {type: "element", tag: "td", start: start, children: []}; } tree.push(cell); // Record information about this cell @@ -121,6 +122,7 @@ var processRow = function(prevColumns) { } // Move back to the closing `|` this.parser.pos--; + cell.end = this.parser.pos; } col++; cellRegExp.lastIndex = this.parser.pos; @@ -169,12 +171,13 @@ exports.parse = function() { rowContainer.children = this.parser.parseInlineRun(rowTermRegExp,{eatTerminator: true}); } else { // Create the row - var theRow = {type: "element", tag: "tr", children: []}; + var theRow = {type: "element", tag: "tr", children: [], start: rowMatch.index}; $tw.utils.addClassToParseTreeNode(theRow,rowCount%2 ? "oddRow" : "evenRow"); rowContainer.children.push(theRow); // Process the row theRow.children = processRow.call(this,prevColumns); this.parser.pos = rowMatch.index + rowMatch[0].length; + theRow.end = this.parser.pos; // Increment the row count rowCount++; } diff --git a/editions/test/tiddlers/tests/test-wikitext-parser.js b/editions/test/tiddlers/tests/test-wikitext-parser.js index 04d041ec9..a488ff030 100644 --- a/editions/test/tiddlers/tests/test-wikitext-parser.js +++ b/editions/test/tiddlers/tests/test-wikitext-parser.js @@ -324,6 +324,100 @@ describe("WikiText parser tests", function() { }); + it("should parse tables", function() { + let wikitext = ` +|!Cell1 |!Cell2 | +|Cell3 |Cell4 |`.trim(); + + let expectedParseTree = [{ + type: 'element', + tag: 'table', + start: 0, + end: 33, + rule: 'table', + children: [{ + type: 'element', + tag: 'tbody', + start: 0, + end: 33, + children: [{ + type: 'element', + tag: 'tr', + attributes: { + 'class': { name: 'class', type: 'string', value: 'evenRow' }, + }, + orderedAttributes: [ + { name: 'class', type: 'string', value: 'evenRow' }, + ], + start: 0, + end: 18, + children: [{ + type: 'element', + tag: 'th', + attributes: { + 'align': { name: 'align', type: 'string', value: 'left' }, + }, + orderedAttributes: [ + { name: 'align', type: 'string', value: 'left' }, + ], + start: 1, + end: 8, + children: [{type: 'text', text: 'Cell1', start: 2, end: 7}], + }, { + type: 'element', + tag: 'th', + attributes: { + 'align': { name: 'align', type: 'string', value: 'left' }, + }, + orderedAttributes: [ + { name: 'align', type: 'string', value: 'left' }, + ], + start: 9, + end: 16, + children: [{type: 'text', text: 'Cell2', start: 10, end: 15}], + }], + }, { + type: 'element', + tag: 'tr', + attributes: { + 'class': { name: 'class', type: 'string', value: 'oddRow' }, + }, + orderedAttributes: [ + { name: 'class', type: 'string', value: 'oddRow' }, + ], + start: 18, + end: 33, + children: [{ + type: 'element', + tag: 'td', + attributes: { + 'align': { name: 'align', type: 'string', value: 'left' }, + }, + orderedAttributes: [ + { name: 'align', type: 'string', value: 'left' }, + ], + start: 19, + end: 25, + children: [{type: 'text', text: 'Cell3', start: 19, end: 24}], + }, { + type: 'element', + tag: 'td', + attributes: { + 'align': { name: 'align', type: 'string', value: 'left' }, + }, + orderedAttributes: [ + { name: 'align', type: 'string', value: 'left' }, + ], + start: 26, + end: 32, + children: [{type: 'text', text: 'Cell4', start: 26, end: 31}], + }], + }], + }], + }]; + + expect(parse(wikitext)).toEqual(expectedParseTree); + }); }); })();