MCPcopy Create free account
hub / github.com/Effect-TS/effect / openScope

Method openScope

packages/effect/src/encoding/Xml.ts:1826–1910  ·  view source on GitHub ↗
(
    name: string,
    attributes: Readonly<Record<string, string>>,
    i: number,
    selfClosing: boolean
  )

Source from the content-addressed store, hash-verified

1824
1825 // Applies the declarations of a start tag and resolves its names
1826 private openScope(
1827 name: string,
1828 attributes: Readonly<Record<string, string>>,
1829 i: number,
1830 selfClosing: boolean
1831 ): Scope {
1832 const undo = this.undoPrefixes.length
1833 let prefixed = 0
1834 for (const attribute in attributes) {
1835 const colon = this.qnameColon("attribute", attribute, i)
1836 let prefix: string
1837 if (attribute === "xmlns") {
1838 prefix = ""
1839 } else if (attribute.startsWith("xmlns:")) {
1840 prefix = attribute.slice(6)
1841 } else {
1842 if (colon !== -1) prefixed++
1843 continue
1844 }
1845 const uri = attributes[attribute]
1846 if (prefix === "xml") {
1847 if (uri !== xmlNamespace) throw this.syntax(`xmlns:xml can only bind ${xmlNamespace}`, i)
1848 continue
1849 }
1850 if (prefix === "xmlns") throw this.syntax("xmlns:xmlns cannot be declared", i)
1851 if (uri === xmlNamespace || uri === xmlnsNamespace) {
1852 throw this.syntax(`${attribute} cannot bind the reserved namespace ${uri}`, i)
1853 }
1854 if (uri === "" && prefix !== "") throw this.syntax(`${attribute} cannot be empty`, i)
1855 // Undeclared prefixes have no binding rather than an empty one
1856 const current = this.bindings.get(prefix)
1857 if ((current?.uri ?? "") === uri) continue
1858 const key = copyString(prefix)
1859 this.undoPrefixes.push(key)
1860 this.undoBindings.push(current)
1861 this.bindings.set(key, uri === "" ? undefined : this.intern(uri))
1862 this.openDeclarationSize += prefix.length + uri.length
1863 }
1864 if (!selfClosing && this.openDeclarationSize > this.maxTokenSize) {
1865 throw this.error(
1866 "Limit",
1867 `namespace declarations of open elements exceed maxTokenSize of ${this.maxTokenSize}`,
1868 this.base + i
1869 )
1870 }
1871 let namespace: string
1872 let localName = name
1873 const colon = this.qnameColon("element", name, i)
1874 if (colon === -1) {
1875 namespace = this.bindings.get("")?.uri ?? ""
1876 } else {
1877 const prefix = name.slice(0, colon)
1878 if (prefix === "xmlns") throw this.syntax(`element name ${name} cannot use the xmlns prefix`, i)
1879 const uri = this.bindings.get(prefix)?.uri
1880 if (uri === undefined) throw this.undeclared(`namespace prefix ${prefix} of element ${name} is not declared`, i)
1881 namespace = uri
1882 localName = name.slice(colon + 1)
1883 }

Callers 1

startTagMethod · 0.95

Calls 12

qnameColonMethod · 0.95
syntaxMethod · 0.95
internMethod · 0.95
errorMethod · 0.95
undeclaredMethod · 0.95
copyStringFunction · 0.85
sliceMethod · 0.80
getMethod · 0.65
pushMethod · 0.65
setMethod · 0.65
hasMethod · 0.65
addMethod · 0.65

Tested by

no test coverage detected