comparison default/node_modules/shoestring/src/dom/replaceWith.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 //>>excludeStart("exclude", pragmas.exclude);
2 define([ "shoestring" ], function(){
3 //>>excludeEnd("exclude");
4
5 /**
6 * Replace each element in the current set with that argument HTML string or HTMLElement.
7 *
8 * @param {string|HTMLElement} fragment The value to assign.
9 * @return shoestring
10 * @this shoestring
11 */
12 shoestring.fn.replaceWith = function( fragment ){
13 if( typeof( fragment ) === "string" ){
14 fragment = shoestring( fragment );
15 }
16
17 var ret = [];
18
19 if( fragment.length > 1 ){
20 fragment = fragment.reverse();
21 }
22 this.each(function( i ){
23 var clone = this.cloneNode( true ),
24 insertEl;
25 ret.push( clone );
26
27 // If there is no parentNode, this is pointless, drop it.
28 if( !this.parentNode ){ return; }
29
30 if( fragment.length === 1 ){
31 insertEl = i > 0 ? fragment[ 0 ].cloneNode( true ) : fragment[ 0 ];
32 this.parentNode.replaceChild( insertEl, this );
33 } else {
34 for( var j = 0, jl = fragment.length; j < jl; j++ ){
35 insertEl = i > 0 ? fragment[ j ].cloneNode( true ) : fragment[ j ];
36 this.parentNode.insertBefore( insertEl, this.nextSibling );
37 }
38 this.parentNode.removeChild( this );
39 }
40 });
41
42 return shoestring( ret );
43 };
44
45 //>>excludeStart("exclude", pragmas.exclude);
46 });
47 //>>excludeEnd("exclude");