55 * @param {* } els
66 */
77function Splitting ( els ) {
8- return $ ( els ) . map ( function ( el , i ) {
8+ return $ ( els ) . map ( function ( el , i ) {
99 if ( ! el . _splitting ) {
1010 el . _splitting = {
1111 el : el
1212 } ;
13- el . className += " splitting" ;
13+ if ( el . classList ) {
14+ el . classList . add ( "splitting" ) ;
15+ }
1416 }
1517 return el . _splitting ;
1618 } ) ;
@@ -25,9 +27,8 @@ function Splitting(els) {
2527 */
2628function $ ( els , parent ) {
2729 return Array . prototype . slice . call (
28- els . nodeName
29- ? [ els ]
30- : els [ 0 ] . nodeName ? els : ( parent || document ) . querySelectorAll ( els ) ,
30+ els . nodeName ? [ els ] :
31+ els [ 0 ] . nodeName ? els : ( parent || document ) . querySelectorAll ( els ) ,
3132 0
3233 ) ;
3334}
@@ -45,7 +46,7 @@ function index(s, key, splits) {
4546 if ( splits ) {
4647 s [ key + "s" ] = splits ;
4748 s . el . style . setProperty ( "--" + key + "-total" , splits . length ) ;
48- splits . map ( function ( el , i ) {
49+ splits . map ( function ( el , i ) {
4950 el . style . setProperty ( "--" + key + "-index" , i ) ;
5051 } ) ;
5152 }
@@ -62,34 +63,46 @@ Splitting.index = index;
6263 * @param {Boolean } space - Add a space to each split if index is greater than 0. Mainly for `Splitting.words`
6364 */
6465function split ( el , key , splitBy , space ) {
66+ // Remove element from DOM to prevent unnecessary thrashing.
6567 var parent = el . parentNode ;
6668 if ( parent ) {
6769 var temp = document . createTextNode ( "" ) ;
6870 parent . replaceChild ( temp , el ) ;
6971 }
7072
73+ // Get all the children and clear out the innerHTML
7174 var children = $ ( el . childNodes ) ;
7275 el . innerHTML = "" ;
7376
74- children = children . reduce ( function ( val , child ) {
75- // Recursively run through child nodes.
77+ // Loop through all children to split them up.
78+ children = children . reduce ( function ( val , child ) {
79+ // Recursively run through child nodes
7680 if ( child && child . childNodes && child . childNodes . length ) {
7781 el . appendChild ( child ) ;
7882 return val . concat ( split ( child , key , splitBy , space ) ) ;
7983 }
8084
85+ // Get the text to split, trimming out the whitespace
8186 var text = ( child . wholeText || "" ) . trim ( ) ;
87+
88+ // If there's no text left after trimming whitespace, continue the loop
8289 if ( ! text . length ) {
8390 return val ;
8491 }
8592
93+ // Concatenate the split text children back into the full array
8694 return val . concat (
87- text . split ( splitBy ) . map ( function ( split ) {
95+ text . split ( splitBy ) . map ( function ( split ) {
96+ // Create a span
8897 var splitEl = document . createElement ( "span" ) ;
98+ // Give it the key as a class
8999 splitEl . className = key ;
90- splitEl . setAttribute ( "data-" + key , split ) ;
91100 splitEl . innerText = split ;
101+ // Populate data-{{key}} with the split value
102+ splitEl . setAttribute ( "data-" + key , split ) ;
92103 el . appendChild ( splitEl ) ;
104+ // If items should be spaced out (Splitting.words, primarily), insert
105+ // the space into the parent before the element.
93106 if ( space ) {
94107 splitEl . insertAdjacentText ( "beforebegin" , " " ) ;
95108 }
@@ -98,6 +111,7 @@ function split(el, key, splitBy, space) {
98111 ) ;
99112 } , [ ] ) ;
100113
114+ // Put the element back into the DOM
101115 if ( parent ) {
102116 parent . replaceChild ( el , temp ) ;
103117 }
@@ -114,8 +128,8 @@ Splitting.split = split;
114128 * @param {String } key -
115129 * @example `Splitting.children('ul','li','item'); // Index every unordered list's items with the --item CSS var.`
116130 */
117- Splitting . children = function ( parent , children , key ) {
118- return Splitting ( parent ) . map ( function ( s ) {
131+ Splitting . children = function ( parent , children , key ) {
132+ return Splitting ( parent ) . map ( function ( s ) {
119133 return index ( s , key , $ ( children , s . el ) ) ;
120134 } ) ;
121135} ;
@@ -125,8 +139,8 @@ Splitting.children = function(parent, children, key) {
125139 * Split an element into words and those words into chars.
126140 * @param {String|Node|NodeList|Array } els - Element(s) or selector to target.
127141 */
128- Splitting . words = function ( els ) {
129- return Splitting ( els ) . map ( function ( s , i ) {
142+ Splitting . words = function ( els ) {
143+ return Splitting ( els ) . map ( function ( s , i ) {
130144 return s . words ? s : index ( s , "word" , split ( s . el , "word" , / \s + / , true ) ) ;
131145 } ) ;
132146} ;
@@ -136,17 +150,17 @@ Splitting.words = function(els) {
136150 * Split an element into words and those words into chars.
137151 * @param {String|Node|NodeList|Array } els - Element(s) or selector to target.
138152 */
139- Splitting . chars = function ( els ) {
140- return Splitting . words ( els ) . map ( function ( s ) {
141- return s . chars
142- ? s
143- : index (
144- s ,
145- "char" ,
146- s . words . reduce ( function ( val , word , i ) {
147- return val . concat ( split ( word , "char" , "" ) ) ;
148- } , [ ] )
149- ) ;
153+ Splitting . chars = function ( els ) {
154+ return Splitting . words ( els ) . map ( function ( s ) {
155+ return s . chars ?
156+ s :
157+ index (
158+ s ,
159+ "char" ,
160+ s . words . reduce ( function ( val , word , i ) {
161+ return val . concat ( split ( word , "char" , "" ) ) ;
162+ } , [ ] )
163+ ) ;
150164 } ) ;
151165} ;
152166
@@ -159,12 +173,12 @@ Splitting.chars = function(els) {
159173 * @param {Boolean } opts.element - Return an element. Defaults to `false` to receive a string
160174 * default is chars
161175 */
162- Splitting . fromString = function ( str , opts ) {
176+ Splitting . fromString = function ( str , opts ) {
163177 opts = opts || { } ;
164178 var el = document . createElement ( "span" ) ;
165179 el . innerHTML = str ;
166180 var s = Splitting [ opts . type || "chars" ] ( el ) ;
167181 return opts . element ? el : el . outerHTML ;
168182} ;
169183
170- export default Splitting ;
184+ export default Splitting ;
0 commit comments