@@ -93,8 +93,10 @@ _.extend.value = function (value) {
9393 * css-classes
9494 **/
9595
96+ /* global normalizeClassName */
97+
9698const _class = function ( stack , className , type ) {
97- return stack . el [ 0 ] . nodeType === 1 && stack . el . forEach ( item => _ . internal . maybeMultiple ( className ) . map ( className => item . classList [ type ] ( className ) ) ) ;
99+ return stack . el [ 0 ] . nodeType === 1 && stack . el . forEach ( item => _ . internal . maybeMultiple ( className ) . map ( className => item . classList [ type ] ( normalizeClassName ( className ) ) ) ) ;
98100} ;
99101
100102_ . extend . addClass = function ( className ) {
@@ -106,15 +108,15 @@ _.extend.removeClass = function (className) {
106108} ;
107109
108110_ . extend . hasClass = function ( className ) {
109- return this . el [ 0 ] . nodeType === 1 && this . filter ( '.' + className ) . length > 0 ;
111+ return this . el [ 0 ] . nodeType === 1 && this . filter ( '.' + normalizeClassName ( className ) ) . length > 0 ;
110112} ;
111113
112114_ . extend . toggleClass = function ( className ) {
113115 return this . el . forEach ( item => {
114116 let classNames = _ . internal . maybeMultiple ( className ) ;
115117
116118 if ( item . nodeType === 1 ) {
117- classNames . forEach ( _className => item . classList . toggle ( _className ) ) ;
119+ classNames . forEach ( className => item . classList . toggle ( normalizeClassName ( className ) ) ) ;
118120 }
119121 } ) ;
120122} ;
@@ -309,15 +311,103 @@ _.ready = function (fn) {
309311 document . addEventListener ( 'DOMContentLoaded' , fn ) ;
310312 }
311313} ;
314+ /**
315+ * utils
316+ **/
317+
318+ /* exported createElementFromString */
319+
320+ // eslint-disable-next-line
321+ const createElementFromString = str => {
322+ let elementString = str . toString ( ) ;
323+
324+ const tagRegex = / ( [ < ] ) ? ( [ a - z A - Z ] + ) / ;
325+ const theTag = elementString . match ( tagRegex ) ;
326+ const attrRegex = / ( \S + ) = [ " ' ] ? ( (?: .(? ! [ " ' ] ? \s + (?: \S + ) = | [ > " ' ] ) ) + .) [ " ' ] ? / g;
327+ const mayHaveAttributes = elementString . match ( attrRegex ) || [ ] ;
328+
329+ const newElement = document . createElement ( theTag [ 2 ] ) ;
330+
331+ mayHaveAttributes . forEach ( attr => {
332+ let singleAttrRegex = / ( [ a - z A - Z - ] + ) = (?: [ ' " ] ) ( .* ) (?: [ ' " ] ) / g;
333+ let theAttr = singleAttrRegex . exec ( attr ) ;
334+
335+ newElement . setAttribute ( theAttr [ 1 ] , theAttr [ 2 ] ) ;
336+ } ) ;
337+
338+ return newElement ;
339+ } ;
340+
341+ // eslint-disable-next-line
342+ const normalizeClassName = className => className . charAt ( 0 ) === '.' ? className . substr ( 1 ) : className ;
312343/**
313344 * wrap
314345 **/
315346
316- _ . extend . wrap = function ( ) {
347+ /* global createElementFromString */
348+
349+ _ . extend . wrap = function ( wrapper ) {
350+ let newWrap = createElementFromString ( wrapper ) ;
351+
352+ let parent = this . el [ 0 ] . parentNode ;
353+ let siblings = this . el [ 0 ] . nextSibling ;
354+
355+ newWrap . appendChild ( this . el [ 0 ] ) ;
356+
357+ if ( siblings ) {
358+ parent . insertBefore ( newWrap , siblings ) ;
359+ }
360+ else {
361+ parent . appendChild ( newWrap ) ;
362+ }
363+
317364 return this ;
318365} ;
319366
320- _ . extend . unwrap = function ( ) {
367+ _ . extend . wrapAll = function ( wrapper ) {
368+ let firstSibling = true ;
369+ let newWrap = createElementFromString ( wrapper ) ;
370+
371+ this . el . forEach ( item => {
372+ if ( firstSibling ) {
373+ let parent = item . parentNode ;
374+ let siblings = item . nextSibling ;
375+
376+ newWrap . appendChild ( item ) ;
377+
378+ if ( siblings ) {
379+ parent . insertBefore ( newWrap , siblings ) ;
380+ }
381+ else {
382+ parent . appendChild ( newWrap ) ;
383+ }
384+
385+ firstSibling = false ;
386+ }
387+
388+ else {
389+ newWrap . appendChild ( item ) ;
390+ }
391+ } ) ;
392+
393+ return this ;
394+ } ;
395+
396+ _ . extend . unwrap = function ( selector ) {
397+ let parent = this . el [ 0 ] . parentNode ;
398+
399+ // if the parent is not the desired one, skip unwrapping
400+ if ( selector && ! parent . matches ( selector . toString ( ) ) ) {
401+ return this ;
402+ }
403+
404+ parent . parentNode . appendChild ( this . el [ 0 ] ) ;
405+
406+ if ( parent . children . length === 0 ) {
407+ parent . parentNode . removeChild ( parent ) ;
408+ }
409+
321410 return this ;
322411} ;
412+ _ . uxr = { version : '0.2.0' } ;
323413} ) ( ) ;
0 commit comments