UNPKG

4.16 kBSource Map (JSON)View Raw
1{"version":3,"file":"template.min.js","sources":["../src/lib/plugin/template.js","../src/web/plugin/template.js"],"sourcesContent":["\nfunction compileTemplate(template) {\n\treturn template.replace(/(?:^|\\n)([^@]*)@?/gm, function (match, p1) {\n\t\tvar templateStr = (\" \" + p1).replace(/([^\\\\])\\$\\{/g, \"$1$${escape html \").substring(1);\n\t\treturn \"\\ncall __ht_template_result.push(`\" + templateStr + \"`)\\n\";\n\t});\n}\n\n/**\n * @param {HyperscriptObject} _hyperscript\n */\nexport default _hyperscript => {\n\n\tfunction renderTemplate(template, ctx) {\n\t\tvar buf = [];\n\t\t_hyperscript.evaluate(template, Object.assign({ __ht_template_result: buf }, ctx));\n\t\treturn buf.join(\"\");\n\t}\n\n\t_hyperscript.addCommand(\"render\", function (parser, runtime, tokens) {\n\t\tif (!tokens.matchToken(\"render\")) return;\n\t\tvar template_ = parser.requireElement(\"expression\", tokens);\n\t\tvar templateArgs = {};\n\t\tif (tokens.matchToken(\"with\")) {\n\t\t\ttemplateArgs = parser.parseElement(\"namedArgumentList\", tokens);\n\t\t}\n\t\treturn {\n\t\t\targs: [template_, templateArgs],\n\t\t\top: function (ctx, template, templateArgs) {\n\t\t\t\tif (!(template instanceof Element)) throw new Error(template_.sourceFor() + \" is not an element\");\n\t\t\t\tctx.result = renderTemplate(compileTemplate(template.innerHTML), templateArgs);\n\t\t\t\treturn runtime.findNext(this, ctx);\n\t\t\t},\n\t\t};\n\t});\n\n\tfunction escapeHTML(html) {\n\t\treturn String(html)\n\t\t\t.replace(/&/g, \"&amp;\")\n\t\t\t.replace(/</g, \"&lt;\")\n\t\t\t.replace(/>/g, \"&gt;\")\n\t\t\t.replace(/\\x22/g, \"&quot;\")\n\t\t\t.replace(/\\x27/g, \"&#039;\");\n\t}\n\n\t_hyperscript.addLeafExpression(\"escape\", function (parser, runtime, tokens) {\n\t\tif (!tokens.matchToken(\"escape\")) return;\n\t\tvar escapeType = tokens.matchTokenType(\"IDENTIFIER\").value;\n\n\t\t// hidden! for use in templates\n\t\tvar unescaped = tokens.matchToken(\"unescaped\");\n\n\t\tvar arg = parser.requireElement(\"expression\", tokens);\n\n\t\treturn {\n\t\t\targs: [arg],\n\t\t\top: function (ctx, arg) {\n\t\t\t\tif (unescaped) return arg;\n\t\t\t\tif (arg === undefined) return \"\";\n\t\t\t\tswitch (escapeType) {\n\t\t\t\t\tcase \"html\":\n\t\t\t\t\t\treturn escapeHTML(arg);\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(\"Unknown escape: \" + escapeType);\n\t\t\t\t}\n\t\t\t},\n\t\t\tevaluate: function (ctx) {\n\t\t\t\treturn runtime.unifiedEval(this, ctx);\n\t\t\t},\n\t\t};\n\t});\n}\n","\nimport template from \"../../lib/plugin/template.js\"\n\ntemplate(window._hyperscript)\n\n"],"names":["_hyperscript","window","addCommand","parser","runtime","tokens","matchToken","template_","requireElement","templateArgs","parseElement","args","op","ctx","template","Element","Error","sourceFor","result","buf","evaluate","Object","assign","__ht_template_result","join","renderTemplate","replace","match","p1","substring","compileTemplate","innerHTML","findNext","this","addLeafExpression","escapeType","matchTokenType","value","unescaped","arg","undefined","String","unifiedEval"],"mappings":"6EAWA,IAAeA,GAAAA,ECRNC,OAAOD,cDgBFE,WAAW,SAAU,SAAUC,EAAQC,EAASC,GAC5D,GAAKA,EAAOC,WAAW,UAAvB,CACA,IAAIC,EAAYJ,EAAOK,eAAe,aAAcH,GAChDI,EAAe,GAInB,OAHIJ,EAAOC,WAAW,UACrBG,EAAeN,EAAOO,aAAa,oBAAqBL,IAElD,CACNM,KAAM,CAACJ,EAAWE,GAClBG,GAAI,SAAUC,EAAKC,EAAUL,GAC5B,KAAMK,aAAoBC,SAAU,UAAUC,MAAMT,EAAUU,YAAc,sBAE5E,OADAJ,EAAIK,OAjBP,SAAwBJ,EAAUD,GACjC,IAAIM,EAAM,GAEV,OADAnB,EAAaoB,SAASN,EAAUO,OAAOC,OAAO,CAAEC,qB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