html/internal/Serializer.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
ca21ce4accd68a970782cd666ac36a9daf61580c
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! The single compact HTML serialization kernel. Traversal uses explicit
//! linked frames that advance through child Arrays by index, so depth never
//! depends on host recursion. Every reachable value is validated before it is
//! interpreted, and all output accumulates in one private Bytes buffer.
Nodes: import("./Nodes.protos")
Rules: import("./Rules.protos")
Escape: import("./Escape.protos")
Limits: import("./Limits.protos")

render: (root, limits, prototype) => {
    captured: Limits.capture(limits)
    state: Limits.create(captured, prototype)
    output: Escape.buffer()
    active: IdentityMap()
    stack: null
    // Output octet count immediately after the most recent pre opening tag.
    preStart: -1

    ascii: (text) => {
        Limits.output(state, text.size())
        Escape.ascii(output, text)
    }

    // Every scalar emits at least one octet, so the String scalar length is an
    // exact work contribution for text and a lower bound on output; both are
    // checked before the String is encoded.
    escaped: (text, attribute) => {
        scalars: text.size()
        // Attribute value scalars are charged when the entry is prepared.
        if(attribute == false) { Limits.work(state, scalars) }
        Limits.outputRoom(state, scalars)
        Limits.workRoom(state, scalars)
        source: Encoding.UTF8.encode(text)
        octets: Escape.measure(source, attribute)
        // An HTML parser drops one newline that directly follows <pre>.
        if((preStart == state.bytes) && (source.size() > 0) && (source[0] == 10)) {
            Limits.output(state, 1)
            output.add(10)
        }
        Limits.output(state, octets)
        Escape.append(output, source, attribute)
    }

    openTag: (node) => {
        name: node.slotValue("name")
        Limits.work(state, name.size())
        // Entries [key, name, kind, value], already charged and ASCII ordered.
        entries: Rules.prepare(name, node.slotValue("attributes"), state)
        ascii("<".concat(name))
        for(entry in entries) {
            if(entry[2] == "boolean") {
                if(entry[3] === true) { ascii(" ".concat(entry[1])) }
            }
            if(entry[2] != "boolean") {
                ascii(" ".concat(entry[1], "=\""))
                escaped(entry[3], true)
                ascii("\"")
            }
        }
        ascii(">")
        if(name == "pre") { preStart = state.bytes }
        name
    }

    // Visits one value occurrence at `depth`; `parent` is the enclosing frame.
    enter: (value, depth, parent, index) => {
        Limits.node(state, depth)
        kind: Nodes.kindOf(value)
        if((parent !== null) && (parent.name !== null) && (parent.name == "title") && (kind != "text")) { fail() }
        if(kind == "text") {
            escaped(value, false)
        }
        if(kind == "doctype") {
            rootFragment: (parent !== null) && (parent.name === null) && (parent.depth == 0) && (index == 0)
            ((parent === null) || rootFragment).ifFalse(() => { fail() })
            ascii("<!DOCTYPE html>")
        }
        if((kind == "element") || (kind == "fragment")) {
            if(active.containsKey(value)) { fail() }
            children: value.slotValue("children")
            name: null
            if(kind == "element") {
                name = value.slotValue("name")
                if(Rules.isVoid(name) && (children.size() > 0)) { fail() }
                openTag(value)
            }
            leaf: (name !== null) && Rules.isVoid(name)
            if(leaf == false) {
                active[value] = true
                stack = { node: value; name: name; children: children; index: 0; depth: depth; next: stack }
            }
        }
    }

    enter(root, 0, null, 0)
    while(stack !== null) {
        frame: stack
        if(frame.index < frame.children.size()) {
            index: frame.index
            frame.index = index + 1
            enter(frame.children[index], frame.depth + 1, frame, index)
        } else {
            stack = frame.next
            active.remove(frame.node)
            if(frame.name !== null) { ascii("</".concat(frame.name, ">")) }
        }
    }
    Escape.string(output)
}