IF Commons GitHub

logical/xml

Written to: 2 requirements: 2 proved, 0 partly, 0 failing, 0 with no test recorded. 0003-anyfs-serve-logical FR-001, 0003-anyfs-serve-logical FR-002

XML for the bodies of WebDAV, strict and bounded. The reader takes one document of elements, attributes, text, CDATA, comments and the five named references and the numeric ones, and refuses everything that a document may use to attack its reader: a document type (so no entity is ever declared or expanded and no external subset is read), a reference it does not know, a name of a prefix that is not declared, and a document larger, deeper or with more parts than the limits allow. The writer escapes every text and attribute and declares each namespace once on the root.

import { parseXml, writeXml, dav } from "./xml.ts";
const doc = parseXml('<D:propfind xmlns:D="DAV:"><D:allprop/></D:propfind>', { maxBytes: 1024, maxDepth: 8, maxNodes: 100 });
writeXml({ name: dav("multistatus"), attrs: [], children: [] }); // '<?xml version="1.0" encoding="utf-8"?>\n<D:multistatus xmlns:D="DAV:"/>'

Source: commons/anyfs-serve/src/logical/xml.ts

7 of 16 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

QNametypeno test imports it
type QName = { readonly ns: string; readonly local: string };

A name with its namespace.

XmlAttrtypeno test imports it
type XmlAttr = { readonly name: string; readonly value: string };

One attribute: a name without a namespace prefix of its own (xmlns declarations are not attributes here) and its value.

XmlElementtype
type XmlElement = { readonly kind: "element"; readonly name: QName; readonly attrs: readonly XmlAttr[]; readonly children: readonly XmlNode[] };

An element: its name, attributes and children.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

XmlTexttypeno test imports it
type XmlText = { readonly kind: "text"; readonly text: string };

A piece of text.

XmlNodetypeno test imports it
type XmlNode = XmlElement | XmlText;

What an element holds.

DAV_NSconstno test imports it
const DAV_NS: "DAV:"

The namespace of WebDAV.

davconst
const dav: (local: string) => QName

A name in the namespace of WebDAV.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

elfunction
function el(name: QName, children?: readonly (string | XmlNode)[], attrs?: readonly XmlAttr[]): XmlElement

An element, with children that are elements or texts.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

XmlLimitstype
type XmlLimits = { readonly maxBytes: number; readonly maxDepth: number; readonly maxNodes: number };

What the reader is allowed.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

XmlProblemtypeno test imports it
type XmlProblem = | { readonly kind: "too-large"; readonly bytes: number } | { readonly kind: "too-deep"; readonly a… (483 characters)
type XmlProblem =
  | { readonly kind: "too-large"; readonly bytes: number }
  | { readonly kind: "too-deep"; readonly at: number }
  | { readonly kind: "too-many"; readonly at: number }
  | { readonly kind: "doctype"; readonly at: number }
  | { readonly kind: "reference"; readonly at: number; readonly text: string }
  | { readonly kind: "namespace"; readonly at: number; readonly prefix: string }
  | { readonly kind: "malformed"; readonly at: number; readonly message: string };

Why a document is not read: a closed list.

describeXmlProblemfunction
function describeXmlProblem(p: XmlProblem): string

A problem in a sentence.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

parseXmlfunction
function parseXml(text: string, limits: XmlLimits): Result<XmlElement, XmlProblem>

One document, or why it is not read.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

childElementsfunctionno test imports it
function childElements(e: XmlElement): readonly XmlElement[]

The child elements of an element, in order.

textOffunctionno test imports it
function textOf(e: XmlElement): string

The text an element holds below it, joined.

isNamedconstno test imports it
const isNamed: (e: XmlElement, ns: string, local: string) => boolean

Whether an element has a name.

writeXmlfunction
function writeXml(root: XmlElement): string

One element as XML, with the namespaces declared once on it: DAV: is D, the others are n1, n2.

Imported by 14 tests of the run (14 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.