@livingdocs/editable.js 5.0.6 → 5.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@livingdocs/editable.js",
3
3
  "description": "Friendly contenteditable API",
4
- "version": "5.0.6",
4
+ "version": "5.0.7",
5
5
  "dependencies": {
6
6
  "@babel/runtime": "^7.16.7",
7
7
  "lodash-es": "^4.17.21"
@@ -39,7 +39,7 @@
39
39
  "url-loader": "^4.1.1",
40
40
  "webpack": "^5.68.0",
41
41
  "webpack-cli": "^5.0.0",
42
- "webpack-dev-server": "^4.7.3"
42
+ "webpack-dev-server": "^5.0.0"
43
43
  },
44
44
  "engines": {
45
45
  "node": ">=16",
@@ -80,11 +80,11 @@
80
80
  "isCi": true,
81
81
  "name": "Drone",
82
82
  "service": "drone",
83
- "commit": "0c9f2822e9727cc03b715b7574cdf076bead4400",
84
- "build": "1438",
83
+ "commit": "c234c7ebb846f50a490747cca6d97093e4447d58",
84
+ "build": "1475",
85
85
  "branch": "master",
86
86
  "isPr": false,
87
87
  "slug": "livingdocsIO/editable.js",
88
- "date": "2024-01-31T09:15:54.845Z"
88
+ "date": "2024-02-28T13:53:13.641Z"
89
89
  }
90
90
  }
package/src/content.js CHANGED
@@ -15,6 +15,10 @@ const zeroWidthSpace = /\u200B/g
15
15
  const zeroWidthNonBreakingSpace = /\uFEFF/g
16
16
  const whitespaceExceptSpace = /[^\S ]/g
17
17
 
18
+ const everythingWhitespace = /^\s+$/
19
+ const leadingWhitespace = /^\s+/
20
+ const trailingWhitespace = /\s+$/
21
+
18
22
  // Clean up the Html.
19
23
  export function tidyHtml (element) {
20
24
  // if (element.normalize) element.normalize()
@@ -143,25 +147,34 @@ export function cloneRangeContents (range) {
143
147
 
144
148
  function removeWhitespaces (node, type, firstCall = true) {
145
149
  let elem
150
+ // loop through all children:
151
+ // from left to right if type = 'firstChild',
152
+ // from right to left if type = 'lastChild'
146
153
  while ((elem = node[type])) {
147
154
  if (elem.nodeType === nodeType.textNode) {
148
- if (/^\s+$/.test(elem.textContent)) node.removeChild(elem)
155
+ // Just remove text nodes if they consist only of whitespace
156
+ if (everythingWhitespace.test(elem.textContent)) node.removeChild(elem)
149
157
  else break
150
158
  } else if (elem.nodeName === 'BR') {
151
159
  elem.remove()
152
160
  } else {
161
+ // For element nodes (e.g. <strong> tags), we repeat the logic recursively
162
+ // to remove empty text nodes or <br> tags.
153
163
  if (elem[type]) removeWhitespaces(elem, type, false)
154
164
  break
155
165
  }
156
166
  }
157
167
 
168
+ // Text nodes with leading/trailing whitespace can be trimmed if config allows.
169
+ // We only do it to the outermost text node. Once a text was wrapped in
170
+ // another element, we preserve the whitespace.
158
171
  if (!firstCall) return
159
172
  elem = node[type]
160
173
  if (elem?.nodeType !== nodeType.textNode) return
161
174
  // Remove whitespaces at the end or start of a block with content
162
175
  // e.g. ' Hello world' > 'Hello World'
163
176
  if (config.trimLeadingAndTrailingWhitespaces) {
164
- elem.textContent = elem.textContent.replace(type.startsWith('last') ? / +$/ : /^ +/, '')
177
+ elem.textContent = elem.textContent.replace(type.startsWith('last') ? trailingWhitespace : leadingWhitespace, '')
165
178
  }
166
179
  }
167
180