/* 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";
/** * This module contains a small element attribute value parser. It's primary * goal is to extract link information from attribute values (like the href in * <a href="/some/link.html"> for example). * * There are several types of linkable attribute values: * - TYPE_URI: a URI (e.g. <a href="uri">). * - TYPE_URI_LIST: a space separated list of URIs (e.g. <a ping="uri1 uri2">). * - TYPE_IDREF: a reference to an other element in the same document via its id * (e.g. <label for="input-id"> or <key command="command-id">). * - TYPE_IDREF_LIST: a space separated list of IDREFs (e.g. * <output for="id1 id2">). * - TYPE_JS_RESOURCE_URI: a URI to a javascript resource that can be opened in * the devtools (e.g. <script src="uri">). * - TYPE_CSS_RESOURCE_URI: a URI to a css resource that can be opened in the * devtools (e.g. <link href="uri">). * * parseAttribute is the parser entry function, exported on this module.
*/
/** * Parse an attribute value. * @param {String} namespaceURI The namespaceURI of the node that has the * attribute. * @param {String} tagName The tagName of the node that has the attribute. * @param {Array} attributes The list of all attributes of the node. This should * be an array of {name, value} objects. * @param {String} attributeName The name of the attribute to parse. * @param {String} attributeValue The value of the attribute to parse. * @return {Array} An array of tokens that represents the value. Each token is * an object {type: [string|uri|jsresource|cssresource|idref], value}. * For instance parsing the ping attribute in <a ping="uri1 uri2"> returns: * [ * {type: "uri", value: "uri2"}, * {type: "string", value: " "}, * {type: "uri", value: "uri1"} * ]
*/ function parseAttribute(
namespaceURI,
tagName,
attributes,
attributeName,
attributeValue
) { const type = getType(namespaceURI, tagName, attributes, attributeName); if (!type) { return [
{
type: TYPE_STRING,
value: attributeValue,
},
];
}
return parsers[type](attributeValue);
}
/** * Get the type for links in this attribute if any. * @param {String} namespaceURI The node's namespaceURI. * @param {String} tagName The node's tagName. * @param {Array} attributes The node's attributes, as a list of {name, value} * objects. * @param {String} attributeName The name of the attribute to get the type for. * @return {Object} null if no type exist for this attribute on this node, the * type object otherwise.
*/ function getType(namespaceURI, tagName, attributes, attributeName) { const attributeType = ATTRIBUTE_TYPES.get(attributeName); if (!attributeType) { returnnull;
}
/** * Split a string by a given character and return an array of objects parts. * The array will contain objects for the split character too, marked with * TYPE_STRING type. * @param {String} value The string to parse. * @param {String} splitChar A 1 length split character. * @return {Array}
*/ function splitBy(value, splitChar) { const data = [];
let i = 0,
buffer = ""; while (i <= value.length) { if (i === value.length && buffer) {
data.push({ value: buffer });
} if (value[i] === splitChar) { if (buffer) {
data.push({ value: buffer });
}
data.push({
type: TYPE_STRING,
value: splitChar,
});
buffer = "";
} else {
buffer += value[i];
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.