@zipify/wysiwyg 1.0.0-dev.58 → 1.0.0-dev.59
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,6 +1,7 @@
|
|
|
1
1
|
import { ContextWindow } from './ContextWidnow';
|
|
2
2
|
|
|
3
3
|
export class ContentNormalizer {
|
|
4
|
+
static PARSER = new DOMParser();
|
|
4
5
|
static BLOCK_STYLES = ['text-align', 'line-height'];
|
|
5
6
|
static BLOCK_NODE_NAMES = ['P', 'H1', 'H2', 'H3', 'H4'];
|
|
6
7
|
|
|
@@ -20,11 +21,17 @@ export class ContentNormalizer {
|
|
|
20
21
|
];
|
|
21
22
|
|
|
22
23
|
static normalize(content) {
|
|
23
|
-
|
|
24
|
+
const options = {
|
|
25
|
+
content,
|
|
26
|
+
parser: ContentNormalizer.PARSER
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return new ContentNormalizer(options).normalize();
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
constructor(content) {
|
|
32
|
+
constructor({ content, parser }) {
|
|
27
33
|
this._content = content;
|
|
34
|
+
this._parser = parser;
|
|
28
35
|
this._dom = null;
|
|
29
36
|
}
|
|
30
37
|
|
|
@@ -36,7 +43,7 @@ export class ContentNormalizer {
|
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
_normalizeTextContent() {
|
|
39
|
-
this._dom =
|
|
46
|
+
this._dom = this._parser.parseFromString(this._content, 'text/html');
|
|
40
47
|
|
|
41
48
|
this._iterateNodes(this._removeEmptyNodes, this._isBlockNode);
|
|
42
49
|
this._iterateNodes(this._normalizeListItems, (node) => node.tagName === 'LI');
|
|
@@ -59,7 +66,7 @@ export class ContentNormalizer {
|
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
_removeEmptyNodes(node) {
|
|
62
|
-
if (node.
|
|
69
|
+
if (!node.innerHTML.replace(/\n/g, '').trim()) {
|
|
63
70
|
node.remove();
|
|
64
71
|
}
|
|
65
72
|
}
|
|
@@ -74,6 +81,8 @@ export class ContentNormalizer {
|
|
|
74
81
|
fragment.append(node);
|
|
75
82
|
};
|
|
76
83
|
|
|
84
|
+
this._assignElementProperties(itemEl, itemEl.parentElement, ContentNormalizer.BLOCK_STYLES);
|
|
85
|
+
|
|
77
86
|
for (const node of children) {
|
|
78
87
|
if (node.tagName === 'P') {
|
|
79
88
|
append(node);
|
|
@@ -79,6 +79,20 @@ describe('normalize text content', () => {
|
|
|
79
79
|
expect(ContentNormalizer.normalize(input)).toBe(output);
|
|
80
80
|
});
|
|
81
81
|
|
|
82
|
+
test('should ignore space only nodes', () => {
|
|
83
|
+
const input = '<p>lorem ipsum 1</p><p> </p><p>lorem ipsum 2</p>';
|
|
84
|
+
const output = '<p>lorem ipsum 1</p><p>lorem ipsum 2</p>';
|
|
85
|
+
|
|
86
|
+
expect(ContentNormalizer.normalize(input)).toBe(output);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('should ignore newline chapters only nodes', () => {
|
|
90
|
+
const input = '<p>lorem ipsum 1</p><p>\n</p><p>lorem ipsum 2</p>';
|
|
91
|
+
const output = '<p>lorem ipsum 1</p><p>lorem ipsum 2</p>';
|
|
92
|
+
|
|
93
|
+
expect(ContentNormalizer.normalize(input)).toBe(output);
|
|
94
|
+
});
|
|
95
|
+
|
|
82
96
|
test('should not ignore setting', () => {
|
|
83
97
|
const input = '<p style="text-decoration-line: underline;">lorem ipsum</p>';
|
|
84
98
|
const output = '<p><span style="text-decoration-line: underline;">lorem ipsum</span></p>';
|
|
@@ -106,4 +120,11 @@ describe('normalize text content', () => {
|
|
|
106
120
|
|
|
107
121
|
expect(ContentNormalizer.normalize(input)).toBe(output);
|
|
108
122
|
});
|
|
123
|
+
|
|
124
|
+
test('should assign block styles from list to paragraph', () => {
|
|
125
|
+
const input = '<ul style="line-height: 2;"><li>lorem ipsum</li></ul>';
|
|
126
|
+
const output = '<ul style="line-height: 2;"><li><p style="line-height: 2;">lorem ipsum</p></li></ul>';
|
|
127
|
+
|
|
128
|
+
expect(ContentNormalizer.normalize(input)).toBe(output);
|
|
129
|
+
});
|
|
109
130
|
});
|