html/internal/Escape.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
474d70fe922979da250c88c0acbdbb90e0be2fdd- 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 contextual escaping kernel shared by public escaping and rendering.
//! It operates on UTF-8 octets: every replaced character is ASCII, and UTF-8
//! never encodes another scalar with an ASCII octet, so every other scalar is
//! copied unchanged without normalization.
entity: (text) => {
octets: Encoding.UTF8.encode(text)
octets.freeze()
}
ampersand: entity("&")
lessThan: entity("<")
greaterThan: entity(">")
carriageReturn: entity(" ")
quotation: entity(""")
apostrophe: entity("'")
// The single replacement classification: answers the frozen replacement octets
// for `octet` in its context, or null when the octet is copied unchanged.
// U+0000 signals an ordinary Error.
replacement: (octet, attribute) => {
if(octet == 0) { fail() }
if(octet == 38) { ^ampersand }
if(octet == 60) { ^lessThan }
if(octet == 62) { ^greaterThan }
if(octet == 13) { ^carriageReturn }
if(attribute && (octet == 34)) { ^quotation }
if(attribute && (octet == 39)) { ^apostrophe }
null
}
// Answers the exact escaped octet count of `source` without materializing it,
// so callers can check capacity before appending.
measure: (source, attribute) => {
total: 0
for(octet in source) {
selected: replacement(octet, attribute)
if(selected === null) { total = total + 1 } else { total = total + selected.size() }
}
total
}
ascii: (output, text) => {
for(octet in Encoding.UTF8.encode(text)) { output.add(octet) }
output
}
// Appends the escaped form of `source` directly to `output`.
append: (output, source, attribute) => {
for(octet in source) {
selected: replacement(octet, attribute)
if(selected === null) {
output.add(octet)
} else {
for(replaced in selected) { output.add(replaced) }
}
}
output
}
// A synthetic BOM is consumed by the ordinary UTF-8 decoder. It is private
// state: it is never counted as emitted output or logical work, and any
// supplied U+FEFF follows it and therefore survives, even at the beginning.
buffer: () => { Encoding.UTF8.encode("\u{feff}") }
string: (output) => { Encoding.UTF8.decode(output) }
escape: (text, attribute) => {
String.recognizes(text).ifFalse(() => { fail() })
source: Encoding.UTF8.encode(text)
measure(source, attribute)
output: buffer()
append(output, source, attribute)
string(output)
}