1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-05 21:33:52 +00:00

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
This commit is contained in:
Rob Hoelz 2024-08-08 04:06:44 -05:00 committed by GitHub
parent fd056d210a
commit 3707e10ef6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 100 additions and 3 deletions

View File

@ -93,11 +93,12 @@ var processRow = function(prevColumns) {
} }
// Check whether this is a heading cell // Check whether this is a heading cell
var cell; var cell;
var start = this.parser.pos;
if(chr === "!") { if(chr === "!") {
this.parser.pos++; this.parser.pos++;
cell = {type: "element", tag: "th", children: []}; cell = {type: "element", tag: "th", start: start, children: []};
} else { } else {
cell = {type: "element", tag: "td", children: []}; cell = {type: "element", tag: "td", start: start, children: []};
} }
tree.push(cell); tree.push(cell);
// Record information about this cell // Record information about this cell
@ -121,6 +122,7 @@ var processRow = function(prevColumns) {
} }
// Move back to the closing `|` // Move back to the closing `|`
this.parser.pos--; this.parser.pos--;
cell.end = this.parser.pos;
} }
col++; col++;
cellRegExp.lastIndex = this.parser.pos; cellRegExp.lastIndex = this.parser.pos;
@ -169,12 +171,13 @@ exports.parse = function() {
rowContainer.children = this.parser.parseInlineRun(rowTermRegExp,{eatTerminator: true}); rowContainer.children = this.parser.parseInlineRun(rowTermRegExp,{eatTerminator: true});
} else { } else {
// Create the row // 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"); $tw.utils.addClassToParseTreeNode(theRow,rowCount%2 ? "oddRow" : "evenRow");
rowContainer.children.push(theRow); rowContainer.children.push(theRow);
// Process the row // Process the row
theRow.children = processRow.call(this,prevColumns); theRow.children = processRow.call(this,prevColumns);
this.parser.pos = rowMatch.index + rowMatch[0].length; this.parser.pos = rowMatch.index + rowMatch[0].length;
theRow.end = this.parser.pos;
// Increment the row count // Increment the row count
rowCount++; rowCount++;
} }

View File

@ -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);
});
}); });
})(); })();