UNPKG

5.58 kBSource Map (JSON)View Raw
1{"version":3,"file":"static.js","sources":["src/static.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2020 Google LLC\n * SPDX-License-Identifier: BSD-3-Clause\n */\nimport {html as coreHtml, svg as coreSvg, TemplateResult} from './lit-html.js';\n\n/**\n * Wraps a string so that it behaves like part of the static template\n * strings instead of a dynamic value.\n *\n * Users must take care to ensure that adding the static string to the template\n * results in well-formed HTML, or else templates may break unexpectedly.\n *\n * Note that this function is unsafe to use on untrusted content, as it will be\n * directly parsed into HTML. Do not pass user input to this function\n * without sanitizing it.\n *\n * Static values can be changed, but they will cause a complete re-render\n * since they effectively create a new template.\n */\nexport const unsafeStatic = (value: string) => ({\n ['_$litStatic$']: value,\n});\n\nconst textFromStatic = (value: StaticValue) => {\n if (value['_$litStatic$'] !== undefined) {\n return value['_$litStatic$'];\n } else {\n throw new Error(\n `Value passed to 'literal' function must be a 'literal' result: ${value}. Use 'unsafeStatic' to pass non-literal values, but\n take care to ensure page security.`\n );\n }\n};\n\n/**\n * Tags a string literal so that it behaves like part of the static template\n * strings instead of a dynamic value.\n *\n * The only values that may be used in template expressions are other tagged\n * `literal` results or `unsafeStatic` values (note that untrusted content\n * should never be passed to `unsafeStatic`).\n *\n * Users must take care to ensure that adding the static string to the template\n * results in well-formed HTML, or else templates may break unexpectedly.\n *\n * Static values can be changed, but they will cause a complete re-render since\n * they effectively create a new template.\n */\nexport const literal = (\n strings: TemplateStringsArray,\n ...values: unknown[]\n) => ({\n ['_$litStatic$']: values.reduce(\n (acc, v, idx) => acc + textFromStatic(v as StaticValue) + strings[idx + 1],\n strings[0]\n ),\n});\n\ntype StaticValue = ReturnType<typeof unsafeStatic>;\n\nconst stringsCache = new Map<string, TemplateStringsArray>();\n\n/**\n * Wraps a lit-html template tag (`html` or `svg`) to add static value support.\n */\nexport const withStatic =\n (coreTag: typeof coreHtml | typeof coreSvg) =>\n (strings: TemplateStringsArray, ...values: unknown[]): TemplateResult => {\n const l = values.length;\n let staticValue: string | undefined;\n let dynamicValue: unknown;\n const staticStrings: Array<string> = [];\n const dynamicValues: Array<unknown> = [];\n let i = 0;\n let hasStatics = false;\n let s: string;\n\n while (i < l) {\n s = strings[i];\n // Collect any unsafeStatic values, and their following template strings\n // so that we treat a run of template strings and unsafe static values as\n // a single template string.\n while (\n i < l &&\n ((dynamicValue = values[i]),\n (staticValue = (dynamicValue as StaticValue)?.['_$litStatic$'])) !==\n undefined\n ) {\n s += staticValue + strings[++i];\n hasStatics = true;\n }\n dynamicValues.push(dynamicValue);\n staticStrings.push(s);\n i++;\n }\n // If the last value isn't static (which would have consumed the last\n // string), then we need to add the last string.\n if (i === l) {\n staticStrings.push(strings[l]);\n }\n\n if (hasStatics) {\n const key = staticStrings.join('$$lit$$');\n strings = stringsCache.get(key)!;\n if (strings === undefined) {\n stringsCache.set(\n key,\n (strings = staticStrings as unknown as TemplateStringsArray)\n );\n }\n values = dynamicValues;\n }\n return coreTag(strings, ...values);\n };\n\n/**\n * Interprets a template literal as an HTML template that can efficiently\n * render to and update a container.\n *\n * Includes static value support from `lit-html/static.js`.\n */\nexport const html = withStatic(coreHtml);\n\n/**\n * Interprets a template literal as an SVG template that can efficiently\n * render to and update a container.\n *\n * Includes static value support from `lit-html/static.js`.\n */\nexport const svg = withStatic(coreSvg);\n"],"names":["unsafeStatic","value","_$litStatic$","literal","strings","values","reduce","acc","v","idx","undefined","Error","textFromStatic","stringsCache","Map","withStatic","coreTag","l","length","staticValue","dynamicValue","staticStrings","dynamicValues","s","i","hasStatics","push","key","join","get","set","html","coreHtml","svg","coreSvg"],"mappings":";;;;;SAqBaA,EAAgBC,KAC3BC,aAAkBD,IA4BPE,EAAU,CACrBC,KACGC,MAEHH,aAAkBG,EAAOC,QACvB,CAACC,EAAKC,EAAGC,IAAQF,EA9BE,CAACN,IACtB,QAA8BS,IAA1BT,EAAoB,aACtB,OAAOA,EAAoB,aAE3B,MAAUU,MACR,kEAAkEV,0GAyB7CW,CAAeJ,GAAoBJ,EAAQK,EAAM,IACxEL,EAAQ,MAMNS,EAAe,IAAIC,IAKZC,EACVC,GACD,CAACZ,KAAkCC,WACjC,MAAMY,EAAIZ,EAAOa,OACjB,IAAIC,EACAC,EACJ,MAAMC,EAA+B,GAC/BC,EAAgC,GACtC,IAEIC,EAFAC,EAAI,EACJC,GAAa,EAGjB,KAAOD,EAAIP,GAAG,CAKZ,IAJAM,EAAInB,EAAQoB,GAKVA,EAAIP,QAGFP,KAFAU,EAAef,EAAOmB,GACvBL,YAAeC,wBAA6C,eAG7DG,GAAKJ,EAAcf,IAAUoB,GAC7BC,GAAa,EAEfH,EAAcI,KAAKN,GACnBC,EAAcK,KAAKH,GACnBC,IAQF,GAJIA,IAAMP,GACRI,EAAcK,KAAKtB,EAAQa,IAGzBQ,EAAY,CACd,MAAME,EAAMN,EAAcO,KAAK,gBAEflB,KADhBN,EAAUS,EAAagB,IAAIF,KAEzBd,EAAaiB,IACXH,EACCvB,EAAUiB,GAGfhB,EAASiB,EAEX,OAAON,EAAQZ,KAAYC,IASlB0B,EAAOhB,EAAWiB,GAQlBC,EAAMlB,EAAWmB"}
\No newline at end of file