mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-20 22:16:52 +00:00
feat: styleblock
This commit is contained in:
parent
078cd5fba2
commit
7c73f1f922
@ -75,54 +75,52 @@ exports.parse = function() {
|
||||
|
||||
exports.serialize = function(tree, serialize) {
|
||||
// serialize: function that serializes an array of nodes or a single node to a string
|
||||
var result = [];
|
||||
var lines = [];
|
||||
var classes = [];
|
||||
var styles = [];
|
||||
|
||||
// Collect all unique classes and styles from child nodes
|
||||
for(var i = 0; i < tree.children.length; i++) {
|
||||
var node = tree.children[i];
|
||||
if(node.attributes && node.attributes.class) {
|
||||
var nodeClasses = node.attributes.class.value.split(" ");
|
||||
for(var j = 0; j < nodeClasses.length; j++) {
|
||||
if(classes.indexOf(nodeClasses[j]) === -1) {
|
||||
classes.push(nodeClasses[j]);
|
||||
}
|
||||
// Same classes are set to each children. So only collect from first child.
|
||||
var node = tree.children[0];
|
||||
if(node && node.attributes && node.attributes.class) {
|
||||
var nodeClasses = node.attributes.class.value.split(" ");
|
||||
for(var j = 0; j < nodeClasses.length; j++) {
|
||||
if(classes.indexOf(nodeClasses[j]) === -1) {
|
||||
classes.push(nodeClasses[j]);
|
||||
}
|
||||
}
|
||||
if(node.attributes && node.attributes.style) {
|
||||
var nodeStyles = node.attributes.style.value.split(";");
|
||||
for(var k = 0; k < nodeStyles.length; k++) {
|
||||
var style = nodeStyles[k].trim();
|
||||
if(style && styles.indexOf(style) === -1) {
|
||||
styles.push(style);
|
||||
}
|
||||
}
|
||||
if(node && node.attributes && node.attributes.style) {
|
||||
var nodeStyles = node.attributes.style.value.split(";");
|
||||
for(var k = 0; k < nodeStyles.length; k++) {
|
||||
var style = nodeStyles[k].trim();
|
||||
if(style && styles.indexOf(style) === -1) {
|
||||
styles.push(style);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the style block header if there are any classes or styles
|
||||
// Add the style block header, sort styles first, and classes later. Original order is not preserved intentionally for simplicity.
|
||||
if(classes.length > 0 || styles.length > 0) {
|
||||
if(styles.length > 0) {
|
||||
result.push("@@");
|
||||
result.push(styles.join(";"));
|
||||
result.push(";\n");
|
||||
lines.push("@@");
|
||||
lines.push(styles.join(";"));
|
||||
lines.push(";\n");
|
||||
}
|
||||
if(classes.length > 0) {
|
||||
result.push("@@.");
|
||||
result.push(classes.join("."));
|
||||
result.push("\n");
|
||||
lines.push("@@.");
|
||||
lines.push(classes.join("."));
|
||||
lines.push("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize each child node and add to result
|
||||
for(var i = 0; i < tree.children.length; i++) {
|
||||
result.push(serialize(tree.children[i]));
|
||||
lines.push(serialize(tree.children[i]));
|
||||
}
|
||||
|
||||
var result = lines.join("").trimEnd();
|
||||
// Add the closing @@ for the style block
|
||||
result.push("@@");
|
||||
return result.join("");
|
||||
result += "\n@@\n\n"
|
||||
return result;
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -315,11 +315,11 @@ describe("WikiAST serialization unit tests", function () {
|
||||
|
||||
wiki.addTiddler({
|
||||
title: "StyleBlockTest",
|
||||
text: "@@.myClass\n@@background-color:red;\nThis paragraph will have the CSS class `myClass`.\n\n* The `<ul>` around this list will also have the class `myClass`\n* List item 2\n@@\n\n@@font-size:1.5em;\n@@.coloured-text\n@@.coloured-bg\n* Block content\n* With custom style and classes\n@@",
|
||||
text: "@@background-color:red;\n@@.myClass\nThis paragraph will have the CSS class `myClass`.\n\n* The `<ul>` around this list will also have the class `myClass`\n* List item 2\n@@\n\n@@font-size:1.5em;\n@@.coloured-text.coloured-bg\n* Block content\n* With custom style and classes\n@@",
|
||||
});
|
||||
wiki.addTiddler({
|
||||
title: "StyleBlockTest2",
|
||||
text: "@@.myFirstClass.mySecondClass\n@@width:100px;.myThirdClass\nThis is a paragraph\n@@\n\n@@background-color:lightcyan;\n* Item one\n* Item two\n@@",
|
||||
text: "@@width:100px;\n@@.myFirstClass.mySecondClass.myThirdClass\nThis is a paragraph\n@@\n\n@@background-color:lightcyan;\n* Item one\n* Item two\n@@",
|
||||
});
|
||||
it("should serialize style blocks correctly", function () {
|
||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("StyleBlockTest").tree).trimEnd();
|
||||
|
Loading…
Reference in New Issue
Block a user