Commit 8826c58
authored
Create mergeNodes.js
```javascript
function* mergeNodes(left, right) {
if (!left && !right) return;
if (!left) { yield right; return; }
if (!right) { yield left; return; }
if (left.nodeName !== right.nodeName) {
yield left; yield right; return;
}
const merged = left.cloneNode(false); // 只複製節點本身
Array.from(right.attributes).forEach(attr =>
merged.setAttribute(attr.name, attr.value)); // 屬性覆蓋
merged.textContent = (left.textContent || '') +
(right.textContent || '');
const lChildren = left.childNodes;
const rChildren = right.childNodes;
const len = Math.max(lChildren.length, rChildren.length);
for (let i = 0; i < len; ++i) {
for (const sub of mergeNodes(lChildren[i], rChildren[i])) {
merged.appendChild(sub);
}
}
yield merged;
}
```1 parent 003b341 commit 8826c58
1 file changed
Lines changed: 27 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
0 commit comments