Improved case rewriting

This commit is contained in:
Talia 2025-02-17 15:12:28 +01:00
parent ef9903adce
commit 97fd4d5f63
2 changed files with 12 additions and 4 deletions

11
mini.js
View file

@ -1,7 +1,14 @@
export default new Proxy(document, {
/**
* @param {String} name
* @return {String}
*/
const snakeToHTML = name => name.replace(/([A-Z])/g, "-$1").replace(/^-/, "").toLowerCase()
// @ts-ignore
export default new Proxy(/** @type {Object<string,(...args: any)=>HTMLElement>} */(document), {
/** @param {string} tag */
get: (_, tag) => /** @param {any[]} args */ (...args) => {
let node = document.createElement(tag)
let node = document.createElement(snakeToHTML(tag))
for (const arg of args) {
if (arg instanceof HTMLElement) {
node.append(arg)

View file

@ -25,7 +25,8 @@ export const nothing = Symbol("Explicit non-argument for Nyooom")
* @param {string} key
* @return {string}
*/
const snakeToCSS = key => key.replace(/^[A-Z]/, a => "-" + a).replace(/[A-Z]/g, a => '-' + a.toLowerCase())
const snakeToCSS = key => key.replace(/([A-Z])/g, "-$1").replace(/^-/, "--").toLowerCase()
const snakeToHTML = key => key.replace(/([A-Z])/g, "-$1").replace(/^-/, "").toLowerCase()
/** @typedef SpecialAttributeDescriptor
* @type {object}
@ -358,7 +359,7 @@ export class DomHtmlRenderer extends DomRenderer {
* @return {Node}
*/
createElement(name, options) {
return document.createElement(name.replace(/([a-z])([A-Z])/g, "$1-$2"), options)
return document.createElement(snakeToHTML(name), options)
}
/** @type {Object<string,SpecialAttributeDescriptor>} */