@searchspring/snap-toolbox 0.63.0 → 0.63.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAEF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAmFzG"}
1
+ {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAgDF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAsGzG"}
@@ -12,6 +12,51 @@ var __assign = (this && this.__assign) || function () {
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.getContext = void 0;
15
+ var JAVASCRIPT_KEYWORDS = new Set([
16
+ 'break',
17
+ 'case',
18
+ 'catch',
19
+ 'class',
20
+ 'const',
21
+ 'continue',
22
+ 'debugger',
23
+ 'default',
24
+ 'delete',
25
+ 'do',
26
+ 'else',
27
+ 'export',
28
+ 'extends',
29
+ 'finally',
30
+ 'for',
31
+ 'function',
32
+ 'if',
33
+ 'import',
34
+ 'in',
35
+ 'instanceof',
36
+ 'new',
37
+ 'return',
38
+ 'super',
39
+ 'switch',
40
+ 'this',
41
+ 'throw',
42
+ 'try',
43
+ 'typeof',
44
+ 'var',
45
+ 'void',
46
+ 'while',
47
+ 'with',
48
+ 'yield',
49
+ 'let',
50
+ 'static',
51
+ 'enum',
52
+ 'await',
53
+ 'implements',
54
+ 'package',
55
+ 'protected',
56
+ 'interface',
57
+ 'private',
58
+ 'public',
59
+ ]);
15
60
  function getContext(evaluate, script) {
16
61
  var _a, _b, _c, _d, _e;
17
62
  if (evaluate === void 0) { evaluate = []; }
@@ -49,16 +94,36 @@ function getContext(evaluate, script) {
49
94
  var scriptVariables = {};
50
95
  var scriptInnerHTML = scriptElem.innerHTML;
51
96
  // attempt to grab inner HTML variables
52
- var scriptInnerVars = (_d = scriptInnerHTML.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s?=/g)) === null || _d === void 0 ? void 0 : _d.map(function (match) { return match.replace(/[\s=]/g, ''); });
97
+ var scriptInnerVars = (_d = scriptInnerHTML
98
+ // first remove all string literals (including template literals) to avoid false matches
99
+ .replace(/`(?:\\[\s\S]|[^`\\])*`|'(?:\\[\s\S]|[^'\\])*'|"(?:\\[\s\S]|[^"\\])*"/g, '')
100
+ // then find variable assignments
101
+ .match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s*=/g)) === null || _d === void 0 ? void 0 : _d.map(function (match) { return match.replace(/[\s=]/g, ''); });
53
102
  var combinedVars = evaluate.concat(scriptInnerVars || []);
54
103
  // de-dupe vars
55
104
  var evaluateVars = combinedVars.filter(function (item, index) {
56
- return combinedVars.indexOf(item) === index;
105
+ var isKeyword = JAVASCRIPT_KEYWORDS.has(item);
106
+ // console error if keyword
107
+ if (isKeyword) {
108
+ console.error("getContext: JavaScript keyword found: '".concat(item, "'! Please use a different variable name."));
109
+ }
110
+ return combinedVars.indexOf(item) === index && !isKeyword;
57
111
  });
58
112
  // evaluate text and put into variables
59
113
  evaluate === null || evaluate === void 0 ? void 0 : evaluate.forEach(function (name) {
60
- var fn = new Function("\n\t\t\tvar ".concat(evaluateVars.join(', '), ";\n\t\t\t").concat(scriptInnerHTML, "\n\t\t\treturn ").concat(name, ";\n\t\t"));
61
- scriptVariables[name] = fn();
114
+ try {
115
+ var fn = new Function("\n\t\t\t\tvar ".concat(evaluateVars.join(', '), ";\n\t\t\t\t").concat(scriptInnerHTML, "\n\t\t\t\treturn ").concat(name, ";\n\t\t\t"));
116
+ scriptVariables[name] = fn();
117
+ }
118
+ catch (err) {
119
+ // if evaluation fails, set to undefined
120
+ var isKeyword = JAVASCRIPT_KEYWORDS.has(name);
121
+ if (!isKeyword) {
122
+ console.error("getContext: error evaluating '".concat(name, "'"));
123
+ console.error(err);
124
+ }
125
+ scriptVariables[name] = undefined;
126
+ }
62
127
  });
63
128
  var variables = __assign(__assign({}, removeUndefined(attributeVariables)), removeUndefined(scriptVariables));
64
129
  if (evaluate.includes(siteIdString)) {
@@ -1 +1 @@
1
- {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAEF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAmFzG"}
1
+ {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAgDF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAsGzG"}
@@ -1,3 +1,48 @@
1
+ const JAVASCRIPT_KEYWORDS = new Set([
2
+ 'break',
3
+ 'case',
4
+ 'catch',
5
+ 'class',
6
+ 'const',
7
+ 'continue',
8
+ 'debugger',
9
+ 'default',
10
+ 'delete',
11
+ 'do',
12
+ 'else',
13
+ 'export',
14
+ 'extends',
15
+ 'finally',
16
+ 'for',
17
+ 'function',
18
+ 'if',
19
+ 'import',
20
+ 'in',
21
+ 'instanceof',
22
+ 'new',
23
+ 'return',
24
+ 'super',
25
+ 'switch',
26
+ 'this',
27
+ 'throw',
28
+ 'try',
29
+ 'typeof',
30
+ 'var',
31
+ 'void',
32
+ 'while',
33
+ 'with',
34
+ 'yield',
35
+ 'let',
36
+ 'static',
37
+ 'enum',
38
+ 'await',
39
+ 'implements',
40
+ 'package',
41
+ 'protected',
42
+ 'interface',
43
+ 'private',
44
+ 'public',
45
+ ]);
1
46
  export function getContext(evaluate = [], script) {
2
47
  if (!script || typeof script === 'string') {
3
48
  const scripts = Array.from(document.querySelectorAll(script || 'script[id^=searchspring], script[src*="snapui.searchspring.io"]'));
@@ -33,20 +78,41 @@ export function getContext(evaluate = [], script) {
33
78
  const scriptVariables = {};
34
79
  const scriptInnerHTML = scriptElem.innerHTML;
35
80
  // attempt to grab inner HTML variables
36
- const scriptInnerVars = scriptInnerHTML.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s?=/g)?.map((match) => match.replace(/[\s=]/g, ''));
81
+ const scriptInnerVars = scriptInnerHTML
82
+ // first remove all string literals (including template literals) to avoid false matches
83
+ .replace(/`(?:\\[\s\S]|[^`\\])*`|'(?:\\[\s\S]|[^'\\])*'|"(?:\\[\s\S]|[^"\\])*"/g, '')
84
+ // then find variable assignments
85
+ .match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s*=/g)
86
+ ?.map((match) => match.replace(/[\s=]/g, ''));
37
87
  const combinedVars = evaluate.concat(scriptInnerVars || []);
38
88
  // de-dupe vars
39
89
  const evaluateVars = combinedVars.filter((item, index) => {
40
- return combinedVars.indexOf(item) === index;
90
+ const isKeyword = JAVASCRIPT_KEYWORDS.has(item);
91
+ // console error if keyword
92
+ if (isKeyword) {
93
+ console.error(`getContext: JavaScript keyword found: '${item}'! Please use a different variable name.`);
94
+ }
95
+ return combinedVars.indexOf(item) === index && !isKeyword;
41
96
  });
42
97
  // evaluate text and put into variables
43
98
  evaluate?.forEach((name) => {
44
- const fn = new Function(`
45
- var ${evaluateVars.join(', ')};
46
- ${scriptInnerHTML}
47
- return ${name};
48
- `);
49
- scriptVariables[name] = fn();
99
+ try {
100
+ const fn = new Function(`
101
+ var ${evaluateVars.join(', ')};
102
+ ${scriptInnerHTML}
103
+ return ${name};
104
+ `);
105
+ scriptVariables[name] = fn();
106
+ }
107
+ catch (err) {
108
+ // if evaluation fails, set to undefined
109
+ const isKeyword = JAVASCRIPT_KEYWORDS.has(name);
110
+ if (!isKeyword) {
111
+ console.error(`getContext: error evaluating '${name}'`);
112
+ console.error(err);
113
+ }
114
+ scriptVariables[name] = undefined;
115
+ }
50
116
  });
51
117
  const variables = {
52
118
  ...removeUndefined(attributeVariables),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@searchspring/snap-toolbox",
3
- "version": "0.63.0",
3
+ "version": "0.63.1",
4
4
  "description": "Snap Toolbox",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -23,5 +23,5 @@
23
23
  "files": [
24
24
  "dist/**/*"
25
25
  ],
26
- "gitHead": "46278e19380f22e90fdff4afc01359be27feb5a8"
26
+ "gitHead": "2feab8c22416a48cb0c9b76b9413c6a0e3ba774c"
27
27
  }