Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add start and end properties to WikiText AST nodes for all elements. #7866

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/modules/parsers/parseutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ exports.parseStringLiteral = function(source,pos) {
var match = reString.exec(source);
if(match && match.index === pos) {
node.value = match[1] !== undefined ? match[1] :(
match[2] !== undefined ? match[2] : match[3]
match[2] !== undefined ? match[2] : match[3]
);
node.end = pos + match[0].length;
return node;
Expand Down
9 changes: 6 additions & 3 deletions core/modules/parsers/wikiparser/rules/codeblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ exports.init = function(parser) {

exports.parse = function() {
var reEnd = /(\r?\n```$)/mg;
var languageStart = this.parser.pos + 3,
languageEnd = languageStart + this.match[1].length;
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;

// Look for the end of the block
reEnd.lastIndex = this.parser.pos;
var match = reEnd.exec(this.parser.source),
text;
text,
codeStart = this.parser.pos;
// Process the block
if(match) {
text = this.parser.source.substring(this.parser.pos,match.index);
Expand All @@ -48,8 +51,8 @@ exports.parse = function() {
return [{
type: "codeblock",
attributes: {
code: {type: "string", value: text},
language: {type: "string", value: this.match[1]}
code: {type: "string", value: text, start: codeStart, end: this.parser.pos},
language: {type: "string", value: this.match[1], start: languageStart, end: languageEnd}
}
}];
};
Expand Down
7 changes: 5 additions & 2 deletions core/modules/parsers/wikiparser/rules/codeinline.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ exports.parse = function() {
// Look for the end marker
reEnd.lastIndex = this.parser.pos;
var match = reEnd.exec(this.parser.source),
text;
text,
start = this.parser.pos;
// Process the text
if(match) {
text = this.parser.source.substring(this.parser.pos,match.index);
Expand All @@ -47,7 +48,9 @@ exports.parse = function() {
tag: "code",
children: [{
type: "text",
text: text
text: text,
start: start,
end: this.parser.pos
}]
}];
};
Expand Down
3 changes: 2 additions & 1 deletion core/modules/parsers/wikiparser/rules/extlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports.init = function(parser) {

exports.parse = function() {
// Move past the match
var start = this.parser.pos;
this.parser.pos = this.matchRegExp.lastIndex;
// Create the link unless it is suppressed
if(this.match[0].substr(0,1) === "~") {
Expand All @@ -46,7 +47,7 @@ exports.parse = function() {
rel: {type: "string", value: "noopener noreferrer"}
},
children: [{
type: "text", text: this.match[0]
type: "text", text: this.match[0], start: start, end: this.parser.pos
}]
}];
}
Expand Down
20 changes: 15 additions & 5 deletions core/modules/parsers/wikiparser/rules/filteredtranscludeblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ exports.init = function(parser) {

exports.parse = function() {
// Move past the match
var filterStart = this.parser.pos + 3;
var filterEnd = filterStart + this.match[1].length;
var toolTipStart = filterEnd + 1;
var toolTipEnd = toolTipStart + (this.match[2] ? this.match[2].length : 0);
var templateStart = toolTipEnd + 2;
var templateEnd = templateStart + (this.match[3] ? this.match[3].length : 0);
var styleStart = templateEnd + 2;
var styleEnd = styleStart + (this.match[4] ? this.match[4].length : 0);
var classesStart = styleEnd + 1;
var classesEnd = classesStart + (this.match[5] ? this.match[5].length : 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always collapse multiple var statements into one:

var filterStart = this.parser.pos + 3,
	filterEnd = .....

This applies to all changes throughout this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this apply while there is assignment?

Hope this can be done by eslint.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would need to be resolved manually.

this.parser.pos = this.matchRegExp.lastIndex;
// Get the match details
var filter = this.match[1],
Expand All @@ -42,21 +52,21 @@ exports.parse = function() {
var node = {
type: "list",
attributes: {
filter: {type: "string", value: filter}
filter: {type: "string", value: filter, start: filterStart, end: filterEnd},

This comment was marked as resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean use {type: "string", value: filter, filterStart, filterEnd} here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@linonetwo I must have misread the code this morning, this code is fine and needs no further changes, and this comment can be ignored.

},
isBlock: true
};
if(tooltip) {
node.attributes.tooltip = {type: "string", value: tooltip};
node.attributes.tooltip = {type: "string", value: tooltip, start: toolTipStart, end: toolTipEnd};
}
if(template) {
node.attributes.template = {type: "string", value: template};
node.attributes.template = {type: "string", value: template, start: templateStart, end: templateEnd};
}
if(style) {
node.attributes.style = {type: "string", value: style};
node.attributes.style = {type: "string", value: style, start: styleStart, end: styleEnd};
}
if(classes) {
node.attributes.itemClass = {type: "string", value: classes.split(".").join(" ")};
node.attributes.itemClass = {type: "string", value: classes.split(".").join(" "), start: classesStart, end: classesEnd};
}
return [node];
};
Expand Down
20 changes: 15 additions & 5 deletions core/modules/parsers/wikiparser/rules/filteredtranscludeinline.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ exports.init = function(parser) {
};

exports.parse = function() {
var filterStart = this.parser.pos + 3;
var filterEnd = filterStart + this.match[1].length;
var toolTipStart = filterEnd + 1;
var toolTipEnd = toolTipStart + (this.match[2] ? this.match[2].length : 0);
var templateStart = toolTipEnd + 2;
var templateEnd = templateStart + (this.match[3] ? this.match[3].length : 0);
var styleStart = templateEnd + 2;
var styleEnd = styleStart + (this.match[4] ? this.match[4].length : 0);
var classesStart = styleEnd + 1;
var classesEnd = classesStart + (this.match[5] ? this.match[5].length : 0);
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Get the match details
Expand All @@ -42,20 +52,20 @@ exports.parse = function() {
var node = {
type: "list",
attributes: {
filter: {type: "string", value: filter}
filter: {type: "string", value: filter, start: filterStart, end: filterEnd},
}
};
if(tooltip) {
node.attributes.tooltip = {type: "string", value: tooltip};
node.attributes.tooltip = {type: "string", value: tooltip, start: toolTipStart, end: toolTipEnd};
}
if(template) {
node.attributes.template = {type: "string", value: template};
node.attributes.template = {type: "string", value: template, start: templateStart, end: templateEnd};
}
if(style) {
node.attributes.style = {type: "string", value: style};
node.attributes.style = {type: "string", value: style, start: styleStart, end: styleEnd};
}
if(classes) {
node.attributes.itemClass = {type: "string", value: classes.split(".").join(" ")};
node.attributes.itemClass = {type: "string", value: classes.split(".").join(" "), start: classesStart, end: classesEnd};
}
return [node];
};
Expand Down
3 changes: 2 additions & 1 deletion core/modules/parsers/wikiparser/rules/hardlinebreaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ exports.parse = function() {
reEnd.lastIndex = this.parser.pos;
match = reEnd.exec(this.parser.source);
if(match) {
var start = this.parser.pos;
this.parser.pos = reEnd.lastIndex;
// Add a line break if the terminator was a line break
if(match[2]) {
tree.push({type: "element", tag: "br"});
tree.push({type: "element", tag: "br", start: start, end: this.parser.pos});
}
}
} while(match && !match[1]);
Expand Down
6 changes: 4 additions & 2 deletions core/modules/parsers/wikiparser/rules/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ exports.parse = function() {
// Move past the !s
this.parser.pos = this.matchRegExp.lastIndex;
// Parse any classes, whitespace and then the heading itself
var classStart = this.parser.pos;
var classes = this.parser.parseClasses();
var classEnd = this.parser.pos;
this.parser.skipWhitespace({treatNewlinesAsNonWhitespace: true});
var tree = this.parser.parseInlineRun(/(\r?\n)/mg);
// Return the heading
return [{
type: "element",
tag: "h" + headingLevel,
tag: "h" + headingLevel,
attributes: {
"class": {type: "string", value: classes.join(" ")}
"class": {type: "string", value: classes.join(" "), start: classStart, end: classEnd}
},
children: tree
}];
Expand Down
25 changes: 25 additions & 0 deletions core/modules/parsers/wikiparser/rules/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Parse the most recent match
exports.parse = function() {
// Retrieve the most recent match so that recursive calls don't overwrite it
var tag = this.nextTag;
if (!tag.isSelfClosing) {
tag.openTagStart = tag.start;
tag.openTagEnd = tag.end;
}
this.nextTag = null;
// Advance the parser position to past the tag
this.parser.pos = tag.end;
Expand All @@ -60,6 +64,27 @@ exports.parse = function() {
var reEnd = new RegExp("(" + reEndString + ")","mg");
tag.children = this.parser.parseInlineRun(reEnd,{eatTerminator: true});
}
tag.end = this.parser.pos;
tag.closeTagEnd = tag.end;
if (tag.closeTagEnd === tag.openTagEnd || this.parser.source[tag.closeTagEnd - 1] !== '>') {
tag.closeTagStart = tag.end;
} else {
tag.closeTagStart = tag.closeTagEnd - 2;
var closeTagMinPos = tag.children.length > 0 ? tag.children[tag.children.length-1].end : tag.openTagEnd;
if (!Number.isSafeInteger(closeTagMinPos)) closeTagMinPos = tag.openTagEnd;
while (tag.closeTagStart >= closeTagMinPos) {
var char = this.parser.source[tag.closeTagStart];
if (char === '>') {
tag.closeTagStart = -1;
break;
}
if (char === '<') break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always use double quotes for string constants.

tag.closeTagStart -= 1;
}
if (tag.closeTagStart < closeTagMinPos) {
tag.closeTagStart = tag.end;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gk0Wk @Jermolene we should make sure that this PR does not introduce any significant performance regression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this won't cost much, because it just while from > to the < in <div>, won't step more than <blockquote>

But why would we need open/closeTagStart/End ? Can this feature be turn on when needed?

}
// Return the tag
return [tag];
Expand Down
4 changes: 2 additions & 2 deletions core/modules/parsers/wikiparser/rules/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ exports.parseImage = function(source,pos) {
}
pos = token.end;
if(token.match[1]) {
node.attributes.tooltip = {type: "string", value: token.match[1].trim()};
node.attributes.tooltip = {type: "string", value: token.match[1].trim(),start: token.start,end:token.start + token.match[1].length - 1};
}
node.attributes.source = {type: "string", value: (token.match[2] || "").trim()};
node.attributes.source = {type: "string", value: (token.match[2] || "").trim(), start: token.start + (token.match[1] ? token.match[1].length : 0), end: token.end - 2};
// Update the end position
node.end = pos;
return node;
Expand Down
3 changes: 2 additions & 1 deletion core/modules/parsers/wikiparser/rules/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ exports.parse = function() {
// Parse the filter terminated by a line break
var reMatch = /(.*)(?:$|\r?\n)/mg;
reMatch.lastIndex = this.parser.pos;
var filterStart = this.parser.source;
var match = reMatch.exec(this.parser.source);
this.parser.pos = reMatch.lastIndex;
// Parse tree nodes to return
return [{
type: "importvariables",
attributes: {
filter: {type: "string", value: match[1]}
filter: {type: "string", value: match[1], start: filterStart, end: this.parser.pos}
},
children: []
}];
Expand Down
33 changes: 29 additions & 4 deletions core/modules/parsers/wikiparser/rules/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ exports.parse = function() {
// Match the list marker
var reMatch = /([\*#;:>]+)/mg;
reMatch.lastIndex = this.parser.pos;
var start = this.parser.pos;
var match = reMatch.exec(this.parser.source);
if(!match || match.index !== this.parser.pos) {
break;
Expand All @@ -94,9 +95,21 @@ exports.parse = function() {
}
// Construct the list element or reuse the previous one at this level
if(listStack.length <= t) {
var listElement = {type: "element", tag: listInfo.listTag, children: [
{type: "element", tag: listInfo.itemTag, children: []}
]};
var listElement = {
type: "element",
tag: listInfo.listTag,
children: [
{
type: "element",
tag: listInfo.itemTag,
children: [],
start: start,
end: this.parser.pos,
}
],
start: start,
end: this.parser.pos,
};
// Link this list element into the last child item of the parent list item
if(t) {
var prevListItem = listStack[t-1].children[listStack[t-1].children.length-1];
Expand All @@ -105,21 +118,33 @@ exports.parse = function() {
// Save this element in the stack
listStack[t] = listElement;
} else if(t === (match[0].length - 1)) {
listStack[t].children.push({type: "element", tag: listInfo.itemTag, children: []});
listStack[t].children.push({
type: "element",
tag: listInfo.itemTag,
children: [],
start: start,
end: this.parser.pos,
});
}
}
if(listStack.length > match[0].length) {
listStack.splice(match[0].length,listStack.length - match[0].length);
}
// Process the body of the list item into the last list item
var classStart = this.parser.pos;
var lastListChildren = listStack[listStack.length-1].children,
lastListItem = lastListChildren[lastListChildren.length-1],
classes = this.parser.parseClasses();
var classEnd = this.parser.pos;
this.parser.skipWhitespace({treatNewlinesAsNonWhitespace: true});
var tree = this.parser.parseInlineRun(/(\r?\n)/mg);
lastListItem.children.push.apply(lastListItem.children,tree);
lastListItem.end = this.parser.pos;
listStack[listStack.length-1].end = this.parser.pos;
if(classes.length > 0) {
$tw.utils.addClassToParseTreeNode(lastListItem,classes.join(" "));
lastListItem.attributes.class.start = classStart;
lastListItem.attributes.class.end = classEnd;
}
// Consume any whitespace following the list item
this.parser.skipWhitespace();
Expand Down
9 changes: 7 additions & 2 deletions core/modules/parsers/wikiparser/rules/prettyextlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,20 @@ exports.parseLink = function(source,pos) {
splitPos = null;
}
// Pull out the tooltip and URL
var tooltip, URL;
var tooltip, URL, urlStart;
textNode.start = pos;
if(splitPos) {
urlStart = splitPos + 1;
URL = source.substring(splitPos + 1,closePos).trim();
textNode.text = source.substring(pos,splitPos).trim();
textNode.end = splitPos;
} else {
urlStart = pos;
URL = source.substring(pos,closePos).trim();
textNode.text = URL;
textNode.end = closePos;
}
node.attributes.href = {type: "string", value: URL};
node.attributes.href = {type: "string", value: URL, start: urlStart, end: closePos};
node.attributes.target = {type: "string", value: "_blank"};
node.attributes.rel = {type: "string", value: "noopener noreferrer"};
// Update the end position
Expand Down
17 changes: 12 additions & 5 deletions core/modules/parsers/wikiparser/rules/prettylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,39 @@ exports.init = function(parser) {

exports.parse = function() {
// Move past the match
var start = this.parser.pos + 2;
this.parser.pos = this.matchRegExp.lastIndex;
// Process the link
var text = this.match[1],
link = this.match[2] || text;
link = this.match[2] || text,
textEndPos = this.parser.source.indexOf("|", start);
if (textEndPos < 0 || textEndPos > this.matchRegExp.lastIndex) {
textEndPos = this.matchRegExp.lastIndex - 2;
}
var linkStart = this.match[2] ? (start + this.match[1].length + 1) : start;
var linkEnd = linkStart + link.length;
if($tw.utils.isLinkExternal(link)) {
return [{
type: "element",
tag: "a",
attributes: {
href: {type: "string", value: link},
href: {type: "string", value: link, start: linkStart, end: linkEnd},
"class": {type: "string", value: "tc-tiddlylink-external"},
target: {type: "string", value: "_blank"},
rel: {type: "string", value: "noopener noreferrer"}
},
children: [{
type: "text", text: text
type: "text", text: text, start: start, end: textEndPos
}]
}];
} else {
return [{
type: "link",
attributes: {
to: {type: "string", value: link}
to: {type: "string", value: link, start: linkStart, end: linkEnd}
},
children: [{
type: "text", text: text
type: "text", text: text, start: start, end: textEndPos
}]
}];
}
Expand Down
Loading
Loading