MCPcopy
hub / github.com/greensock/GSAP / split

Method split

src/SplitText.ts:351–475  ·  view source on GitHub ↗
(config: SplitTextConfig)

Source from the content-addressed store, hash-verified

349 }
350
351 split(config: SplitTextConfig) {
352 (this._ctx || _defaultContext).add(() => {
353 this.isSplit && this.revert();
354 this.vars = config = config || this.vars || {};
355 let {type = "chars,words,lines", aria = "auto", deepSlice = true, smartWrap, onSplit, autoSplit = false, specialChars, mask} = this.vars,
356 splitLines: boolean = type.indexOf("lines") > -1,
357 splitCharacters: boolean = type.indexOf("chars") > -1,
358 splitWords: boolean = type.indexOf("words") > -1,
359 onlySplitCharacters: boolean = splitCharacters && !splitWords && !splitLines,
360 specialCharsRegEx: RegExp | undefined = specialChars && (("push" in specialChars) ? new RegExp("(?:" + specialChars.join("|") + ")", "gu") : specialChars),
361 finalCharSplitRegEx: RegExp = specialCharsRegEx ? new RegExp(specialCharsRegEx.source + "|" + _emojiSafeRegEx.source, "gu") : _emojiSafeRegEx,
362 ignore: HTMLElement[] | false = !!config.ignore && _elements(config.ignore),
363 {orig, animTime, obs} = this._data,
364 onSplitResult: any;
365 if (splitCharacters || splitWords || splitLines) {
366 this.elements.forEach((element, index) => {
367 orig[index] = {
368 element,
369 html: element.innerHTML,
370 ariaL: element.getAttribute("aria-label"),
371 ariaH: element.getAttribute("aria-hidden")
372 };
373 aria === "auto" ? element.setAttribute("aria-label", (element.textContent || "").trim()) : aria === "hidden" && element.setAttribute("aria-hidden", "true");
374 let chars: HTMLElement[] = [],
375 words: HTMLElement[] = [],
376 lines: HTMLElement[] = [],
377 charWrapper = splitCharacters ? _getWrapper("char", config, chars) : null,
378 wordWrapper = _getWrapper("word", config, words),
379 i: number, curWord: Element, smartWrapSpan: HTMLElement, nextSibling: Node;
380
381 // split words (always) and characters (if necessary)
382 _splitWordsAndCharsRecursively(element, config, wordWrapper, charWrapper, onlySplitCharacters, deepSlice && (splitLines || onlySplitCharacters), ignore, finalCharSplitRegEx, specialCharsRegEx, false);
383
384 // subdivide lines
385 if (splitLines) {
386 let nodes: Node[] = _toArray(element.childNodes),
387 wrapLine = _getLineWrapper(element, nodes, config, lines),
388 curNode: Node,
389 toRemove: Node[] = [],
390 lineStartIndex: number = 0,
391 allBounds: BoundingRect[] = nodes.map((n) => n.nodeType === 1 ? (n as Element).getBoundingClientRect() : _emptyBounds), // do all these measurements at once so that we don't trigger layout thrashing
392 lastBounds: BoundingRect = _emptyBounds,
393 curBounds: BoundingRect;
394 for (i = 0; i < nodes.length; i++) {
395 curNode = nodes[i];
396 if (curNode.nodeType === 1) {
397 if (curNode.nodeName === "BR") { // remove any <br> tags because breaking up by lines already handles that.
398 if (!i || nodes[i-1].nodeName !== "BR") {
399 toRemove.push(curNode);
400 wrapLine(lineStartIndex, i+1);
401 }
402 lineStartIndex = i+1;
403 lastBounds = _findNextValidBounds(allBounds, i);
404 } else {
405 curBounds = allBounds[i];
406 if (i && curBounds.top > lastBounds.top && curBounds.left < lastBounds.left + lastBounds.width - 1) {
407 wrapLine(lineStartIndex, i);
408 lineStartIndex = i;

Callers 15

constructorMethod · 0.95
_multiListenerFunction · 0.45
_parsePositionFunction · 0.45
initMethod · 0.45
_saveStyleFunction · 0.45
_getStyleSaverFunction · 0.45
CSSPlugin.jsFile · 0.45
_getFunction · 0.45
_renderClearPropsFunction · 0.45
_applySVGOriginFunction · 0.45
_parseTransformFunction · 0.45

Calls 12

revertMethod · 0.95
getBoundingClientRectMethod · 0.80
addEventListenerMethod · 0.80
_elementsFunction · 0.70
_getWrapperFunction · 0.70
_toArrayFunction · 0.70
_getLineWrapperFunction · 0.70
_findNextValidBoundsFunction · 0.70
addMethod · 0.65
removeMethod · 0.65
totalTimeMethod · 0.45

Tested by

no test coverage detected