@burger-editor/frozen-patty 4.0.0-alpha.4 → 4.0.0-alpha.42

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/README.md CHANGED
@@ -218,7 +218,6 @@ data-field="フィールド名1:属性名1, フィールド名2:属性名2"
218
218
  このHTMLから抽出する際に、以下の特別な考慮がされます:
219
219
 
220
220
  1. 配列データの順序:
221
-
222
221
  - HTML内の`picture`要素では一般的に`source`要素が先に配置され、`img`要素が最後に配置されます
223
222
  - 抽出されるデータの配列では逆順になり、img要素のデータが先、source要素のデータが後
224
223
 
@@ -311,12 +310,10 @@ const data = {
311
310
  適用時には以下の特別処理が行われます:
312
311
 
313
312
  1. 要素の配置と変換:
314
-
315
313
  - 配列の最初の要素(インデックス0)は`img`要素に変換され、DOMでは最後に配置されます
316
314
  - 配列の2番目以降の要素は`source`要素に変換され、配列の逆順にDOMに挿入されます(配列の末尾から先頭方向に)
317
315
 
318
316
  2. 属性の自動変換:
319
-
320
317
  - `img`要素:`srcset`属性は`src`属性に変換され、`sizes`属性は削除されます
321
318
  - `source`要素:`src`属性は`srcset`属性に変換され、`alt`属性と`loading`属性は削除されます
322
319
  - `data-field`属性のシンタックスは、それぞれの要素に対応する属性名に再設定されます
@@ -480,7 +477,6 @@ fpUnsafe.merge(data).toHTML();
480
477
  以下のセキュリティ対策が実装されています:
481
478
 
482
479
  1. 危険な要素の削除:
483
-
484
480
  - `script`, `style`, `template`, `object`, `embed`, `iframe`, `frame`, `frameset`, `applet`
485
481
 
486
482
  2. 危険な属性の削除:
@@ -1,5 +1,6 @@
1
+ import { kebabCase } from '@burger-editor/utils';
2
+ import { normalizeIndent } from './normalize-indent.js';
1
3
  import { parseFields } from './parse-fields.js';
2
- import { kebabCase } from './utils.js';
3
4
  /**
4
5
  * Get value from an element
5
6
  * @param el A target element
@@ -30,7 +31,7 @@ export function getValues(el, convertType = false, attr = 'field', filter) {
30
31
  break;
31
32
  }
32
33
  case 'html': {
33
- value = el.innerHTML.trim();
34
+ value = normalizeIndent(el.innerHTML);
34
35
  break;
35
36
  }
36
37
  case 'style': {
@@ -67,7 +68,7 @@ export function getValues(el, convertType = false, attr = 'field', filter) {
67
68
  value = convertType ? convert(val) : val;
68
69
  }
69
70
  else {
70
- value = el.innerHTML.trim();
71
+ value = normalizeIndent(el.innerHTML);
71
72
  }
72
73
  }
73
74
  if (filter) {
@@ -108,7 +109,8 @@ function getAttribute(el, keyAttr, attr, typeConvert) {
108
109
  // So to get inactive it is necessary to use the "hasAttribute" method.
109
110
  return el.hasAttribute('download') ? el.getAttribute('download') : null;
110
111
  }
111
- case 'href': {
112
+ case 'href':
113
+ case 'src': {
112
114
  // Example: (el as HTMLAnchorElement).href;
113
115
  // Expected: return defined value of plain string
114
116
  return el.getAttribute(keyAttr) ?? '';
@@ -126,7 +128,8 @@ function getAttribute(el, keyAttr, attr, typeConvert) {
126
128
  // For shorthand notation, get value from data-field-* attribute
127
129
  const dataAttr = ['data', attr, kebabCase(keyAttr)].join('-');
128
130
  if (el.hasAttribute(dataAttr)) {
129
- return el.getAttribute(dataAttr) ?? '';
131
+ const value = el.getAttribute(dataAttr);
132
+ return typeConvert ? convert(value) : value;
130
133
  }
131
134
  value = el.getAttribute(keyAttr) ?? null;
132
135
  if ((value === '' || value == null) &&
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Normalizes the indentation of a multi-line HTML string by detecting the indentation
3
+ * of the first line and removing the same amount of leading whitespace (spaces or tabs)
4
+ * from all lines.
5
+ *
6
+ * - The function first removes any leading empty lines (lines containing only line breaks).
7
+ * - It then detects the leading whitespace (indent) from the first line.
8
+ * - For each line, if that indent is present at the start of the line, it is removed.
9
+ * - After normalization, any trailing empty lines (containing only whitespace or line breaks)
10
+ * are also removed.
11
+ * - Supports both LF (\n) and CRLF (\r\n) line endings.
12
+ *
13
+ * This is useful when copying and pasting or serializing innerHTML that includes
14
+ * indentation due to template formatting, so the returned HTML has normalized,
15
+ * consistent indentation and no unwanted leading/trailing blank lines.
16
+ * @param html - The multi-line HTML string to normalize.
17
+ * @returns The HTML string with normalized indentation and no leading/trailing empty lines.
18
+ */
19
+ export declare function normalizeIndent(html: string): string;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Normalizes the indentation of a multi-line HTML string by detecting the indentation
3
+ * of the first line and removing the same amount of leading whitespace (spaces or tabs)
4
+ * from all lines.
5
+ *
6
+ * - The function first removes any leading empty lines (lines containing only line breaks).
7
+ * - It then detects the leading whitespace (indent) from the first line.
8
+ * - For each line, if that indent is present at the start of the line, it is removed.
9
+ * - After normalization, any trailing empty lines (containing only whitespace or line breaks)
10
+ * are also removed.
11
+ * - Supports both LF (\n) and CRLF (\r\n) line endings.
12
+ *
13
+ * This is useful when copying and pasting or serializing innerHTML that includes
14
+ * indentation due to template formatting, so the returned HTML has normalized,
15
+ * consistent indentation and no unwanted leading/trailing blank lines.
16
+ * @param html - The multi-line HTML string to normalize.
17
+ * @returns The HTML string with normalized indentation and no leading/trailing empty lines.
18
+ */
19
+ export function normalizeIndent(html) {
20
+ // Remove leading line breaks
21
+ const trimmedHtml = html.replace(/^(?:\r?\n)+/, '');
22
+ const lines = trimmedHtml.split(/(?<=\r\n|\n|$)/);
23
+ const firstLineIndent = lines[0]?.match(/^\s+/)?.[0] ?? '';
24
+ if (firstLineIndent.length === 0) {
25
+ return trimmedHtml.trimEnd();
26
+ }
27
+ const normalizedLines = lines.map((line) => line.replace(firstLineIndent, ''));
28
+ return normalizedLines.join('').trimEnd();
29
+ }
@@ -1,4 +1,4 @@
1
- import { camelCase } from './utils.js';
1
+ import { camelCase } from '@burger-editor/utils';
2
2
  const cache = new Map();
3
3
  /**
4
4
  *
@@ -1,6 +1,7 @@
1
+ import { kebabCase } from '@burger-editor/utils';
1
2
  import { setValue } from './set-value.js';
2
3
  import { stringifyFields } from './stringify-fields.js';
3
- import { definedFields, flattenData, getFields, hasField, kebabCase, maxLengthOf, removeProp, replaceNode, replaceProp, } from './utils.js';
4
+ import { definedFields, flattenData, getFields, hasField, maxLengthOf, removeProp, replaceNode, replaceProp, } from './utils.js';
4
5
  /**
5
6
  *
6
7
  * @param el
@@ -39,6 +40,8 @@ export function setComponent(el, data, attr, filter, xssSanitize = true) {
39
40
  while (listRoot.firstChild) {
40
41
  listRoot.firstChild.remove();
41
42
  }
43
+ // Track used paths for picture elements to avoid duplicates
44
+ const usedPaths = new Set();
42
45
  for (let i = 0; i < maxLength; i++) {
43
46
  let item = listItem.cloneNode(true);
44
47
  const itemData = flattenData(data, i);
@@ -46,6 +49,14 @@ export function setComponent(el, data, attr, filter, xssSanitize = true) {
46
49
  switch (listRoot.localName) {
47
50
  case 'picture': {
48
51
  reverse = true;
52
+ // Check for duplicate paths and skip if found
53
+ const pathValue = itemData.path;
54
+ if (pathValue != null && usedPaths.has(String(pathValue))) {
55
+ continue;
56
+ }
57
+ if (pathValue != null) {
58
+ usedPaths.add(String(pathValue));
59
+ }
49
60
  let fields = getFields(item, attr);
50
61
  if (i === 0) {
51
62
  // Convert first item to img element
package/dist/set-value.js CHANGED
@@ -1,5 +1,6 @@
1
+ import { kebabCase } from '@burger-editor/utils';
1
2
  import { parseFields } from './parse-fields.js';
2
- import { kebabCase, propInElement, replaceNode, sanitizeAttributeValue, sanitizeHtml, } from './utils.js';
3
+ import { propInElement, replaceNode, sanitizeAttributeValue, sanitizeHtml, } from './utils.js';
3
4
  /**
4
5
  * Set value to an element
5
6
  *
@@ -92,13 +93,7 @@ function set(el, prefix, name, datum, xssSanitize = true) {
92
93
  return;
93
94
  }
94
95
  case 'html': {
95
- // Sanitize HTML if XSS protection is enabled
96
- if (xssSanitize && typeof datum === 'string') {
97
- setContent(el, sanitizeHtml(datum), true, xssSanitize);
98
- }
99
- else {
100
- setContent(el, datum, true, xssSanitize);
101
- }
96
+ setContent(el, datum, true, xssSanitize);
102
97
  return;
103
98
  }
104
99
  case 'node': {
@@ -707,11 +702,27 @@ export function setContent(el, datum, asHtml = true, xssSanitize = true) {
707
702
  el instanceof HTMLOutputElement) {
708
703
  if (el instanceof HTMLInputElement &&
709
704
  (el.type === 'radio' || el.type === 'checkbox')) {
705
+ // Boolean values are always set directly regardless of value attribute
710
706
  if (typeof datum === 'boolean') {
711
707
  el.checked = datum;
712
708
  return;
713
709
  }
714
- el.checked = el.value === `${datum}`;
710
+ // Checkbox/radio with value attribute: traditional value comparison
711
+ if (el.hasAttribute('value')) {
712
+ el.checked = el.value === `${datum}`;
713
+ return;
714
+ }
715
+ // Checkbox without value attribute: switch-like behavior (string boolean processing)
716
+ if (datum === 'true') {
717
+ el.checked = true;
718
+ return;
719
+ }
720
+ if (datum === 'false') {
721
+ el.checked = false;
722
+ return;
723
+ }
724
+ // Other values default to false
725
+ el.checked = false;
715
726
  return;
716
727
  }
717
728
  el.value = `${datum}`;
package/dist/utils.d.ts CHANGED
@@ -10,16 +10,6 @@ export declare const DANGEROUS_ELEMENTS: string[];
10
10
  export declare function arrayToHash(kvs: [keyof FrozenPattyData, PrimitiveDatum, boolean][]): {
11
11
  [index: string]: PrimitiveDatum | PrimitiveDatum[];
12
12
  };
13
- /**
14
- *
15
- * @param str
16
- */
17
- export declare function kebabCase(str: string): string;
18
- /**
19
- *
20
- * @param str
21
- */
22
- export declare function camelCase(str: string): string;
23
13
  /**
24
14
  *
25
15
  * @param el
package/dist/utils.js CHANGED
@@ -36,32 +36,6 @@ export function arrayToHash(kvs) {
36
36
  }
37
37
  return result;
38
38
  }
39
- const kebabCaseCache = new Map();
40
- /**
41
- *
42
- * @param str
43
- */
44
- export function kebabCase(str) {
45
- if (kebabCaseCache.has(str)) {
46
- return kebabCaseCache.get(str);
47
- }
48
- const result = str.replaceAll(/([A-Z])/g, '-$1').toLowerCase();
49
- kebabCaseCache.set(str, result);
50
- return result;
51
- }
52
- const camelCaseCache = new Map();
53
- /**
54
- *
55
- * @param str
56
- */
57
- export function camelCase(str) {
58
- if (camelCaseCache.has(str)) {
59
- return camelCaseCache.get(str);
60
- }
61
- const result = str.replaceAll(/-([a-z])/g, (_, c) => c.toUpperCase());
62
- camelCaseCache.set(str, result);
63
- return result;
64
- }
65
39
  /**
66
40
  *
67
41
  * @param el
@@ -122,22 +96,30 @@ export function sanitizeHtml(html) {
122
96
  sanitized = sanitized.replace(regex, '$1');
123
97
  }
124
98
  // Parse HTML safely using DOM parser for remaining sanitization
125
- const doc = new DOMParser().parseFromString(sanitized, 'text/html');
126
- const body = doc.body;
99
+ const fragment = new Range().createContextualFragment(sanitized);
127
100
  // Remove event handler attributes from all elements
128
- sanitizeElementAndChildren(body);
129
- return body.innerHTML;
101
+ sanitizeElementAndChildren(fragment);
102
+ const newHtml = [...fragment.childNodes]
103
+ .map((node) => {
104
+ if (node instanceof Element) {
105
+ return node.outerHTML;
106
+ }
107
+ return node.textContent ?? node.nodeValue ?? '';
108
+ })
109
+ .join('');
110
+ return newHtml;
130
111
  }
131
112
  /**
132
113
  * Sanitizes an element and all its child elements
133
- * @param element Target element
114
+ * @param node Target element
134
115
  */
135
- function sanitizeElementAndChildren(element) {
136
- // Remove dangerous attributes from current element
137
- sanitizeAttributes(element);
116
+ function sanitizeElementAndChildren(node) {
117
+ if (node instanceof Element) {
118
+ // Remove dangerous attributes from current element
119
+ sanitizeAttributes(node);
120
+ }
138
121
  // Process child elements recursively
139
- const children = element.children;
140
- for (const child of children) {
122
+ for (const child of node.childNodes) {
141
123
  sanitizeElementAndChildren(child);
142
124
  }
143
125
  }
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@burger-editor/frozen-patty",
3
- "version": "4.0.0-alpha.4",
3
+ "version": "4.0.0-alpha.42",
4
4
  "description": "Pure HTML to JSON converter that does not use a template engine.",
5
5
  "author": "D-ZERO",
6
- "license": "MIT",
6
+ "license": "(MIT OR Apache-2.0)",
7
7
  "repository": {
8
8
  "url": "https://github.com/d-zero-dev/BurgerEditor.git"
9
9
  },
10
- "private": false,
11
10
  "publishConfig": {
12
11
  "access": "public"
13
12
  },
@@ -41,5 +40,8 @@
41
40
  "dev": "tsc --watch --project tsconfig.build.json",
42
41
  "clean": "tsc --build --clean"
43
42
  },
44
- "gitHead": "823486d58579275ae556942c5d7a4aef4e1f634e"
43
+ "dependencies": {
44
+ "@burger-editor/utils": "4.0.0-alpha.42"
45
+ },
46
+ "gitHead": "2e7526f26cfb3e09f6dc57b66175dd88d209a45c"
45
47
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-2024 D-ZERO Co., Ltd.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,8 +0,0 @@
1
- /**
2
- *
3
- * @param field
4
- */
5
- export declare function fieldNameParser(field: string): {
6
- fieldName: string;
7
- propName: string;
8
- };
@@ -1,18 +0,0 @@
1
- import { camelCase } from './utils.js';
2
- /**
3
- *
4
- * @param field
5
- */
6
- export function fieldNameParser(field) {
7
- field = field.trim().toLowerCase();
8
- if (!field) {
9
- throw new Error('Field name is empty.');
10
- }
11
- let [fieldName, propName] = field.split(':');
12
- propName = propName?.trim() || '';
13
- fieldName = fieldName?.trim() || propName || field;
14
- return {
15
- fieldName: camelCase(fieldName),
16
- propName,
17
- };
18
- }
@@ -1,14 +0,0 @@
1
- import type { FieldDefinition } from './types.js';
2
- /**
3
- * フィールド名と属性名からフィールド定義文字列を生成します
4
- * @param fieldName フィールド名
5
- * @param propName 属性名(オプション)
6
- * @returns フィールド定義文字列
7
- */
8
- export declare function fieldNameStringify(fieldName: string, propName?: string): string;
9
- /**
10
- * 複数のフィールド定義をカンマ区切りで結合します
11
- * @param fields フィールド定義の配列
12
- * @returns カンマ区切りのフィールド定義文字列
13
- */
14
- export declare function fieldsStringify(fields: readonly FieldDefinition[]): string;
@@ -1,26 +0,0 @@
1
- /**
2
- * フィールド名と属性名からフィールド定義文字列を生成します
3
- * @param fieldName フィールド名
4
- * @param propName 属性名(オプション)
5
- * @returns フィールド定義文字列
6
- */
7
- export function fieldNameStringify(fieldName, propName) {
8
- if (!propName) {
9
- return fieldName;
10
- }
11
- // fieldNameとpropNameが同じ場合は省略記法を使用
12
- if (fieldName === propName) {
13
- return `:${propName}`;
14
- }
15
- return `${fieldName}:${propName}`;
16
- }
17
- /**
18
- * 複数のフィールド定義をカンマ区切りで結合します
19
- * @param fields フィールド定義の配列
20
- * @returns カンマ区切りのフィールド定義文字列
21
- */
22
- export function fieldsStringify(fields) {
23
- return fields
24
- .map((field) => fieldNameStringify(field.fieldName, field.propName))
25
- .join(', ');
26
- }
package/dist/get.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { Filter, FrozenPattyData } from './types.js';
2
- /**
3
- *
4
- * @param el
5
- * @param attr
6
- * @param typeConvert
7
- * @param filter
8
- */
9
- export default function (el: Element, attr: string, typeConvert: boolean, filter?: Filter): FrozenPattyData;
package/dist/get.js DELETED
@@ -1,19 +0,0 @@
1
- import { getValues } from './get-values.js';
2
- import { arrayToHash } from './utils.js';
3
- /**
4
- *
5
- * @param el
6
- * @param attr
7
- * @param typeConvert
8
- * @param filter
9
- */
10
- export default function (el, attr, typeConvert, filter) {
11
- // eslint-disable-next-line unicorn/prefer-spread
12
- const filedElements = Array.from(el.querySelectorAll(`[data-${attr}]`));
13
- let values = [];
14
- for (const _el of filedElements) {
15
- values = [...values, ...getValues(_el, typeConvert, attr, filter)];
16
- }
17
- const result = arrayToHash(values);
18
- return result;
19
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * picture要素の特別な処理を行います
3
- * @param pictureElement picture要素
4
- * @param attr
5
- */
6
- export declare function handlePictureElement(pictureElement: HTMLPictureElement, attr: string): void;
@@ -1,31 +0,0 @@
1
- import { parseFields } from './parse-fields.js';
2
- import { setValue } from './set-value.js';
3
- /**
4
- * picture要素の特別な処理を行います
5
- * @param pictureElement picture要素
6
- * @param attr
7
- */
8
- export function handlePictureElement(pictureElement, attr) {
9
- const children = [...pictureElement.children];
10
- const fieldAttrName = `data-${attr}`;
11
- for (const [i, child] of children.entries()) {
12
- const fields = parseFields(child.getAttribute(fieldAttrName) ?? '');
13
- const newChild = child.cloneNode(false);
14
- child.remove();
15
- if (i > 0) {
16
- setValue(newChild, 'node', 'source', attr);
17
- }
18
- switch (newChild.localName) {
19
- case 'source': {
20
- // newChild.removeAttribute('alt');
21
- // setValue(newChild, name, attr.value);
22
- break;
23
- }
24
- case 'img': {
25
- //
26
- break;
27
- }
28
- }
29
- pictureElement.insertBefore(newChild, pictureElement.firstChild);
30
- }
31
- }
@@ -1,10 +0,0 @@
1
- import type { Filter, FrozenPattyData } from './types.js';
2
- /**
3
- *
4
- * @param el
5
- * @param data
6
- * @param attr
7
- * @param filter
8
- * @param xssSanitize Enable XSS protection
9
- */
10
- export declare function setComponent(el: Element, data: FrozenPattyData, attr: string, filter?: Filter, xssSanitize?: boolean): Element;
@@ -1,71 +0,0 @@
1
- import { setValue } from './set-value.js';
2
- import { stringifyFields } from './stringify-fields.js';
3
- import { definedFields, flattenData, getFields, hasField, kebabCase, maxLengthOf, removeProp, replaceNode, replaceProp, } from './utils.js';
4
- /**
5
- *
6
- * @param el
7
- * @param data
8
- * @param attr
9
- * @param filter
10
- * @param xssSanitize Enable XSS protection
11
- */
12
- export function setComponent(el, data, attr, filter, xssSanitize = true) {
13
- el = el.cloneNode(true);
14
- for (const dataKeyName in data) {
15
- const datum = data[dataKeyName];
16
- if (Array.isArray(datum)) {
17
- continue;
18
- }
19
- const targetList = [
20
- // 自身を含める
21
- el,
22
- // パフォーマンスのために雑なフィルタリングを行う
23
- ...el.querySelectorAll(`[data-${attr}*="${kebabCase(dataKeyName)}"]`),
24
- ]
25
- // より正確なフィルタリングを行う
26
- .filter((el) => hasField(el, attr, dataKeyName));
27
- for (const targetEl of targetList) {
28
- setValue(targetEl, dataKeyName, datum, attr, filter, xssSanitize);
29
- }
30
- }
31
- for (const listRoot of el.querySelectorAll(`[data-${attr}-list]`)) {
32
- const decendants = listRoot.querySelectorAll('*');
33
- const definedFieldsOfDecendants = new Set([...decendants].flatMap((el) => definedFields(el, attr)));
34
- const maxLength = maxLengthOf(data, [...definedFieldsOfDecendants]);
35
- const listItem = listRoot.children[0]?.cloneNode(true);
36
- if (!listItem) {
37
- continue;
38
- }
39
- while (listRoot.firstChild) {
40
- listRoot.firstChild.remove();
41
- }
42
- for (let i = 0; i < maxLength; i++) {
43
- let item = listItem.cloneNode(true);
44
- const itemData = flattenData(data, i);
45
- let reverse = false;
46
- switch (listRoot.localName) {
47
- case 'picture': {
48
- reverse = true;
49
- if (i > 0) {
50
- // 2つ目以降はsource要素に変換
51
- let fields = getFields(item, attr);
52
- item = replaceNode(item, 'source', attr, xssSanitize);
53
- fields = replaceProp(fields, 'src', 'srcset');
54
- fields = removeProp(fields, 'alt');
55
- const fieldQuery = stringifyFields(fields);
56
- item.setAttribute(`data-${attr}`, fieldQuery);
57
- }
58
- break;
59
- }
60
- }
61
- const newEl = setComponent(item, itemData, attr, filter, xssSanitize);
62
- if (reverse) {
63
- listRoot.prepend(newEl);
64
- }
65
- else {
66
- listRoot.append(newEl);
67
- }
68
- }
69
- }
70
- return el;
71
- }
package/dist/set.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { Filter, FrozenPattyData } from './types.js';
2
- /**
3
- *
4
- * @param el
5
- * @param data
6
- * @param attr
7
- * @param filter
8
- * @param xssSanitize Enable XSS protection
9
- */
10
- export default function (el: Element, data: FrozenPattyData, attr: string, filter?: Filter, xssSanitize?: boolean): Element;
package/dist/set.js DELETED
@@ -1,61 +0,0 @@
1
- import { setValue } from './set-value.js';
2
- import { kebabCase } from './utils.js';
3
- /**
4
- *
5
- * @param el
6
- * @param data
7
- * @param attr
8
- * @param filter
9
- * @param xssSanitize Enable XSS protection
10
- */
11
- export default function (el, data, attr, filter, xssSanitize = true) {
12
- el = el.cloneNode(true);
13
- for (const dataKeyName in data) {
14
- if (dataKeyName in data) {
15
- const datum = data[dataKeyName];
16
- const selector = `[data-${attr}*="${kebabCase(dataKeyName)}"]`;
17
- // eslint-disable-next-line unicorn/prefer-spread
18
- const targetList = Array.from(el.querySelectorAll(selector));
19
- if (Array.isArray(datum)) {
20
- const targetEl = targetList[0];
21
- if (!targetEl) {
22
- continue;
23
- }
24
- const listRoot = targetEl.closest(`[data-${attr}-list]`);
25
- if (!listRoot || (listRoot && listRoot.children.length === 0)) {
26
- continue;
27
- }
28
- const listItem = listRoot.children[0]?.cloneNode(true);
29
- while (listItem && datum.length > listRoot.children.length) {
30
- listRoot.append(listItem.cloneNode(true));
31
- }
32
- // eslint-disable-next-line unicorn/prefer-spread
33
- const newChildren = Array.from(listRoot.querySelectorAll(selector));
34
- // eslint-disable-next-line unicorn/prefer-spread
35
- const oldChildList = Array.from(listRoot.children);
36
- let deleteNodeList = [];
37
- for (const [i, child] of [...newChildren].entries()) {
38
- if (datum[i] == null) {
39
- setValue(child, dataKeyName, '', attr, filter, xssSanitize);
40
- const oldChild = oldChildList[i];
41
- if (oldChild) {
42
- deleteNodeList.push(oldChild);
43
- }
44
- }
45
- else {
46
- setValue(child, dataKeyName, datum[i], attr, filter, xssSanitize);
47
- deleteNodeList = [];
48
- }
49
- }
50
- for (const node of deleteNodeList)
51
- node.remove();
52
- }
53
- else {
54
- for (const [, targetEl] of [...targetList].entries()) {
55
- setValue(targetEl, dataKeyName, datum, attr, filter, xssSanitize);
56
- }
57
- }
58
- }
59
- }
60
- return el;
61
- }