简体   繁体   中英

serializing dynamically created html with indentation

Having created a bunch of elements in an html document with appendChild(), I am trying to to save the modified page on the client. Sending it off to the server seems a bit unnecessary, so I've opted for :

var save = document.createElement("a");

save.classList.add("button");
save.textContent = "save";
save.download = "layout-save.html"
save.onclick = function(event) {
    var output = [];

    // serialize document to output

    var file = new window.Blob(output,{type:"text/html"});
    save.href = window.URL.createObjectURL(file);
}

document.body.appendChild(save);

However, the newly created elements aren't indented of course. I've been looking at js-beautify but I also noticed that the mozilla page on parsing and serializing claims that you can use treewalker.

Would anyone know how I might go about doing such a thing? Or failing that, would there be a way to serialize a node without it's children in order to run a recursive loop like this :

var output = [];
var serializer = new XMLSerializer();

function indent(node) {
    var ancestor = node;

    while (ancestor != document.documentElement) {
        output.push("   ");
        ancestor = ancestor.parentNode;
    }

    output.push(/* serialize node tagname + attributes */);
    output.push("\n");

    for (let child of node.children) {
        indent(child);
    }

    output.push(/* node closing tag*/);
}

indent(document.documentElement);

Don't hesitate tell me if I'm barking up the wrong tree, and thank you for your time.

By way of a reply to my own question, you can serialize a shallow clone to get the opening and closing tags of a node :

var save = document.createElement("a");

save.classList.add("button");
save.textContent = "save";
save.download = "layout.html"
save.onclick = function(event) {
    document.body.removeChild(save);

    var output = [];
    var serializer = new XMLSerializer();

    function indent(node) {
        function offset(node) {
            var count = 0;
            var ancestor = node;

            while (ancestor != document.documentElement) {
                count++;
                ancestor = ancestor.parentNode;
            }
            return "\t".repeat(count);
        }

        var buffer = offset(node);
        var nodeClone = serializer.serializeToString(node.cloneNode(false)).replace(' xmlns="http://www.w3.org/1999/xhtml"',"");

        if (node.children.length) {
            let tagSplit = nodeClone.replace(/(<.+>)(<\/.+>)/,"$1<!--children-->$2").split("<!--children-->");

            output.push(buffer + tagSplit[0] + "\n");

            for (let child of node.children) {
                indent(child);
            }

            output.push(buffer + tagSplit[1] + "\n");
        } else {
            output.push(buffer + nodeClone + "\n");
        }
    }

    indent(document.documentElement);

    var file = new window.Blob(output,{type:"text/html"});
    save.href = window.URL.createObjectURL(file);
}

document.body.appendChild(save);

manually removing the xhtml namespace is a bit of a shame but since it's XMLSerializer I couldn't see any way around that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM