From c9c1b0fbb4af35b2e38e066bb6da93fae3fb089f Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 14 May 2014 08:51:08 +0100 Subject: [PATCH] Get rid of the tweakParseTreeNode() hack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s an embarrassing hangover from a refactoring of the parsing mechanism last year. --- core/modules/parsers/csvparser.js | 2 +- core/modules/parsers/textparser.js | 3 +- .../parsers/wikiparser/rules/codeblock.js | 3 +- .../rules/filteredtranscludeblock.js | 3 +- .../rules/filteredtranscludeinline.js | 3 +- core/modules/parsers/wikiparser/rules/html.js | 13 ++++---- .../modules/parsers/wikiparser/rules/image.js | 8 ++--- .../parsers/wikiparser/rules/prettylink.js | 3 +- .../wikiparser/rules/transcludeblock.js | 6 ++-- .../wikiparser/rules/transcludeinline.js | 6 ++-- .../parsers/wikiparser/rules/wikilink.js | 3 +- core/modules/utils/parsetree.js | 30 ++++++++----------- core/modules/wiki.js | 15 ---------- .../test/tiddlers/tests/test-html-parser.js | 8 ++--- .../tiddlers/tests/test-wikitext-parser.js | 2 +- 15 files changed, 39 insertions(+), 69 deletions(-) diff --git a/core/modules/parsers/csvparser.js b/core/modules/parsers/csvparser.js index e359de6b5..408638f80 100644 --- a/core/modules/parsers/csvparser.js +++ b/core/modules/parsers/csvparser.js @@ -15,7 +15,7 @@ The CSV text parser processes CSV files into a table wrapped in a scrollable wid var CsvParser = function(type,text,options) { // Table framework this.tree = [{ - "type": "element", "tag": "$scrollable", "children": [{ + "type": "scrollable", "children": [{ "type": "element", "tag": "table", "children": [{ "type": "element", "tag": "tbody", "children": [] }], "attributes": { diff --git a/core/modules/parsers/textparser.js b/core/modules/parsers/textparser.js index 518bba021..4f55f6f0c 100644 --- a/core/modules/parsers/textparser.js +++ b/core/modules/parsers/textparser.js @@ -14,8 +14,7 @@ The plain text parser processes blocks of source text into a degenerate parse tr var TextParser = function(type,text,options) { this.tree = [{ - type: "element", - tag: "$codeblock", + type: "codeblock", attributes: { code: {type: "string", value: text}, language: {type: "string", value: type} diff --git a/core/modules/parsers/wikiparser/rules/codeblock.js b/core/modules/parsers/wikiparser/rules/codeblock.js index b00a0bb74..262038f87 100644 --- a/core/modules/parsers/wikiparser/rules/codeblock.js +++ b/core/modules/parsers/wikiparser/rules/codeblock.js @@ -46,8 +46,7 @@ exports.parse = function() { } // Return the $codeblock widget return [{ - type: "element", - tag: "$codeblock", + type: "codeblock", attributes: { code: {type: "string", value: text}, language: {type: "string", value: this.match[1]} diff --git a/core/modules/parsers/wikiparser/rules/filteredtranscludeblock.js b/core/modules/parsers/wikiparser/rules/filteredtranscludeblock.js index a8d223ccd..5a863a42a 100644 --- a/core/modules/parsers/wikiparser/rules/filteredtranscludeblock.js +++ b/core/modules/parsers/wikiparser/rules/filteredtranscludeblock.js @@ -40,8 +40,7 @@ exports.parse = function() { classes = this.match[5]; // Return the list widget var node = { - type: "element", - tag: "$list", + type: "list", attributes: { filter: {type: "string", value: filter} }, diff --git a/core/modules/parsers/wikiparser/rules/filteredtranscludeinline.js b/core/modules/parsers/wikiparser/rules/filteredtranscludeinline.js index a7595dd46..abda75f95 100644 --- a/core/modules/parsers/wikiparser/rules/filteredtranscludeinline.js +++ b/core/modules/parsers/wikiparser/rules/filteredtranscludeinline.js @@ -40,8 +40,7 @@ exports.parse = function() { classes = this.match[5]; // Return the list widget var node = { - type: "element", - tag: "$list", + type: "list", attributes: { filter: {type: "string", value: filter} } diff --git a/core/modules/parsers/wikiparser/rules/html.js b/core/modules/parsers/wikiparser/rules/html.js index 9d35569ac..0062d5988 100644 --- a/core/modules/parsers/wikiparser/rules/html.js +++ b/core/modules/parsers/wikiparser/rules/html.js @@ -97,6 +97,9 @@ exports.parseTag = function(source,pos,options) { return null; } node.tag = token.match[1]; + if(node.tag.charAt(0) === "$") { + node.type = node.tag.substr(1); + } pos = token.end; // Process attributes var attribute = $tw.utils.parseAttribute(source,pos); @@ -142,7 +145,7 @@ exports.findNextTag = function(source,pos,options) { // Try to parse the candidate as a tag var tag = this.parseTag(source,match.index,options); // Return success - if(tag && this.isLegalTag(tag.tag)) { + if(tag && this.isLegalTag(tag)) { return tag; } // Look for the next match @@ -154,11 +157,11 @@ exports.findNextTag = function(source,pos,options) { }; exports.isLegalTag = function(tag) { - // If it starts with a $ then we'll let anything go - if(tag.charAt(0) === "$") { + // Widgets are always OK + if(tag.type !== "element") { return true; - // If it starts with a dash then it's not legal - } else if(tag.charAt(0) === "-") { + // If it's an HTML tag that starts with a dash then it's not legal + } else if(tag.tag.charAt(0) === "-") { return false; } else { // Otherwise it's OK diff --git a/core/modules/parsers/wikiparser/rules/image.js b/core/modules/parsers/wikiparser/rules/image.js index 7670f2d9f..4dd1855e3 100644 --- a/core/modules/parsers/wikiparser/rules/image.js +++ b/core/modules/parsers/wikiparser/rules/image.js @@ -40,8 +40,7 @@ exports.parse = function() { // Move past the match this.parser.pos = this.nextImage.end; var node = { - type: "element", - tag: "$image", + type: "image", attributes: this.nextImage.attributes }; return [node]; @@ -72,13 +71,12 @@ exports.findNextImage = function(source,pos) { }; /* -Look for an image at the specified position. Returns null if not found, otherwise returns {type: "element", name: "$image", attributes: [], isSelfClosing:, start:, end:,} +Look for an image at the specified position. Returns null if not found, otherwise returns {type: "image", attributes: [], isSelfClosing:, start:, end:,} */ exports.parseImage = function(source,pos) { var token, node = { - type: "element", - name: "$image", + type: "image", start: pos, attributes: {} }; diff --git a/core/modules/parsers/wikiparser/rules/prettylink.js b/core/modules/parsers/wikiparser/rules/prettylink.js index f852f39df..2b6cfeb22 100644 --- a/core/modules/parsers/wikiparser/rules/prettylink.js +++ b/core/modules/parsers/wikiparser/rules/prettylink.js @@ -53,8 +53,7 @@ exports.parse = function() { }]; } else { return [{ - type: "element", - tag: "$link", + type: "link", attributes: { to: {type: "string", value: link} }, diff --git a/core/modules/parsers/wikiparser/rules/transcludeblock.js b/core/modules/parsers/wikiparser/rules/transcludeblock.js index 087e95706..6cfb472fc 100644 --- a/core/modules/parsers/wikiparser/rules/transcludeblock.js +++ b/core/modules/parsers/wikiparser/rules/transcludeblock.js @@ -36,8 +36,7 @@ exports.parse = function() { textRef = $tw.utils.trim(this.match[1]); // Prepare the transclude widget var transcludeNode = { - type: "element", - tag: "$transclude", + type: "transclude", attributes: {}, isBlock: true }; @@ -48,8 +47,7 @@ exports.parse = function() { targetField = tr.field, targetIndex = tr.index, tiddlerNode = { - type: "element", - tag: "$tiddler", + type: "tiddler", attributes: { tiddler: {type: "string", value: targetTitle} }, diff --git a/core/modules/parsers/wikiparser/rules/transcludeinline.js b/core/modules/parsers/wikiparser/rules/transcludeinline.js index 04a7ff5bb..66f16bb4b 100644 --- a/core/modules/parsers/wikiparser/rules/transcludeinline.js +++ b/core/modules/parsers/wikiparser/rules/transcludeinline.js @@ -34,8 +34,7 @@ exports.parse = function() { textRef = $tw.utils.trim(this.match[1]); // Prepare the transclude widget var transcludeNode = { - type: "element", - tag: "$transclude", + type: "transclude", attributes: {} }; // Prepare the tiddler widget @@ -45,8 +44,7 @@ exports.parse = function() { targetField = tr.field, targetIndex = tr.index, tiddlerNode = { - type: "element", - tag: "$tiddler", + type: "tiddler", attributes: { tiddler: {type: "string", value: targetTitle} }, diff --git a/core/modules/parsers/wikiparser/rules/wikilink.js b/core/modules/parsers/wikiparser/rules/wikilink.js index 5e744aea8..152b4e193 100644 --- a/core/modules/parsers/wikiparser/rules/wikilink.js +++ b/core/modules/parsers/wikiparser/rules/wikilink.js @@ -64,8 +64,7 @@ exports.parse = function() { } } return [{ - type: "element", - tag: "$link", + type: "link", attributes: { to: {type: "string", value: linkText} }, diff --git a/core/modules/utils/parsetree.js b/core/modules/utils/parsetree.js index 123ca011b..dc811c727 100644 --- a/core/modules/utils/parsetree.js +++ b/core/modules/utils/parsetree.js @@ -13,14 +13,12 @@ Parse tree utility functions. "use strict"; exports.addAttributeToParseTreeNode = function(node,name,value) { - if(node.type === "element") { - node.attributes = node.attributes || {}; - node.attributes[name] = {type: "string", value: value}; - } + node.attributes = node.attributes || {}; + node.attributes[name] = {type: "string", value: value}; }; exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) { - if(node.type === "element" && node.attributes && node.attributes[name] && node.attributes[name].value !== undefined) { + if(node.attributes && node.attributes[name] && node.attributes[name].value !== undefined) { return node.attributes[name].value; } return defaultValue; @@ -28,29 +26,25 @@ exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) { exports.addClassToParseTreeNode = function(node,classString) { var classes = []; - if(node.type === "element") { - node.attributes = node.attributes || {}; - node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""}; - if(node.attributes["class"].type === "string") { - if(node.attributes["class"].value !== "") { - classes = node.attributes["class"].value.split(" "); - } - if(classString !== "") { - $tw.utils.pushTop(classes,classString.split(" ")); - } - node.attributes["class"].value = classes.join(" "); + node.attributes = node.attributes || {}; + node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""}; + if(node.attributes["class"].type === "string") { + if(node.attributes["class"].value !== "") { + classes = node.attributes["class"].value.split(" "); } + if(classString !== "") { + $tw.utils.pushTop(classes,classString.split(" ")); + } + node.attributes["class"].value = classes.join(" "); } }; exports.addStyleToParseTreeNode = function(node,name,value) { - if(node.type === "element") { node.attributes = node.attributes || {}; node.attributes["style"] = node.attributes["style"] || {type: "string", value: ""}; if(node.attributes["style"].type === "string") { node.attributes["style"].value += name + ":" + value + ";"; } - } }; exports.findParseTreeNode = function(nodeArray,search) { diff --git a/core/modules/wiki.js b/core/modules/wiki.js index af398aec9..daf61ecc5 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -741,19 +741,6 @@ exports.old_parseTiddler = function(title,options) { }) : null; }; -// We need to tweak parse trees generated by the existing parser because of the change from {type:"element",tag:"$tiddler",...} to {type:"tiddler",...} -var tweakParseTreeNode = function(node) { - if(node.type === "element" && node.tag.charAt(0) === "$") { - node.type = node.tag.substr(1); - delete node.tag; - } - tweakParseTreeNodes(node.children); -}; - -var tweakParseTreeNodes = function(nodeList) { - $tw.utils.each(nodeList,tweakParseTreeNode); -}; - var tweakMacroDefinition = function(nodeList) { if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") { nodeList[0].type = "set"; @@ -770,8 +757,6 @@ var tweakMacroDefinition = function(nodeList) { var tweakParser = function(parser) { // Move any macro definitions to contain the body tree tweakMacroDefinition(parser.tree); - // Tweak widgets - tweakParseTreeNodes(parser.tree); }; exports.parseText = function(type,text,options) { diff --git a/editions/test/tiddlers/tests/test-html-parser.js b/editions/test/tiddlers/tests/test-html-parser.js index 00aa8d4b2..e4c195cf1 100644 --- a/editions/test/tiddlers/tests/test-html-parser.js +++ b/editions/test/tiddlers/tests/test-html-parser.js @@ -165,22 +165,22 @@ describe("HTML tag new parser tests", function() { { type : 'element', start : 0, attributes : { attrib1 : { type : 'string', value : 'true', start : 6, name : 'attrib1', end : 14 } }, tag : 'mytag', isSelfClosing : true, end : 16 } ); expect(parser.parseTag("<$view field=\"title\" format=\"link\"/>",0)).toEqual( - { type : 'element', start : 0, attributes : { field : { start : 6, name : 'field', type : 'string', value : 'title', end : 20 }, format : { start : 20, name : 'format', type : 'string', value : 'link', end : 34 } }, tag : '$view', isSelfClosing : true, end : 36 } + { type : 'view', start : 0, attributes : { field : { start : 6, name : 'field', type : 'string', value : 'title', end : 20 }, format : { start : 20, name : 'format', type : 'string', value : 'link', end : 34 } }, tag : '$view', isSelfClosing : true, end : 36 } ); expect(parser.parseTag("",0)).toEqual( { type : 'element', start : 0, attributes : { attrib1 : { type : 'string', start : 6, name : 'attrib1', value : 'something', end : 26 } }, tag : 'mytag', end : 27 } ); expect(parser.parseTag("<$mytag attrib1='something' attrib2=else thing>",0)).toEqual( - { type : 'element', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 46 } }, tag : '$mytag', end : 47 } + { type : 'mytag', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 46 } }, tag : '$mytag', end : 47 } ); expect(parser.parseTag("< $mytag attrib1='something' attrib2=else thing>",0)).toEqual( null ); expect(parser.parseTag("<$mytag attrib3=<>>",0)).toEqual( - { type : 'element', start : 0, attributes : { attrib3 : { type : 'macro', start : 7, name : 'attrib3', value : { type : 'macrocall', start : 16, params : [ { type : 'macro-parameter', start : 25, value : 'two', name : 'one', end : 33 }, { type : 'macro-parameter', start : 33, value : 'four and five', name : 'three', end : 55 } ], name : 'myMacro', end : 57 }, end : 57 } }, tag : '$mytag', end : 58 } + { type : 'mytag', start : 0, attributes : { attrib3 : { type : 'macro', start : 7, name : 'attrib3', value : { type : 'macrocall', start : 16, params : [ { type : 'macro-parameter', start : 25, value : 'two', name : 'one', end : 33 }, { type : 'macro-parameter', start : 33, value : 'four and five', name : 'three', end : 55 } ], name : 'myMacro', end : 57 }, end : 57 } }, tag : '$mytag', end : 58 } ); expect(parser.parseTag("<$mytag attrib1='something' attrib2=else thing attrib3=<>>",0)).toEqual( - { type : 'element', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 47 }, attrib3 : { type : 'macro', start : 47, name : 'attrib3', value : { type : 'macrocall', start : 55, params : [ { type : 'macro-parameter', start : 64, value : 'two', name : 'one', end : 72 }, { type : 'macro-parameter', start : 72, value : 'four and five', name : 'three', end : 94 } ], name : 'myMacro', end : 96 }, end : 96 } }, tag : '$mytag', end : 97 } + { type : 'mytag', start : 0, attributes : { attrib1 : { type : 'string', start : 7, name : 'attrib1', value : 'something', end : 27 }, attrib2 : { type : 'string', start : 27, name : 'attrib2', value : 'else', end : 40 }, thing : { type : 'string', start : 40, name : 'thing', value : 'true', end : 47 }, attrib3 : { type : 'macro', start : 47, name : 'attrib3', value : { type : 'macrocall', start : 55, params : [ { type : 'macro-parameter', start : 64, value : 'two', name : 'one', end : 72 }, { type : 'macro-parameter', start : 72, value : 'four and five', name : 'three', end : 94 } ], name : 'myMacro', end : 96 }, end : 96 } }, tag : '$mytag', end : 97 } ); }); diff --git a/editions/test/tiddlers/tests/test-wikitext-parser.js b/editions/test/tiddlers/tests/test-wikitext-parser.js index 67bbad0ce..d525e277f 100644 --- a/editions/test/tiddlers/tests/test-wikitext-parser.js +++ b/editions/test/tiddlers/tests/test-wikitext-parser.js @@ -70,7 +70,7 @@ describe("WikiText parser tests", function() { ); expect(parse("<$reveal state='$:/temp/search' type='nomatch' text=''>")).toEqual( - [ { type : 'element', tag : 'p', children : [ { type : 'reveal', start : 0, attributes : { state : { start : 8, name : 'state', type : 'string', value : '$:/temp/search', end : 31 }, type : { start : 31, name : 'type', type : 'string', value : 'nomatch', end : 46 }, text : { start : 46, name : 'text', type : 'string', value : '', end : 54 } }, end : 55, isBlock : false, children : [ ] } ] } ] + [ { type : 'element', tag : 'p', children : [ { type : 'reveal', tag: '$reveal', start : 0, attributes : { state : { start : 8, name : 'state', type : 'string', value : '$:/temp/search', end : 31 }, type : { start : 31, name : 'type', type : 'string', value : 'nomatch', end : 46 }, text : { start : 46, name : 'text', type : 'string', value : '', end : 54 } }, end : 55, isBlock : false, children : [ ] } ] } ] ); expect(parse("
some text
")).toEqual(