@livingdocs/editable.js 3.0.7 → 3.0.8

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": "3.0.7",
4
+ "version": "3.0.8",
5
5
  "dependencies": {
6
6
  "@babel/runtime": "^7.13.17",
7
7
  "bowser": "2.11.0",
@@ -83,11 +83,11 @@
83
83
  "isCi": true,
84
84
  "name": "Drone",
85
85
  "service": "drone",
86
- "commit": "71d6d7f7aec6d75122c779631b3873f6f8ebb816",
87
- "build": "384",
86
+ "commit": "e4c7383e5620ccbb13b71e155a8f26d5f245b64d",
87
+ "build": "398",
88
88
  "branch": "master",
89
89
  "isPr": false,
90
90
  "slug": "livingdocsIO/editable.js",
91
- "date": "2021-11-17T10:32:17.555Z"
91
+ "date": "2021-12-15T14:59:21.802Z"
92
92
  }
93
93
  }
package/src/content.js CHANGED
@@ -29,6 +29,12 @@ export function tidyHtml (element) {
29
29
  export function normalizeTags (element) {
30
30
  const fragment = document.createDocumentFragment()
31
31
 
32
+ // Remove line breaks at the beginning of a content block
33
+ removeWhitespaces(element, 'firstChild')
34
+
35
+ // Remove line breaks at the end of a content block
36
+ removeWhitespaces(element, 'lastChild')
37
+
32
38
  for (const node of element.childNodes) {
33
39
  // skip empty tags, so they'll get removed
34
40
  if (node.nodeName !== 'BR' && !node.textContent) continue
@@ -136,7 +142,7 @@ export function cloneRangeContents (range) {
136
142
  return fragment
137
143
  }
138
144
 
139
- function removeWhitespaces (node, type) {
145
+ function removeWhitespaces (node, type, firstCall = true) {
140
146
  let elem
141
147
  while ((elem = node[type])) {
142
148
  if (elem.nodeType === nodeType.textNode) {
@@ -145,10 +151,17 @@ function removeWhitespaces (node, type) {
145
151
  } else if (elem.nodeName === 'BR') {
146
152
  elem.remove()
147
153
  } else {
148
- if (elem[type]) removeWhitespaces(elem, type)
154
+ if (elem[type]) removeWhitespaces(elem, type, false)
149
155
  break
150
156
  }
151
157
  }
158
+
159
+ if (!firstCall) return
160
+ elem = node[type]
161
+ if (elem?.nodeType !== nodeType.textNode) return
162
+ // Remove whitespaces at the end or start of a block with content
163
+ // e.g. ' Hello world' > 'Hello World'
164
+ elem.textContent = elem.textContent.replace(type.startsWith('last') ? /\s+$/ : /^\s+/, '')
152
165
  }
153
166
 
154
167
  // Remove elements that were inserted for internal or user interface purposes