Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | 1x 1x 1x 5x 5x 1x 4x 2x 1x 3x 1x 9x 7x 3x 3x 7x 7x 2x 2x 9x 9x 1x 8x 4x 3x 5x 1x 10x 9x 3x 3x 9x 9x 1x 1x 10x 10x 10x 3x 8x 1x 9x 1x 12x 12x 2x 10x 2x 1x 9x 1x 3x 1x 3x 2x 1x 2x 2x 1x 1x 1x 1x 5x 1x 5x 2x 1x 1x 1x 1x 1x 5x 1x 2x 1x 2x 1x 1x 1x 1x 2x 2x 2x 2x 1x 24x 24x 6x 24x 39x 1x 7x 7x 7x 2x 5x 1x 7x 7x 7x 2x 5x 1x 8x 4x 8x 4x 4x 1x | import * as types from './types'; import * as throws from './throws'; /** * ID から要素取得を強制。 * * @param elementId * @param elementType * @returns * @throws {throws.NotFoundDomSelectorError} セレクタから要素が見つからない * @throws {throws.ElementTypeError} 要素に指定された型が合わない */ export function requireElementById<THtmlElement extends HTMLElement>(elementId: string, elementType?: types.Constructor<THtmlElement>): THtmlElement { const result = document.getElementById(elementId); if (!result) { throw new throws.NotFoundDomSelectorError(elementId); } if (elementType) { if (!types.instanceOf(result, elementType)) { throw new throws.ElementTypeError(`${result.constructor.name} != ${elementType.prototype.constructor.name}`); } } return result as THtmlElement; } /** * セレクタから要素取得を強制。 * * @param element * @param selectors * @returns */ export function requireSelector<K extends keyof HTMLElementTagNameMap>(element: ParentNode, selectors: K): HTMLElementTagNameMap[K]; export function requireSelector<K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K]; export function requireSelector<K extends keyof SVGElementTagNameMap>(element: ParentNode, selectors: K): SVGElementTagNameMap[K]; export function requireSelector<K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K]; export function requireSelector<TElement extends Element = Element>(selectors: string, elementType?: types.Constructor<TElement>): TElement; export function requireSelector<TElement extends Element = Element>(element: ParentNode, selectors: string, elementType?: types.Constructor<TElement>): TElement; export function requireSelector<TElement extends Element = Element>(element: ParentNode | string | null, selectors?: string | types.Constructor<TElement>, elementType?: types.Constructor<TElement>): TElement { if (types.isString(element)) { if (selectors) { Iif (types.isString(selectors)) { throw new throws.MismatchArgumentError('selectors'); } else { elementType = selectors; } } selectors = element; element = null; } else { Iif (types.isUndefined(selectors)) { throw new throws.MismatchArgumentError('selectors'); } else Iif (!types.isString(selectors)) { throw new throws.MismatchArgumentError('selectors'); } } const result = (element ?? document).querySelector(selectors); if (!result) { throw new throws.NotFoundDomSelectorError(selectors); } if (elementType) { if (!types.instanceOf(result, elementType)) { throw new throws.ElementTypeError(`${result.constructor.name} != ${elementType.prototype.constructor.name}`); } } return result as TElement; } /** * セレクタに一致する要素リストの取得を強制。 * @param element * @param selectors */ export function requireSelectorAll<K extends keyof HTMLElementTagNameMap>(element: ParentNode, selectors: K): NodeListOf<HTMLElementTagNameMap[K]>; export function requireSelectorAll<K extends keyof HTMLElementTagNameMap>(selectors: K): NodeListOf<HTMLElementTagNameMap[K]>; export function requireSelectorAll<K extends keyof SVGElementTagNameMap>(element: ParentNode, selectors: K): NodeListOf<SVGElementTagNameMap[K]>; export function requireSelectorAll<K extends keyof SVGElementTagNameMap>(selectors: K): NodeListOf<SVGElementTagNameMap[K]>; export function requireSelectorAll<TElement extends Element = Element>(selectors: string, elementType?: types.Constructor<TElement>): NodeListOf<TElement>; export function requireSelectorAll<TElement extends Element = Element>(element: ParentNode, selectors: string, elementType?: types.Constructor<TElement>): NodeListOf<TElement>; export function requireSelectorAll<TElement extends Element = Element>(element: ParentNode | string | null, selectors?: string | types.Constructor<TElement>, elementType?: types.Constructor<TElement>): NodeListOf<TElement> { if (types.isString(element)) { if (selectors) { Iif (types.isString(selectors)) { throw new throws.MismatchArgumentError('selectors'); } else { elementType = selectors; } } selectors = element; element = null; } else { Iif (types.isUndefined(selectors)) { throw new throws.MismatchArgumentError('selectors'); } else Iif (!types.isString(selectors)) { throw new throws.MismatchArgumentError('selectors'); } } const result = (element ?? document).querySelectorAll<TElement>(selectors); Iif (!result) { throw new throws.NotFoundDomSelectorError(selectors); } if (elementType) { for (const elm of result) { if (!types.instanceOf(elm, elementType)) { throw new throws.ElementTypeError(`elm ${elm} != ${elementType.prototype.constructor.name}`); } } } return result; } /** * セレクタから先祖要素を取得。 * * @param selectors * @param element * @returns */ export function requireClosest<K extends keyof HTMLElementTagNameMap>(element: Element, selectors: K): HTMLElementTagNameMap[K]; export function requireClosest<K extends keyof SVGElementTagNameMap>(element: Element, selectors: K): SVGElementTagNameMap[K]; export function requireClosest<E extends Element = Element>(element: Element, selectors: string, elementType?: types.Constructor<E>): E; export function requireClosest<TElement extends Element = Element>(element: Element, selectors: string, elementType?: types.Constructor<TElement>): Element { const result = element.closest(selectors); if (!result) { throw new throws.NotFoundDomSelectorError(selectors); } if (elementType) { if (!types.instanceOf(result, elementType)) { throw new throws.ElementTypeError(`${result.constructor.name} != ${elementType.prototype.constructor.name}`); } } return result; } /** * 対象要素から所属する `Form` 要素を取得する。 * @param element `Form` に所属する要素。 * @returns */ export function getParentForm(element: Element): HTMLFormElement { return requireClosest(element, 'form'); } /** * テンプレートを実体化。 * @param selectors */ export function cloneTemplate(selectors: string): DocumentFragment; export function cloneTemplate(element: HTMLTemplateElement): DocumentFragment; export function cloneTemplate(input: string | HTMLTemplateElement): DocumentFragment { if (typeof input === 'string') { const element = requireSelector(input, HTMLTemplateElement); input = element; } const result = input.content.cloneNode(true); return result as DocumentFragment; } /** * 要素生成処理の構築。 * * @param tagName * @param options */ export function createFactory<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementCreationOptions): TagFactory<HTMLElementTagNameMap[K]>; /** @deprecated */ export function createFactory<K extends keyof HTMLElementDeprecatedTagNameMap>(tagName: K, options?: ElementCreationOptions): TagFactory<HTMLElementDeprecatedTagNameMap[K]>; export function createFactory<THTMLElement extends HTMLElement>(tagName: string, options?: ElementCreationOptions): TagFactory<THTMLElement>; export function createFactory(tagName: string, options?: ElementCreationOptions): TagFactory<HTMLElement> { const element = document.createElement(tagName, options); return new TagFactory(element); } /** * 要素の追加位置。 */ export const enum AttachPosition { /** 最後。 */ Last, /** 最初。 */ First, /** 直前。 */ Previous, /** 直後。 */ Next, } /** * 指定した要素から見た特定の位置に要素をくっつける * @param parent 指定要素。 * @param position 位置。 * @param factory 追加する要素。 */ export function attach(parent: Element, position: AttachPosition, factory: NodeFactory): Node; export function attach<TElement extends Element = Element>(parent: Element, position: AttachPosition, factory: TagFactory<TElement>): TElement; export function attach(parent: Element, position: AttachPosition, node: Node): Node; export function attach(parent: Element, position: AttachPosition, node: Node | NodeFactory): Node { if (isNodeFactory(node)) { node = node.element; } switch (position) { case AttachPosition.Last: return parent.appendChild(node); case AttachPosition.First: return parent.insertBefore(node, parent.firstChild); case AttachPosition.Previous: Iif (!parent.parentNode) { throw new TypeError('parent.parentNode'); } return parent.parentNode.insertBefore(node, parent); case AttachPosition.Next: Iif (!parent.parentNode) { throw new TypeError('parent.parentNode'); } return parent.parentNode.insertBefore(node, parent.nextSibling); default: throw new throws.NotImplementedError(); } } function isNodeFactory(arg: unknown): arg is NodeFactory { return types.hasObject(arg, 'element'); } /** * ノード生成処理。 */ export interface NodeFactory { //#region property readonly element: Node; //#endregion } /** * テキストノード生成処理。 */ export class TextFactory implements NodeFactory { constructor(public readonly element: Text) { } } /** * 要素生成処理。 */ export class TagFactory<TElement extends Element> implements NodeFactory { constructor(public readonly element: TElement) { } public createTag<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementCreationOptions): TagFactory<HTMLElementTagNameMap[K]>; /** @deprecated */ public createTag<K extends keyof HTMLElementDeprecatedTagNameMap>(tagName: K, options?: ElementCreationOptions): TagFactory<HTMLElementDeprecatedTagNameMap[K]>; public createTag<THTMLElement extends HTMLElement>(tagName: string, options?: ElementCreationOptions): TagFactory<THTMLElement>; public createTag(tagName: string, options?: ElementCreationOptions): TagFactory<HTMLElement> { const createdElement = document.createElement(tagName, options); this.element.appendChild(createdElement); const nodeFactory = new TagFactory(createdElement); return nodeFactory; } public createText(text: string): TextFactory { const createdNode = document.createTextNode(text); this.element.appendChild(createdNode); const nodeFactory = new TextFactory(createdNode); return nodeFactory; } } /** * カスタムデータ属性のケバブ名を dataset アクセス可能な名前に変更 * @param kebab データ属性名。 * @param removeDataAttributeBegin 先頭の `data-`* を破棄するか。 */ export function toCustomKey(kebab: string, removeDataAttributeBegin: boolean = true): string { const dataHead = 'data-'; if (removeDataAttributeBegin && kebab.startsWith(dataHead)) { kebab = kebab.substring(dataHead.length); } return kebab .split('-') .map((item, index) => index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item.toLowerCase() ) .join('') ; } /** * データ属性から値を取得。 * * @param element 要素。 * @param dataKey データ属性名。 * @param removeDataAttributeBegin 先頭の `data-` を破棄するか。 * @returns */ export function getDataset(element: HTMLOrSVGElement, dataKey: string, removeDataAttributeBegin: boolean = true): string { const key = toCustomKey(dataKey, removeDataAttributeBegin); const value = element.dataset[key]; if (types.isUndefined(value)) { throw new Error(`${element}.${key}`); } return value; } /** * データ属性から値を取得。 * * @param element 要素。 * @param dataKey データ属性名。 * @param fallback 取得失敗時の返却値。 * @param removeDataAttributeBegin 先頭の `data-`* を破棄するか。 * @returns */ export function getDatasetOr(element: HTMLOrSVGElement, dataKey: string, fallback: string, removeDataAttributeBegin: boolean = true): string { const key = toCustomKey(dataKey, removeDataAttributeBegin); const value = element.dataset[key]; if (types.isUndefined(value)) { return fallback; } return value; } type HtmlTagName = Uppercase<keyof HTMLElementTagNameMap | keyof HTMLElementDeprecatedTagNameMap | keyof SVGElementTagNameMap> | Lowercase<keyof HTMLElementTagNameMap | keyof HTMLElementDeprecatedTagNameMap | keyof SVGElementTagNameMap>; /** * 要素のタグ名の一致判定。 * * @param element 対象要素 * @param value タグ名 * @returns */ export function equalTagName(element: Element, value: HtmlTagName): boolean; export function equalTagName(element: Element, value: string): boolean; export function equalTagName(element: Element, value: Element): boolean export function equalTagName(element: Element, value: string | Element): boolean { if (!types.isString(value)) { value = value.tagName; } if (element.tagName === value) { return true; } return element.tagName.toUpperCase() === value.toUpperCase(); } /** * 指定要素を兄弟間で上下させる。 * @param current 対象要素。 * @param isUp 上に移動させるか(偽の場合下に移動)。 */ export function moveElement(current: HTMLElement, isUp: boolean): void { const refElement = isUp ? current.previousElementSibling : current.nextElementSibling ; Iif (refElement) { const newItem = isUp ? current : refElement; const oldItem = isUp ? refElement : current; current.parentElement!.insertBefore(newItem, oldItem); } } |