This repository was archived by the owner on Jun 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
Add patchBetween() #455
Open
justinfagnani
wants to merge
1
commit into
google:master
Choose a base branch
from
PolymerLabs:patch-between
base: master
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.
Open
Add patchBetween() #455
Changes from all commits
Commits
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
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,233 @@ | ||
| /** | ||
| * Copyright 2015 The Incremental DOM Authors. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS-IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // taze: chai from //third_party/javascript/typings/chai | ||
|
|
||
| import { | ||
| patch, | ||
| patchBetween, | ||
| elementOpen, | ||
| elementOpenStart, | ||
| elementOpenEnd, | ||
| elementClose, | ||
| elementVoid, | ||
| text | ||
| } from '../../index'; | ||
| import { | ||
| assertHTMLElement, | ||
| } from '../util/dom'; | ||
| const {expect} = chai; | ||
|
|
||
|
|
||
| describe('patching between', () => { | ||
| let container:HTMLElement; | ||
|
|
||
| beforeEach(() => { | ||
| container = document.createElement('div'); | ||
| container.setAttribute('tid', 'container'); | ||
| document.body.appendChild(container); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| document.body.removeChild(container); | ||
| }); | ||
|
|
||
| it('should create the required DOM nodes between comments', () => { | ||
| const startNode = document.createComment('start'); | ||
| const endNode = document.createComment('end'); | ||
| container.append(startNode, endNode); | ||
|
|
||
| patchBetween(startNode, endNode, () => { | ||
| elementOpen('div', null, null, 'id', 'aDiv'); | ||
| elementClose('div'); | ||
| elementOpen('div', null, null, 'id', 'bDiv'); | ||
| elementClose('div'); | ||
| }); | ||
|
|
||
| expect(container.innerHTML).to.eq('<!--start--><div id="aDiv"></div><div id="bDiv"></div><!--end-->'); | ||
| expect(assertHTMLElement(container.childNodes[1]).id).to.equal('aDiv'); | ||
| expect(assertHTMLElement(container.childNodes[2]).id).to.equal('bDiv'); | ||
| }); | ||
|
|
||
| it('should create the required DOM nodes between divs', () => { | ||
| const startNode = document.createElement('div'); | ||
| // startNode.setAttribute('tid', 'start'); | ||
| const endNode = document.createElement('div'); | ||
| // endNode.setAttribute('tid', 'end'); | ||
| container.append(startNode, endNode); | ||
|
|
||
| patchBetween(startNode, endNode, () => { | ||
| elementOpen('div', null, null, 'id', 'aDiv'); | ||
| elementClose('div'); | ||
| elementOpen('div', null, null, 'id', 'bDiv'); | ||
| elementClose('div'); | ||
| }); | ||
|
|
||
| expect(container.innerHTML).to.eq('<div></div><div id="aDiv"></div><div id="bDiv"></div><div></div>'); | ||
| expect(assertHTMLElement(container.childNodes[1]).id).to.equal('aDiv'); | ||
| expect(assertHTMLElement(container.childNodes[2]).id).to.equal('bDiv'); | ||
| }); | ||
|
|
||
| describe('with an existing document tree', () => { | ||
| // TODO: add test w/ keyed items after the endNode | ||
|
|
||
| let div:HTMLElement; | ||
| let startNode; | ||
| let endNode; | ||
|
|
||
| function render() { | ||
| elementVoid('div', null, null, 'tabindex', '0'); | ||
| } | ||
|
|
||
| beforeEach(function() { | ||
| div = document.createElement('div'); | ||
| div.setAttribute('tabindex', '-1'); | ||
| startNode = document.createComment('start'); | ||
| endNode = document.createComment('end'); | ||
| container.append(startNode, div, endNode); | ||
| }); | ||
|
|
||
| it('should preserve existing nodes', () => { | ||
| patchBetween(startNode, endNode, render); | ||
| const child = container.childNodes[1]; | ||
| expect(child).to.equal(div); | ||
| }); | ||
|
|
||
| describe('should return DOM node', () => { | ||
| let node:HTMLElement; | ||
|
|
||
| it('from elementOpen', () => { | ||
| patchBetween(startNode, endNode, () => { | ||
| node = elementOpen('div'); | ||
| elementClose('div'); | ||
| }); | ||
|
|
||
| expect(node).to.equal(div); | ||
| }); | ||
|
|
||
| it('from elementClose', () => { | ||
| patchBetween(startNode, endNode, () => { | ||
| elementOpen('div'); | ||
| node = assertHTMLElement(elementClose('div')); | ||
| }); | ||
|
|
||
| expect(node).to.equal(div); | ||
| }); | ||
|
|
||
| it('from elementVoid', () => { | ||
| patchBetween(startNode, endNode, () => { | ||
| node = assertHTMLElement(elementVoid('div')); | ||
| }); | ||
|
|
||
| expect(node).to.equal(div); | ||
| }); | ||
|
|
||
| it('from elementOpenEnd', () => { | ||
| patchBetween(startNode, endNode, () => { | ||
| elementOpenStart('div'); | ||
| node = elementOpenEnd(); | ||
| elementClose('div'); | ||
| }); | ||
|
|
||
| expect(node).to.equal(div); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should be re-entrant', () => { | ||
| const startNode = document.createElement('div'); | ||
| // startNode.setAttribute('tid', 'start'); | ||
| const endNode = document.createElement('div'); | ||
| // endNode.setAttribute('tid', 'end'); | ||
| container.append(startNode, endNode); | ||
|
|
||
| patchBetween(startNode, endNode, () => { | ||
| const div1 = elementOpen('div', null, null, 'id', 'aDiv'); | ||
| elementClose('div'); | ||
| const div2 = elementOpen('div', null, null, 'id', 'bDiv'); | ||
| elementClose('div'); | ||
| patchBetween(div1, div2, () => { | ||
| elementOpen('div', null, null, 'id', 'cDiv'); | ||
| elementClose('div'); | ||
| }); | ||
| elementOpen('div', null, null, 'id', 'dDiv'); | ||
| elementClose('div'); | ||
| }); | ||
|
|
||
| expect(container.innerHTML).to.eq( | ||
| '<div></div><div id="aDiv"></div><div id="cDiv"></div><div id="bDiv"></div><div id="dDiv"></div><div></div>'); | ||
| expect(assertHTMLElement(container.childNodes[1]).id).to.equal('aDiv'); | ||
| expect(assertHTMLElement(container.childNodes[2]).id).to.equal('cDiv'); | ||
| }); | ||
|
|
||
| it('should pass third argument to render function', () => { | ||
| const startNode = document.createElement('div'); | ||
| const endNode = document.createElement('div'); | ||
| container.append(startNode, endNode); | ||
|
|
||
| const render = (content:unknown) => { | ||
| text(content as string); | ||
| }; | ||
|
|
||
| patchBetween(startNode, endNode, render, 'foobar'); | ||
|
|
||
| expect(container.textContent).to.equal('foobar'); | ||
| }); | ||
|
|
||
| it('should patch a detached node', () => { | ||
| const container = document.createElement('div'); | ||
| const startNode = document.createElement('div'); | ||
| const endNode = document.createElement('div'); | ||
| container.append(startNode, endNode); | ||
|
|
||
| const render = () => { | ||
| elementVoid('span'); | ||
| }; | ||
|
|
||
| patchBetween(startNode, endNode, render); | ||
|
|
||
| expect(assertHTMLElement(container.childNodes[1]).tagName).to.equal('SPAN'); | ||
| }); | ||
|
|
||
| it('should throw when an element is unclosed', () => { | ||
| const startNode = document.createComment('start'); | ||
| const endNode = document.createComment('end'); | ||
| container.append(startNode, endNode); | ||
|
|
||
| expect(() => { | ||
| patchBetween(startNode, endNode, () => { | ||
| elementOpen('div'); | ||
| }); | ||
| }).to.throw('One or more tags were not closed:\ndiv'); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| describe('patching a documentFragment', () => { | ||
| it('should create the required DOM nodes', () => { | ||
| const frag = document.createDocumentFragment(); | ||
| const startNode = document.createComment('start'); | ||
| const endNode = document.createComment('end'); | ||
| frag.append(startNode, endNode); | ||
|
|
||
| patchBetween(startNode, endNode, () => { | ||
| elementOpen('div', null, null, 'id', 'aDiv'); | ||
| elementClose('div'); | ||
| }); | ||
|
|
||
| expect(assertHTMLElement(frag.childNodes[1]).id).to.equal('aDiv'); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be possible to have unvisited dom but currentNode.nextSibling === endNode.
{template a}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you expand on that a bit? What would the unvisited DOM be? What are the idom opcode that produce it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be
function fn(a:boolean) {
elementOpen('div', 1);
elementClose();
if (a) {
elementOpen('div', 2);
elementClose();
} else {
elementOpen('div', 3);
elementClose();
}
elementOpen('div', 4);
elementClose();
}
In the first route (A), you'd have
if you flip a to false, you'd first have, I think, the following behavior
<-- insertthen the jump to 4
<-- insert <--and then never deleting [key=2]