-
-
Save ikasoba/e43e231514a905df77d084121c2a5aef to your computer and use it in GitHub Desktop.
htmlを出力するための関数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface HElement { | |
tag: string, attrs: Record<string,string>, children: (HElement|string)[], toHTML(): string, toDOM(): Element | |
} | |
export type HComponent = ((attrs:Record<string,string>,children:HElement["children"],componentUtil:HComponentUtil)=>HElement) | |
export interface HComponentUtil_nonPrivate { | |
__nativeElement__?:Element | |
__event__: { | |
didMount:null|(()=>void) | |
} | |
} | |
export class HComponentUtil { | |
private __nativeElement__?:HComponentUtil_nonPrivate["__nativeElement__"] | |
private __event__:HComponentUtil_nonPrivate["__event__"] = { | |
didMount:null as null|(()=>void) | |
} | |
didMount(f:(()=>void)): void { | |
if (!this.__event__.didMount)this.__event__.didMount = f | |
} | |
} | |
export const isNode = (): (typeof globalThis.process) extends undefined ? false : true => globalThis.process !== undefined | |
export const h = (()=>{ | |
const escapeHTML = (text:string) => ["&","<",">","=",'"',"'","`","/"].forEach( c => text = text.replaceAll(c,"&#x"+c.charCodeAt(0).toString(16)+";") ) as undefined || text | |
return (tag:string|HComponent,attrs: Record<string,string>,...children: HElement["children"]): HElement => { | |
if (typeof tag === "string"){ | |
let __tag = tag | |
const __attrs = attrs | |
let __element:ReturnType<typeof isNode> extends true ? Element : undefined | |
if (!isNode()){ | |
//TODO | |
} | |
return ({ | |
set tag(v:string){ | |
__tag = v | |
}, | |
get tag(){ | |
return __tag | |
}, | |
get attrs(){ | |
return __attrs | |
}, | |
children: children, | |
toHTML(){ | |
return `<${tag} ${Object.entries(attrs).map(([k,v]) => `${k}="${escapeHTML(v)}"`).join(" ")}>${children.map( x => typeof x === "string" ? escapeHTML(x) : x.toHTML()).join("")}</${tag}>` | |
}, | |
toDOM(){ | |
// TODO | |
} | |
}) | |
} | |
else return tag(attrs,children,new HComponentUtil()) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment