const output = document.querySelector("output");
const divs = document.querySelectorAll("div");
const [count, cancel] = document.querySelectorAll("input");
// Keep a log of last N events, in reverse temporal order
function log(html) {
if (typeof html === "string") {
const cont = document.createElement("div");
cont.innerHTML = html;
html = cont;
}
output.prepend(html);
while (output.children.length > count.valueAsNumber)
output.lastElementChild.remove();
}
// Print div's html with some extra coloring/labeling features
class HTMLSerialize {
// divisions in hue, when rotating colors for next element
static HUE_DIVS = 7;
// attributes to serialize; {js attribute -> html attribute}
static SHOW_ATTRS = {
className: "class"
};
// singelton tags/eleemnts
static SINGLETON = new Set(["br", "hr", "wbr", "col", "command", "img"]);
// counter for unique ids and colors; {container -> {id/hue: int}
static counter = new Map();
/**
* @param src source element to serialize
* @param target where to render the serialization
* @param anchors output of ranges2anchors
*/
constructor(src, target, anchors = null) {
this.src = src;
if (!HTMLSerialize.counter.has(src))
HTMLSerialize.counter.set(src, {
id: 0,
hue: 0
});
this.frag = document.createDocumentFragment();
this.serialize_recursive(src, anchors, true);
target.replaceChildren(this.frag);
}
/** Give each node a unqiue id, and each element a hue. Can be used
to indicate whether a node was recreated/destroyed
*/
assign_id(el) {
if (typeof el.serialization === "undefined") {
let c = HTMLSerialize.counter.get(this.src);
el.serialization = Object.assign({}, c);
c.id++;
if (el.nodeType == Node.ELEMENT_NODE)
c.hue = (c.hue + 360 / (HTMLSerialize.HUE_DIVS + 0.5)) % 360;
}
}
add_cursor(cursor, root = null) {
const s = document.createElement("span");
s.dataset.range =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.