UNPKG

4.13 kBSource Map (JSON)View Raw
1{"version":3,"file":"template.modern.js","sources":["../src/lib/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\tconsole.log(compileTemplate(template.innerHTML));\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"],"names":["compileTemplate","template","replace","match","p1","substring","_hyperscript","addCommand","parser","runtime","tokens","matchToken","template_","requireElement","templateArgs","parseElement","args","op","ctx","Element","Error","sourceFor","console","log","innerHTML","result","buf","evaluate","Object","assign","__ht_template_result","join","renderTemplate","findNext","this","addLeafExpression","escapeType","matchTokenType","value","unescaped","arg","undefined","String","unifiedEval"],"mappings":"AACA,SAASA,EAAgBC,GACxB,OAAOA,EAASC,QAAQ,sBAAuB,SAAUC,EAAOC,GAE/D,MAAO,sCADY,IAAMA,GAAIF,QAAQ,eAAgB,qBAAqBG,UAAU,GACxB,SAO9D,MAAeC,IAQdA,EAAaC,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,EAAKjB,EAAUa,GAC5B,KAAMb,aAAoBkB,SAAU,UAAUC,MAAMR,EAAUS,YAAc,sBAG5E,OAFAC,QAAQC,IAA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