Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created October 29, 2018 14:53
Show Gist options
  • Save matthewp/9b379550d05d73b972086c3013cb45f5 to your computer and use it in GitHub Desktop.
Save matthewp/9b379550d05d73b972086c3013cb45f5 to your computer and use it in GitHub Desktop.
Debugging incremental rendering instruction mismatch
function printDocument(node, startIndex = 0) {
let skip, tmp;
let depth = 0;
let index = startIndex;
let items = [];
function add(node) {
let str = index + "-" + node.nodeName;
items.push(str);
}
// Always start with the initial element.
do {
if ( !skip && (tmp = node.firstChild) ) {
depth++;
add(tmp);
index++;
} else if ( tmp = node.nextSibling ) {
skip = false;
add(tmp);
index++;
} else {
// Skipped or no first child and no next sibling, so traverse upwards,
tmp = node.parentNode;
// and decrement the depth.
depth--;
// Enable skipping, so that in the next loop iteration, the children of
// the now-current node (parent node) aren't processed again.
skip = true;
}
// Instead of setting node explicitly in each conditional block, use the
// tmp var and set it here.
node = tmp;
// Stop if depth comes back to 0 (or goes below zero, in conditions where
// the passed node has neither children nore next siblings).
} while ( depth > 0 );
console.log(items.join("|"));
}
function printMap(map) {
let out = [];
for(let [node, index] of map) {
out[index] = index + "-" + node.nodeName;
}
console.log(out.join("|"));
}
function sep(pth) {
var fs = require("fs");
var content = fs.readFileSync(pth, "utf8");
var out = content.split("|").join("\n");
fs.writeFileSync(pth, out, "utf8");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment