|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var utils = require("./utils"); |
|
|
|
|
|
|
|
|
|
|
|
var createClass = function(classes) { |
|
classes = classes.slice(); |
|
for (var i = classes.length - 1; i >= 0; i--) { |
|
if (!classes[i]) { |
|
classes.splice(i, 1); |
|
} |
|
} |
|
|
|
return classes.join(" "); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
function span(classes, children, height, depth, maxFontSize, style) { |
|
this.classes = classes || []; |
|
this.children = children || []; |
|
this.height = height || 0; |
|
this.depth = depth || 0; |
|
this.maxFontSize = maxFontSize || 0; |
|
this.style = style || {}; |
|
this.attributes = {}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
span.prototype.setAttribute = function(attribute, value) { |
|
this.attributes[attribute] = value; |
|
}; |
|
|
|
|
|
|
|
|
|
span.prototype.toNode = function() { |
|
var span = document.createElement("span"); |
|
|
|
|
|
span.className = createClass(this.classes); |
|
|
|
|
|
for (var style in this.style) { |
|
if (Object.prototype.hasOwnProperty.call(this.style, style)) { |
|
span.style[style] = this.style[style]; |
|
} |
|
} |
|
|
|
|
|
for (var attr in this.attributes) { |
|
if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) { |
|
span.setAttribute(attr, this.attributes[attr]); |
|
} |
|
} |
|
|
|
|
|
for (var i = 0; i < this.children.length; i++) { |
|
span.appendChild(this.children[i].toNode()); |
|
} |
|
|
|
return span; |
|
}; |
|
|
|
|
|
|
|
|
|
span.prototype.toMarkup = function() { |
|
var markup = "<span"; |
|
|
|
|
|
if (this.classes.length) { |
|
markup += " class=\""; |
|
markup += utils.escape(createClass(this.classes)); |
|
markup += "\""; |
|
} |
|
|
|
var styles = ""; |
|
|
|
|
|
for (var style in this.style) { |
|
if (this.style.hasOwnProperty(style)) { |
|
styles += utils.hyphenate(style) + ":" + this.style[style] + ";"; |
|
} |
|
} |
|
|
|
if (styles) { |
|
markup += " style=\"" + utils.escape(styles) + "\""; |
|
} |
|
|
|
|
|
for (var attr in this.attributes) { |
|
if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) { |
|
markup += " " + attr + "=\""; |
|
markup += utils.escape(this.attributes[attr]); |
|
markup += "\""; |
|
} |
|
} |
|
|
|
markup += ">"; |
|
|
|
|
|
for (var i = 0; i < this.children.length; i++) { |
|
markup += this.children[i].toMarkup(); |
|
} |
|
|
|
markup += "</span>"; |
|
|
|
return markup; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function documentFragment(children, height, depth, maxFontSize) { |
|
this.children = children || []; |
|
this.height = height || 0; |
|
this.depth = depth || 0; |
|
this.maxFontSize = maxFontSize || 0; |
|
} |
|
|
|
|
|
|
|
|
|
documentFragment.prototype.toNode = function() { |
|
|
|
var frag = document.createDocumentFragment(); |
|
|
|
|
|
for (var i = 0; i < this.children.length; i++) { |
|
frag.appendChild(this.children[i].toNode()); |
|
} |
|
|
|
return frag; |
|
}; |
|
|
|
|
|
|
|
|
|
documentFragment.prototype.toMarkup = function() { |
|
var markup = ""; |
|
|
|
|
|
for (var i = 0; i < this.children.length; i++) { |
|
markup += this.children[i].toMarkup(); |
|
} |
|
|
|
return markup; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
function symbolNode(value, height, depth, italic, skew, classes, style) { |
|
this.value = value || ""; |
|
this.height = height || 0; |
|
this.depth = depth || 0; |
|
this.italic = italic || 0; |
|
this.skew = skew || 0; |
|
this.classes = classes || []; |
|
this.style = style || {}; |
|
this.maxFontSize = 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
symbolNode.prototype.toNode = function() { |
|
var node = document.createTextNode(this.value); |
|
var span = null; |
|
|
|
if (this.italic > 0) { |
|
span = document.createElement("span"); |
|
span.style.marginRight = this.italic + "em"; |
|
} |
|
|
|
if (this.classes.length > 0) { |
|
span = span || document.createElement("span"); |
|
span.className = createClass(this.classes); |
|
} |
|
|
|
for (var style in this.style) { |
|
if (this.style.hasOwnProperty(style)) { |
|
span = span || document.createElement("span"); |
|
span.style[style] = this.style[style]; |
|
} |
|
} |
|
|
|
if (span) { |
|
span.appendChild(node); |
|
return span; |
|
} else { |
|
return node; |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
symbolNode.prototype.toMarkup = function() { |
|
|
|
|
|
var needsSpan = false; |
|
|
|
var markup = "<span"; |
|
|
|
if (this.classes.length) { |
|
needsSpan = true; |
|
markup += " class=\""; |
|
markup += utils.escape(createClass(this.classes)); |
|
markup += "\""; |
|
} |
|
|
|
var styles = ""; |
|
|
|
if (this.italic > 0) { |
|
styles += "margin-right:" + this.italic + "em;"; |
|
} |
|
for (var style in this.style) { |
|
if (this.style.hasOwnProperty(style)) { |
|
styles += utils.hyphenate(style) + ":" + this.style[style] + ";"; |
|
} |
|
} |
|
|
|
if (styles) { |
|
needsSpan = true; |
|
markup += " style=\"" + utils.escape(styles) + "\""; |
|
} |
|
|
|
var escaped = utils.escape(this.value); |
|
if (needsSpan) { |
|
markup += ">"; |
|
markup += escaped; |
|
markup += "</span>"; |
|
return markup; |
|
} else { |
|
return escaped; |
|
} |
|
}; |
|
|
|
module.exports = { |
|
span: span, |
|
documentFragment: documentFragment, |
|
symbolNode: symbolNode, |
|
}; |
|
|