Skip to content

Commit

Permalink
Merge pull request #108 from lukebayes/lbj-rerender
Browse files Browse the repository at this point in the history
Fixed bug were value='0' attributes disappear on re-render
  • Loading branch information
lukebayes authored Apr 15, 2020
2 parents d7936ae + cc553a7 commit ff4944b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DOM_CONTENT_LOADED = 'DOMContentLoaded';

function client(doc) {
// Render the application immediately. This can begin before the DOM is fully ready.
const app = renderElement(dom.h1('HELLO WORLD 34343'), doc);
const app = renderElement(dom.h1('HELLO WORLD'), doc);

ready(doc, () => {
// Get the element where we will attach the application.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Nomplate: Node template engine",
"main": "index.js",
"engines": {
"node": ">=12.4.0"
"node": ">=12.4.1"
},
"bugs": "https://github.com/lukebayes/nomplate/issues",
"homepage": "https://github.com/lukebayes/nomplate",
Expand Down
6 changes: 3 additions & 3 deletions src/render_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ function elementAttributesToOperations(ops, nomElement, optDomElement) {
}
} else if (optDomElement) {
const domValue = optDomElement.getAttribute(keyWithCase);
if (!value && domValue !== 'false' && domValue !== false && domValue !== 0) {
// Remove attributes that transition to falsy values:
if (value === '' || value === undefined || value === null || value === NaN) {
// Remove invalid/empty-ish attribute values
ops.push(operations.removeAttribute(keyWithCase, value));
} else if (value != domValue) {
// Update attributes with new values:
Expand Down Expand Up @@ -284,7 +284,7 @@ function nomElementToOperations(ops, nomElement, doc, optDomElement) {
*/
function renderElement(nomElement, doc, optDomElement) {
if (!doc) {
throw new IllegalOperationError('renderElement requires a reference to an HTML document.');
throw new Error('renderElement requires a reference to an HTML document.');
}

const ops = [];
Expand Down
38 changes: 37 additions & 1 deletion test/render_element_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('renderElement', () => {
doc = win.document;
});


describe('creation', () => {
it('creates simple element', () => {
const domElement = renderElement(dom.div(), doc);
Expand All @@ -41,6 +40,11 @@ describe('renderElement', () => {
assert.equal(domElement.outerHTML, '<input type="checkbox">');
});

it('leaves attributes with a falsy zero', () => {
const domElement = renderElement(dom.option({value: 0}), doc);
assert.equal(domElement.outerHTML, '<option value="0"></option>');
});

it('creates children', () => {
const domElement = renderElement(dom.div(() => {
dom.ul(() => {
Expand Down Expand Up @@ -106,6 +110,38 @@ describe('renderElement', () => {
assert.equal(ul.childNodes[2].textContent, 'three');
});

it('leaves zero value attributes after update', () => {
let updater = null;
let selectedIndex = 0;

const domElement = renderElement(dom.div(() => {
dom.div((update) => {
updater = update;
dom.select(() => {
dom.option({value: 0, selected: 0 === selectedIndex}, "aye");
dom.option({value: 1, selected: 1 === selectedIndex}, "bee");
dom.option({value: 2, selected: 2 === selectedIndex}, "cee");
});
});
}), doc);

assert.equal(domElement.outerHTML, '<div><div><select>' +
'<option value="0" selected="true">aye</option>' +
'<option value="1">bee</option>' +
'<option value="2">cee</option>' +
'</select></div></div>');
selectedIndex = 2;

updater();
builder.forceUpdate();

assert.equal(domElement.outerHTML, '<div><div><select>' +
'<option value="0">aye</option>' +
'<option value="1">bee</option>' +
'<option value="2" selected="true">cee</option>' +
'</select></div></div>');
});

it('assigns click handler', () => {
const handler = sinon.spy();
const element = renderElement(dom.button({onClick: handler}), doc);
Expand Down

0 comments on commit ff4944b

Please sign in to comment.