Improved case rewriting
This commit is contained in:
parent
ef9903adce
commit
97fd4d5f63
2 changed files with 12 additions and 4 deletions
11
mini.js
11
mini.js
|
@ -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 */
|
/** @param {string} tag */
|
||||||
get: (_, tag) => /** @param {any[]} args */ (...args) => {
|
get: (_, tag) => /** @param {any[]} args */ (...args) => {
|
||||||
let node = document.createElement(tag)
|
let node = document.createElement(snakeToHTML(tag))
|
||||||
for (const arg of args) {
|
for (const arg of args) {
|
||||||
if (arg instanceof HTMLElement) {
|
if (arg instanceof HTMLElement) {
|
||||||
node.append(arg)
|
node.append(arg)
|
||||||
|
|
|
@ -25,7 +25,8 @@ export const nothing = Symbol("Explicit non-argument for Nyooom")
|
||||||
* @param {string} key
|
* @param {string} key
|
||||||
* @return {string}
|
* @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
|
/** @typedef SpecialAttributeDescriptor
|
||||||
* @type {object}
|
* @type {object}
|
||||||
|
@ -358,7 +359,7 @@ export class DomHtmlRenderer extends DomRenderer {
|
||||||
* @return {Node}
|
* @return {Node}
|
||||||
*/
|
*/
|
||||||
createElement(name, options) {
|
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>} */
|
/** @type {Object<string,SpecialAttributeDescriptor>} */
|
||||||
|
|
Loading…
Reference in a new issue