@lesjoursfr/edith 1.1.0 → 1.1.1
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 +1 -1
- package/src/core/dom.js +25 -0
- package/src/core/edit.js +2 -1
- package/src/ui/editor.js +5 -2
package/package.json
CHANGED
package/src/core/dom.js
CHANGED
|
@@ -92,6 +92,31 @@ export function textifyNode(node) {
|
|
|
92
92
|
return newNode;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Know if a tag si a self-closing tag
|
|
97
|
+
* @param {String} tagName
|
|
98
|
+
* @returns {Boolean}
|
|
99
|
+
*/
|
|
100
|
+
export function isSelfClosing(tagName) {
|
|
101
|
+
return [
|
|
102
|
+
"AREA",
|
|
103
|
+
"BASE",
|
|
104
|
+
"BR",
|
|
105
|
+
"COL",
|
|
106
|
+
"EMBED",
|
|
107
|
+
"HR",
|
|
108
|
+
"IMG",
|
|
109
|
+
"INPUT",
|
|
110
|
+
"KEYGEN",
|
|
111
|
+
"LINK",
|
|
112
|
+
"META",
|
|
113
|
+
"PARAM",
|
|
114
|
+
"SOURCE",
|
|
115
|
+
"TRACK",
|
|
116
|
+
"WBR",
|
|
117
|
+
].includes(tagName);
|
|
118
|
+
}
|
|
119
|
+
|
|
95
120
|
/**
|
|
96
121
|
* Remove all node's child nodes that pass the test implemented by the provided function.
|
|
97
122
|
* @param {Node} node the node to process
|
package/src/core/edit.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
createNodeWith,
|
|
7
7
|
unwrapNode,
|
|
8
8
|
textifyNode,
|
|
9
|
+
isSelfClosing,
|
|
9
10
|
removeNodes,
|
|
10
11
|
removeEmptyTextNodes,
|
|
11
12
|
removeCommentNodes,
|
|
@@ -170,7 +171,7 @@ export function wrapInsideTag(tag, options = {}) {
|
|
|
170
171
|
range.insertNode(node);
|
|
171
172
|
|
|
172
173
|
// Remove empty tags
|
|
173
|
-
removeNodes(parent, (el) => el.textContent.length === 0);
|
|
174
|
+
removeNodes(parent, (el) => !isSelfClosing(el.tagName) && el.textContent.length === 0);
|
|
174
175
|
|
|
175
176
|
// Return the node
|
|
176
177
|
selectNodeContents(node);
|
package/src/ui/editor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EditorView, basicSetup } from "codemirror";
|
|
2
2
|
import { html } from "@codemirror/lang-html";
|
|
3
|
-
import { hasClass, hasTagName, createNodeWith, removeNodesRecursively } from "../core/dom.js";
|
|
3
|
+
import { hasClass, hasTagName, createNodeWith, isSelfClosing, removeNodesRecursively } from "../core/dom.js";
|
|
4
4
|
import {
|
|
5
5
|
wrapInsideTag,
|
|
6
6
|
replaceSelectionByHtml,
|
|
@@ -103,7 +103,10 @@ EdithEditor.prototype.getContent = function () {
|
|
|
103
103
|
|
|
104
104
|
// Remove empty tags
|
|
105
105
|
const placeholder = createNodeWith("div", { innerHTML: code });
|
|
106
|
-
removeNodesRecursively(
|
|
106
|
+
removeNodesRecursively(
|
|
107
|
+
placeholder,
|
|
108
|
+
(el) => el.nodeType === Node.ELEMENT_NODE && !isSelfClosing(el.tagName) && el.textContent.length === 0
|
|
109
|
+
);
|
|
107
110
|
|
|
108
111
|
// Return clean code
|
|
109
112
|
return placeholder.innerHTML
|