From 10c25c169261f9037d69bb676157753a81b789b6 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 23 Dec 2013 08:54:33 +0000 Subject: [PATCH] Fix slow regexp in importing TiddlyWiki HTML files --- core/modules/deserializers.js | 45 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/core/modules/deserializers.js b/core/modules/deserializers.js index 7a4ce7cc6..6c6119e25 100644 --- a/core/modules/deserializers.js +++ b/core/modules/deserializers.js @@ -36,30 +36,35 @@ var parseTiddlerDiv = function(text /* [,fields] */) { } } // Parse the DIV body - var divRegExp = /^\s*]*)>([(?:\s|\S)]*)<\/div>\s*$/gi, - subDivRegExp = /^\s*
([(?:\s|\S)]*)<\/pre>\s*$/gi,
-		attrRegExp = /\s*([^=\s]+)\s*=\s*"([^"]*)"/gi,
-		match = divRegExp.exec(text);
+	var startRegExp = /^\s*]*)>(\s*
)?/gi,
+		endRegExp,
+		match = startRegExp.exec(text);
 	if(match) {
-		var subMatch = subDivRegExp.exec(match[2]); // Body of the 
tag - if(subMatch) { - result.text = subMatch[1]; + // Old-style DIVs don't have the
 tag
+		if(match[2]) {
+			endRegExp = /<\/pre>\s*<\/div>\s*$/gi;
 		} else {
-			result.text = match[2]; 
+			endRegExp = /<\/div>\s*$/gi;
+		}
+		var endMatch = endRegExp.exec(text);
+		if(endMatch) {
+			// Extract the text
+			result.text = text.substring(match.index + match[0].length,endMatch.index);
+			// Process the attributes
+			var attrRegExp = /\s*([^=\s]+)\s*=\s*"([^"]*)"/gi,
+				attrMatch;
+			do {
+				attrMatch = attrRegExp.exec(match[1]);
+				if(attrMatch) {
+					var name = attrMatch[1];
+					var value = attrMatch[2];
+					result[name] = value;
+				}
+			} while(attrMatch);
+			return result;
 		}
-		var attrMatch;
-		do {
-			attrMatch = attrRegExp.exec(match[1]);
-			if(attrMatch) {
-				var name = attrMatch[1];
-				var value = attrMatch[2];
-				result[name] = value;
-			}
-		} while(attrMatch);
-		return result;
-	} else {
-		return undefined;
 	}
+	return undefined;
 };
 
 exports["application/x-tiddler-html-div"] = function(text,fields) {