Fix for broken style block wikitext (#6599)

* Fix for broken style block wikitext

* Fixed it so styles are correct too
This commit is contained in:
Cameron Fischer 2022-04-06 03:33:38 -04:00 committed by GitHub
parent c5ea6628f5
commit 6624ce3716
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 12 deletions

View File

@ -13,8 +13,12 @@ Parse tree utility functions.
"use strict";
exports.addAttributeToParseTreeNode = function(node,name,value) {
var attribute = {name: name, type: "string", value: value};
node.attributes = node.attributes || {};
node.attributes[name] = {type: "string", value: value};
node.attributes[name] = attribute;
if(node.orderedAttributes) {
node.orderedAttributes.push(attribute);
}
};
exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
@ -25,26 +29,45 @@ exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
};
exports.addClassToParseTreeNode = function(node,classString) {
var classes = [];
var classes = [],
attribute;
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(" ");
attribute = node.attributes["class"];
if(!attribute) {
// If the class attribute does not exist, we must create it first.
attribute = {name: "class", type: "string", value: ""};
node.attributes["class"] = attribute;
if(node.orderedAttributes) {
// If there are orderedAttributes, we've got to add them there too.
node.orderedAttributes.push(attribute);
}
}
if(attribute.type === "string") {
if(attribute.value !== "") {
classes = attribute.value.split(" ");
}
if(classString !== "") {
$tw.utils.pushTop(classes,classString.split(" "));
}
node.attributes["class"].value = classes.join(" ");
attribute.value = classes.join(" ");
}
};
exports.addStyleToParseTreeNode = function(node,name,value) {
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 + ";";
var attribute;
node.attributes = node.attributes || {};
attribute = node.attributes.style;
if(!attribute) {
attribute = {name: "style", type: "string", value: ""};
node.attributes.style = attribute;
if(node.orderedAttributes) {
// If there are orderedAttributes, we've got to add them there too.
node.orderedAttributes.push(attribute);
}
}
if(attribute.type === "string") {
attribute.value += name + ":" + value + ";";
}
};
exports.findParseTreeNode = function(nodeArray,search) {

View File

@ -55,7 +55,14 @@ describe("WikiText tests", function() {
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","No -WikiLink here").indexOf("<a") !== -1).toBe(false);
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","No _WikiLink here").indexOf("<a") !== -1).toBe(false);
});
it("handles style wikitext notation", function() {
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","@@.myclass\n!header\n@@")).toBe("<h1 class=\"myclass\">header</h1>");
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","@@.myclass\n<div>\n\nContent</div>\n@@")).toBe("<div class=\"myclass\"><p>Content</p></div>");
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","@@.myclass\n---\n@@")).toBe("<hr class=\"myclass\">");
// Test styles can be added too
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","@@color:red;\n<div>\n\nContent</div>\n@@")).toBe("<div style=\"color:red;\"><p>Content</p></div>");
expect(wiki.renderText("text/html","text/vnd-tiddlywiki","@@color:red;\n---\n@@")).toBe("<hr style=\"color:red;\">");
});
});
})();