@burger-editor/frozen-patty 4.0.0-alpha.34 → 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/dist/get-values.js +3 -2
- package/dist/normalize-indent.d.ts +19 -0
- package/dist/normalize-indent.js +29 -0
- package/dist/utils.js +18 -10
- package/package.json +3 -3
package/dist/get-values.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { kebabCase } from '@burger-editor/utils';
|
|
2
|
+
import { normalizeIndent } from './normalize-indent.js';
|
|
2
3
|
import { parseFields } from './parse-fields.js';
|
|
3
4
|
/**
|
|
4
5
|
* Get value from an 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
|
|
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
|
|
71
|
+
value = normalizeIndent(el.innerHTML);
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
if (filter) {
|
|
@@ -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
|
+
}
|
package/dist/utils.js
CHANGED
|
@@ -96,22 +96,30 @@ export function sanitizeHtml(html) {
|
|
|
96
96
|
sanitized = sanitized.replace(regex, '$1');
|
|
97
97
|
}
|
|
98
98
|
// Parse HTML safely using DOM parser for remaining sanitization
|
|
99
|
-
const
|
|
100
|
-
const body = doc.body;
|
|
99
|
+
const fragment = new Range().createContextualFragment(sanitized);
|
|
101
100
|
// Remove event handler attributes from all elements
|
|
102
|
-
sanitizeElementAndChildren(
|
|
103
|
-
|
|
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;
|
|
104
111
|
}
|
|
105
112
|
/**
|
|
106
113
|
* Sanitizes an element and all its child elements
|
|
107
|
-
* @param
|
|
114
|
+
* @param node Target element
|
|
108
115
|
*/
|
|
109
|
-
function sanitizeElementAndChildren(
|
|
110
|
-
|
|
111
|
-
|
|
116
|
+
function sanitizeElementAndChildren(node) {
|
|
117
|
+
if (node instanceof Element) {
|
|
118
|
+
// Remove dangerous attributes from current element
|
|
119
|
+
sanitizeAttributes(node);
|
|
120
|
+
}
|
|
112
121
|
// Process child elements recursively
|
|
113
|
-
const
|
|
114
|
-
for (const child of children) {
|
|
122
|
+
for (const child of node.childNodes) {
|
|
115
123
|
sanitizeElementAndChildren(child);
|
|
116
124
|
}
|
|
117
125
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@burger-editor/frozen-patty",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
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
6
|
"license": "(MIT OR Apache-2.0)",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"clean": "tsc --build --clean"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@burger-editor/utils": "4.0.0-alpha.
|
|
44
|
+
"@burger-editor/utils": "4.0.0-alpha.42"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "2e7526f26cfb3e09f6dc57b66175dd88d209a45c"
|
|
47
47
|
}
|