Skip to content

Commit 989fa9f

Browse files
committed
maint(core dom template): Warn about using dom.template due to a CSR probmel.
Warn about a problem of dom.template with a Content-Security-Policy set. If a CSR rule is set then dom.template would break the code unless 'unsafe-eval' is allowed (which you wouldn't normally allow when using a CSR). Therefore it is not recommended to use this template function.
1 parent 5588f36 commit 989fa9f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/core/dom.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* Utilities for DOM traversal or navigation */
22
import events from "./events";
3+
import logging from "./logging";
4+
5+
const logger = logging.getLogger("core dom");
36

47
const DATA_PREFIX = "__patternslib__data_prefix__";
58
const DATA_STYLE_DISPLAY = "__patternslib__style__display";
@@ -309,6 +312,10 @@ const delete_data = (el, name) => {
309312
/**
310313
* Simple template engine, based on JS template literal
311314
*
315+
* NOTE: This uses eval and would break if Content-Security-Policy does not
316+
* allow 'unsafe-eval'.
317+
* Because of this CSR problem the use of this method is not recommended.
318+
*
312319
* Please note: You cannot pass a template literal as template_string.
313320
* JavaScript itself would try to expand it and would fail.
314321
*
@@ -323,6 +330,9 @@ const delete_data = (el, name) => {
323330
* @returns {String} - Returns the a string as template expanded with the template_variables.
324331
*/
325332
const template = (template_string, template_variables = {}) => {
333+
logger.warn(
334+
"Using dom.template is not recommended due to a problem with Content-Security-Policy."
335+
);
326336
return new Function("return `" + template_string + "`;").call(template_variables);
327337
};
328338

0 commit comments

Comments
 (0)