Skip to content

Commit

Permalink
Add skip function to allow skipping subtrees dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
sparhami committed Oct 29, 2015
1 parent e5974d6 commit 35b0f3c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
elementOpen,
elementClose,
elementPlaceholder,
skip,
text,
attr,
} from './src/virtual_elements';
Expand Down
3 changes: 2 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ var getContext = function() {
return context;
};


/**
* Gets the current Element being patched.
* @return {!Node}
*/
var currentElement = function() {
if (process.env.NODE_ENV !== 'production' && !context) {
throw new Error('cannot call currentElement() while not in patch');
throw new Error('Cannot call currentElement() while not in patch.');
}
return context.walker.currentParent;
};
Expand Down
25 changes: 20 additions & 5 deletions src/virtual_elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
} from './alignment';
import { updateAttribute } from './attributes';
import { getData } from './node_data';
import { getContext } from './context';
import {
getContext,
currentElement
} from './context';
import {
firstChild,
nextSibling,
Expand Down Expand Up @@ -116,6 +119,18 @@ if (process.env.NODE_ENV !== 'production') {
}


/**
* Skips the children in a subtree, allowing an Element to be closed without
* clearing out the children.
*/

This comment has been minimized.

Copy link
@jridgewell

jridgewell Oct 29, 2015

Contributor

Mind doing this in a PR next time? There are other things like the placeholder attribute that could be removed with this.

This comment has been minimized.

Copy link
@sparhami

sparhami Oct 29, 2015

Contributor

I thought about removing it, but it is in 0.2.0 so it needs to have a deprecation warning added first, then removed in 0.4.0 at the earliest.

var skip = function() {
var openElement = currentElement();
var data = getData(openElement);

This comment has been minimized.

Copy link
@jridgewell

jridgewell Oct 29, 2015

Contributor

We could add a skip property to NodeData to allow skipping at the beginning of an element:

elementOpen('div');
  skip();
  elementVoid('inner');
elementClose('div');
data.lastVisitedChild = openElement.lastChild;
};


/**
* @param {string} tag The element's tag.
* @param {?string=} key The key used to identify this element. This can be an
Expand Down Expand Up @@ -307,10 +322,9 @@ var elementPlaceholder = function(tag, key, statics, var_args) {
assertPlaceholderKeySpecified(key);
}

var node = elementOpen.apply(null, arguments);
updateAttribute(node, symbols.placeholder, true);
elementClose.apply(null, arguments);
return node;
elementOpen.apply(null, arguments);
skip();
return elementClose.apply(null, arguments);
};


Expand Down Expand Up @@ -355,6 +369,7 @@ export {
elementVoid,
elementClose,
elementPlaceholder,
skip,
text,
attr
};
56 changes: 56 additions & 0 deletions test/functional/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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.
*/

import {
patch,
elementOpen,
elementClose,
skip,
text
} from '../../index';

describe('skip', () => {
var container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
});

function render(data) {
elementOpen('div');
if (data.skip) {
skip();
} else {
text('some ');
text('text');
}
elementClose('div');
}

it('should keep any DOM nodes in the subtree', () => {
patch(container, render, { skip: false });
patch(container, render, { skip: true });

expect(container.textContent).to.equal('some text');
});

});

0 comments on commit 35b0f3c

Please sign in to comment.