2012-12-15 11:38:59 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/parsetree.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Parse tree utility functions.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.addAttributeToParseTreeNode = function(node,name,value) {
|
|
|
|
if(node.type === "element") {
|
|
|
|
node.attributes = node.attributes || {};
|
|
|
|
node.attributes[name] = {type: "string", value: value};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-03 10:39:55 +00:00
|
|
|
exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) {
|
2014-01-03 10:48:00 +00:00
|
|
|
if(node.type === "element" && node.attributes && node.attributes[name] && node.attributes[name].value !== undefined) {
|
2014-01-03 10:39:55 +00:00
|
|
|
return node.attributes[name].value;
|
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
};
|
|
|
|
|
2012-12-15 11:38:59 +00:00
|
|
|
exports.addClassToParseTreeNode = function(node,classString) {
|
2012-12-23 10:36:37 +00:00
|
|
|
var classes = [];
|
2012-12-15 11:38:59 +00:00
|
|
|
if(node.type === "element") {
|
|
|
|
node.attributes = node.attributes || {};
|
|
|
|
node.attributes["class"] = node.attributes["class"] || {type: "string", value: ""};
|
|
|
|
if(node.attributes["class"].type === "string") {
|
2012-12-23 10:36:37 +00:00
|
|
|
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(" ");
|
2012-12-15 11:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-20 09:21:58 +00:00
|
|
|
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 + ";";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-30 17:23:44 +00:00
|
|
|
exports.findParseTreeNode = function(nodeArray,search) {
|
|
|
|
for(var t=0; t<nodeArray.length; t++) {
|
|
|
|
if(nodeArray[t].type === search.type && nodeArray[t].tag === search.tag) {
|
|
|
|
return nodeArray[t];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2012-12-15 11:38:59 +00:00
|
|
|
})();
|