@@ -15,20 +15,6 @@ const DOM = {
1515 getByTag : / ^ [ a - z ] /
1616 } ,
1717
18- xgetElements ( elementsObj , rootEl ) {
19- for ( let elementKey in elementsObj ) {
20- if ( elementsObj . hasOwnProperty ( elementKey ) && elementKey !== 'rootEl' ) {
21- let elementObj = elementsObj [ elementKey ] ;
22- let { selector } = elementObj ;
23-
24- rootEl = elementObj . rootEl || rootEl ;
25- rootEl = typeof rootEl === 'function' ? rootEl ( ) : rootEl ;
26-
27- elementObj . el = DOM . get ( selector , rootEl ) ;
28- }
29- }
30- } ,
31-
3218 // Public methods
3319 get ( selector , domRoot = document ) {
3420 if ( DOM . isElement ( selector ) ) {
@@ -114,37 +100,6 @@ const DOM = {
114100 }
115101 } ,
116102
117- getFurthest ( node , selector ) {
118- const rootEl = DOM . getRootEl ( ) ;
119- let currentNode = node ;
120- let furthest = null ;
121-
122- selector = selector instanceof Array ? selector : [ selector ] ;
123-
124- while ( currentNode && currentNode !== rootEl ) {
125- if ( selector . indexOf ( currentNode . nodeName ) > - 1 ) {
126- furthest = currentNode ;
127- }
128- currentNode = currentNode . parentNode || currentNode . parentElement ;
129- }
130-
131- return furthest ;
132- } ,
133-
134- nextNode ( node ) {
135- if ( node . hasChildNodes ( ) ) {
136- return node . firstChild ;
137- } else {
138- while ( node && ! node . nextSibling ) {
139- node = node . parentNode ;
140- }
141- if ( ! node ) {
142- return null ;
143- }
144- return node . nextSibling ;
145- }
146- } ,
147-
148103 appendTo ( selector , tag ) {
149104 const htmlNode = DOM . isElement ( tag ) ? tag : document . createElement ( tag ) ;
150105 const targetEl = DOM . get ( selector ) ;
@@ -181,14 +136,6 @@ const DOM = {
181136 return styleEl ;
182137 } ,
183138
184- // From http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
185- isNode ( o ) {
186- return (
187- typeof Node === 'object' ? o instanceof Node :
188- o && typeof o === 'object' && typeof o . nodeType === 'number' && typeof o . nodeName === 'string'
189- ) ;
190- } ,
191-
192139 isElement ( elem ) {
193140 let isElement = false ;
194141
@@ -247,10 +194,6 @@ const DOM = {
247194 el . classList . remove ( classStr ) ;
248195 } ,
249196
250- isBlock ( node ) {
251- return DOM . getStyle ( node , 'display' ) === 'block' ;
252- } ,
253-
254197 closestElement ( node ) {
255198 let returnNode = node ;
256199
@@ -261,17 +204,6 @@ const DOM = {
261204 return returnNode ;
262205 } ,
263206
264- getStyles ( node ) {
265- const closestElement = DOM . closestElement ( node ) ;
266- const gcs = 'getComputedStyle' in window ;
267- return ( gcs ? window . getComputedStyle ( closestElement ) : closestElement . currentStyle ) ;
268- } ,
269-
270- getStyle ( node , property ) {
271- const nodeStyles = DOM . getStyles ( node ) ;
272- return nodeStyles [ property ] ;
273- } ,
274-
275207 insertBefore ( newNode , referenceNode ) {
276208 const parentNode = referenceNode . parentNode ;
277209 parentNode . insertBefore ( newNode , referenceNode ) ;
@@ -288,27 +220,11 @@ const DOM = {
288220 } ,
289221
290222 isLastChild ( node ) {
291- return node === node . parentNode . lastChild ;
223+ return node . parentNode && node === node . parentNode . lastChild ;
292224 } ,
293225
294226 isFirstChild ( node ) {
295- return node === node . parentNode . firstChild ;
296- } ,
297-
298- wrapRange ( nodeName , nodeOpts ) {
299- const sel = window . getSelection ( ) ;
300- const range = sel . getRangeAt ( 0 ) ;
301- const wrapper = document . createElement ( nodeName ) ;
302-
303- for ( let optKey in nodeOpts ) {
304- if ( nodeOpts . hasOwnProperty ( optKey ) ) {
305- wrapper [ optKey ] = nodeOpts [ optKey ] ;
306- }
307- }
308-
309- range . surroundContents ( wrapper ) ;
310-
311- return wrapper ;
227+ return node . parentNode && node === node . parentNode . firstChild ;
312228 } ,
313229
314230 unwrap ( node , opts = { } ) {
@@ -327,49 +243,13 @@ const DOM = {
327243 return unwrappedNodes ;
328244 } ,
329245
330- unwrapFrom ( node , wrappers ) {
331- const rootEl = DOM . getRootEl ( ) ;
332- let currentNode = node ;
333- let unwrappedNodes = [ currentNode ] ;
334-
335- while ( currentNode !== rootEl ) {
336- let parentNode = currentNode . parentNode || currentNode . parentElement ;
337-
338- if ( wrappers . indexOf ( currentNode . nodeName ) > - 1 ) {
339- unwrappedNodes = DOM . unwrap ( currentNode ) ;
340- }
341-
342- currentNode = parentNode ;
343- }
344-
345- return unwrappedNodes ;
346- } ,
347-
348- unwrapToRoot ( node ) {
349- const rootEl = DOM . getRootEl ( ) ;
350- let currentNode = node . parentNode ;
351-
352- while ( currentNode !== rootEl ) {
353- let parentNode = currentNode . parentNode ;
354- DOM . unwrap ( currentNode ) ;
355- currentNode = parentNode ;
356- }
357- } ,
358-
359246 removeNode ( node ) {
360247 const parentNode = node . parentElement || node . parentNode ;
361248 if ( parentNode ) {
362249 parentNode . removeChild ( node ) ;
363250 }
364251 } ,
365252
366- replaceNode ( node , newNode ) {
367- const parentNode = node . parentNode || node . parentElement ;
368- if ( parentNode ) {
369- parentNode . replaceChild ( newNode , node ) ;
370- }
371- } ,
372-
373253 getContainerZIndex ( node ) {
374254 let container = node ;
375255 let topMostContainerZIndex = 0 ;
@@ -385,28 +265,6 @@ const DOM = {
385265 return topMostContainerZIndex ;
386266 } ,
387267
388- // // From http://stackoverflow.com/questions/6139107/programmatically-select-text-in-a-contenteditable-html-element
389- // selectNodeContents(node) {
390- // node = node || DOM.getAnchorNode();
391- // if (!node) {
392- // return;
393- // }
394- //
395- // const nodes = node instanceof Array ? node : [node];
396- // const startNode = nodes[0];
397- // const endNode = nodes[nodes.length - 1];
398- //
399- // const range = document.createRange();
400- // range.setStart(startNode, 0);
401- // range.setEnd(endNode, endNode.length);
402- //
403- // const sel = window.getSelection();
404- // if (sel.rangeCount > 0) {
405- // sel.removeAllRanges();
406- // }
407- // sel.addRange(range);
408- // },
409-
410268 getRootEl ( ) {
411269 const selection = document . getSelection ( ) ;
412270 const anchorNode = selection . anchorNode ;
@@ -419,100 +277,6 @@ const DOM = {
419277 return rootEl ;
420278 } ,
421279
422- removeInvalidTagsUpward ( node , acceptedTags ) {
423- const rootEl = DOM . getRootEl ( ) ;
424- let currentNode = node ;
425- let invalidTags = [ ] ;
426- let unwrappedNodes = [ node ] ;
427-
428- while ( currentNode !== rootEl ) {
429- if ( currentNode . nodeType === 1 && acceptedTags . indexOf ( currentNode . nodeName ) < 0 ) {
430- invalidTags . push ( currentNode ) ;
431- }
432- currentNode = currentNode . parentNode || currentNode . parentElement ;
433- }
434-
435- for ( let i = 0 ; i < invalidTags . length ; i ++ ) {
436- let invalidTag = invalidTags [ i ] ;
437- unwrappedNodes = DOM . unwrap ( invalidTag ) ;
438- }
439-
440- return unwrappedNodes ;
441- } ,
442-
443- // From: http://stackoverflow.com/questions/37025488/remove-whitespace-from-window-selection-in-js
444- trimSelection ( opts ) {
445- opts = opts || { fromEnd : true } ;
446-
447- const sel = window . getSelection ( ) ;
448- const range = sel . getRangeAt ( 0 ) ;
449- const selStr = sel . toString ( ) ;
450-
451- let regEx , container , method , regExResult ,
452- offset = range . startOffset , rangeClone ;
453-
454- if ( opts . bothEnds ) {
455- opts . fromEnd = true ;
456- }
457-
458- if ( opts . fromEnd ) {
459- regEx = / \s + $ / ;
460- container = range . endContainer ;
461- method = range . setEnd ;
462- } else if ( opts . fromStart ) {
463- regEx = / [ ^ \s ] / ;
464- container = range . startContainer ;
465- method = range . setStart ;
466- }
467-
468-
469- regExResult = regEx . exec ( selStr ) ;
470- if ( regExResult && regExResult . index > 0 ) {
471- if ( opts . fromEnd && offset + regExResult . index > container . length ) {
472- regExResult = regEx . exec ( container . textContent ) ;
473- if ( regExResult ) {
474- method . call ( range , container , regExResult . index ) ;
475- }
476- } else {
477- method . call ( range , container , offset + regExResult . index ) ;
478- }
479-
480- rangeClone = range . cloneRange ( ) ;
481- sel . removeAllRanges ( ) ;
482- sel . addRange ( rangeClone ) ;
483- }
484-
485- if ( opts . bothEnds ) {
486- if ( opts . fromEnd ) {
487- DOM . trimSelection ( { fromStart : true } ) ;
488- } else {
489- DOM . trimSelection ( { fromEnd : true } ) ;
490- }
491- }
492- } ,
493-
494- createPseudoSelect ( ) {
495- const rootEl = DOM . getRootEl ( ) ;
496- const wrapper = DOM . wrapRange ( 'SPAN' , {
497- className : 'pseudo-selection'
498- } ) ;
499- let selectionStyles ;
500-
501- if ( browser . isFirefox ( ) ) {
502- selectionStyles = window . getComputedStyle ( rootEl , '::-moz-selection' ) ;
503- } else {
504- selectionStyles = window . getComputedStyle ( rootEl , '::selection' ) ;
505- }
506-
507- wrapper . style [ 'background-color' ] = selectionStyles [ 'background-color' ] ;
508- if ( wrapper . style [ 'background-color' ] === 'transparent' ) {
509- wrapper . style [ 'background-color' ] = '#EEEEEE' ;
510- }
511- wrapper . style . color = selectionStyles . color ;
512-
513- return wrapper ;
514- } ,
515-
516280 // From: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY
517281 getScrollOffset ( ) {
518282 const supportPageOffset = window . pageXOffset !== undefined ;
@@ -574,10 +338,9 @@ const DOM = {
574338 } ,
575339
576340 trimmableSides ( node ) {
577- const { parentNode } = node ;
578341 const isInline = DOM . nodeIsInline ( node ) ;
579- const isFirstChild = parentNode && node === parentNode . firstChild ;
580- const isLastChild = parentNode && node === parentNode . lastChild ;
342+ const isFirstChild = DOM . isFirstChild ( node ) ;
343+ const isLastChild = DOM . isLastChild ( node ) ;
581344
582345 return {
583346 left : ! isInline && isFirstChild ,
@@ -629,10 +392,6 @@ const DOM = {
629392
630393 _cleanSelector ( selector ) {
631394 return selector . replace ( / ^ [ \. # ] / , '' ) ;
632- } ,
633-
634- _createEl ( tag ) {
635- return document . createElement ( tag ) ;
636395 }
637396} ;
638397
0 commit comments