@burger-editor/utils 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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/normalize-html.d.ts +8 -0
- package/dist/normalize-html.js +109 -0
- package/dist/normalize-indent.d.ts +8 -0
- package/dist/normalize-indent.js +18 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTMLの構造を正規化して比較する
|
|
3
|
+
* 空白や属性順序の違いを無視し、タグ構造のみを比較する
|
|
4
|
+
* @param html1 比較対象のHTML文字列1
|
|
5
|
+
* @param html2 比較対象のHTML文字列2
|
|
6
|
+
* @returns 構造が同じ場合true、異なる場合false
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeHtmlStructure(html1, html2) {
|
|
9
|
+
const doc1 = new Range().createContextualFragment(html1);
|
|
10
|
+
const doc2 = new Range().createContextualFragment(html2);
|
|
11
|
+
return compareElements(doc1, doc2);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 2つの要素の構造を比較する
|
|
15
|
+
* @param el1 比較対象の要素1
|
|
16
|
+
* @param el2 比較対象の要素2
|
|
17
|
+
* @returns 構造が同じ場合true、異なる場合false
|
|
18
|
+
*/
|
|
19
|
+
function compareElements(el1, el2) {
|
|
20
|
+
// 子要素の数を取得(テキストノードは無視)
|
|
21
|
+
const children1 = [...el1.children];
|
|
22
|
+
const children2 = [...el2.children];
|
|
23
|
+
// 子要素の数が異なる場合は構造が異なる
|
|
24
|
+
if (children1.length !== children2.length) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// 子要素がない場合(テキストのみまたは空要素)
|
|
28
|
+
if (children1.length === 0) {
|
|
29
|
+
// テキスト内容を正規化して比較
|
|
30
|
+
const text1 = normalizeText(el1.textContent ?? '');
|
|
31
|
+
const text2 = normalizeText(el2.textContent ?? '');
|
|
32
|
+
return text1 === text2;
|
|
33
|
+
}
|
|
34
|
+
// 各子要素を比較
|
|
35
|
+
for (const [i, child1] of children1.entries()) {
|
|
36
|
+
const child2 = children2[i];
|
|
37
|
+
if (!child1 || !child2 || !compareElementStructure(child1, child2)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 2つの要素の構造を詳細に比較する
|
|
45
|
+
* @param el1 比較対象の要素1
|
|
46
|
+
* @param el2 比較対象の要素2
|
|
47
|
+
* @returns 構造が同じ場合true、異なる場合false
|
|
48
|
+
*/
|
|
49
|
+
function compareElementStructure(el1, el2) {
|
|
50
|
+
// タグ名が異なる場合は構造が異なる
|
|
51
|
+
if (el1.tagName !== el2.tagName) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
// 属性を比較(順序は無視)
|
|
55
|
+
const attrs1 = getNormalizedAttributes(el1);
|
|
56
|
+
const attrs2 = getNormalizedAttributes(el2);
|
|
57
|
+
if (!compareAttributes(attrs1, attrs2)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
// 子要素を再帰的に比較
|
|
61
|
+
return compareElements(el1, el2);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 要素の属性を正規化して取得する
|
|
65
|
+
* @param el 対象の要素
|
|
66
|
+
* @returns 属性名をキー、属性値を値とするオブジェクト
|
|
67
|
+
*/
|
|
68
|
+
function getNormalizedAttributes(el) {
|
|
69
|
+
const attrs = {};
|
|
70
|
+
for (const attr of el.attributes) {
|
|
71
|
+
attrs[attr.name] = attr.value;
|
|
72
|
+
}
|
|
73
|
+
return attrs;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 2つの属性オブジェクトを比較する
|
|
77
|
+
* @param attrs1 比較対象の属性1
|
|
78
|
+
* @param attrs2 比較対象の属性2
|
|
79
|
+
* @returns 属性が同じ場合true、異なる場合false
|
|
80
|
+
*/
|
|
81
|
+
function compareAttributes(attrs1, attrs2) {
|
|
82
|
+
const keys1 = Object.keys(attrs1).sort();
|
|
83
|
+
const keys2 = Object.keys(attrs2).sort();
|
|
84
|
+
// 属性名の数が異なる場合は異なる
|
|
85
|
+
if (keys1.length !== keys2.length) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
// 各属性を比較
|
|
89
|
+
for (const [i, key1] of keys1.entries()) {
|
|
90
|
+
const key2 = keys2[i];
|
|
91
|
+
// 属性名が異なる場合は異なる
|
|
92
|
+
if (key1 !== key2) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
// 属性値が異なる場合は異なる
|
|
96
|
+
if (attrs1[key1] !== attrs2[key2]) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* テキストを正規化する(空白文字を統一)
|
|
104
|
+
* @param text 対象のテキスト
|
|
105
|
+
* @returns 正規化されたテキスト
|
|
106
|
+
*/
|
|
107
|
+
function normalizeText(text) {
|
|
108
|
+
return text.replaceAll(/\s+/g, ' ').trim();
|
|
109
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTMLの先頭行のインデントを検出し、全行から同じインデントを削除する
|
|
3
|
+
*
|
|
4
|
+
* CRLF (\r\n) と LF (\n) の両方に対応
|
|
5
|
+
* @param html - 正規化するHTML文字列
|
|
6
|
+
* @returns インデントが正規化され、前後の空白行が削除されたHTML文字列
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeIndent(html) {
|
|
9
|
+
// 先頭の改行を削除
|
|
10
|
+
const trimmedHtml = html.replace(/^(?:\r?\n)+/, '');
|
|
11
|
+
const lines = trimmedHtml.split(/\r?\n/);
|
|
12
|
+
const firstLineIndent = lines[0]?.match(/^\s+/)?.[0] ?? '';
|
|
13
|
+
if (firstLineIndent.length === 0) {
|
|
14
|
+
return trimmedHtml;
|
|
15
|
+
}
|
|
16
|
+
const normalizedLines = lines.map((line) => line.replace(firstLineIndent, ''));
|
|
17
|
+
return normalizedLines.join('\n');
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@burger-editor/utils",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.42",
|
|
4
4
|
"description": "BurgerEditor Editor Utility Functions",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "(MIT OR Apache-2.0)",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/turndown": "5.0.6"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "2e7526f26cfb3e09f6dc57b66175dd88d209a45c"
|
|
37
37
|
}
|