2013-10-18 14:01:20 +00:00
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const {Cc, Ci, Cu} = require("chrome");
|
|
|
|
|
const {colorUtils} = require("devtools/css-color");
|
|
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
|
|
|
|
|
|
2013-11-11 15:17:41 +00:00
|
|
|
|
const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
|
|
|
|
2013-11-05 17:00:13 +00:00
|
|
|
|
const MAX_ITERATIONS = 100;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
|
2015-05-19 15:56:16 +00:00
|
|
|
|
const BEZIER_KEYWORDS = ["linear", "ease-in-out", "ease-in", "ease-out", "ease"];
|
2014-12-26 11:40:35 +00:00
|
|
|
|
|
2015-05-19 15:56:16 +00:00
|
|
|
|
// Functions that accept a color argument.
|
|
|
|
|
const COLOR_TAKING_FUNCTIONS = ["linear-gradient",
|
|
|
|
|
"-moz-linear-gradient",
|
|
|
|
|
"repeating-linear-gradient",
|
|
|
|
|
"-moz-repeating-linear-gradient",
|
|
|
|
|
"radial-gradient",
|
|
|
|
|
"-moz-radial-gradient",
|
|
|
|
|
"repeating-radial-gradient",
|
|
|
|
|
"-moz-repeating-radial-gradient"];
|
2013-10-18 14:01:20 +00:00
|
|
|
|
|
|
|
|
|
loader.lazyGetter(this, "DOMUtils", function () {
|
|
|
|
|
return Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This module is used to process text for output by developer tools. This means
|
|
|
|
|
* linking JS files with the debugger, CSS files with the style editor, JS
|
|
|
|
|
* functions with the debugger, placing color swatches next to colors and
|
|
|
|
|
* adding doorhanger previews where possible (images, angles, lengths,
|
|
|
|
|
* border radius, cubic-bezier etc.).
|
|
|
|
|
*
|
|
|
|
|
* Usage:
|
|
|
|
|
* const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
|
|
|
|
|
* const {OutputParser} = devtools.require("devtools/output-parser");
|
|
|
|
|
*
|
|
|
|
|
* let parser = new OutputParser();
|
|
|
|
|
*
|
|
|
|
|
* parser.parseCssProperty("color", "red"); // Returns document fragment.
|
|
|
|
|
*/
|
|
|
|
|
function OutputParser() {
|
|
|
|
|
this.parsed = [];
|
2015-04-17 10:53:06 +00:00
|
|
|
|
this.colorSwatches = new WeakMap();
|
|
|
|
|
this._onSwatchMouseDown = this._onSwatchMouseDown.bind(this);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.OutputParser = OutputParser;
|
|
|
|
|
|
|
|
|
|
OutputParser.prototype = {
|
|
|
|
|
/**
|
|
|
|
|
* Parse a CSS property value given a property name.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} name
|
|
|
|
|
* CSS Property Name
|
|
|
|
|
* @param {String} value
|
|
|
|
|
* CSS Property value
|
|
|
|
|
* @param {Object} [options]
|
|
|
|
|
* Options object. For valid options and default values see
|
|
|
|
|
* _mergeOptions().
|
|
|
|
|
* @return {DocumentFragment}
|
|
|
|
|
* A document fragment containing color swatches etc.
|
|
|
|
|
*/
|
|
|
|
|
parseCssProperty: function(name, value, options={}) {
|
|
|
|
|
options = this._mergeOptions(options);
|
|
|
|
|
|
2015-05-11 10:33:00 +00:00
|
|
|
|
options.expectCubicBezier =
|
|
|
|
|
safeCssPropertySupportsType(name, DOMUtils.TYPE_TIMING_FUNCTION);
|
2015-04-09 09:00:42 +00:00
|
|
|
|
options.expectFilter = name === "filter";
|
2015-05-11 10:33:00 +00:00
|
|
|
|
options.supportsColor =
|
|
|
|
|
safeCssPropertySupportsType(name, DOMUtils.TYPE_COLOR) ||
|
|
|
|
|
safeCssPropertySupportsType(name, DOMUtils.TYPE_GRADIENT);
|
2015-04-09 09:00:42 +00:00
|
|
|
|
|
2013-11-11 15:17:41 +00:00
|
|
|
|
if (this._cssPropertySupportsValue(name, value)) {
|
|
|
|
|
return this._parse(value, options);
|
|
|
|
|
}
|
|
|
|
|
this._appendTextNode(value);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
|
2013-11-11 15:17:41 +00:00
|
|
|
|
return this._toDOM();
|
2013-10-18 14:01:20 +00:00
|
|
|
|
},
|
|
|
|
|
|
2014-02-05 10:53:46 +00:00
|
|
|
|
/**
|
2015-05-19 15:56:16 +00:00
|
|
|
|
* Given an initial FUNCTION token, read tokens from |tokenStream|
|
|
|
|
|
* and collect all the (non-comment) text. Return the collected
|
|
|
|
|
* text. The function token and the close paren are included in the
|
|
|
|
|
* result.
|
|
|
|
|
*
|
|
|
|
|
* @param {CSSToken} initialToken
|
|
|
|
|
* The FUNCTION token.
|
|
|
|
|
* @param {String} text
|
|
|
|
|
* The original CSS text.
|
|
|
|
|
* @param {CSSLexer} tokenStream
|
|
|
|
|
* The token stream from which to read.
|
|
|
|
|
* @return {String}
|
|
|
|
|
* The text of body of the function call.
|
2014-02-05 10:53:46 +00:00
|
|
|
|
*/
|
2015-05-19 15:56:16 +00:00
|
|
|
|
_collectFunctionText: function(initialToken, text, tokenStream) {
|
|
|
|
|
let result = text.substring(initialToken.startOffset,
|
|
|
|
|
initialToken.endOffset);
|
|
|
|
|
let depth = 1;
|
|
|
|
|
while (depth > 0) {
|
|
|
|
|
let token = tokenStream.nextToken();
|
|
|
|
|
if (!token) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (token.tokenType === "comment") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
result += text.substring(token.startOffset, token.endOffset);
|
|
|
|
|
if (token.tokenType === "symbol") {
|
|
|
|
|
if (token.text === "(") {
|
|
|
|
|
++depth;
|
|
|
|
|
} else if (token.text === ")") {
|
|
|
|
|
--depth;
|
|
|
|
|
}
|
|
|
|
|
} else if (token.tokenType === "function") {
|
|
|
|
|
++depth;
|
|
|
|
|
}
|
2014-02-05 10:53:46 +00:00
|
|
|
|
}
|
2015-05-19 15:56:16 +00:00
|
|
|
|
return result;
|
2014-02-05 10:53:46 +00:00
|
|
|
|
},
|
|
|
|
|
|
2013-10-18 14:01:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parse a string.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} text
|
|
|
|
|
* Text to parse.
|
|
|
|
|
* @param {Object} [options]
|
|
|
|
|
* Options object. For valid options and default values see
|
|
|
|
|
* _mergeOptions().
|
|
|
|
|
* @return {DocumentFragment}
|
2013-11-11 15:17:41 +00:00
|
|
|
|
* A document fragment.
|
2013-10-18 14:01:20 +00:00
|
|
|
|
*/
|
|
|
|
|
_parse: function(text, options={}) {
|
|
|
|
|
text = text.trim();
|
|
|
|
|
this.parsed.length = 0;
|
2015-05-17 23:54:49 +00:00
|
|
|
|
|
2015-05-19 15:56:16 +00:00
|
|
|
|
if (options.expectFilter) {
|
|
|
|
|
this._appendFilter(text, options);
|
|
|
|
|
} else {
|
|
|
|
|
let tokenStream = DOMUtils.getCSSLexer(text);
|
|
|
|
|
let i = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
let token = tokenStream.nextToken();
|
|
|
|
|
if (!token)
|
|
|
|
|
break;
|
|
|
|
|
if (token.tokenType === "comment") {
|
2014-07-09 21:50:18 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-11-18 16:07:24 +00:00
|
|
|
|
|
2015-05-19 15:56:16 +00:00
|
|
|
|
// Prevent this loop from slowing down the browser with too
|
|
|
|
|
// many nodes being appended into output. In practice it is very unlikely
|
|
|
|
|
// that this will ever happen.
|
|
|
|
|
i++;
|
|
|
|
|
if (i > MAX_ITERATIONS) {
|
|
|
|
|
this._appendTextNode(text.substring(token.startOffset,
|
|
|
|
|
token.endOffset));
|
2013-11-18 16:07:24 +00:00
|
|
|
|
continue;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
2015-05-17 23:54:49 +00:00
|
|
|
|
|
2015-05-19 15:56:16 +00:00
|
|
|
|
switch (token.tokenType) {
|
|
|
|
|
case "function": {
|
|
|
|
|
if (COLOR_TAKING_FUNCTIONS.indexOf(token.text) >= 0) {
|
|
|
|
|
// The function can accept a color argument, and we know
|
|
|
|
|
// it isn't special in some other way. So, we let it
|
|
|
|
|
// through to the ordinary parsing loop so that colors
|
|
|
|
|
// can be handled in a single place.
|
|
|
|
|
this._appendTextNode(text.substring(token.startOffset,
|
|
|
|
|
token.endOffset));
|
|
|
|
|
} else {
|
|
|
|
|
let functionText = this._collectFunctionText(token, text,
|
|
|
|
|
tokenStream);
|
|
|
|
|
|
|
|
|
|
if (options.expectCubicBezier && token.text === "cubic-bezier") {
|
|
|
|
|
this._appendCubicBezier(functionText, options);
|
|
|
|
|
} else if (options.supportsColor &&
|
|
|
|
|
DOMUtils.isValidCSSColor(functionText)) {
|
|
|
|
|
this._appendColor(functionText, options);
|
|
|
|
|
} else {
|
|
|
|
|
this._appendTextNode(functionText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "ident":
|
|
|
|
|
if (options.expectCubicBezier &&
|
|
|
|
|
BEZIER_KEYWORDS.indexOf(token.text) >= 0) {
|
|
|
|
|
this._appendCubicBezier(token.text, options);
|
|
|
|
|
} else if (options.supportsColor &&
|
|
|
|
|
DOMUtils.isValidCSSColor(token.text)) {
|
|
|
|
|
this._appendColor(token.text, options);
|
|
|
|
|
} else {
|
|
|
|
|
this._appendTextNode(text.substring(token.startOffset,
|
|
|
|
|
token.endOffset));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "id":
|
|
|
|
|
case "hash": {
|
|
|
|
|
let original = text.substring(token.startOffset, token.endOffset);
|
|
|
|
|
if (options.supportsColor && DOMUtils.isValidCSSColor(original)) {
|
|
|
|
|
this._appendColor(original, options);
|
|
|
|
|
} else {
|
|
|
|
|
this._appendTextNode(original);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "url":
|
|
|
|
|
case "bad_url":
|
|
|
|
|
this._appendURL(text.substring(token.startOffset, token.endOffset),
|
|
|
|
|
token.text, options);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
this._appendTextNode(text.substring(token.startOffset,
|
|
|
|
|
token.endOffset));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-05 17:00:13 +00:00
|
|
|
|
}
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._toDOM();
|
|
|
|
|
},
|
|
|
|
|
|
2014-07-09 21:50:18 +00:00
|
|
|
|
/**
|
|
|
|
|
* Append a cubic-bezier timing function value to the output
|
|
|
|
|
*
|
|
|
|
|
* @param {String} bezier
|
|
|
|
|
* The cubic-bezier timing function
|
|
|
|
|
* @param {Object} options
|
|
|
|
|
* Options object. For valid options and default values see
|
|
|
|
|
* _mergeOptions()
|
|
|
|
|
*/
|
|
|
|
|
_appendCubicBezier: function(bezier, options) {
|
|
|
|
|
let container = this._createNode("span", {
|
|
|
|
|
"data-bezier": bezier
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (options.bezierSwatchClass) {
|
|
|
|
|
let swatch = this._createNode("span", {
|
|
|
|
|
class: options.bezierSwatchClass
|
|
|
|
|
});
|
|
|
|
|
container.appendChild(swatch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let value = this._createNode("span", {
|
|
|
|
|
class: options.bezierClass
|
|
|
|
|
}, bezier);
|
|
|
|
|
|
|
|
|
|
container.appendChild(value);
|
|
|
|
|
this.parsed.push(container);
|
|
|
|
|
},
|
|
|
|
|
|
2013-10-18 14:01:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Check if a CSS property supports a specific value.
|
|
|
|
|
*
|
2013-11-11 15:17:41 +00:00
|
|
|
|
* @param {String} name
|
2013-10-18 14:01:20 +00:00
|
|
|
|
* CSS Property name to check
|
2013-11-11 15:17:41 +00:00
|
|
|
|
* @param {String} value
|
2013-10-18 14:01:20 +00:00
|
|
|
|
* CSS Property value to check
|
|
|
|
|
*/
|
2013-11-11 15:17:41 +00:00
|
|
|
|
_cssPropertySupportsValue: function(name, value) {
|
2014-12-17 13:43:30 +00:00
|
|
|
|
return DOMUtils.cssPropertyIsValid(name, value);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
},
|
|
|
|
|
|
2014-01-30 15:04:47 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests if a given colorObject output by CssColor is valid for parsing.
|
|
|
|
|
* Valid means it's really a color, not any of the CssColor SPECIAL_VALUES
|
|
|
|
|
* except transparent
|
|
|
|
|
*/
|
|
|
|
|
_isValidColor: function(colorObj) {
|
|
|
|
|
return colorObj.valid &&
|
|
|
|
|
(!colorObj.specialValue || colorObj.specialValue === "transparent");
|
|
|
|
|
},
|
|
|
|
|
|
2013-10-18 14:01:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Append a color to the output.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} color
|
|
|
|
|
* Color to append
|
|
|
|
|
* @param {Object} [options]
|
|
|
|
|
* Options object. For valid options and default values see
|
|
|
|
|
* _mergeOptions().
|
2013-11-11 15:17:41 +00:00
|
|
|
|
* @returns {Boolean}
|
|
|
|
|
* true if the color passed in was valid, false otherwise. Special
|
|
|
|
|
* values such as transparent also return false.
|
2013-10-18 14:01:20 +00:00
|
|
|
|
*/
|
|
|
|
|
_appendColor: function(color, options={}) {
|
2013-11-11 15:17:41 +00:00
|
|
|
|
let colorObj = new colorUtils.CssColor(color);
|
|
|
|
|
|
2014-01-30 15:04:47 +00:00
|
|
|
|
if (this._isValidColor(colorObj)) {
|
2014-06-12 10:02:00 +00:00
|
|
|
|
let container = this._createNode("span", {
|
|
|
|
|
"data-color": color
|
|
|
|
|
});
|
|
|
|
|
|
2013-10-18 14:01:20 +00:00
|
|
|
|
if (options.colorSwatchClass) {
|
2014-06-12 10:02:00 +00:00
|
|
|
|
let swatch = this._createNode("span", {
|
2013-10-18 14:01:20 +00:00
|
|
|
|
class: options.colorSwatchClass,
|
|
|
|
|
style: "background-color:" + color
|
|
|
|
|
});
|
2015-04-17 10:53:06 +00:00
|
|
|
|
this.colorSwatches.set(swatch, colorObj);
|
|
|
|
|
swatch.addEventListener("mousedown", this._onSwatchMouseDown, false);
|
2014-06-12 10:02:00 +00:00
|
|
|
|
container.appendChild(swatch);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
2014-06-12 10:02:00 +00:00
|
|
|
|
|
2013-10-18 14:01:20 +00:00
|
|
|
|
if (options.defaultColorType) {
|
2013-11-11 15:17:41 +00:00
|
|
|
|
color = colorObj.toString();
|
2015-04-17 10:53:06 +00:00
|
|
|
|
container.dataset.color = color;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
2014-06-12 10:02:00 +00:00
|
|
|
|
|
|
|
|
|
let value = this._createNode("span", {
|
2013-12-03 13:45:29 +00:00
|
|
|
|
class: options.colorClass
|
|
|
|
|
}, color);
|
2014-06-12 10:02:00 +00:00
|
|
|
|
|
|
|
|
|
container.appendChild(value);
|
|
|
|
|
this.parsed.push(container);
|
2013-11-11 15:17:41 +00:00
|
|
|
|
return true;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
2013-11-11 15:17:41 +00:00
|
|
|
|
return false;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
},
|
|
|
|
|
|
2015-04-09 09:00:42 +00:00
|
|
|
|
_appendFilter: function(filters, options={}) {
|
|
|
|
|
let container = this._createNode("span", {
|
|
|
|
|
"data-filters": filters
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (options.filterSwatchClass) {
|
|
|
|
|
let swatch = this._createNode("span", {
|
|
|
|
|
class: options.filterSwatchClass
|
|
|
|
|
});
|
|
|
|
|
container.appendChild(swatch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let value = this._createNode("span", {
|
|
|
|
|
class: options.filterClass
|
|
|
|
|
}, filters);
|
|
|
|
|
|
|
|
|
|
container.appendChild(value);
|
|
|
|
|
this.parsed.push(container);
|
|
|
|
|
},
|
|
|
|
|
|
2015-04-17 10:53:06 +00:00
|
|
|
|
_onSwatchMouseDown: function(event) {
|
|
|
|
|
// Prevent text selection in the case of shift-click or double-click.
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
if (!event.shiftKey) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let swatch = event.target;
|
|
|
|
|
let color = this.colorSwatches.get(swatch);
|
|
|
|
|
let val = color.nextColorUnit();
|
|
|
|
|
|
|
|
|
|
swatch.nextElementSibling.textContent = val;
|
|
|
|
|
},
|
|
|
|
|
|
2013-11-11 15:17:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* Append a URL to the output.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} match
|
|
|
|
|
* Complete match that may include "url(xxx)"
|
|
|
|
|
* @param {String} url
|
|
|
|
|
* Actual URL
|
|
|
|
|
* @param {Object} [options]
|
|
|
|
|
* Options object. For valid options and default values see
|
|
|
|
|
* _mergeOptions().
|
|
|
|
|
*/
|
|
|
|
|
_appendURL: function(match, url, options={}) {
|
|
|
|
|
if (options.urlClass) {
|
2014-08-11 13:17:52 +00:00
|
|
|
|
this._appendTextNode("url(\"");
|
2013-11-11 15:17:41 +00:00
|
|
|
|
|
|
|
|
|
let href = url;
|
|
|
|
|
if (options.baseURI) {
|
|
|
|
|
href = options.baseURI.resolve(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._appendNode("a", {
|
|
|
|
|
target: "_blank",
|
|
|
|
|
class: options.urlClass,
|
|
|
|
|
href: href
|
|
|
|
|
}, url);
|
|
|
|
|
|
2014-08-11 13:17:52 +00:00
|
|
|
|
this._appendTextNode("\")");
|
2013-11-11 15:17:41 +00:00
|
|
|
|
} else {
|
2014-08-11 13:17:52 +00:00
|
|
|
|
this._appendTextNode("url(\"" + url + "\")");
|
2013-11-11 15:17:41 +00:00
|
|
|
|
}
|
2013-10-18 14:01:20 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-12 10:02:00 +00:00
|
|
|
|
* Create a node.
|
2013-10-18 14:01:20 +00:00
|
|
|
|
*
|
|
|
|
|
* @param {String} tagName
|
|
|
|
|
* Tag type e.g. "div"
|
|
|
|
|
* @param {Object} attributes
|
|
|
|
|
* e.g. {class: "someClass", style: "cursor:pointer"};
|
|
|
|
|
* @param {String} [value]
|
|
|
|
|
* If a value is included it will be appended as a text node inside
|
|
|
|
|
* the tag. This is useful e.g. for span tags.
|
2014-06-12 10:02:00 +00:00
|
|
|
|
* @return {Node} Newly created Node.
|
2013-10-18 14:01:20 +00:00
|
|
|
|
*/
|
2014-06-12 10:02:00 +00:00
|
|
|
|
_createNode: function(tagName, attributes, value="") {
|
2013-10-18 14:01:20 +00:00
|
|
|
|
let win = Services.appShell.hiddenDOMWindow;
|
|
|
|
|
let doc = win.document;
|
2013-11-11 15:17:41 +00:00
|
|
|
|
let node = doc.createElementNS(HTML_NS, tagName);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
let attrs = Object.getOwnPropertyNames(attributes);
|
|
|
|
|
|
|
|
|
|
for (let attr of attrs) {
|
2013-12-03 13:45:29 +00:00
|
|
|
|
if (attributes[attr]) {
|
|
|
|
|
node.setAttribute(attr, attributes[attr]);
|
|
|
|
|
}
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value) {
|
2013-11-11 15:17:41 +00:00
|
|
|
|
let textNode = doc.createTextNode(value);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
node.appendChild(textNode);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 10:02:00 +00:00
|
|
|
|
return node;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append a node to the output.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} tagName
|
|
|
|
|
* Tag type e.g. "div"
|
|
|
|
|
* @param {Object} attributes
|
|
|
|
|
* e.g. {class: "someClass", style: "cursor:pointer"};
|
|
|
|
|
* @param {String} [value]
|
|
|
|
|
* If a value is included it will be appended as a text node inside
|
|
|
|
|
* the tag. This is useful e.g. for span tags.
|
|
|
|
|
*/
|
|
|
|
|
_appendNode: function(tagName, attributes, value="") {
|
|
|
|
|
let node = this._createNode(tagName, attributes, value);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
this.parsed.push(node);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append a text node to the output. If the previously output item was a text
|
|
|
|
|
* node then we append the text to that node.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} text
|
|
|
|
|
* Text to append
|
|
|
|
|
*/
|
|
|
|
|
_appendTextNode: function(text) {
|
|
|
|
|
let lastItem = this.parsed[this.parsed.length - 1];
|
2013-11-05 17:00:13 +00:00
|
|
|
|
if (typeof lastItem === "string") {
|
2013-11-18 16:07:24 +00:00
|
|
|
|
this.parsed[this.parsed.length - 1] = lastItem + text;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
} else {
|
2013-11-05 17:00:13 +00:00
|
|
|
|
this.parsed.push(text);
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Take all output and append it into a single DocumentFragment.
|
|
|
|
|
*
|
|
|
|
|
* @return {DocumentFragment}
|
|
|
|
|
* Document Fragment
|
|
|
|
|
*/
|
|
|
|
|
_toDOM: function() {
|
|
|
|
|
let win = Services.appShell.hiddenDOMWindow;
|
|
|
|
|
let doc = win.document;
|
|
|
|
|
let frag = doc.createDocumentFragment();
|
|
|
|
|
|
|
|
|
|
for (let item of this.parsed) {
|
2013-11-05 17:00:13 +00:00
|
|
|
|
if (typeof item === "string") {
|
|
|
|
|
frag.appendChild(doc.createTextNode(item));
|
|
|
|
|
} else {
|
|
|
|
|
frag.appendChild(item);
|
|
|
|
|
}
|
2013-10-18 14:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.parsed.length = 0;
|
|
|
|
|
return frag;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merges options objects. Default values are set here.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} overrides
|
|
|
|
|
* The option values to override e.g. _mergeOptions({colors: false})
|
|
|
|
|
*
|
|
|
|
|
* Valid options are:
|
|
|
|
|
* - defaultColorType: true // Convert colors to the default type
|
|
|
|
|
* // selected in the options panel.
|
|
|
|
|
* - colorSwatchClass: "" // The class to use for color swatches.
|
2013-12-03 13:45:29 +00:00
|
|
|
|
* - colorClass: "" // The class to use for the color value
|
|
|
|
|
* // that follows the swatch.
|
2014-07-09 21:50:18 +00:00
|
|
|
|
* - bezierSwatchClass: "" // The class to use for bezier swatches.
|
|
|
|
|
* - bezierClass: "" // The class to use for the bezier value
|
|
|
|
|
* // that follows the swatch.
|
2015-05-11 10:33:00 +00:00
|
|
|
|
* - supportsColor: false // Does the CSS property support colors?
|
2013-11-11 15:17:41 +00:00
|
|
|
|
* - urlClass: "" // The class to be used for url() links.
|
|
|
|
|
* - baseURI: "" // A string or nsIURI used to resolve
|
|
|
|
|
* // relative links.
|
2013-10-18 14:01:20 +00:00
|
|
|
|
* @return {Object}
|
|
|
|
|
* Overridden options object
|
|
|
|
|
*/
|
|
|
|
|
_mergeOptions: function(overrides) {
|
|
|
|
|
let defaults = {
|
|
|
|
|
defaultColorType: true,
|
|
|
|
|
colorSwatchClass: "",
|
2013-12-03 13:45:29 +00:00
|
|
|
|
colorClass: "",
|
2014-07-09 21:50:18 +00:00
|
|
|
|
bezierSwatchClass: "",
|
|
|
|
|
bezierClass: "",
|
2015-05-11 10:33:00 +00:00
|
|
|
|
supportsColor: false,
|
2013-11-11 15:17:41 +00:00
|
|
|
|
urlClass: "",
|
|
|
|
|
baseURI: ""
|
2013-10-18 14:01:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-11-11 15:17:41 +00:00
|
|
|
|
if (typeof overrides.baseURI === "string") {
|
|
|
|
|
overrides.baseURI = Services.io.newURI(overrides.baseURI, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 14:01:20 +00:00
|
|
|
|
for (let item in overrides) {
|
|
|
|
|
defaults[item] = overrides[item];
|
|
|
|
|
}
|
|
|
|
|
return defaults;
|
2014-01-30 15:04:47 +00:00
|
|
|
|
}
|
2013-10-18 14:01:20 +00:00
|
|
|
|
};
|
2015-05-11 10:33:00 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A wrapper for DOMUtils.cssPropertySupportsType that ignores invalid
|
|
|
|
|
* properties.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} name The property name.
|
|
|
|
|
* @param {number} type The type tested for support.
|
|
|
|
|
* @return {Boolean} Whether the property supports the type.
|
|
|
|
|
* If the property is unknown, false is returned.
|
|
|
|
|
*/
|
|
|
|
|
function safeCssPropertySupportsType(name, type) {
|
|
|
|
|
try {
|
|
|
|
|
return DOMUtils.cssPropertySupportsType(name, type);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|