json-diff-viewer-component 0.6.2 → 0.6.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-diff-viewer-component",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "type": "module",
5
5
  "description": "Vanilla JS web component for side-by-side JSON diff visualization",
6
6
  "bugs": "https://github.com/metaory/json-diff-viewer-component/issues",
package/src/lib/viewer.js CHANGED
@@ -2,6 +2,8 @@ import { diff, TYPE } from "./diff.js";
2
2
  import styles from "./styles.js";
3
3
 
4
4
  const STAT_TYPES = ["added", "removed", "modified"];
5
+ const ENTITIES = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
6
+ const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (char) => ENTITIES[char]);
5
7
 
6
8
  const format = (val) => {
7
9
  if (val === null) return ["null", "null"];
@@ -20,7 +22,7 @@ const filterChildren = (children, showOnlyChanged) =>
20
22
  const isExpanded = (proxy, path) => proxy[path] !== false;
21
23
 
22
24
  const buildKeyHtml = (key, root, hidden = false) =>
23
- root ? "" : `<span class="key"${hidden ? ' style="visibility: hidden;"' : ""}>${key}</span><span class="colon"${hidden ? ' style="visibility: hidden;"' : ""}>:</span>`;
25
+ root ? "" : `<span class="key"${hidden ? ' style="visibility: hidden;"' : ""}>${escapeHtml(key)}</span><span class="colon"${hidden ? ' style="visibility: hidden;"' : ""}>:</span>`;
24
26
 
25
27
  const buildRootClass = (root) => root ? " root" : "";
26
28
 
@@ -182,18 +184,18 @@ class JsonDiffViewer extends HTMLElement {
182
184
  const otherValue = side === 'left' ? node.right : node.left;
183
185
  const [val, type] = format(otherValue);
184
186
  const hiddenKey = buildKeyHtml(node.key, root, true);
185
- return `<div class="node${rootClass}"><div class="line placeholder">${hiddenKey}<span class="val-${type}" style="visibility: hidden;">${val}</span></div></div>`;
187
+ return `<div class="node${rootClass}"><div class="line placeholder">${hiddenKey}<span class="val-${type}" style="visibility: hidden;">${escapeHtml(val)}</span></div></div>`;
186
188
  }
187
189
 
188
190
  if (value !== undefined && !node.isArray && !node.isObject) {
189
191
  const [val, type] = format(value);
190
192
  if (placeholder) {
191
- return `<div class="node${rootClass}"><div class="line placeholder">${keyHtml}<span class="val-${type}"${hidden}>${val}</span></div></div>`;
193
+ return `<div class="node${rootClass}"><div class="line placeholder">${keyHtml}<span class="val-${type}"${hidden}>${escapeHtml(val)}</span></div></div>`;
192
194
  }
193
195
  const hasDiff = node.hasDiff && node.type !== TYPE.UNCHANGED;
194
196
  const diffClass = hasDiff ? `diff-${node.type}` : "";
195
197
  const nodeDiffClass = hasDiff ? ` ${diffClass}` : "";
196
- return `<div class="node${rootClass}${nodeDiffClass}"><div class="line"><span class="tog"></span>${keyHtml}<span class="val-${type}">${val}</span></div></div>`;
198
+ return `<div class="node${rootClass}${nodeDiffClass}"><div class="line"><span class="tog"></span>${keyHtml}<span class="val-${type}">${escapeHtml(val)}</span></div></div>`;
197
199
  }
198
200
 
199
201
  const [open, close] = getBrackets(node.isArray);
@@ -219,7 +221,7 @@ class JsonDiffViewer extends HTMLElement {
219
221
  const dot = hasChildDiff ? `<span class="dot dot-${dotType}"></span>` : "";
220
222
  const nodeDiffClass = hasDiff && !hasChildDiff ? ` ${diffClass}` : "";
221
223
  const toggle = expanded ? "▼" : "▶";
222
- const dataPath = ` data-p="${currentPath}"`;
224
+ const dataPath = ` data-p="${escapeHtml(currentPath)}"`;
223
225
 
224
226
  if (!expanded) {
225
227
  return `<div class="node${rootClass}${nodeDiffClass}"><div class="line"${dataPath}><span class="tog">${toggle}</span>${dot}${keyHtml}<span class="br">${open}</span><span class="preview">${preview}</span><span class="br">${close}</span></div></div>`;