Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
642e9a45
Commit
642e9a45
authored
Sep 17, 2013
by
Oleg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify replaceWith method. Closes gh-1276
parent
80538b04
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
28 deletions
+19
-28
manipulation.js
src/manipulation.js
+11
-21
manipulation.js
test/unit/manipulation.js
+8
-7
No files found.
src/manipulation.js
View file @
642e9a45
...
@@ -442,38 +442,28 @@ jQuery.fn.extend({
...
@@ -442,38 +442,28 @@ jQuery.fn.extend({
},
},
replaceWith
:
function
()
{
replaceWith
:
function
()
{
var
var
arg
=
arguments
[
0
];
// Snapshot the DOM in case .domManip sweeps something relevant into its fragment
args
=
jQuery
.
map
(
this
,
function
(
elem
)
{
return
[
elem
.
nextSibling
,
elem
.
parentNode
];
}),
i
=
0
;
// Make the changes, replacing each context element with the new content
// Make the changes, replacing each context element with the new content
this
.
domManip
(
arguments
,
function
(
elem
)
{
this
.
domManip
(
arguments
,
function
(
elem
)
{
var
next
=
args
[
i
++
],
arg
=
this
.
parentNode
;
parent
=
args
[
i
++
];
if
(
parent
)
{
jQuery
.
cleanData
(
getAll
(
this
)
);
// Don't use the snapshot next if it has moved (#13810)
if
(
next
&&
next
.
parentNode
!==
parent
)
{
if
(
arg
)
{
next
=
this
.
nextSibling
;
arg
.
replaceChild
(
elem
,
this
);
}
jQuery
(
this
).
remove
();
parent
.
insertBefore
(
elem
,
next
);
}
}
// Allow new content to include elements from the context set
});
},
true
);
// Force removal if there was no new content (e.g., from empty arguments)
// Force removal if there was no new content (e.g., from empty arguments)
return
i
?
this
:
this
.
remove
();
return
arg
&&
(
arg
.
length
||
arg
.
nodeType
)
?
this
:
this
.
remove
();
},
},
detach
:
function
(
selector
)
{
detach
:
function
(
selector
)
{
return
this
.
remove
(
selector
,
true
);
return
this
.
remove
(
selector
,
true
);
},
},
domManip
:
function
(
args
,
callback
,
allowIntersection
)
{
domManip
:
function
(
args
,
callback
)
{
// Flatten any nested arrays
// Flatten any nested arrays
args
=
concat
.
apply
(
[],
args
);
args
=
concat
.
apply
(
[],
args
);
...
@@ -495,12 +485,12 @@ jQuery.fn.extend({
...
@@ -495,12 +485,12 @@ jQuery.fn.extend({
if
(
isFunction
)
{
if
(
isFunction
)
{
args
[
0
]
=
value
.
call
(
this
,
index
,
self
.
html
()
);
args
[
0
]
=
value
.
call
(
this
,
index
,
self
.
html
()
);
}
}
self
.
domManip
(
args
,
callback
,
allowIntersection
);
self
.
domManip
(
args
,
callback
);
});
});
}
}
if
(
l
)
{
if
(
l
)
{
fragment
=
jQuery
.
buildFragment
(
args
,
this
[
0
].
ownerDocument
,
false
,
!
allowIntersection
&&
this
);
fragment
=
jQuery
.
buildFragment
(
args
,
this
[
0
].
ownerDocument
,
false
,
this
);
first
=
fragment
.
firstChild
;
first
=
fragment
.
firstChild
;
if
(
fragment
.
childNodes
.
length
===
1
)
{
if
(
fragment
.
childNodes
.
length
===
1
)
{
...
...
test/unit/manipulation.js
View file @
642e9a45
...
@@ -1010,20 +1010,21 @@ test( "replaceWith(string) for more than one element", function() {
...
@@ -1010,20 +1010,21 @@ test( "replaceWith(string) for more than one element", function() {
equal
(
jQuery
(
"#foo p"
).
length
,
0
,
"verify that all the three original element have been replaced"
);
equal
(
jQuery
(
"#foo p"
).
length
,
0
,
"verify that all the three original element have been replaced"
);
});
});
test
(
"empty replaceWith (#13401; #13596)"
,
4
,
function
()
{
test
(
"Empty replaceWith (#13401; #13596)"
,
8
,
function
()
{
expect
(
6
);
var
$el
=
jQuery
(
"<div/>"
),
var
$el
=
jQuery
(
"<div/>"
),
tests
=
{
tests
=
{
"empty string"
:
""
,
"empty string"
:
""
,
"empty array"
:
[],
"empty array"
:
[],
"empty collection"
:
jQuery
(
"#nonexistent"
)
"empty collection"
:
jQuery
(
"#nonexistent"
),
// in case of jQuery(...).replaceWith();
"empty undefined"
:
undefined
};
};
jQuery
.
each
(
tests
,
function
(
label
,
input
)
{
jQuery
.
each
(
tests
,
function
(
label
,
input
)
{
$el
.
html
(
"<a/>"
).
children
().
replaceWith
(
input
);
$el
.
html
(
"<a/>"
).
children
().
replaceWith
(
input
);
strictEqual
(
$el
.
html
(),
""
,
"replaceWith("
+
label
+
")"
);
strictEqual
(
$el
.
html
(),
""
,
"replaceWith("
+
label
+
")"
);
$el
.
html
(
"<b/>"
).
children
().
replaceWith
(
function
()
{
return
input
;
});
$el
.
html
(
"<b/>"
).
children
().
replaceWith
(
function
()
{
return
input
;
});
strictEqual
(
$el
.
html
(),
""
,
"replaceWith(function returning "
+
label
+
")"
);
strictEqual
(
$el
.
html
(),
""
,
"replaceWith(function returning "
+
label
+
")"
);
});
});
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment