Skip to content

Commit

Permalink
empty fragment does not break
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Aug 22, 2020
1 parent a8fa3a3 commit dbd5fcf
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
4 changes: 1 addition & 3 deletions cjs/index.js
Expand Up @@ -23,10 +23,8 @@ exports.diffable = diffable;
const persistent = fragment => {
const {childNodes} = fragment;
const {length} = childNodes;
// If the fragment has no content
// it should return undefined and break
if (length < 2)
return childNodes[0];
return length ? childNodes[0] : fragment;
const nodes = slice.call(childNodes, 0);
const firstChild = nodes[0];
const lastChild = nodes[length - 1];
Expand Down
2 changes: 1 addition & 1 deletion es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions esm/index.js
Expand Up @@ -21,10 +21,8 @@ export const diffable = (node, operation) => node.nodeType === nodeType ?
export const persistent = fragment => {
const {childNodes} = fragment;
const {length} = childNodes;
// If the fragment has no content
// it should return undefined and break
if (length < 2)
return childNodes[0];
return length ? childNodes[0] : fragment;
const nodes = slice.call(childNodes, 0);
const firstChild = nodes[0];
const lastChild = nodes[length - 1];
Expand Down
6 changes: 2 additions & 4 deletions index.js
Expand Up @@ -22,10 +22,8 @@ var uwire = (function (exports) {
};
var persistent = function persistent(fragment) {
var childNodes = fragment.childNodes;
var length = childNodes.length; // If the fragment has no content
// it should return undefined and break

if (length < 2) return childNodes[0];
var length = childNodes.length;
if (length < 2) return length ? childNodes[0] : fragment;
var nodes = slice.call(childNodes, 0);
var firstChild = nodes[0];
var lastChild = nodes[length - 1];
Expand Down
2 changes: 1 addition & 1 deletion min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions package.json
Expand Up @@ -8,7 +8,7 @@
"cjs": "ascjs esm cjs",
"rollup:es": "rollup --config rollup/es.config.js",
"rollup:babel": "rollup --config rollup/babel.config.js",
"min": "uglifyjs index.js --support-ie8 --comments=/^!/ -c -m -o min.js",
"min": "terser index.js --comments=/^!/ -c -m -o min.js",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "nyc node test/index.js"
},
Expand All @@ -21,17 +21,17 @@
"author": "Andrea Giammarchi",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"ascjs": "^3.1.2",
"basichtml": "^2.1.4",
"coveralls": "^3.0.9",
"nyc": "^15.0.0",
"rollup": "^2.0.5",
"@babel/core": "^7.11.4",
"@babel/preset-env": "^7.11.0",
"ascjs": "^4.0.1",
"basichtml": "^2.3.0",
"coveralls": "^3.1.0",
"nyc": "^15.1.0",
"rollup": "^2.26.5",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.3.0",
"uglify-js": "^3.8.0"
"rollup-plugin-terser": "^7.0.0",
"terser": "^5.2.1"
},
"module": "./esm/index.js",
"type": "module",
Expand Down
5 changes: 4 additions & 1 deletion test/index.js
Expand Up @@ -3,7 +3,10 @@ const {document} = require('basichtml').init();
const {diffable, persistent} = require('../cjs');

const fragmentSingle = document.createDocumentFragment();
fragmentSingle.appendChild(document.createTextNode('a'));
console.assert(persistent(fragmentSingle) === fragmentSingle, 'no childNodes');

const a = fragmentSingle.appendChild(document.createTextNode('a'));
console.assert(persistent(fragmentSingle) === a, 'a childNode');

const fragmentMulti = document.createDocumentFragment();
fragmentMulti.appendChild(document.createTextNode('a'));
Expand Down

0 comments on commit dbd5fcf

Please sign in to comment.