@livingdocs/editable.js 3.0.6 → 3.0.10

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.6",
4
+ "version": "3.0.10",
5
5
  "dependencies": {
6
6
  "@babel/runtime": "^7.13.17",
7
7
  "bowser": "2.11.0",
@@ -74,8 +74,7 @@
74
74
  "test:all": "npm run test:karma -s -- --browsers Chrome,Firefox,Safari",
75
75
  "test:karma": "karma start",
76
76
  "test:watch": "npm run test:karma -s -- --single-run false",
77
- "lint": "eslint --ignore-path .gitignore .",
78
- "postversion": "/release/node_modules/.bin/set-ci"
77
+ "lint": "eslint --ignore-path .gitignore ."
79
78
  },
80
79
  "release": {
81
80
  "extends": "@livingdocs/semantic-release-presets/npm-github-verify"
@@ -84,11 +83,11 @@
84
83
  "isCi": true,
85
84
  "name": "Drone",
86
85
  "service": "drone",
87
- "commit": "371fd4f350c9d109eb57b0e68d2308702137f6c7",
88
- "build": "376",
86
+ "commit": "ba2101da4792e70d5ee82e2d4fb0fb2e3879002a",
87
+ "build": "412",
89
88
  "branch": "master",
90
89
  "isPr": false,
91
90
  "slug": "livingdocsIO/editable.js",
92
- "date": "2021-10-26T16:02:22.840Z"
91
+ "date": "2021-12-29T08:41:38.566Z"
93
92
  }
94
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
@@ -117,7 +117,8 @@ const highlightSupport = {
117
117
  return {
118
118
  start: textRange.start,
119
119
  end: textRange.end,
120
- text: range.text()
120
+ text: range.text(),
121
+ nativeRange: range.nativeRange
121
122
  }
122
123
  },
123
124
 
@@ -9,8 +9,8 @@ export default class WordHighlighting {
9
9
 
10
10
  isElement (obj) {
11
11
  try {
12
- // Using W3 DOM2 (works for FF, Opera and Chrome)
13
- return obj instanceof HTMLElement
12
+ if (!obj) return false
13
+ return obj instanceof obj.ownerDocument?.defaultView.HTMLElement
14
14
  } catch (e) {
15
15
  // Browsers not supporting W3 DOM2 don't have HTMLElement and
16
16
  // an exception is thrown and we end up here. Testing some
@@ -11,17 +11,35 @@ let boundaryMarkerId = 0
11
11
  // (U+FEFF) zero width no-break space
12
12
  const markerTextChar = '\ufeff'
13
13
 
14
+ function isChildOf (parent, possibleChild) {
15
+ for (let i = 0; i < parent.children.length; i++) {
16
+ if (parent.children[i] === possibleChild) return true
17
+ }
18
+ return false
19
+ }
20
+
21
+ function startContainerIsChild (range) {
22
+ const parent = range.endContainer.parentElement
23
+ const possibleChild = range.startContainer.parentElement
24
+ return isChildOf(parent, possibleChild)
25
+ }
26
+
27
+ function endContainerIsChild (range) {
28
+ const parent = range.startContainer.parentElement
29
+ const possibleChild = range.endContainer.parentElement
30
+ return isChildOf(parent, possibleChild)
31
+ }
32
+
14
33
  export function insertRangeBoundaryMarker (range, atStart) {
15
34
  const container = range.commonAncestorContainer
35
+ const directlyBeforeFormatTag = startContainerIsChild(range) && atStart
36
+ const directlyAfterFormatTag = endContainerIsChild(range) && !atStart
37
+
16
38
  // If ownerDocument is null the commonAncestorContainer is window.document
17
39
  if (container.ownerDocument === null || container.ownerDocument === undefined) {
18
40
  error('Cannot save range: range is emtpy')
19
41
  }
20
42
 
21
- // Clone the Range and collapse to the appropriate boundary point
22
- const boundaryRange = range.cloneRange()
23
- boundaryRange.collapse(atStart)
24
-
25
43
  // Create the marker element containing a single
26
44
  // invisible character using DOM methods and insert it
27
45
  const doc = container.ownerDocument.defaultView.document
@@ -32,7 +50,21 @@ export function insertRangeBoundaryMarker (range, atStart) {
32
50
  markerEl.style.display = 'none'
33
51
  markerEl.appendChild(doc.createTextNode(markerTextChar))
34
52
 
35
- boundaryRange.insertNode(markerEl)
53
+ // if another tag is trailing exactly at the start or end, prepend
54
+ // or append the marker element directly on the parent in order to prevent breaking HTML markup.
55
+ if (directlyBeforeFormatTag) {
56
+ range.endContainer.parentElement.insertBefore(markerEl, range.startContainer.parentElement)
57
+ } else if (directlyAfterFormatTag) {
58
+ // emulating insertAfter with nextSibling
59
+ range.startContainer.parentElement.insertBefore(markerEl,
60
+ range.endContainer.parentElement.nextSibling)
61
+ } else {
62
+ // Clone the Range and collapse to the appropriate boundary point
63
+ const boundaryRange = range.cloneRange()
64
+ boundaryRange.collapse(atStart)
65
+ boundaryRange.insertNode(markerEl)
66
+ }
67
+
36
68
  return markerEl
37
69
  }
38
70