@livingdocs/editable.js 3.0.9 → 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.9",
4
+ "version": "3.0.10",
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": "a8c9c2c907cca213099b512c9b0c4321bc4e2b9a",
87
- "build": "408",
86
+ "commit": "ba2101da4792e70d5ee82e2d4fb0fb2e3879002a",
87
+ "build": "412",
88
88
  "branch": "master",
89
89
  "isPr": false,
90
90
  "slug": "livingdocsIO/editable.js",
91
- "date": "2021-12-28T11:06:57.120Z"
91
+ "date": "2021-12-29T08:41:38.566Z"
92
92
  }
93
93
  }
@@ -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