|
| 1 | +type AttributeValue = string | number | boolean | null | undefined; |
| 2 | + |
| 3 | +type ContentOptions = |
| 4 | + | { text?: string; html?: never } |
| 5 | + | { html?: string; text?: never } |
| 6 | + | { text?: undefined; html?: undefined }; |
| 7 | + |
| 8 | +export type CommonOptions = { |
| 9 | + id?: string; |
| 10 | + dataset?: Record<string, AttributeValue>; |
| 11 | + attrs?: Record<string, AttributeValue>; |
| 12 | +}; |
| 13 | + |
| 14 | +export type BaseOptions = CommonOptions & { |
| 15 | + classList?: string[]; |
| 16 | + style?: Partial<CSSStyleDeclaration>; |
| 17 | +} & ContentOptions; |
| 18 | + |
| 19 | +export type AnchorOptions = BaseOptions & { |
| 20 | + href?: URL | string; |
| 21 | + target?: '_blank' | '_self' | '_parent' | '_top' | (string & {}); |
| 22 | +} |
| 23 | + |
| 24 | +export type ScriptOptions = CommonOptions & { |
| 25 | + src?: URL | string; |
| 26 | + type?: 'module' | 'importmap' | 'text/javascript' | (string & {}); |
| 27 | + async?: boolean; |
| 28 | + defer?: boolean; |
| 29 | +}; |
| 30 | + |
| 31 | +export type LinkOptions = CommonOptions & { |
| 32 | + as?: string; |
| 33 | + rel?: 'stylesheet' | (string & {}); |
| 34 | + href?: URL | string; |
| 35 | + type?: string; |
| 36 | + media?: string; |
| 37 | + title?: string; |
| 38 | +} |
| 39 | + |
| 40 | +type TagsWithCustomOptions = 'a' | 'script' | 'link'; |
| 41 | + |
| 42 | +function normalizeAttributeValue(value: AttributeValue, dataset = false): string | null { |
| 43 | + if (typeof value === 'undefined' || null === value) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof value === 'boolean') { |
| 48 | + return value ? (dataset ? 'true' : '1') : (dataset ? 'false' : '0'); |
| 49 | + } |
| 50 | + |
| 51 | + return String(value); |
| 52 | +} |
| 53 | + |
| 54 | +function applyAnchorOptions(el: HTMLAnchorElement, options: AnchorOptions): void { |
| 55 | + if (options.href !== undefined) { |
| 56 | + el.href = options.href instanceof URL ? options.href.href : options.href; |
| 57 | + } |
| 58 | + |
| 59 | + if (options.target !== undefined) { |
| 60 | + el.target = options.target; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function applyScriptOptions(el: HTMLScriptElement, options: ScriptOptions): void { |
| 65 | + if (options.src) { |
| 66 | + el.src = options.src instanceof URL ? options.src.href : options.src; |
| 67 | + } |
| 68 | + |
| 69 | + if (options.type !== undefined) { |
| 70 | + el.type = options.type; |
| 71 | + } |
| 72 | + |
| 73 | + if (options.async !== undefined) { |
| 74 | + el.async = options.async; |
| 75 | + } |
| 76 | + |
| 77 | + if (options.defer !== undefined) { |
| 78 | + el.defer = options.defer; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function applyLinkOptions(el: HTMLLinkElement, options: LinkOptions): void { |
| 83 | + if (options.as !== undefined) { |
| 84 | + el.as = options.as; |
| 85 | + } |
| 86 | + |
| 87 | + if (options.rel !== undefined) { |
| 88 | + el.rel = options.rel; |
| 89 | + } |
| 90 | + |
| 91 | + if (options.href !== undefined) { |
| 92 | + el.href = options.href instanceof URL ? options.href.href : options.href; |
| 93 | + } |
| 94 | + |
| 95 | + if (options.type !== undefined) { |
| 96 | + el.type = options.type; |
| 97 | + } |
| 98 | + |
| 99 | + if (options.media !== undefined) { |
| 100 | + el.media = options.media; |
| 101 | + } |
| 102 | + |
| 103 | + if (options.title !== undefined) { |
| 104 | + el.title = options.title; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +export function createElement(tag: 'a', options: AnchorOptions): HTMLAnchorElement; |
| 109 | +export function createElement(tag: 'script', options: ScriptOptions): HTMLScriptElement; |
| 110 | +export function createElement(tag: 'link', options: LinkOptions): HTMLLinkElement; |
| 111 | +export function createElement<K extends Exclude<keyof HTMLElementTagNameMap, TagsWithCustomOptions>>(tag: K, options?: BaseOptions): HTMLElementTagNameMap[K]; |
| 112 | + |
| 113 | +export function createElement(tag: string, options: BaseOptions = {}): HTMLElement { |
| 114 | + const el = document.createElement(tag); |
| 115 | + |
| 116 | + if (options.id) { |
| 117 | + el.id = options.id; |
| 118 | + } |
| 119 | + |
| 120 | + if (options.classList && options.classList.length) { |
| 121 | + el.classList.add(...options.classList); |
| 122 | + } |
| 123 | + |
| 124 | + if (options.dataset) { |
| 125 | + Object.entries(options.dataset).forEach(([k, v]) => { |
| 126 | + v = normalizeAttributeValue(v, true); |
| 127 | + |
| 128 | + if (null !== v) { |
| 129 | + (el.dataset)[k] = v; |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + if (options.attrs) { |
| 135 | + Object.entries(options.attrs).forEach(([k, v]) => { |
| 136 | + v = normalizeAttributeValue(v, false); |
| 137 | + |
| 138 | + if (null !== v) { |
| 139 | + el.setAttribute(k, v); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + if (options.text !== undefined) { |
| 145 | + el.textContent = options.text; |
| 146 | + } |
| 147 | + |
| 148 | + if (options.html !== undefined) { |
| 149 | + el.innerHTML = options.html; |
| 150 | + } |
| 151 | + |
| 152 | + if (options.style) { |
| 153 | + Object.assign(el.style, options.style); |
| 154 | + } |
| 155 | + |
| 156 | + if ('a' === tag) { |
| 157 | + applyAnchorOptions(el as HTMLAnchorElement, options as AnchorOptions); |
| 158 | + } |
| 159 | + |
| 160 | + if ('script' === tag) { |
| 161 | + applyScriptOptions(el as HTMLScriptElement, options as ScriptOptions); |
| 162 | + } |
| 163 | + |
| 164 | + if ('link' === tag) { |
| 165 | + applyLinkOptions(el as HTMLLinkElement, options as LinkOptions); |
| 166 | + } |
| 167 | + |
| 168 | + return el; |
| 169 | +} |
0 commit comments