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
3dcee021
Commit
3dcee021
authored
Dec 23, 2014
by
Oleg Gaidarenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Manipulation: make wrapAll funarg execute only once
Ref 359b03cac74d7336676a6992f14b7ccab9b28659
parent
ae30fb6c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
9 deletions
+69
-9
wrap.js
src/wrap.js
+9
-9
wrap.js
test/unit/wrap.js
+60
-0
No files found.
src/wrap.js
View file @
3dcee021
...
...
@@ -7,18 +7,18 @@ define([
jQuery
.
fn
.
extend
({
wrapAll
:
function
(
html
)
{
if
(
jQuery
.
isFunction
(
html
)
)
{
return
this
.
each
(
function
(
i
)
{
jQuery
(
this
).
wrapAll
(
html
.
call
(
this
,
i
)
);
});
}
var
wrap
;
if
(
this
[
0
]
)
{
if
(
jQuery
.
isFunction
(
html
)
)
{
html
=
html
.
call
(
this
[
0
]
);
}
if
(
this
[
0
]
)
{
// The elements to wrap the target around
var
wrap
=
jQuery
(
html
,
this
[
0
].
ownerDocument
).
eq
(
0
).
clone
(
true
);
wrap
=
jQuery
(
html
,
this
[
0
].
ownerDocument
).
eq
(
0
).
clone
(
true
);
if
(
this
[
0
].
parentNode
)
{
wrap
.
insertBefore
(
this
[
0
]
);
if
(
this
[
0
].
parentNode
)
{
wrap
.
insertBefore
(
this
[
0
]
);
}
wrap
.
map
(
function
()
{
...
...
test/unit/wrap.js
View file @
3dcee021
...
...
@@ -149,6 +149,66 @@ test( "wrapAll(String)", function() {
});
test
(
"wrapAll(Function)"
,
5
,
function
()
{
var
prev
=
jQuery
(
"#firstp"
)[
0
].
previousSibling
,
p
=
jQuery
(
"#firstp,#first"
)[
0
].
parentNode
,
result
=
jQuery
(
"#firstp,#first"
).
wrapAll
(
function
()
{
return
"<div class='red'><div class='tmp'></div></div>"
;
});
equal
(
result
.
parent
().
length
,
1
,
"Check for wrapping of on-the-fly html"
);
ok
(
jQuery
(
"#first"
).
parent
().
parent
().
is
(
".red"
),
"Check if wrapper has class 'red'"
);
ok
(
jQuery
(
"#firstp"
).
parent
().
parent
().
is
(
".red"
),
"Check if wrapper has class 'red'"
);
ok
(
jQuery
(
"#first"
).
parent
().
parent
().
parent
().
is
(
p
),
"Correct Parent"
);
strictEqual
(
jQuery
(
"#first"
).
parent
().
parent
()[
0
].
previousSibling
,
prev
,
"Correct Previous Sibling"
);
});
test
(
"wrapAll(Function) check execution characteristics"
,
3
,
function
()
{
var
i
=
0
;
jQuery
(
"non-existent"
).
wrapAll
(
function
()
{
i
++
;
return
""
;
});
ok
(
!
i
,
"should not execute function argument if target element does not exist"
);
jQuery
(
"#firstp"
).
wrapAll
(
function
(
index
)
{
strictEqual
(
this
,
jQuery
(
"#firstp"
)[
0
],
"context must be the first found element"
);
strictEqual
(
index
,
undefined
,
"index argument should not be included in function execution"
);
});
});
test
(
"wrapAll(Function)"
,
5
,
function
()
{
var
prev
=
jQuery
(
"#firstp"
)[
0
].
previousSibling
,
p
=
jQuery
(
"#firstp,#first"
)[
0
].
parentNode
,
result
=
jQuery
(
"#firstp,#first"
).
wrapAll
(
function
()
{
return
"<div class='red'><div class='tmp'></div></div>"
;
});
equal
(
result
.
parent
().
length
,
1
,
"Check for wrapping of on-the-fly html"
);
ok
(
jQuery
(
"#first"
).
parent
().
parent
().
is
(
".red"
),
"Check if wrapper has class 'red'"
);
ok
(
jQuery
(
"#firstp"
).
parent
().
parent
().
is
(
".red"
),
"Check if wrapper has class 'red'"
);
ok
(
jQuery
(
"#first"
).
parent
().
parent
().
parent
().
is
(
p
),
"Correct Parent"
);
strictEqual
(
jQuery
(
"#first"
).
parent
().
parent
()[
0
].
previousSibling
,
prev
,
"Correct Previous Sibling"
);
});
test
(
"wrapAll(Function) check execution characteristics"
,
3
,
function
()
{
var
i
=
0
;
jQuery
(
"non-existent"
).
wrapAll
(
function
()
{
i
++
;
return
""
;
});
ok
(
!
i
,
"should not execute function argument if target element does not exist"
);
jQuery
(
"#firstp"
).
wrapAll
(
function
(
index
)
{
strictEqual
(
this
,
jQuery
(
"#firstp"
)[
0
],
"context must be the first found element"
);
strictEqual
(
index
,
undefined
,
"index argument should not be included in function execution"
);
});
});
test
(
"wrapAll(Element)"
,
function
()
{
expect
(
3
);
...
...
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