1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-10 15:53:58 +00:00

Make the html parser rule work in block mode as well as inline mode

This commit is contained in:
Jeremy Ruston 2012-12-30 17:21:38 +00:00
parent 300b6cc485
commit 7d5c355d9e

View File

@ -24,14 +24,18 @@ This is a widget invocation
"use strict"; "use strict";
exports.name = "html"; exports.name = "html";
exports.types = {inline: true}; exports.types = {inline: true, block: true};
var voidElements = "area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(","); var voidElements = "area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr".split(",");
exports.init = function(parser) { exports.init = function(parser) {
this.parser = parser; this.parser = parser;
// Regexp to match // Regexp to match
this.matchRegExp = /<(\$)?([A-Za-z]+)(\s*[^>]*?)(\/)?>/mg; if(this.is.block) {
this.matchRegExp = /<(\$)?([A-Za-z]+)(\s*[^>]*?)(\/)?>(\r?\n)/mg;
} else {
this.matchRegExp = /<(\$)?([A-Za-z]+)(\s*[^>]*?)(\/)?>(\r?\n)?/mg;
}
}; };
/* /*
@ -42,12 +46,11 @@ exports.parse = function() {
var isWidget = !!this.match[1], var isWidget = !!this.match[1],
tagName = this.match[2], tagName = this.match[2],
attributeString = this.match[3], attributeString = this.match[3],
isSelfClosing = !!this.match[4]; isSelfClosing = !!this.match[4],
hasLineBreak = !!this.match[5];
// Move past the tag name and parameters // Move past the tag name and parameters
this.parser.pos = this.matchRegExp.lastIndex; this.parser.pos = this.matchRegExp.lastIndex;
var reLineBreak = /(\r?\n)/mg, var reAttr = /\s*([A-Za-z\-_]+)(?:\s*=\s*(?:("[^"]*")|('[^']*')|(\{\{[^\}]*\}\})|([^"'\s]+)))?/mg;
reAttr = /\s*([A-Za-z\-_]+)(?:\s*=\s*(?:("[^"]*")|('[^']*')|(\{\{[^\}]*\}\})|([^"'\s]+)))?/mg,
isBlock;
// Process the attributes // Process the attributes
var attrMatch = reAttr.exec(attributeString), var attrMatch = reAttr.exec(attributeString),
attributes = {}; attributes = {};
@ -68,20 +71,12 @@ exports.parse = function() {
attributes[name] = value; attributes[name] = value;
attrMatch = reAttr.exec(attributeString); attrMatch = reAttr.exec(attributeString);
} }
// Check for a line break immediate after the opening tag // Process the end tag
reLineBreak.lastIndex = this.parser.pos;
var lineBreakMatch = reLineBreak.exec(this.parser.source);
if(lineBreakMatch && lineBreakMatch.index === this.parser.pos) {
this.parser.pos = lineBreakMatch.index + lineBreakMatch[0].length;
isBlock = true;
} else {
isBlock = false;
}
if(!isSelfClosing && (isWidget || voidElements.indexOf(tagName) === -1)) { if(!isSelfClosing && (isWidget || voidElements.indexOf(tagName) === -1)) {
var reEndString = "(</" + (isWidget ? "\\$" : "") + tagName + ">)", var reEndString = "(</" + (isWidget ? "\\$" : "") + tagName + ">)",
reEnd = new RegExp(reEndString,"mg"), reEnd = new RegExp(reEndString,"mg"),
content; content;
if(isBlock) { if(hasLineBreak) {
content = this.parser.parseBlocks(reEndString); content = this.parser.parseBlocks(reEndString);
} else { } else {
content = this.parser.parseInlineRun(reEnd); content = this.parser.parseInlineRun(reEnd);
@ -94,7 +89,13 @@ exports.parse = function() {
} else { } else {
content = []; content = [];
} }
var element = {type: isWidget ? "widget" : "element", tag: tagName, isBlock: isBlock, attributes: attributes, children: content}; var element = {
type: isWidget ? "widget" : "element",
tag: tagName,
isBlock: this.is.block || hasLineBreak,
attributes: attributes,
children: content
};
return [element]; return [element];
}; };