-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[BUGFIX] DocumentFragment support — {{#in-element}} with DocumentFragment targets
#21253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/extract-document-fragment-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e1a05ff
Extract document-fragment support from shadow DOM PR
Copilot 5c6b385
Fix misleading comment about querySelector on DocumentFragment
Copilot 3a93b79
Add test: verify text updates and conditional elements after fragment…
Copilot 3e1e1c1
Refactor test: use step helper called from template instead of DOM as…
Copilot 8eb9b7d
Fix test: use per-test assert parameter and reactive step helper to f…
Copilot 9b96a2c
Fix test: only rerender while fragment is detached, not after appendi…
Copilot 122e989
Fix: make rerenders work after DocumentFragment is appended to DOM
Copilot ef0a958
Fix: prettier formatting in in-element-document-fragment.ts
Copilot 0e0c6d5
The true cost (in types) of | SimpleDocumentFragment
NullVoxPopuli d6fc612
Move to SimpleNode
NullVoxPopuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
packages/@glimmer-workspace/integration-tests/lib/suites/in-element-document-fragment.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import { RenderTest } from '../render-test'; | ||
| import { test } from '../test-decorator'; | ||
|
|
||
| export class InElementDocumentFragmentSuite extends RenderTest { | ||
| static suiteName = '#in-element (DocumentFragment)'; | ||
|
|
||
| @test | ||
| 'Renders curlies into a detached DocumentFragment'() { | ||
| const fragment = document.createDocumentFragment(); | ||
|
|
||
| this.render('{{#in-element this.fragment}}[{{this.foo}}]{{/in-element}}', { | ||
| fragment, | ||
| foo: 'Hello Fragment!', | ||
| }); | ||
|
|
||
| this.assert.strictEqual( | ||
| fragment.textContent, | ||
| '[Hello Fragment!]', | ||
| 'content rendered in document fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
| this.assertStableRerender(); | ||
|
|
||
| this.rerender({ foo: 'Updated!' }); | ||
| this.assert.strictEqual( | ||
| fragment.textContent, | ||
| '[Updated!]', | ||
| 'content updated in document fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
|
|
||
| this.rerender({ foo: 'Hello Fragment!' }); | ||
| this.assert.strictEqual( | ||
| fragment.textContent, | ||
| '[Hello Fragment!]', | ||
| 'content reverted in document fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
| } | ||
|
|
||
| @test | ||
| 'Renders curlies into a template.content fragment'() { | ||
| const templateEl = document.createElement('template'); | ||
| const fragment = templateEl.content; | ||
|
|
||
| this.render('{{#in-element this.fragment}}[{{this.foo}}]{{/in-element}}', { | ||
| fragment, | ||
| foo: 'Hello Template Content!', | ||
| }); | ||
|
|
||
| this.assert.strictEqual( | ||
| fragment.textContent, | ||
| '[Hello Template Content!]', | ||
| 'content rendered in template.content fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
| this.assertStableRerender(); | ||
|
|
||
| this.rerender({ foo: 'Updated!' }); | ||
| this.assert.strictEqual( | ||
| fragment.textContent, | ||
| '[Updated!]', | ||
| 'content updated in template.content fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
|
|
||
| this.rerender({ foo: 'Hello Template Content!' }); | ||
| this.assert.strictEqual( | ||
| fragment.textContent, | ||
| '[Hello Template Content!]', | ||
| 'content reverted in template.content fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
| } | ||
|
|
||
| @test | ||
| 'Renders elements into a fragment that is later attached to the DOM'() { | ||
| const fragment = document.createDocumentFragment(); | ||
| const container = document.createElement('div'); | ||
|
|
||
| this.render('{{#in-element this.fragment}}<p id="frag-p">{{this.message}}</p>{{/in-element}}', { | ||
| fragment, | ||
| message: 'in fragment', | ||
| }); | ||
|
|
||
| this.assert.strictEqual( | ||
| fragment.querySelector('#frag-p')?.textContent, | ||
| 'in fragment', | ||
| 'content rendered in detached fragment' | ||
| ); | ||
| this.assertHTML('<!---->'); | ||
|
|
||
| // Attach fragment's children to the DOM | ||
| container.appendChild(fragment); | ||
| this.assert.strictEqual( | ||
| container.querySelector('#frag-p')?.textContent, | ||
| 'in fragment', | ||
| 'content is in the DOM after fragment is appended' | ||
| ); | ||
| // Fragment itself is now empty (children moved to container) | ||
| this.assert.strictEqual(fragment.childNodes.length, 0, 'fragment is empty after append'); | ||
| } | ||
|
|
||
| @test | ||
| 'Multiple in-element calls to the same DocumentFragment'() { | ||
| const fragment = document.createDocumentFragment(); | ||
|
|
||
| this.render( | ||
| '{{#in-element this.fragment}}[{{this.foo}}]{{/in-element}}' + | ||
| '{{#in-element this.fragment insertBefore=null}}[{{this.bar}}]{{/in-element}}', | ||
| { | ||
| fragment, | ||
| foo: 'first', | ||
| bar: 'second', | ||
| } | ||
| ); | ||
|
|
||
| this.assert.ok(fragment.textContent?.includes('[first]'), 'first block present in fragment'); | ||
| this.assert.ok(fragment.textContent?.includes('[second]'), 'second block present in fragment'); | ||
| this.assertHTML('<!----><!---->'); | ||
| this.assertStableRerender(); | ||
|
|
||
| this.rerender({ foo: 'updated-first', bar: 'updated-second' }); | ||
| this.assert.ok( | ||
| fragment.textContent?.includes('[updated-first]'), | ||
| 'first block updated in fragment' | ||
| ); | ||
| this.assert.ok( | ||
| fragment.textContent?.includes('[updated-second]'), | ||
| 'second block updated in fragment' | ||
| ); | ||
| this.assertHTML('<!----><!---->'); | ||
| } | ||
|
|
||
| @test | ||
| 'Rerenders work after DocumentFragment is appended to the DOM'(assert: typeof QUnit.assert) { | ||
| const fragment = document.createDocumentFragment(); | ||
| const container = document.createElement('div'); | ||
| const step = (text: string) => { | ||
| assert.step(text); | ||
| return text; | ||
| }; | ||
|
|
||
| this.render( | ||
| '{{#in-element this.fragment}}' + | ||
| '<p id="msg">{{this.step this.message}}</p>' + | ||
| '{{#if this.show}}' + | ||
| '<span id="extra">extra {{this.step "extra rendered"}}</span>' + | ||
| '{{/if}}' + | ||
| '{{/in-element}}', | ||
| { | ||
| fragment, | ||
| message: 'initial', | ||
| show: false, | ||
| step, | ||
| } | ||
| ); | ||
|
|
||
| assert.verifySteps(['initial'], 'initial render fires step from inside fragment'); | ||
|
|
||
| // Move the fragment's children into the container. After this the fragment is | ||
| // empty, but the rendered nodes (including Glimmer's bounds markers) are live | ||
| // children of `container`. | ||
| container.appendChild(fragment); | ||
| assert.strictEqual(fragment.childNodes.length, 0, 'fragment is empty after append'); | ||
| assert.ok(container.querySelector('#msg'), 'paragraph is present in container after append'); | ||
|
|
||
| // Rerenders should continue to work after the fragment is attached — Glimmer | ||
| // resolves the live parent from the bounds markers' actual parentNode. | ||
| this.rerender({ message: 'updated' }); | ||
| assert.verifySteps(['updated'], 'text update fires step after fragment was attached to DOM'); | ||
| assert.strictEqual( | ||
| container.querySelector('#msg')?.textContent, | ||
| 'updated', | ||
| 'paragraph text is updated in container' | ||
| ); | ||
|
|
||
| // New conditional element should appear in the container. | ||
| this.rerender({ show: true }); | ||
| assert.verifySteps( | ||
| ['extra rendered'], | ||
| 'conditional element step fires in container after fragment was attached to DOM' | ||
| ); | ||
| assert.ok( | ||
| container.querySelector('#extra'), | ||
| 'conditional span appears in container after fragment was attached to DOM' | ||
| ); | ||
| } | ||
|
|
||
| @test | ||
| 'Multiple in-element calls to the same DocumentFragment with insertBefore=null'() { | ||
| const fragment = document.createDocumentFragment(); | ||
|
|
||
| this.render( | ||
| '{{#in-element this.fragment insertBefore=null}}<p id="a">{{this.foo}}</p>{{/in-element}}' + | ||
| '{{#in-element this.fragment insertBefore=null}}<p id="b">{{this.bar}}</p>{{/in-element}}', | ||
| { | ||
| fragment, | ||
| foo: 'first', | ||
| bar: 'second', | ||
| } | ||
| ); | ||
|
|
||
| // Use childNodes to traverse the fragment's direct children since glimmer also | ||
| // inserts comment marker nodes alongside the rendered elements. | ||
| const nodes = Array.from(fragment.childNodes); | ||
| const pA = nodes.find((n) => (n as Element).id === 'a') as HTMLElement | undefined; | ||
| const pB = nodes.find((n) => (n as Element).id === 'b') as HTMLElement | undefined; | ||
|
|
||
| this.assert.strictEqual(pA?.textContent, 'first', 'first block appended to fragment'); | ||
| this.assert.strictEqual(pB?.textContent, 'second', 'second block appended to fragment'); | ||
| this.assertHTML('<!----><!---->'); | ||
| this.assertStableRerender(); | ||
|
|
||
| this.rerender({ foo: 'updated-first', bar: 'updated-second' }); | ||
| this.assert.strictEqual(pA?.textContent, 'updated-first', 'first block updated in fragment'); | ||
| this.assert.strictEqual(pB?.textContent, 'updated-second', 'second block updated in fragment'); | ||
| this.assertHTML('<!----><!---->'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| import type { Nullable } from '../core.js'; | ||
| import type { SimpleElement, SimpleNode } from './simple.js'; | ||
| import type { SimpleNode } from './simple.js'; | ||
|
|
||
| export interface Bounds { | ||
| // a method to future-proof for wormholing; may not be needed ultimately | ||
| parentElement(): SimpleElement; | ||
| parentElement(): SimpleNode; | ||
| firstNode(): SimpleNode; | ||
| lastNode(): SimpleNode; | ||
| } | ||
|
|
||
| export interface Cursor { | ||
| readonly element: SimpleElement; | ||
| readonly element: SimpleNode; | ||
| readonly nextSibling: Nullable<SimpleNode>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.