Make name filtering exclusive to HTML nodes

This commit is contained in:
Talia 2021-07-30 12:12:00 +02:00
parent f8db2ca876
commit cb7ad57874
Signed by: darkwiiplayer
GPG key ID: 7808674088232B3E

View file

@ -38,17 +38,18 @@ const parseArgs = (element, ...args) => {
element.setAttribute(key, parseAttribute(arg[key])) element.setAttribute(key, parseAttribute(arg[key]))
} }
const node = (name, args, xmlns) => { const node = (name, args, options) => {
let element let element
if (xmlns) if (options.nameFilter) name = options.nameFilter(name)
element = document.createElementNS(xmlns, name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()) if (options.xmlns)
element = document.createElementNS(options.xmlns, name)
else else
element = document.createElement(name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()) element = document.createElement(name)
parseArgs(element, args) parseArgs(element, args)
return element return element
} }
const nameSpacedProxy = (xmlns) => new Proxy(Window, { get: (target, prop, receiver) => { return (...args) => node(prop, args, xmlns) }}) const nameSpacedProxy = (options={}) => new Proxy(Window, { get: (target, prop, receiver) => { return (...args) => node(prop, args, options) }})
export const html = nameSpacedProxy() export const html = nameSpacedProxy({nameFilter: name => name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()})
export const svg = nameSpacedProxy("http://www.w3.org/2000/svg") export const svg = nameSpacedProxy({xmlns: "http://www.w3.org/2000/svg"})