From 43f3f8b5ed5e39d116df225116e38da64a030384 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Fri, 9 Aug 2024 19:50:07 +0800 Subject: [PATCH] feat: styleinline --- .../parsers/wikiparser/rules/styleinline.js | 31 ++++++++----------- .../tiddlers/tests/test-wikitext-serialize.js | 4 +-- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/styleinline.js b/core/modules/parsers/wikiparser/rules/styleinline.js index 281c2bc8d..873226f03 100644 --- a/core/modules/parsers/wikiparser/rules/styleinline.js +++ b/core/modules/parsers/wikiparser/rules/styleinline.js @@ -25,7 +25,7 @@ exports.types = {inline: true}; exports.init = function(parser) { this.parser = parser; // Regexp to match - this.matchRegExp = /@@((?:[^\.\r\n\s:]+:[^\r\n;]+;)+)?(\.(?:[^\r\n\s]+)\s+)?/mg; + this.matchRegExp = /@@((?:[^\.\r\n\s:]+:[^\r\n;]+;)+)?\s*(\.(?:[^\r\n\s]+)\s+)?/mg; }; exports.parse = function() { @@ -56,25 +56,20 @@ exports.parse = function() { }; exports.serialize = function(tree, serialize) { - // tree: { type: 'element', tag: 'span', attributes: { class: { type: 'string', value: 'myClass' }, style: { type: 'string', value: 'background-color:red;' } }, children: [{ type: 'text', text: 'This is some text with a class and a background colour' }] } - // serialize: function that accepts array of nodes or a node and returns a string - // Initialize the serialized string with the opening delimiter - var serialized = "@@"; - // Check for styles and append them to the serialized string - if(tree.attributes.style) { - serialized += tree.attributes.style.value; + // tree: {type: "element", tag: "span", children: [...], attributes: {class: {name: "class", type: "string", value: " myClass "}, style: {name: "style", type: "string", value: "background-color:red;"}}, orderedAttributes: [...], start: 0, end: 43, rule: "styleinline"} + // serialize: function that accepts an array of nodes or a single node and returns a string + var result = "@@"; + // Add styles if present + if(tree.attributes && tree.attributes.style) { + result += tree.attributes.style.value.trim(); } - // Check for classes and append them to the serialized string - if(tree.attributes.class) { - var classes = tree.attributes.class.value.split(" "); - for(var i = 0; i < classes.length; i++) { - serialized += "." + classes[i] + " "; - } + // Add classes if present + if(tree.attributes && tree.attributes.class) { + result += "." + tree.attributes.class.value.trim().split(" ").join("."); } - // Append the serialized children and the closing delimiter - serialized += serialize(tree.children) + "@@"; - // Return the complete serialized string - return serialized; + // Serialize children and append to result + result += " " + serialize(tree.children) + "@@"; + return result; }; })(); diff --git a/editions/test/tiddlers/tests/test-wikitext-serialize.js b/editions/test/tiddlers/tests/test-wikitext-serialize.js index 7ee728cf6..bfc4523ad 100644 --- a/editions/test/tiddlers/tests/test-wikitext-serialize.js +++ b/editions/test/tiddlers/tests/test-wikitext-serialize.js @@ -327,11 +327,10 @@ describe("WikiAST serialization unit tests", function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("StyleBlockTest2").tree).trimEnd(); expect(serialized).toBe(wiki.getTiddlerText("StyleBlockTest2").trimEnd()); }); - return; wiki.addTiddler({ title: "StyleInlineTest", - text: "@@.myClass This is some text with a class@@\n@@background-color:red;This is some text with a background colour@@\n@@width:100px;.myClass This is some text with a class and a width@@", + text: "@@.myClass This is some text with a class@@\n@@background-color:red; This is some text with a background colour@@\n@@width:100px;.myClass This is some text with a class and a width@@", }); it("should serialize style inlines correctly", function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("StyleInlineTest").tree).trimEnd(); @@ -355,6 +354,7 @@ describe("WikiAST serialization unit tests", function () { var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("TableTest").tree).trimEnd(); expect(serialized).toBe(wiki.getTiddlerText("TableTest").trimEnd()); }); + return; wiki.addTiddler({ title: "TranscludeBlockTest",