|
| 1 | +/** |
| 2 | + * Local DOM coverage not provided by our enabled WPT cases. |
| 3 | + * These are independent tests, not adaptations of upstream WPT tests. |
| 4 | + * Sibling traversal lives here while Node-properties.html requires unsupported |
| 5 | + * HTML/XML document fixtures. Prefer WPT coverage when its dependencies are met. |
| 6 | + */ |
| 7 | +import { describe, expect, it } from 'vitest'; |
| 8 | +import { Document } from './src/dom.ts'; |
| 9 | + |
| 10 | +describe('Node sibling properties outside WPT coverage', () => { |
| 11 | + it.each(['createElement', 'createTextNode', 'createComment'] as const)( |
| 12 | + '%s has no siblings when detached or an only child', |
| 13 | + (create) => { |
| 14 | + const document = new Document(() => {}); |
| 15 | + const parent = document.documentElement; |
| 16 | + const node = document[create]('document'); |
| 17 | + |
| 18 | + expect(node.previousSibling).toBeNull(); |
| 19 | + expect(node.nextSibling).toBeNull(); |
| 20 | + |
| 21 | + parent.appendChild(node); |
| 22 | + expect(node.previousSibling).toBeNull(); |
| 23 | + expect(node.nextSibling).toBeNull(); |
| 24 | + |
| 25 | + parent.removeChild(node); |
| 26 | + expect(node.previousSibling).toBeNull(); |
| 27 | + expect(node.nextSibling).toBeNull(); |
| 28 | + }, |
| 29 | + ); |
| 30 | + |
| 31 | + it('traverses immediate siblings including text and comments', () => { |
| 32 | + const document = new Document(() => {}); |
| 33 | + const parent = document.documentElement; |
| 34 | + const first = parent.appendChild(document.createElement('document')); |
| 35 | + const text = parent.appendChild(document.createTextNode('text')); |
| 36 | + const comment = parent.appendChild(document.createComment('comment')); |
| 37 | + const last = parent.appendChild(document.createElement('document')); |
| 38 | + const nested = first.appendChild(document.createTextNode('nested')); |
| 39 | + |
| 40 | + expect(first.previousSibling).toBeNull(); |
| 41 | + expect(first.nextSibling).toBe(text); |
| 42 | + expect(text.previousSibling).toBe(first); |
| 43 | + expect(text.nextSibling).toBe(comment); |
| 44 | + expect(comment.previousSibling).toBe(text); |
| 45 | + expect(comment.nextSibling).toBe(last); |
| 46 | + expect(last.previousSibling).toBe(comment); |
| 47 | + expect(last.nextSibling).toBeNull(); |
| 48 | + expect(nested.previousSibling).toBeNull(); |
| 49 | + expect(nested.nextSibling).toBeNull(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('updates both directions when existing children are reordered', () => { |
| 53 | + const document = new Document(() => {}); |
| 54 | + const parent = document.documentElement; |
| 55 | + const first = parent.appendChild(document.createElement('document')); |
| 56 | + const middle = parent.appendChild(document.createTextNode('middle')); |
| 57 | + const last = parent.appendChild(document.createComment('last')); |
| 58 | + |
| 59 | + parent.insertBefore(last, middle); |
| 60 | + expect(first.previousSibling).toBeNull(); |
| 61 | + expect(first.nextSibling).toBe(last); |
| 62 | + expect(last.previousSibling).toBe(first); |
| 63 | + expect(last.nextSibling).toBe(middle); |
| 64 | + expect(middle.previousSibling).toBe(last); |
| 65 | + expect(middle.nextSibling).toBeNull(); |
| 66 | + |
| 67 | + parent.appendChild(first); |
| 68 | + expect(last.previousSibling).toBeNull(); |
| 69 | + expect(last.nextSibling).toBe(middle); |
| 70 | + expect(middle.previousSibling).toBe(last); |
| 71 | + expect(middle.nextSibling).toBe(first); |
| 72 | + expect(first.previousSibling).toBe(middle); |
| 73 | + expect(first.nextSibling).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('updates both trees when a child is moved to a different parent', () => { |
| 77 | + const document = new Document(() => {}); |
| 78 | + const source = document.createElement('document'); |
| 79 | + const destination = document.createElement('document'); |
| 80 | + const first = source.appendChild(document.createTextNode('first')); |
| 81 | + const moved = source.appendChild(document.createElement('document')); |
| 82 | + const last = source.appendChild(document.createComment('last')); |
| 83 | + const anchor = destination.appendChild(document.createTextNode('anchor')); |
| 84 | + |
| 85 | + destination.insertBefore(moved, anchor); |
| 86 | + expect(first.previousSibling).toBeNull(); |
| 87 | + expect(first.nextSibling).toBe(last); |
| 88 | + expect(last.previousSibling).toBe(first); |
| 89 | + expect(last.nextSibling).toBeNull(); |
| 90 | + expect(moved.previousSibling).toBeNull(); |
| 91 | + expect(moved.nextSibling).toBe(anchor); |
| 92 | + expect(anchor.previousSibling).toBe(moved); |
| 93 | + expect(anchor.nextSibling).toBeNull(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('closes the sibling gap on removal and clears detached relationships', () => { |
| 97 | + const document = new Document(() => {}); |
| 98 | + const parent = document.documentElement; |
| 99 | + const first = parent.appendChild(document.createTextNode('first')); |
| 100 | + const middle = parent.appendChild(document.createComment('middle')); |
| 101 | + const last = parent.appendChild(document.createElement('document')); |
| 102 | + |
| 103 | + parent.removeChild(middle); |
| 104 | + expect(first.nextSibling).toBe(last); |
| 105 | + expect(last.previousSibling).toBe(first); |
| 106 | + expect(middle.previousSibling).toBeNull(); |
| 107 | + expect(middle.nextSibling).toBeNull(); |
| 108 | + |
| 109 | + parent.removeChild(first); |
| 110 | + expect(first.previousSibling).toBeNull(); |
| 111 | + expect(first.nextSibling).toBeNull(); |
| 112 | + expect(last.previousSibling).toBeNull(); |
| 113 | + expect(last.nextSibling).toBeNull(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments