Skip to content
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

Add nothing sentinel #673

Merged
merged 2 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ export interface Part {
* should not be written to the DOM.
*/
export const noChange = {};

/**
* A sentinel value that signals a NodePart to fully clear its content.
*/
export const nothing = {};
5 changes: 4 additions & 1 deletion src/lib/parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import {isDirective} from './directive.js';
import {removeNodes} from './dom.js';
import {noChange, Part} from './part.js';
import {noChange, nothing, Part} from './part.js';
import {RenderOptions} from './render-options.js';
import {TemplateInstance} from './template-instance.js';
import {TemplateResult} from './template-result.js';
Expand Down Expand Up @@ -196,6 +196,9 @@ export class NodePart implements Part {
this._commitNode(value);
} else if (Array.isArray(value) || value[Symbol.iterator]) {
this._commitIterable(value);
} else if (value === nothing) {
this.value = undefined;
ruphin marked this conversation as resolved.
Show resolved Hide resolved
this.clear();
} else {
// Fallback, will render the string representation
this._commitText(value);
Expand Down
2 changes: 1 addition & 1 deletion src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export {DefaultTemplateProcessor, defaultTemplateProcessor} from './lib/default-
export {directive, DirectiveFn, isDirective} from './lib/directive.js';
// TODO(justinfagnani): remove line when we get NodePart moving methods
export {removeNodes, reparentNodes} from './lib/dom.js';
export {noChange, Part} from './lib/part.js';
export {noChange, nothing, Part} from './lib/part.js';
export {AttributeCommitter, AttributePart, BooleanAttributePart, EventPart, isPrimitive, NodePart, PropertyCommitter, PropertyPart} from './lib/parts.js';
export {RenderOptions} from './lib/render-options.js';
export {parts, render} from './lib/render.js';
Expand Down
20 changes: 17 additions & 3 deletions src/test/lib/render_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* http://polymer.github.io/PATENTS.txt
*/

import {AttributePart, directive, html, NodePart, Part, render, svg, templateFactory} from '../../lit-html.js';
import {AttributePart, directive, html, noChange, NodePart, nothing, Part, render, svg, templateFactory} from '../../lit-html.js';
import {stripExpressionMarkers} from '../test-utils/strip-markers.js';

const assert = chai.assert;
Expand Down Expand Up @@ -43,8 +43,7 @@ suite('render()', () => {

suite('text', () => {
test('renders plain text expression', () => {
const result = html`test`;
render(result, container);
render(html`test`, container);
assert.equal(stripExpressionMarkers(container.innerHTML), 'test');
});

Expand All @@ -70,6 +69,21 @@ suite('render()', () => {
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
});

test('renders noChange', () => {
const template = (i: any) => html`<div>${i}</div>`;
render(template('foo'), container);
render(template(noChange), container)
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>foo</div>');
});

test('renders nothing', () => {
const template = (i: any) => html`<div>${i}</div>`;
render(template('foo'), container);
render(template(nothing), container)
assert.equal(container.querySelector('div')!.innerHTML, '');
});

testIfHasSymbol('renders a Symbol', () => {
render(html`<div>${Symbol('A')}</div>`, container);
assert.include(
Expand Down