|
| 1 | +*For this library, the following would be true:* |
| 2 | + |
| 3 | +Consider you have this `Group` setup: |
| 4 | +```js |
| 5 | +const group = new Group |
| 6 | +group.append("A", "B", "C") |
| 7 | +``` |
| 8 | +Then the diagram would be: |
| 9 | +```js |
| 10 | +Group |
| 11 | + ├─ A |
| 12 | + ├─ B |
| 13 | + └─ C |
| 14 | + |
| 15 | +document |
| 16 | +└─ body // No children. |
| 17 | +``` |
| 18 | + |
| 19 | +Adding a group to a parent (that is attached to `document`, it is required due to `connectedCallback` reliance) would look like that: |
| 20 | +```js |
| 21 | +const parent = document.createElement("div") |
| 22 | +parent.append(group) |
| 23 | + |
| 24 | +// document.body.append(parent) // The order doesn't matter, though for group to appear in the `parent`, must be attached to a `document` in some way. |
| 25 | +``` |
| 26 | +This diagram would represent it: |
| 27 | +```js |
| 28 | +Group |
| 29 | + ├─ A |
| 30 | + ├─ B |
| 31 | + └─ C |
| 32 | + |
| 33 | +document |
| 34 | +└─ body |
| 35 | + ├─ A |
| 36 | + ├─ B |
| 37 | + └─ C |
| 38 | +``` |
| 39 | + |
| 40 | +Let's break down this diagram in terms of properties (interfaces): |
| 41 | + |
| 42 | +- Accessing any properties of `group` will (*should*) be 100% same as `DocumentFragment` in the moment it is not yet connected anywhere (which flushes its children), expect those that are `null` by design like `nextSibling` or `parent`. |
| 43 | +- `childNodes` of `group` are both in `group` and `document.body` |
| 44 | +```js |
| 45 | +group.childNodes // ["A", "B", "C"] |
| 46 | +document.body.childNodes // ["A", "B", "C"] |
| 47 | +``` |
| 48 | +- `parent` return the parent where `group` was previously attached to - the same with other properties. |
| 49 | + |
| 50 | +In short, `group` behaves just like a normal `ParentNode`. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +That was a simple part, let's see what probably confuses people: |
| 55 | + |
| 56 | +- `group` node itself can't be find in the `document`, even if attached and `parent` property shows it is. :exploding_head: |
| 57 | +- `group` node doesn't have a wrapper and is completely transparent to CSS selectors, Box View Model and Layout. |
| 58 | + |
| 59 | +In short, `group` is a virtual node, but faking to be real `ParentNode`. |
| 60 | +Another interpretation would be "a `NodeList` with `ParentNode` and `ChildNode` interfaces". |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +Unaffected `group` children for special elements like `table`, `select`: |
| 65 | +In contradiction to other parent elements, `group` will never mess up with its children, they are attached as given, you will never face a situation where elements are forcefully formatted in `group` node when it's being attached since `group` is a limbo. |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +Adding `group` in between other elements: |
| 70 | + |
| 71 | +```js |
| 72 | +group.append(A, B, C) |
| 73 | +document.body.append(X, Y, Z) |
| 74 | +``` |
| 75 | +:arrow_down: |
| 76 | +```js |
| 77 | +Group |
| 78 | +├─ A |
| 79 | +├─ B |
| 80 | +└─ C |
| 81 | + |
| 82 | +(document) |
| 83 | +└─ body |
| 84 | + ├─ X |
| 85 | + ├─ Y |
| 86 | + └─ Z |
| 87 | +``` |
| 88 | + |
| 89 | +Now let's add the `group` before `Y` |
| 90 | +```js |
| 91 | +document.body.insertBefore(group, Y) |
| 92 | +``` |
| 93 | +:arrow_down: |
| 94 | +```js |
| 95 | +Group |
| 96 | +├─ A |
| 97 | +├─ B |
| 98 | +└─ C |
| 99 | + |
| 100 | +(document) |
| 101 | +└─ body |
| 102 | + ├─ X |
| 103 | + ├─ A |
| 104 | + ├─ B |
| 105 | + ├─ C |
| 106 | + ├─ Y |
| 107 | + └─ Z |
| 108 | +``` |
| 109 | + |
| 110 | +Expectations |
| 111 | +```js |
| 112 | +console.log(group.firstChild === A) // true |
| 113 | +console.log(A.parentNode === document.body) // true |
| 114 | +console.log(X.nextSibling === A) // true |
| 115 | +console.log(C.nextSibling === Y) // true |
| 116 | +``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +Accessing properties of children that are part of `group`: |
| 121 | +Reading node properties always reflects the "real" tree - not the `Group` logical ownership. |
| 122 | + |
| 123 | +```js |
| 124 | +group.append(A, B, C) |
| 125 | +``` |
| 126 | +:arrow_down: |
| 127 | +```js |
| 128 | +Group |
| 129 | +├─ A |
| 130 | +├─ B |
| 131 | +└─ C |
| 132 | + |
| 133 | +(document) |
| 134 | +└─ body // No children. |
| 135 | +``` |
| 136 | +:arrow_down: |
| 137 | +```js |
| 138 | +console.log(group.firstChild === A) // true |
| 139 | +console.log(A.parentNode === null) // true |
| 140 | +console.log(A.isConnected === false) // true |
| 141 | +console.log(A.nextSibling === null) // true |
| 142 | +``` |
| 143 | + |
| 144 | +Now connect `group` |
| 145 | + |
| 146 | +```js |
| 147 | +document.body.append(group) |
| 148 | +``` |
| 149 | +:arrow_down: |
| 150 | +```js |
| 151 | +Group |
| 152 | +├─ A |
| 153 | +├─ B |
| 154 | +└─ C |
| 155 | + |
| 156 | +(document) |
| 157 | +└─ body |
| 158 | + ├─ A |
| 159 | + ├─ B |
| 160 | + └─ C |
| 161 | +``` |
| 162 | +:arrow_down: |
| 163 | +```js |
| 164 | +console.log(group.firstChild === A) // true |
| 165 | +console.log(A.parentNode === document.body) // true |
| 166 | +console.log(A.isConnected === true) // true |
| 167 | +console.log(A.nextSibling === B) // true |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
0 commit comments