html/internal/Nodes.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
d03791834db20deb40cdd8755616e17250b57f2a
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.

//! Structured node construction and structural recognition. Nodes are ordinary
//! frozen objects; Strings are text. Freezing is shallow: caller-supplied
//! descendants keep their own state and are validated again when rendered.
Rules: import("./Rules.protos")
Arrays: import("std:collections/array")

// An empty matcher Map recognizes exactly normal standard Maps without
// invoking any behavior of the candidate.
mapMatcher: %{}
isMap: (value) => { mapMatcher.match(value) === true }

// Rejects scalar families before any message is sent to the candidate, so no
// Integer, Float, Boolean, or null is coerced and no Closure is invoked.
scalar: (value) => {
    (value === null) || (value === true) || (value === false) ||
        Integer.recognizes(value) || Float.recognizes(value)
}

// Answers "text", "element", "fragment" or "doctype" for an admissible value
// whose ordinary structural fields have exactly the node shape; otherwise
// signals an ordinary Error. Element names and attribute Maps are checked
// here; attribute entries are checked by the shared Rules traversal.
kindOf: (value) => {
    if(String.recognizes(value)) { ^"text" }
    if(scalar(value)) { fail() }
    value.hasSlot("kind").ifFalse(() => { fail() })
    kind: value.slotValue("kind")
    String.recognizes(kind).ifFalse(() => { fail() })
    size: value.slotNames().size()
    if(kind == "doctype") {
        if(size != 1) { fail() }
        ^kind
    }
    if(kind == "fragment") {
        if((size != 2) || (value.hasSlot("children") == false)) { fail() }
        Array.recognizes(value.slotValue("children")).ifFalse(() => { fail() })
        ^kind
    }
    if(kind == "element") {
        if(size != 4) { fail() }
        (value.hasSlot("name") && value.hasSlot("attributes") && value.hasSlot("children")).ifFalse(() => { fail() })
        Rules.isElement(value.slotValue("name")).ifFalse(() => { fail() })
        isMap(value.slotValue("attributes")).ifFalse(() => { fail() })
        Array.recognizes(value.slotValue("children")).ifFalse(() => { fail() })
        ^kind
    }
    fail()
}

// Shallow child admission at construction; the renderer validates the
// complete reachable structure.
admitChildren: (name, children) => {
    if((name !== null) && Rules.isVoid(name) && (children.size() > 0)) { fail() }
    index: 0
    for(child in children) {
        kind: kindOf(child)
        if((name !== null) && (name == "title") && (kind != "text")) { fail() }
        if((kind == "doctype") && ((name !== null) || (index > 0))) { fail() }
        index = index + 1
    }
}

element: (name, items) => {
    Rules.isElement(name).ifFalse(() => { fail() })
    attributes: Map()
    start: 0
    if((items.size() > 0) && isMap(items[0])) {
        items[0].each((key, value) => { attributes[key] = value })
        start = 1
    }
    Rules.validate(name, attributes)
    children: Arrays.slice(items, start, items.size())
    admitChildren(name, children)
    attributes.freeze()
    children.freeze()
    node: {
        kind: "element"
        name: name
        attributes: attributes
        children: children
    }
    node.freeze()
}

fragment: (items) => {
    children: Arrays.slice(items, 0, items.size())
    admitChildren(null, children)
    children.freeze()
    node: {
        kind: "fragment"
        children: children
    }
    node.freeze()
}

doctype: () => {
    node: { kind: "doctype" }
    node.freeze()
}