1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-17 07:14:50 +00:00

Simplify the regular expression for HTML comments

We were getting catastrophic backtracking in Chrome for some input
texts where the closing “—>” of the comment was omitted.
This commit is contained in:
Jermolene 2014-02-12 15:24:03 +00:00
parent 5c66bc6dbc
commit d5b526914b
2 changed files with 36 additions and 6 deletions

View File

@ -9,6 +9,8 @@ Wiki text block rule for HTML comments. For example:
<!-- This is a comment --> <!-- This is a comment -->
``` ```
Note that the syntax for comments is simplified to an opening "<!--" sequence and a closing "-->" sequence -- HTML itself implements a more complex format (see http://ostermiller.org/findhtmlcomment.html)
\*/ \*/
(function(){ (function(){
@ -21,13 +23,26 @@ exports.types = {block: true};
exports.init = function(parser) { exports.init = function(parser) {
this.parser = parser; this.parser = parser;
// Regexp to match - HTML comment regexp by Stephen Ostermiller, http://ostermiller.org/findhtmlcomment.html this.matchRegExp = /\<!--/mg;
this.matchRegExp = /\<![ \r\n\t]*(?:--(?:[^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>\r?\n/mg; this.endMatchRegExp = /--\>/mg;
};
exports.findNextMatch = function(startPos) {
this.matchRegExp.lastIndex = startPos;
this.match = this.matchRegExp.exec(this.parser.source);
if(this.match) {
this.endMatchRegExp.lastIndex = startPos + this.match[0].length;
this.endMatch = this.endMatchRegExp.exec(this.parser.source);
if(this.endMatch) {
return this.match.index;
}
}
return undefined;
}; };
exports.parse = function() { exports.parse = function() {
// Move past the match // Move past the match
this.parser.pos = this.matchRegExp.lastIndex; this.parser.pos = this.endMatchRegExp.lastIndex;
// Don't return any elements // Don't return any elements
return []; return [];
}; };

View File

@ -9,6 +9,8 @@ Wiki text inline rule for HTML comments. For example:
<!-- This is a comment --> <!-- This is a comment -->
``` ```
Note that the syntax for comments is simplified to an opening "<!--" sequence and a closing "-->" sequence -- HTML itself implements a more complex format (see http://ostermiller.org/findhtmlcomment.html)
\*/ \*/
(function(){ (function(){
@ -21,13 +23,26 @@ exports.types = {inline: true};
exports.init = function(parser) { exports.init = function(parser) {
this.parser = parser; this.parser = parser;
// Regexp to match - HTML comment regexp by Stephen Ostermiller, http://ostermiller.org/findhtmlcomment.html this.matchRegExp = /\<!--/mg;
this.matchRegExp = /\<![ \r\n\t]*(?:--(?:[^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/mg; this.endMatchRegExp = /--\>/mg;
};
exports.findNextMatch = function(startPos) {
this.matchRegExp.lastIndex = startPos;
this.match = this.matchRegExp.exec(this.parser.source);
if(this.match) {
this.endMatchRegExp.lastIndex = startPos + this.match[0].length;
this.endMatch = this.endMatchRegExp.exec(this.parser.source);
if(this.endMatch) {
return this.match.index;
}
}
return undefined;
}; };
exports.parse = function() { exports.parse = function() {
// Move past the match // Move past the match
this.parser.pos = this.matchRegExp.lastIndex; this.parser.pos = this.endMatchRegExp.lastIndex;
// Don't return any elements // Don't return any elements
return []; return [];
}; };