Links: import("./Links.protos")
Limits: import("./Limits.protos")
Arrays: import("std:collections/array")
elementNames: Array(
"html", "head", "body", "title", "meta", "link",
"header", "footer", "main", "nav", "section", "article", "aside",
"div", "span", "p",
"h1", "h2", "h3", "h4", "h5", "h6",
"a", "img",
"strong", "em", "b", "i", "del", "s", "small", "mark",
"code", "pre", "blockquote",
"ul", "ol", "li", "dl", "dt", "dd",
"table", "caption", "thead", "tbody", "tfoot", "tr", "th", "td",
"br", "hr", "wbr",
"figure", "figcaption", "details", "summary",
"input", "label"
)
elementNames.freeze()
lookup: (...names) => {
result: Map()
for(name in names) { result[name] = true }
result.freeze()
}
elements: lookup(...elementNames)
voids: lookup("br", "hr", "img", "input", "link", "meta", "wbr")
globalNames: Array("id", "class", "title", "lang", "dir", "role")
globalNames.freeze()
kinds: Map()
for(name in elementNames) {
table: Map()
for(attribute in globalNames) { table[attribute] = "string" }
table["hidden"] = "boolean"
kinds[name] = table
}
add: (element, kind, ...names) => {
for(name in names) { kinds[element][name] = kind }
}
add("meta", "string", "charset", "name", "content", "property")
add("link", "string", "rel", "type", "media")
add("link", "uri", "href")
add("a", "string", "rel", "hreflang", "type")
add("a", "uri", "href")
add("img", "string", "alt", "width", "height", "loading")
add("img", "uri", "src")
add("ol", "string", "start", "type")
add("ol", "boolean", "reversed")
add("li", "string", "value")
add("th", "string", "colspan", "rowspan", "scope", "headers", "align")
add("td", "string", "colspan", "rowspan", "headers", "align")
add("label", "string", "for")
add("input", "string", "type")
add("input", "boolean", "checked", "disabled")
add("details", "boolean", "open")
add("blockquote", "uri", "cite")
add("del", "uri", "cite")
for(name in elementNames) { kinds[name].freeze() }
kinds.freeze()
metaNames: lookup(
"description", "author", "viewport", "robots", "keywords", "theme-color",
"twitter:title", "twitter:description", "twitter:image:alt", "twitter:card", "twitter:image"
)
metaProperties: lookup(
"og:title", "og:description", "og:site_name", "og:image:alt", "og:type",
"og:url", "og:image", "og:image:width", "og:image:height"
)
openGraphTypes: lookup("website", "article")
twitterCards: lookup("summary", "summary_large_image")
decimalDigits: lookup("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
linkRelations: lookup("stylesheet", "canonical", "alternate")
alignments: lookup("left", "center", "right")
isElement: (name) => { String.recognizes(name) && elements.containsKey(name) }
isVoid: (name) => { voids.containsKey(name) }
extension: (name) => {
source: Encoding.UTF8.encode(name)
if(source.size() <= 5) { ^false }
if(source[4] != 45) { ^false }
data: (source[0] == 100) && (source[1] == 97) && (source[2] == 116) && (source[3] == 97)
aria: (source[0] == 97) && (source[1] == 114) && (source[2] == 105) && (source[3] == 97)
if((data || aria) == false) { ^false }
index: 5
while(index < source.size()) {
octet: source[index]
allowed: ((octet >= 97) && (octet <= 122)) || ((octet >= 48) && (octet <= 57)) || (octet == 45)
if(allowed == false) { ^false }
index = index + 1
}
true
}
dimension: (text) => {
if(text.size() == 0) { fail() }
leading: true
text.each((scalar) => {
decimalDigits.containsKey(scalar).ifFalse(() => { fail() })
if(leading && (scalar == "0")) { fail() }
leading = false
})
}
metadata: (attributes) => {
has: (key) => { attributes.containsKey(key) }
charset: has("charset")
named: has("name")
described: has("property")
content: has("content")
if(charset) {
if(named || described || content) { fail() }
if(attributes["charset"] != "utf-8") { fail() }
^null
}
(content && (named != described)).ifFalse(() => { fail() })
value: attributes["content"]
if(named) {
selected: attributes["name"]
metaNames.containsKey(selected).ifFalse(() => { fail() })
if(selected == "twitter:card") { twitterCards.containsKey(value).ifFalse(() => { fail() }) }
if(selected == "twitter:image") { Links.admitHttps(value) }
^null
}
selector: attributes["property"]
metaProperties.containsKey(selector).ifFalse(() => { fail() })
if(selector == "og:type") { openGraphTypes.containsKey(value).ifFalse(() => { fail() }) }
if((selector == "og:url") || (selector == "og:image")) { Links.admitAbsolute(value) }
if((selector == "og:image:width") || (selector == "og:image:height")) { dimension(value) }
null
}
kindOf: (element, name) => {
String.recognizes(name).ifFalse(() => { fail() })
table: kinds[element]
if(table.containsKey(name)) { ^table[name] }
if(extension(name)) { ^"string" }
fail()
}
sized: null
sized = (count) => {
result: Array()
if(count > 0) {
half: sized(count.div(2))
result = Array(...half, ...half)
if((count % 2) == 1) {
result = Array(...result, null)
}
}
result
}
precedes: (left, right) => {
index: 0
while((index < left.size()) && (index < right.size())) {
if(left[index] != right[index]) { ^(left[index] < right[index]) }
index = index + 1
}
left.size() < right.size()
}
traverse: (element, attributes, visit) => {
attributes.each((name, value) => {
kind: kindOf(element, name)
if(kind == "boolean") {
((value === true) || (value === false)).ifFalse(() => { fail() })
}
if(kind == "string") { String.recognizes(value).ifFalse(() => { fail() }) }
if(kind == "uri") { Links.admit(value) }
visit(name, kind, value)
})
has: (name) => { attributes.containsKey(name) }
if(element == "meta") { metadata(attributes) }
if((element == "link") && has("rel")) {
linkRelations.containsKey(attributes["rel"]).ifFalse(() => { fail() })
}
if(element == "input") {
(has("type") && (attributes["type"] == "checkbox")).ifFalse(() => { fail() })
(has("disabled") && (attributes["disabled"] === true)).ifFalse(() => { fail() })
}
if(has("align")) { alignments.containsKey(attributes["align"]).ifFalse(() => { fail() }) }
}
validate: (element, attributes) => {
traverse(element, attributes, (name, kind, value) => { null })
attributes
}
prepare: (element, attributes, state) => {
count: attributes.size()
Limits.workRoom(state, count)
result: sized(count)
index: 0
traverse(element, attributes, (name, kind, value) => {
units: 1 + name.size()
if(kind != "boolean") { units = units + value.size() }
Limits.work(state, units)
result[index] = Array(Encoding.UTF8.encode(name), name, kind, value)
index = index + 1
})
Arrays.sort(result, (left, right) => precedes(left[0], right[0]))
}