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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | 94x 94x 94x 94x 94x 94x 94x 94x 94x 92x 92x 19x 19x 92x 92x 92x 94x 94x 21x 21x 21x 21x 21x 94x 94x 52x 30x 30x 52x 94x 94x 9x 9x 9x 9x 94x 94x 28x 18x 23x 28x 94x 94x 405x 405x 405x 58x 405x 150x 405x 361x 44x 94x 94x 25x 23x 94x 54x 54x 50x 50x 54x 94x 25x 94x 94x 24x 24x 24x 2x 22x 94x 128x 22x 106x 106x 98x 3x 95x 106x 94x 94x 94x 17x 94x 94x 1x 1x 1x 1x 1x 1x 94x 94x 94x 94x 94x 288x 94x 1022x 94x 258x 240x 94x 10x 10x 10x 10x 10x 94x 94x 17x 17x 17x 17x 17x 17x 10x 10x 10x 10x 17x | /** * @module Utils * * @categoryDescription DOM: Altering * These functions alter the DOM tree, but could lead to forced layout if not * scheduled using {@link Utils.waitForMutateTime}. * * @categoryDescription DOM: Altering (optimized) * These functions alter the DOM tree in an optimized way using * {@link Utils.waitForMutateTime} and so are asynchronous. */ import * as MC from "@lisn/globals/minification-constants"; import * as MH from "@lisn/globals/minification-helpers"; import { settings } from "@lisn/globals/settings"; import { hideElement, hasAnyClass, addClassesNow, removeClassesNow, getData, setDataNow, setBooleanData, } from "@lisn/utils/css-alter"; import { asyncMutatorFor } from "@lisn/utils/dom-optimize"; import { isInlineTag } from "@lisn/utils/dom-query"; import { logWarn } from "@lisn/utils/log"; import { randId } from "@lisn/utils/text"; /** * Wraps the element in the given wrapper, or a newly created element if not given. * * @param [options.wrapper] * If it's an element, it is used as the wrapper. If it's a string * tag name, then a new element with this tag is created as the * wrapper. If not given, then `div` is used if the element to be * wrapped has an block-display tag, or otherwise `span` (if the * element to be wrapped has an inline tag name). * @param [options.ignoreMove] * If true, the DOM watcher instances will ignore the operation of * replacing the element (so as to not trigger relevant callbacks). * @returns The wrapper element that was either passed in options or created. * * @category DOM: Altering */ export const wrapElementNow = ( element: Element, options?: { wrapper?: HTMLElement | keyof HTMLElementTagNameMap; ignoreMove?: boolean; }, ) => { const wrapper = createWrapperFor(element, options?.wrapper); if (options?.ignoreMove === true) { ignoreMove(element, { from: MH.parentOf(element), to: wrapper, }); ignoreMove(wrapper, { to: MH.parentOf(element), }); } element.replaceWith(wrapper); wrapper.append(element); return wrapper; }; /** * Like {@link wrapElementNow} except it will {@link Utils.waitForMutateTime}. * * @category DOM: Altering (optimized) */ export const wrapElement = asyncMutatorFor(wrapElementNow); /** * Wraps the element's children in the given wrapper, or a newly created element * if not given. * * @see {@link wrapElementNow}. * * @category DOM: Altering */ export const wrapChildrenNow = ( element: Element, options?: { wrapper?: HTMLElement | keyof HTMLElementTagNameMap; ignoreMove?: boolean; }, ) => { const wrapper = createWrapperFor(element, options?.wrapper); const { ignoreMove } = options ?? {}; moveChildrenNow(element, wrapper, { ignoreMove }); moveElementNow(wrapper, { to: element, ignoreMove, }); return wrapper; }; /** * Like {@link wrapChildrenNow} except it will {@link Utils.waitForMutateTime}. * * @category DOM: Altering (optimized) */ export const wrapChildren = asyncMutatorFor(wrapChildrenNow); /** * Replace an element with another one. * * @param [options.ignoreMove] * If true, the DOM watcher instances will ignore the operation of * moving the element (so as to not trigger relevant callbacks). * * @category DOM: Altering */ export const replaceElementNow = ( element: Element, newElement: Element, options?: { ignoreMove?: boolean; }, ) => { if (options?.ignoreMove === true) { ignoreMove( // remove element element, { from: MH.parentOf(element) }, ); ignoreMove( // move newElement to element's current parent newElement, { from: MH.parentOf(newElement), to: MH.parentOf(element) }, ); } element.replaceWith(newElement); }; /** * Like {@link replaceElementNow} except it will {@link Utils.waitForMutateTime}. * * @category DOM: Altering (optimized) */ export const replaceElement = asyncMutatorFor(replaceElementNow); /** * Replace an element with another one. * * @param [options.ignoreMove] * If true, the DOM watcher instances will ignore the operation of * moving the element (so as to not trigger relevant callbacks). * * @category DOM: Altering */ export const swapElementsNow = ( elementA: Element, elementB: Element, options?: { ignoreMove?: boolean; }, ) => { const temp = MH.createElement("div"); replaceElementNow(elementA, temp, options); replaceElementNow(elementB, elementA, options); replaceElementNow(temp, elementB, options); }; /** * Like {@link swapElementsNow} except it will {@link Utils.waitForMutateTime}. * * @category DOM: Altering (optimized) */ export const swapElements = asyncMutatorFor(swapElementsNow); // [TODO v2]: moveChildren to accept newParent as options.to /** * Move an element's children to a new element * * @param [options.ignoreMove] * If true, the DOM watcher instances will ignore the operation of * moving the children (so as to not trigger relevant callbacks). * * @category DOM: Altering */ export const moveChildrenNow = ( oldParent: Element, newParent: Element, options?: { ignoreMove?: boolean; }, ) => { if (options?.ignoreMove === true) { for (const child of MH.childrenOf(oldParent)) { ignoreMove(child, { from: oldParent, to: newParent, }); } } newParent.append(...MH.childrenOf(oldParent)); }; /** * Like {@link moveChildrenNow} except it will {@link Utils.waitForMutateTime}. * * @category DOM: Altering (optimized) */ export const moveChildren = asyncMutatorFor(moveChildrenNow); /** * Moves an element to a new position. * * @param [options.to] The new parent or sibling (depending on * `options.position`). If not given, the * element is removed from the DOM. * @param [options.position] - append (default): append to `options.to` * - prepend: prepend to `options.to` * - before: insert before `options.to` * - after: insert after `options.to` * @param [options.ignoreMove] If true, the DOM watcher instances will * ignore the operation of moving the element * (so as to not trigger relevant callbacks). * * @category DOM: Altering */ export const moveElementNow = ( element: Element, options?: { to?: Element; position?: "append" | "prepend" | "before" | "after"; ignoreMove?: boolean; }, ) => { let parentEl = options?.to ?? null; const position = options?.position || "append"; if (position === "before" || position === "after") { parentEl = MH.parentOf(options?.to); } if (options?.ignoreMove === true) { ignoreMove(element, { from: MH.parentOf(element), to: parentEl, }); } if (options?.to) { options.to[position](element); } else { MH.remove(element); } }; /** * Like {@link moveElementNow} except it will {@link Utils.waitForMutateTime}. * * @category DOM: Altering (optimized) */ export const moveElement = asyncMutatorFor(moveElementNow); /** * It will {@link hideElement} and then remove it from the DOM. * * @param [options.ignoreMove] * If true, the DOM watcher instances will ignore the operation of * replacing the element (so as to not trigger relevant callbacks). * * @category DOM: Altering (optimized) */ export const hideAndRemoveElement = async ( element: Element, delay = 0, options?: { ignoreMove?: boolean; }, ) => { await hideElement(element, delay); moveElementNow(element, options); }; /** * @ignore * @internal */ export const getOrAssignID = (element: Element, prefix = "") => { let domID = element.id; if (!domID) { domID = `${prefix}-${randId()}`; element.id = domID; } return domID; }; /** * @ignore * @internal * * @since v1.2.0 */ export const isAllowedToWrap = (element: Element) => settings.contentWrappingAllowed === true && getData(element, MC.PREFIX_NO_WRAP) === null; /** * @ignore * @internal * * @param [options.classNames] Default is [MC.PREFIX_WRAPPER]. * Pass `null` to disable check. * * @since v1.2.0 */ export const getWrapper = ( element: Element, options?: { _tagName?: keyof HTMLElementTagNameMap; _classNames?: string[] | null; }, ) => { const { _tagName: tagName, _classNames: classNames = [MC.PREFIX_WRAPPER] } = options ?? {}; const parent = MH.parentOf(element); Iif ( MH.lengthOf(MH.childrenOf(parent)) === 1 && MH.isHTMLElement(parent) && (!tagName || MH.hasTagName(parent, tagName)) && (!classNames || hasAnyClass(parent, ...classNames)) ) { // Already wrapped return parent; } return null; // don't check the element itself, only its parent }; /** * @ignore * @internal * * @param [options.classNames] Default is [MC.PREFIX_WRAPPER_CONTENT]. * Pass `null` to disable check. * * @since v1.2.0 */ export const getContentWrapper = ( element: Element, options?: { _tagName?: keyof HTMLElementTagNameMap; _classNames?: string[] | null; }, ) => { const { _tagName: tagName, _classNames: classNames = [MC.PREFIX_WRAPPER_CONTENT], } = options ?? {}; const firstChild = MH.childrenOf(element)[0]; if ( MH.lengthOf(MH.childrenOf(element)) === 1 && MH.isHTMLElement(firstChild) && (!tagName || MH.hasTagName(firstChild, tagName)) && (!classNames || hasAnyClass(firstChild, ...classNames)) ) { // Already wrapped return firstChild; } return null; }; /** * @ignore * @internal */ export const createWrapperFor = ( element: Element, wrapper?: HTMLElement | keyof HTMLElementTagNameMap, ) => { if (MH.isElement(wrapper)) { return wrapper; } let tag = wrapper; if (!tag) { if (isInlineTag(MH.tagName(element))) { tag = "span"; } else { tag = "div"; } } return MH.createElement(tag); }; /** * @ignore * @internal * * @since v1.2.0 */ export const tryWrapNow = <O extends ContentWrappingOptions>( element: Element, options?: O, ) => _tryWrapNow(element, options); /** * @ignore * @internal * * @since v1.2.0 */ export const tryWrap = asyncMutatorFor(tryWrapNow); /** * @ignore * @internal * * @since v1.2.0 */ export const tryWrapContentNow = <O extends ContentWrappingOptions>( element: Element, options?: O, ) => _tryWrapNow(element, options, true); /** * @ignore * @internal * * @since v1.2.0 */ export const tryWrapContent = asyncMutatorFor(tryWrapContentNow); /** * @ignore * @internal * * @since v1.2.0 */ export const unwrapContentNow = (wrapper: Element, classNames?: string[]) => { const parent = wrapper.parentElement; if (parent) { moveChildrenNow(wrapper, parent, { ignoreMove: true }); moveElementNow(wrapper, { ignoreMove: true }); if (classNames) { removeClassesNow(wrapper, ...classNames); } } }; /** * @ignore * @internal * * @since v1.2.0 */ export const unwrapContent = asyncMutatorFor(unwrapContentNow); /** * @ignore * @internal */ export const cloneElement = <E extends Element>(element: E) => { const clone = element.cloneNode(true) as E; setBooleanData(clone, MH.prefixName("clone")); return clone; }; /** * Creates a dummy hidden clone that's got animation and transitions disabled * and absolute position, wrapped in a wrapper (of size 0) and inserts it just * before the `insertBefore` element (or if not given, the original element), * so that the hidden clone overlaps the actual element's regular * (pre-transformed) position. * * It clears the ID of the clone. * * Returns the clone. * * @ignore * @internal */ export const insertGhostCloneNow = <E extends Element>( element: E, insertBefore: Element | null = null, ) => { const clone = cloneElement(element); clone.id = ""; addClassesNow( clone, MC.PREFIX_GHOST, MC.PREFIX_TRANSITION_DISABLE, MC.PREFIX_ANIMATE_DISABLE, ); const wrapper = _tryWrapNow(clone, { _classNames: [MC.PREFIX_RELATIVE, MC.PREFIX_WRAPPER], _required: true, }); moveElementNow(wrapper, { to: insertBefore ?? element, position: "before", ignoreMove: true, }); return { _wrapper: wrapper, _clone: clone }; }; /** * @ignore * @internal * * Exposed via DOMWatcher */ export const insertGhostClone = asyncMutatorFor(insertGhostCloneNow); /** * @ignore * @internal * * Exposed via DOMWatcher */ export const ignoreMove = ( target: Element, options: { from?: Element | null; to?: Element | null }, ) => recordsToSkipOnce.set(target, { from: options.from ?? null, to: options.to ?? null, }); /** * @ignore * @internal */ export const getIgnoreMove = ( target: Element, ): { from: Element | null; to: Element | null } | null => recordsToSkipOnce.get(target) ?? null; /** * @ignore * @internal */ export const clearIgnoreMove = (target: Element) => { // We should not clear the entry the first time the operation is observed // (when we return true here), because there may be multiple DOMWatcher // instances that will observe it and need to query it. Instead do it shortly. MH.setTimer(() => { MH.deleteKey(recordsToSkipOnce, target); }, 100); }; /** * @ignore * @internal */ export const insertArrow = ( target: Element, direction: "up" | "down" | "left" | "right", position: "prepend" | "append" | "before" | "after" = "append", tag = "span", ) => { const arrow = MH.createElement(tag); addClassesNow(arrow, MH.prefixName(MC.S_ARROW)); setDataNow(arrow, MH.prefixName("direction"), direction); moveElement(arrow, { to: target, position, ignoreMove: true }); return arrow; }; // ---------------------------------------- type ContentWrappingOptions = { _tagName?: keyof HTMLElementTagNameMap; _classNames?: string[]; // if the wrapper has any one of these, it will be re-used _ignoreMove?: boolean; // default is true here _required?: boolean; // if true, will ignore contentWrappingAllowed and data-lisn-no-wrap _requiredBy?: string; // for logging purposes }; const recordsToSkipOnce = MH.newMap< /* target being moved */ Element, { from: Element | null; to: Element | null } >(); const _tryWrapNow = <O extends ContentWrappingOptions>( element: Element, options: O | undefined, wrapContent = false, // if true, wrap its children, otherwise given element ) => { const { _tagName: tagName, _classNames: classNames = wrapContent ? [MC.PREFIX_WRAPPER_CONTENT] : [MC.PREFIX_WRAPPER], _ignoreMove: ignoreMove = true, _required: required = false, _requiredBy: requiredBy = "", } = options ?? {}; const getWrapperFn = wrapContent ? getContentWrapper : getWrapper; const wrapFn = wrapContent ? wrapChildrenNow : wrapElementNow; const allowedToWrap = isAllowedToWrap(element); let wrapper = getWrapperFn(element, options); if (!wrapper && (required || allowedToWrap)) { wrapper = wrapFn(element, { wrapper: tagName, ignoreMove }); if (classNames) { addClassesNow(wrapper, ...classNames); } Iif (!allowedToWrap && requiredBy) { logWarn( `content wrapping is disabled for element but wrapping is required by ${requiredBy}`, ); } } return wrapper as O extends { _required: true } ? HTMLElement : HTMLElement | null; }; |