You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
24 KiB
1 lines
24 KiB
{"version":3,"sources":["htmljs/preamble.js","htmljs/visitors.js","htmljs/html.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,U;;AAEA,sC;AACA,8B;;;;;;;;;;;;;;;;;;;ACHA,uC;;AAEA,0D;AACA,4D;AACA,O;AACA,sD;AACA,mC;AACA,sB;AACA,qC;AACA,sB;AACA,G;AACA,a;AACA,E;;AAEA,iC;AACA,uB;AACA,E;;AAEA,uC;AACA,mC;AACA,E;;AAEA,0C;AACA,qB;AACA,4D;AACA,wC;AACA,I;AACA,kC;AACA,kC;AACA,4B;AACA,c;AACA,wC;AACA,iB;AACA,E;;AAEA,kB;AACA,sC;AACA,wB;AACA,2B;AACA,mD;;AAEA,sC;AACA,+B;AACA,qC;AACA,iC;AACA,sD;AACA,qC;AACA,0D;AACA,qC;AACA,0D;AACA,iC;AACA,sD;AACA,gB;AACA,wE;AACA,S;AACA,O;;AAEA,gC;AACA,sD;;AAEA,qD;;AAEA,+C;AACA,gD;AACA,+C;AACA,wD;;AAEA,+C;AACA,uD;AACA,K;;AAEA,+D;;AAEA,I;AACA,oD;AACA,+D;AACA,2C;AACA,+C;AACA,+C;AACA,uC;AACA,uC;AACA,wC;AACA,2D;AACA,I;AACA,0C;AACA,6D;AACA,G;AACA,G;;AAEA,iD;AACA,8B;AACA,sB;AACA,2B;AACA,yC;AACA,yC;AACA,uB;AACA,4C;AACA,6B;AACA,4B;AACA,qD;AACA,gC;AACA,gC;AACA,6B;AACA,iC;AACA,4B;AACA,O;AACA,K;AACA,kB;AACA,I;AACA,yB;AACA,yB;AACA,qB;AACA,wB;AACA,0B;AACA,qC;AACA,mC;AACA,yC;AACA,8B;AACA,+D;;AAEA,6B;AACA,2B;AACA,8D;;AAEA,6D;AACA,iB;;AAEA,mE;AACA,4B;AACA,kB;AACA,I;AACA,+C;AACA,kD;AACA,I;AACA,yE;AACA,yD;AACA,qB;AACA,8C;AACA,8B;AACA,2C;AACA,yB;AACA,8C;AACA,+B;AACA,8B;AACA,iE;AACA,kC;AACA,0B;AACA,+B;AACA,mC;AACA,8B;AACA,S;AACA,O;AACA,oB;AACA,K;;AAEA,mD;AACA,8E;AACA,0E;AACA,wD;AACA,K;;AAEA,yB;AACA,4B;AACA,mB;AACA,kC;AACA,+C;AACA,+B;AACA,mC;AACA,wB;AACA,+B;AACA,iE;AACA,oC;AACA,0B;AACA,oC;AACA,6C;AACA,iC;AACA,S;AACA,O;AACA,K;;AAEA,oB;AACA,I;AACA,0D;AACA,2B;AACA,wD;AACA,wC;AACA,oB;AACA,wC;AACA,G;AACA,G;;;AAGA,2C;AACA,wB;AACA,yC;AACA,c;AACA,I;AACA,oD;AACA,4C;AACA,iD;AACA,8D;AACA,2D;AACA,sD;AACA,gE;AACA,Y;AACA,iB;AACA,K;AACA,I;AACA,gC;AACA,mB;AACA,0C;AACA,uC;AACA,0B;AACA,I;AACA,oC;AACA,iD;AACA,I;AACA,oC;AACA,iD;AACA,oD;AACA,0B;AACA,Y;AACA,yB;AACA,K;AACA,I;AACA,4B;AACA,qB;AACA,I;AACA,4B;AACA,iE;AACA,4D;AACA,sE;AACA,kE;AACA,uE;AACA,iE;AACA,0D;AACA,wC;AACA,I;AACA,6B;AACA,mE;AACA,I;AACA,2B;AACA,6B;AACA,G;AACA,G;;;;AAIA,2C;AACA,wB;AACA,yC;AACA,c;AACA,I;AACA,oD;AACA,4C;AACA,4D;AACA,I;AACA,gC;AACA,mB;AACA,0C;AACA,uC;AACA,0B;AACA,I;AACA,oC;AACA,mD;AACA,I;AACA,oC;AACA,wB;AACA,I;AACA,4B;AACA,qB;AACA,I;AACA,4B;AACA,sB;;AAEA,8B;AACA,gC;;AAEA,0B;AACA,gB;AACA,4C;AACA,4B;AACA,sD;AACA,0C;AACA,gB;AACA,iE;AACA,kD;AACA,S;AACA,O;AACA,K;;AAEA,2D;;AAEA,uB;AACA,gB;AACA,iC;;AAEA,+C;AACA,uE;;AAEA,mC;AACA,uC;AACA,gE;AACA,uB;AACA,iC;;AAEA,Y;AACA,+C;AACA,gD;;AAEA,mC;AACA,K;;AAEA,oC;;AAEA,2D;AACA,yE;AACA,yE;AACA,kD;AACA,qC;AACA,K;;AAEA,kB;AACA,I;AACA,6B;AACA,mE;AACA,I;AACA,qC;AACA,uC;AACA,G;AACA,G;;;;;;;;;;;;;;;;;;;;;ACxUA,0B;AACA,qE;AACA,gC;AACA,qE;AACA,8D;;AAEA,0C;AACA,6C;AACA,kE;AACA,0C;AACA,6D;AACA,qE;AACA,8C;AACA,mE;;AAEA,c;AACA,iD;AACA,+C;AACA,6D;AACA,8C;AACA,+B;AACA,Y;AACA,+C;AACA,gC;AACA,iC;AACA,oC;AACA,sC;AACA,iC;AACA,S;AACA,Y;AACA,O;AACA,K;;;AAGA,wE;AACA,sE;AACA,0E;AACA,8C;AACA,6B;AACA,mD;;AAEA,oB;AACA,I;AACA,mC;AACA,0C;AACA,sC;;AAEA,iB;AACA,E;;AAEA,wE;AACA,iE;AACA,4D;AACA,2D;AACA,mE;AACA,4C;AACA,4D;;AAEA,yC;;AAEA,kB;AACA,E;;AAEA,6C;;AAEA,kC;AACA,+C;AACA,iD;AACA,qF;;AAEA,yB;AACA,mD;;AAEA,0B;AACA,E;;AAEA,qC;AACA,0C;AACA,E;;AAEA,wC;AACA,sC;AACA,E;;AAEA,yC;AACA,2B;AACA,kD;AACA,E;;AAEA,utB;AACA,8B;;AAEA,qxB;AACA,0D;AACA,kF;;AAEA,yH;;AAEA,8E;AACA,c;AACA,qB;AACA,gC;AACA,e;AACA,wC;AACA,wB;AACA,a;AACA,E;AACA,oD;AACA,sD;AACA,4D;;AAEA,0C;AACA,0C;AACA,E;;AAEA,6C;AACA,6C;AACA,E;;AAEA,yC;AACA,yC;AACA,E;;;AAGA,qC;AACA,uD;AACA,4C;;;AAGA,+C;AACA,kC;AACA,2B;AACA,8B;;AAEA,2C;AACA,oB;AACA,qE;;AAEA,yB;AACA,uB;AACA,E;AACA,gE;;AAEA,+C;AACA,kC;AACA,2B;AACA,8B;;AAEA,gC;AACA,sE;;AAEA,qB;AACA,0E;AACA,wD;AACA,E;AACA,gE;;AAEA,uC;AACA,8B;AACA,2B;AACA,0B;;AAEA,gC;AACA,kE;;AAEA,qB;AACA,E;AACA,oD;;;AAGA,6B;AACA,2E;AACA,yE;AACA,e;AACA,8B;AACA,E;;AAEA,yC;AACA,yC;AACA,uC;AACA,sE;AACA,E;;AAEA,gC;AACA,mB;AACA,wB;AACA,gB;;AAEA,2B;AACA,2D;AACA,yC;AACA,kC;AACA,qB;AACA,gB;AACA,G;;AAEA,e;AACA,E;;AAEA,6C;AACA,mD;AACA,E;;AAEA,mE;AACA,kD;AACA,2C;AACA,c;AACA,iB;;AAEA,oC;AACA,oC;AACA,gB;;AAEA,kB;AACA,iE;AACA,gD;AACA,yC;AACA,2C;AACA,+E;AACA,gC;AACA,4C;AACA,gE;AACA,iC;AACA,gC;AACA,6B;AACA,K;AACA,G;;AAEA,gB;AACA,E;;;;AAIA,qC;;AAEA,kC;AACA,iD;AACA,E;;AAEA,2D;AACA,iB;AACA,Y;AACA,Y;AACA,c;AACA,E;;;AAGA,4C;AACA,iB;AACA,yD;AACA,6C;AACA,6C;AACA,+C;AACA,qD;;AAEA,8D;AACA,gC;AACA,E","file":"/packages/htmljs.js","sourcesContent":["HTML = {};\n\nIDENTITY = function (x) { return x; };\nSLICE = Array.prototype.slice;\n","////////////////////////////// VISITORS\n\n// _assign is like _.extend or the upcoming Object.assign.\n// Copy src's own, enumerable properties onto tgt and return\n// tgt.\nvar _hasOwnProperty = Object.prototype.hasOwnProperty;\nvar _assign = function (tgt, src) {\n for (var k in src) {\n if (_hasOwnProperty.call(src, k))\n tgt[k] = src[k];\n }\n return tgt;\n};\n\nHTML.Visitor = function (props) {\n _assign(this, props);\n};\n\nHTML.Visitor.def = function (options) {\n _assign(this.prototype, options);\n};\n\nHTML.Visitor.extend = function (options) {\n var curType = this;\n var subType = function HTMLVisitorSubtype(/*arguments*/) {\n HTML.Visitor.apply(this, arguments);\n };\n subType.prototype = new curType;\n subType.extend = curType.extend;\n subType.def = curType.def;\n if (options)\n _assign(subType.prototype, options);\n return subType;\n};\n\nHTML.Visitor.def({\n visit: function (content/*, ...*/) {\n if (content == null)\n // null or undefined.\n return this.visitNull.apply(this, arguments);\n\n if (typeof content === 'object') {\n if (content.htmljsType) {\n switch (content.htmljsType) {\n case HTML.Tag.htmljsType:\n return this.visitTag.apply(this, arguments);\n case HTML.CharRef.htmljsType:\n return this.visitCharRef.apply(this, arguments);\n case HTML.Comment.htmljsType:\n return this.visitComment.apply(this, arguments);\n case HTML.Raw.htmljsType:\n return this.visitRaw.apply(this, arguments);\n default:\n throw new Error(\"Unknown htmljs type: \" + content.htmljsType);\n }\n }\n\n if (HTML.isArray(content))\n return this.visitArray.apply(this, arguments);\n\n return this.visitObject.apply(this, arguments);\n\n } else if ((typeof content === 'string') ||\n (typeof content === 'boolean') ||\n (typeof content === 'number')) {\n return this.visitPrimitive.apply(this, arguments);\n\n } else if (typeof content === 'function') {\n return this.visitFunction.apply(this, arguments);\n }\n\n throw new Error(\"Unexpected object in htmljs: \" + content);\n\n },\n visitNull: function (nullOrUndefined/*, ...*/) {},\n visitPrimitive: function (stringBooleanOrNumber/*, ...*/) {},\n visitArray: function (array/*, ...*/) {},\n visitComment: function (comment/*, ...*/) {},\n visitCharRef: function (charRef/*, ...*/) {},\n visitRaw: function (raw/*, ...*/) {},\n visitTag: function (tag/*, ...*/) {},\n visitObject: function (obj/*, ...*/) {\n throw new Error(\"Unexpected object in htmljs: \" + obj);\n },\n visitFunction: function (obj/*, ...*/) {\n throw new Error(\"Unexpected function in htmljs: \" + obj);\n }\n});\n\nHTML.TransformingVisitor = HTML.Visitor.extend();\nHTML.TransformingVisitor.def({\n visitNull: IDENTITY,\n visitPrimitive: IDENTITY,\n visitArray: function (array/*, ...*/) {\n var argsCopy = SLICE.call(arguments);\n var result = array;\n for (var i = 0; i < array.length; i++) {\n var oldItem = array[i];\n argsCopy[0] = oldItem;\n var newItem = this.visit.apply(this, argsCopy);\n if (newItem !== oldItem) {\n // copy `array` on write\n if (result === array)\n result = array.slice();\n result[i] = newItem;\n }\n }\n return result;\n },\n visitComment: IDENTITY,\n visitCharRef: IDENTITY,\n visitRaw: IDENTITY,\n visitObject: IDENTITY,\n visitFunction: IDENTITY,\n visitTag: function (tag/*, ...*/) {\n var oldChildren = tag.children;\n var argsCopy = SLICE.call(arguments);\n argsCopy[0] = oldChildren;\n var newChildren = this.visitChildren.apply(this, argsCopy);\n\n var oldAttrs = tag.attrs;\n argsCopy[0] = oldAttrs;\n var newAttrs = this.visitAttributes.apply(this, argsCopy);\n\n if (newAttrs === oldAttrs && newChildren === oldChildren)\n return tag;\n\n var newTag = HTML.getTag(tag.tagName).apply(null, newChildren);\n newTag.attrs = newAttrs;\n return newTag;\n },\n visitChildren: function (children/*, ...*/) {\n return this.visitArray.apply(this, arguments);\n },\n // Transform the `.attrs` property of a tag, which may be a dictionary,\n // an array, or in some uses, a foreign object (such as\n // a template tag).\n visitAttributes: function (attrs/*, ...*/) {\n if (HTML.isArray(attrs)) {\n var argsCopy = SLICE.call(arguments);\n var result = attrs;\n for (var i = 0; i < attrs.length; i++) {\n var oldItem = attrs[i];\n argsCopy[0] = oldItem;\n var newItem = this.visitAttributes.apply(this, argsCopy);\n if (newItem !== oldItem) {\n // copy on write\n if (result === attrs)\n result = attrs.slice();\n result[i] = newItem;\n }\n }\n return result;\n }\n\n if (attrs && HTML.isConstructedObject(attrs)) {\n throw new Error(\"The basic HTML.TransformingVisitor does not support \" +\n \"foreign objects in attributes. Define a custom \" +\n \"visitAttributes for this case.\");\n }\n\n var oldAttrs = attrs;\n var newAttrs = oldAttrs;\n if (oldAttrs) {\n var attrArgs = [null, null];\n attrArgs.push.apply(attrArgs, arguments);\n for (var k in oldAttrs) {\n var oldValue = oldAttrs[k];\n attrArgs[0] = k;\n attrArgs[1] = oldValue;\n var newValue = this.visitAttribute.apply(this, attrArgs);\n if (newValue !== oldValue) {\n // copy on write\n if (newAttrs === oldAttrs)\n newAttrs = _assign({}, oldAttrs);\n newAttrs[k] = newValue;\n }\n }\n }\n\n return newAttrs;\n },\n // Transform the value of one attribute name/value in an\n // attributes dictionary.\n visitAttribute: function (name, value, tag/*, ...*/) {\n var args = SLICE.call(arguments, 2);\n args[0] = value;\n return this.visit.apply(this, args);\n }\n});\n\n\nHTML.ToTextVisitor = HTML.Visitor.extend();\nHTML.ToTextVisitor.def({\n visitNull: function (nullOrUndefined) {\n return '';\n },\n visitPrimitive: function (stringBooleanOrNumber) {\n var str = String(stringBooleanOrNumber);\n if (this.textMode === HTML.TEXTMODE.RCDATA) {\n return str.replace(/&/g, '&').replace(/</g, '<');\n } else if (this.textMode === HTML.TEXTMODE.ATTRIBUTE) {\n // escape `&` and `\"` this time, not `&` and `<`\n return str.replace(/&/g, '&').replace(/\"/g, '"');\n } else {\n return str;\n }\n },\n visitArray: function (array) {\n var parts = [];\n for (var i = 0; i < array.length; i++)\n parts.push(this.visit(array[i]));\n return parts.join('');\n },\n visitComment: function (comment) {\n throw new Error(\"Can't have a comment here\");\n },\n visitCharRef: function (charRef) {\n if (this.textMode === HTML.TEXTMODE.RCDATA ||\n this.textMode === HTML.TEXTMODE.ATTRIBUTE) {\n return charRef.html;\n } else {\n return charRef.str;\n }\n },\n visitRaw: function (raw) {\n return raw.value;\n },\n visitTag: function (tag) {\n // Really we should just disallow Tags here. However, at the\n // moment it's useful to stringify any HTML we find. In\n // particular, when you include a template within `{{#markdown}}`,\n // we render the template as text, and since there's currently\n // no way to make the template be *parsed* as text (e.g. `<template\n // type=\"text\">`), we hackishly support HTML tags in markdown\n // in templates by parsing them and stringifying them.\n return this.visit(this.toHTML(tag));\n },\n visitObject: function (x) {\n throw new Error(\"Unexpected object in htmljs in toText: \" + x);\n },\n toHTML: function (node) {\n return HTML.toHTML(node);\n }\n});\n\n\n\nHTML.ToHTMLVisitor = HTML.Visitor.extend();\nHTML.ToHTMLVisitor.def({\n visitNull: function (nullOrUndefined) {\n return '';\n },\n visitPrimitive: function (stringBooleanOrNumber) {\n var str = String(stringBooleanOrNumber);\n return str.replace(/&/g, '&').replace(/</g, '<');\n },\n visitArray: function (array) {\n var parts = [];\n for (var i = 0; i < array.length; i++)\n parts.push(this.visit(array[i]));\n return parts.join('');\n },\n visitComment: function (comment) {\n return '<!--' + comment.sanitizedValue + '-->';\n },\n visitCharRef: function (charRef) {\n return charRef.html;\n },\n visitRaw: function (raw) {\n return raw.value;\n },\n visitTag: function (tag) {\n var attrStrs = [];\n\n var tagName = tag.tagName;\n var children = tag.children;\n\n var attrs = tag.attrs;\n if (attrs) {\n attrs = HTML.flattenAttributes(attrs);\n for (var k in attrs) {\n if (k === 'value' && tagName === 'textarea') {\n children = [attrs[k], children];\n } else {\n var v = this.toText(attrs[k], HTML.TEXTMODE.ATTRIBUTE);\n attrStrs.push(' ' + k + '=\"' + v + '\"');\n }\n }\n }\n\n var startTag = '<' + tagName + attrStrs.join('') + '>';\n\n var childStrs = [];\n var content;\n if (tagName === 'textarea') {\n\n for (var i = 0; i < children.length; i++)\n childStrs.push(this.toText(children[i], HTML.TEXTMODE.RCDATA));\n\n content = childStrs.join('');\n if (content.slice(0, 1) === '\\n')\n // TEXTAREA will absorb a newline, so if we see one, add\n // another one.\n content = '\\n' + content;\n\n } else {\n for (var i = 0; i < children.length; i++)\n childStrs.push(this.visit(children[i]));\n\n content = childStrs.join('');\n }\n\n var result = startTag + content;\n\n if (children.length || ! HTML.isVoidElement(tagName)) {\n // \"Void\" elements like BR are the only ones that don't get a close\n // tag in HTML5. They shouldn't have contents, either, so we could\n // throw an error upon seeing contents here.\n result += '</' + tagName + '>';\n }\n\n return result;\n },\n visitObject: function (x) {\n throw new Error(\"Unexpected object in htmljs in toHTML: \" + x);\n },\n toText: function (node, textMode) {\n return HTML.toText(node, textMode);\n }\n});\n","\n\nHTML.Tag = function () {};\nHTML.Tag.prototype.tagName = ''; // this will be set per Tag subclass\nHTML.Tag.prototype.attrs = null;\nHTML.Tag.prototype.children = Object.freeze ? Object.freeze([]) : [];\nHTML.Tag.prototype.htmljsType = HTML.Tag.htmljsType = ['Tag'];\n\n// Given \"p\" create the function `HTML.P`.\nvar makeTagConstructor = function (tagName) {\n // HTMLTag is the per-tagName constructor of a HTML.Tag subclass\n var HTMLTag = function (/*arguments*/) {\n // Work with or without `new`. If not called with `new`,\n // perform instantiation by recursively calling this constructor.\n // We can't pass varargs, so pass no args.\n var instance = (this instanceof HTML.Tag) ? this : new HTMLTag;\n\n var i = 0;\n var attrs = arguments.length && arguments[0];\n if (attrs && (typeof attrs === 'object')) {\n // Treat vanilla JS object as an attributes dictionary.\n if (! HTML.isConstructedObject(attrs)) {\n instance.attrs = attrs;\n i++;\n } else if (attrs instanceof HTML.Attrs) {\n var array = attrs.value;\n if (array.length === 1) {\n instance.attrs = array[0];\n } else if (array.length > 1) {\n instance.attrs = array;\n }\n i++;\n }\n }\n\n\n // If no children, don't create an array at all, use the prototype's\n // (frozen, empty) array. This way we don't create an empty array\n // every time someone creates a tag without `new` and this constructor\n // calls itself with no arguments (above).\n if (i < arguments.length)\n instance.children = SLICE.call(arguments, i);\n\n return instance;\n };\n HTMLTag.prototype = new HTML.Tag;\n HTMLTag.prototype.constructor = HTMLTag;\n HTMLTag.prototype.tagName = tagName;\n\n return HTMLTag;\n};\n\n// Not an HTMLjs node, but a wrapper to pass multiple attrs dictionaries\n// to a tag (for the purpose of implementing dynamic attributes).\nvar Attrs = HTML.Attrs = function (/*attrs dictionaries*/) {\n // Work with or without `new`. If not called with `new`,\n // perform instantiation by recursively calling this constructor.\n // We can't pass varargs, so pass no args.\n var instance = (this instanceof Attrs) ? this : new Attrs;\n\n instance.value = SLICE.call(arguments);\n\n return instance;\n};\n\n////////////////////////////// KNOWN ELEMENTS\n\nHTML.getTag = function (tagName) {\n var symbolName = HTML.getSymbolName(tagName);\n if (symbolName === tagName) // all-caps tagName\n throw new Error(\"Use the lowercase or camelCase form of '\" + tagName + \"' here\");\n\n if (! HTML[symbolName])\n HTML[symbolName] = makeTagConstructor(tagName);\n\n return HTML[symbolName];\n};\n\nHTML.ensureTag = function (tagName) {\n HTML.getTag(tagName); // don't return it\n};\n\nHTML.isTagEnsured = function (tagName) {\n return HTML.isKnownElement(tagName);\n};\n\nHTML.getSymbolName = function (tagName) {\n // \"foo-bar\" -> \"FOO_BAR\"\n return tagName.toUpperCase().replace(/-/g, '_');\n};\n\nHTML.knownElementNames = 'a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup command data datagrid datalist dd del details dfn dir div dl dt em embed eventsource fieldset figcaption figure font footer form frame frameset h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins isindex kbd keygen label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small source span strike strong style sub summary sup table tbody td textarea tfoot th thead time title tr track tt u ul var video wbr'.split(' ');\n// (we add the SVG ones below)\n\nHTML.knownSVGElementNames = 'altGlyph altGlyphDef altGlyphItem animate animateColor animateMotion animateTransform circle clipPath color-profile cursor defs desc ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter font font-face font-face-format font-face-name font-face-src font-face-uri foreignObject g glyph glyphRef hkern image line linearGradient marker mask metadata missing-glyph path pattern polygon polyline radialGradient rect script set stop style svg switch symbol text textPath title tref tspan use view vkern'.split(' ');\n// Append SVG element names to list of known element names\nHTML.knownElementNames = HTML.knownElementNames.concat(HTML.knownSVGElementNames);\n\nHTML.voidElementNames = 'area base br col command embed hr img input keygen link meta param source track wbr'.split(' ');\n\n// Speed up search through lists of known elements by creating internal \"sets\"\n// of strings.\nvar YES = {yes:true};\nvar makeSet = function (array) {\n var set = {};\n for (var i = 0; i < array.length; i++)\n set[array[i]] = YES;\n return set;\n};\nvar voidElementSet = makeSet(HTML.voidElementNames);\nvar knownElementSet = makeSet(HTML.knownElementNames);\nvar knownSVGElementSet = makeSet(HTML.knownSVGElementNames);\n\nHTML.isKnownElement = function (tagName) {\n return knownElementSet[tagName] === YES;\n};\n\nHTML.isKnownSVGElement = function (tagName) {\n return knownSVGElementSet[tagName] === YES;\n};\n\nHTML.isVoidElement = function (tagName) {\n return voidElementSet[tagName] === YES;\n};\n\n\n// Ensure tags for all known elements\nfor (var i = 0; i < HTML.knownElementNames.length; i++)\n HTML.ensureTag(HTML.knownElementNames[i]);\n\n\nvar CharRef = HTML.CharRef = function (attrs) {\n if (! (this instanceof CharRef))\n // called without `new`\n return new CharRef(attrs);\n\n if (! (attrs && attrs.html && attrs.str))\n throw new Error(\n \"HTML.CharRef must be constructed with ({html:..., str:...})\");\n\n this.html = attrs.html;\n this.str = attrs.str;\n};\nCharRef.prototype.htmljsType = CharRef.htmljsType = ['CharRef'];\n\nvar Comment = HTML.Comment = function (value) {\n if (! (this instanceof Comment))\n // called without `new`\n return new Comment(value);\n\n if (typeof value !== 'string')\n throw new Error('HTML.Comment must be constructed with a string');\n\n this.value = value;\n // Kill illegal hyphens in comment value (no way to escape them in HTML)\n this.sanitizedValue = value.replace(/^-|--+|-$/g, '');\n};\nComment.prototype.htmljsType = Comment.htmljsType = ['Comment'];\n\nvar Raw = HTML.Raw = function (value) {\n if (! (this instanceof Raw))\n // called without `new`\n return new Raw(value);\n\n if (typeof value !== 'string')\n throw new Error('HTML.Raw must be constructed with a string');\n\n this.value = value;\n};\nRaw.prototype.htmljsType = Raw.htmljsType = ['Raw'];\n\n\nHTML.isArray = function (x) {\n // could change this to use the more convoluted Object.prototype.toString\n // approach that works when objects are passed between frames, but does\n // it matter?\n return (x instanceof Array);\n};\n\nHTML.isConstructedObject = function (x) {\n return (x && (typeof x === 'object') &&\n (x.constructor !== Object) &&\n (! Object.prototype.hasOwnProperty.call(x, 'constructor')));\n};\n\nHTML.isNully = function (node) {\n if (node == null)\n // null or undefined\n return true;\n\n if (HTML.isArray(node)) {\n // is it an empty array or an array of all nully items?\n for (var i = 0; i < node.length; i++)\n if (! HTML.isNully(node[i]))\n return false;\n return true;\n }\n\n return false;\n};\n\nHTML.isValidAttributeName = function (name) {\n return /^[:_A-Za-z][:_A-Za-z0-9.\\-]*/.test(name);\n};\n\n// If `attrs` is an array of attributes dictionaries, combines them\n// into one. Removes attributes that are \"nully.\"\nHTML.flattenAttributes = function (attrs) {\n if (! attrs)\n return attrs;\n\n var isArray = HTML.isArray(attrs);\n if (isArray && attrs.length === 0)\n return null;\n\n var result = {};\n for (var i = 0, N = (isArray ? attrs.length : 1); i < N; i++) {\n var oneAttrs = (isArray ? attrs[i] : attrs);\n if ((typeof oneAttrs !== 'object') ||\n HTML.isConstructedObject(oneAttrs))\n throw new Error(\"Expected plain JS object as attrs, found: \" + oneAttrs);\n for (var name in oneAttrs) {\n if (! HTML.isValidAttributeName(name))\n throw new Error(\"Illegal HTML attribute name: \" + name);\n var value = oneAttrs[name];\n if (! HTML.isNully(value))\n result[name] = value;\n }\n }\n\n return result;\n};\n\n\n\n////////////////////////////// TOHTML\n\nHTML.toHTML = function (content) {\n return (new HTML.ToHTMLVisitor).visit(content);\n};\n\n// Escaping modes for outputting text when generating HTML.\nHTML.TEXTMODE = {\n STRING: 1,\n RCDATA: 2,\n ATTRIBUTE: 3\n};\n\n\nHTML.toText = function (content, textMode) {\n if (! textMode)\n throw new Error(\"textMode required for HTML.toText\");\n if (! (textMode === HTML.TEXTMODE.STRING ||\n textMode === HTML.TEXTMODE.RCDATA ||\n textMode === HTML.TEXTMODE.ATTRIBUTE))\n throw new Error(\"Unknown textMode: \" + textMode);\n\n var visitor = new HTML.ToTextVisitor({textMode: textMode});;\n return visitor.visit(content);\n};\n"]} |