Files
meteor/packages/htmljs/utils.js
2014-03-03 15:51:35 -08:00

43 lines
1.4 KiB
JavaScript

HTML = {};
HTML.isNully = function (node) {
if (node == null)
// null or undefined
return true;
if (node instanceof Array) {
// is it an empty array or an array of all nully items?
for (var i = 0; i < node.length; i++)
if (! HTML.isNully(node[i]))
return false;
return true;
}
return false;
};
HTML.escapeData = function (str) {
// string; escape the two special chars in HTML data and RCDATA
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;');
};
// The HTML spec and the DOM API (in particular `setAttribute`) have different
// definitions of what characters are legal in an attribute. The HTML
// parser is extremely permissive (allowing, for example, `<a %=%>`), while
// `setAttribute` seems to use something like the XML grammar for names (and
// throws an error if a name is invalid, making that attribute unsettable).
// If we knew exactly what grammar browsers used for `setAttribute`, we could
// include various Unicode ranges in what's legal. For now, allow ASCII chars
// that are known to be valid XML, valid HTML, and settable via `setAttribute`:
//
// * Starts with `:`, `_`, `A-Z` or `a-z`
// * Consists of any of those plus `-`, `.`, and `0-9`.
//
// See <http://www.w3.org/TR/REC-xml/#NT-Name> and
// <http://dev.w3.org/html5/markup/syntax.html#syntax-attributes>.
HTML.isValidAttributeName = function (name) {
return /^[:_A-Za-z][:_A-Za-z0-9.\-]*/.test(name);
};