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
b3cee018
Commit
b3cee018
authored
Dec 10, 2009
by
John Resig
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of git@github.com:jquery/jquery
parents
cf72fba6
da51cd0e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
28 deletions
+93
-28
attributes.js
src/attributes.js
+28
-5
manipulation.js
src/manipulation.js
+7
-1
attributes.js
test/unit/attributes.js
+54
-19
manipulation.js
test/unit/manipulation.js
+4
-3
No files found.
src/attributes.js
View file @
b3cee018
...
@@ -4,6 +4,12 @@ jQuery.fn.extend({
...
@@ -4,6 +4,12 @@ jQuery.fn.extend({
},
},
addClass
:
function
(
value
)
{
addClass
:
function
(
value
)
{
if
(
jQuery
.
isFunction
(
value
))
{
return
this
.
each
(
function
()
{
jQuery
(
this
).
addClass
(
value
.
call
(
this
)
);
});
}
if
(
value
&&
typeof
value
===
"string"
)
{
if
(
value
&&
typeof
value
===
"string"
)
{
var
classNames
=
(
value
||
""
).
split
(
/
\s
+/
);
var
classNames
=
(
value
||
""
).
split
(
/
\s
+/
);
...
@@ -29,6 +35,12 @@ jQuery.fn.extend({
...
@@ -29,6 +35,12 @@ jQuery.fn.extend({
},
},
removeClass
:
function
(
value
)
{
removeClass
:
function
(
value
)
{
if
(
jQuery
.
isFunction
(
value
))
{
return
this
.
each
(
function
()
{
jQuery
(
this
).
removeClass
(
value
.
call
(
this
)
);
});
}
if
(
(
value
&&
typeof
value
===
"string"
)
||
value
===
undefined
)
{
if
(
(
value
&&
typeof
value
===
"string"
)
||
value
===
undefined
)
{
var
classNames
=
(
value
||
""
).
split
(
/
\s
+/
);
var
classNames
=
(
value
||
""
).
split
(
/
\s
+/
);
...
@@ -158,6 +170,13 @@ jQuery.each({
...
@@ -158,6 +170,13 @@ jQuery.each({
},
},
toggleClass
:
function
(
classNames
,
state
)
{
toggleClass
:
function
(
classNames
,
state
)
{
if
(
jQuery
.
isFunction
(
classNames
)
)
{
return
this
.
each
(
function
()
{
console
.
log
(
this
);
jQuery
(
this
).
toggleClass
(
classNames
.
call
(
this
),
state
);
});
}
var
type
=
typeof
classNames
;
var
type
=
typeof
classNames
;
if
(
type
===
"string"
)
{
if
(
type
===
"string"
)
{
// toggle individual class names
// toggle individual class names
...
@@ -178,7 +197,11 @@ jQuery.each({
...
@@ -178,7 +197,11 @@ jQuery.each({
}
}
}
}
},
function
(
name
,
fn
){
},
function
(
name
,
fn
){
jQuery
.
fn
[
name
]
=
function
(){
jQuery
.
fn
[
name
]
=
function
(
val
,
state
){
if
(
jQuery
.
isFunction
(
val
)
)
{
return
this
.
each
(
function
()
{
jQuery
(
this
)[
name
](
val
.
call
(
this
),
state
);
});
}
return
this
.
each
(
fn
,
arguments
);
return
this
.
each
(
fn
,
arguments
);
};
};
});
});
...
...
src/manipulation.js
View file @
b3cee018
...
@@ -32,6 +32,12 @@ if ( !jQuery.support.htmlSerialize ) {
...
@@ -32,6 +32,12 @@ if ( !jQuery.support.htmlSerialize ) {
jQuery
.
fn
.
extend
({
jQuery
.
fn
.
extend
({
text
:
function
(
text
)
{
text
:
function
(
text
)
{
if
(
jQuery
.
isFunction
(
text
))
{
return
this
.
each
(
function
()
{
return
jQuery
(
this
).
text
(
text
.
call
(
this
)
);
});
}
if
(
typeof
text
!==
"object"
&&
text
!==
undefined
)
{
if
(
typeof
text
!==
"object"
&&
text
!==
undefined
)
{
return
this
.
empty
().
append
(
(
this
[
0
]
&&
this
[
0
].
ownerDocument
||
document
).
createTextNode
(
text
)
);
return
this
.
empty
().
append
(
(
this
[
0
]
&&
this
[
0
].
ownerDocument
||
document
).
createTextNode
(
text
)
);
}
}
...
...
test/unit/attributes.js
View file @
b3cee018
module
(
"attributes"
);
module
(
"attributes"
);
var
bareObj
=
function
(
value
)
{
return
value
;
};
var
functionReturningObj
=
function
(
value
)
{
return
(
function
()
{
return
value
;
});
};
test
(
"attr(String)"
,
function
()
{
test
(
"attr(String)"
,
function
()
{
expect
(
28
);
expect
(
28
);
...
@@ -294,10 +297,10 @@ test("attr('tabindex', value)", function() {
...
@@ -294,10 +297,10 @@ test("attr('tabindex', value)", function() {
equals
(
element
.
attr
(
'tabindex'
),
-
1
,
'set negative tabindex'
);
equals
(
element
.
attr
(
'tabindex'
),
-
1
,
'set negative tabindex'
);
});
});
test
(
"addClass(String)"
,
function
(
)
{
var
testAddClass
=
function
(
valueObj
)
{
expect
(
2
);
expect
(
2
);
var
div
=
jQuery
(
"div"
);
var
div
=
jQuery
(
"div"
);
div
.
addClass
(
"test"
);
div
.
addClass
(
valueObj
(
"test"
)
);
var
pass
=
true
;
var
pass
=
true
;
for
(
var
i
=
0
;
i
<
div
.
size
();
i
++
)
{
for
(
var
i
=
0
;
i
<
div
.
size
();
i
++
)
{
if
(
div
.
get
(
i
).
className
.
indexOf
(
"test"
)
==
-
1
)
pass
=
false
;
if
(
div
.
get
(
i
).
className
.
indexOf
(
"test"
)
==
-
1
)
pass
=
false
;
...
@@ -306,16 +309,24 @@ test("addClass(String)", function() {
...
@@ -306,16 +309,24 @@ test("addClass(String)", function() {
// using contents will get regular, text, and comment nodes
// using contents will get regular, text, and comment nodes
var
j
=
jQuery
(
"#nonnodes"
).
contents
();
var
j
=
jQuery
(
"#nonnodes"
).
contents
();
j
.
addClass
(
"asdf"
);
j
.
addClass
(
valueObj
(
"asdf"
)
);
ok
(
j
.
hasClass
(
"asdf"
),
"Check node,textnode,comment for addClass"
);
ok
(
j
.
hasClass
(
"asdf"
),
"Check node,textnode,comment for addClass"
);
}
test
(
"addClass(String)"
,
function
()
{
testAddClass
(
bareObj
);
});
});
test
(
"removeClass(String) - simple"
,
function
()
{
test
(
"addClass(Function)"
,
function
()
{
testAddClass
(
functionReturningObj
);
});
var
testRemoveClass
=
function
(
valueObj
)
{
expect
(
5
);
expect
(
5
);
var
$divs
=
jQuery
(
'div'
);
var
$divs
=
jQuery
(
'div'
);
$divs
.
addClass
(
"test"
).
removeClass
(
"test"
);
$divs
.
addClass
(
"test"
).
removeClass
(
valueObj
(
"test"
)
);
ok
(
!
$divs
.
is
(
'.test'
),
"Remove Class"
);
ok
(
!
$divs
.
is
(
'.test'
),
"Remove Class"
);
...
@@ -323,7 +334,7 @@ test("removeClass(String) - simple", function() {
...
@@ -323,7 +334,7 @@ test("removeClass(String) - simple", function() {
$divs
=
jQuery
(
'div'
);
$divs
=
jQuery
(
'div'
);
$divs
.
addClass
(
"test"
).
addClass
(
"foo"
).
addClass
(
"bar"
);
$divs
.
addClass
(
"test"
).
addClass
(
"foo"
).
addClass
(
"bar"
);
$divs
.
removeClass
(
"test"
).
removeClass
(
"bar"
).
removeClass
(
"foo"
);
$divs
.
removeClass
(
valueObj
(
"test"
)
).
removeClass
(
valueObj
(
"bar"
)
).
removeClass
(
valueObj
(
"foo"
)
);
ok
(
!
$divs
.
is
(
'.test,.bar,.foo'
),
"Remove multiple classes"
);
ok
(
!
$divs
.
is
(
'.test,.bar,.foo'
),
"Remove multiple classes"
);
...
@@ -331,42 +342,50 @@ test("removeClass(String) - simple", function() {
...
@@ -331,42 +342,50 @@ test("removeClass(String) - simple", function() {
$divs
=
jQuery
(
'div'
);
$divs
=
jQuery
(
'div'
);
// Make sure that a null value doesn't cause problems
// Make sure that a null value doesn't cause problems
$divs
.
eq
(
0
).
addClass
(
"test"
).
removeClass
(
null
);
$divs
.
eq
(
0
).
addClass
(
"test"
).
removeClass
(
valueObj
(
null
)
);
ok
(
$divs
.
eq
(
0
).
is
(
'.test'
),
"Null value passed to removeClass"
);
ok
(
$divs
.
eq
(
0
).
is
(
'.test'
),
"Null value passed to removeClass"
);
$divs
.
eq
(
0
).
addClass
(
"test"
).
removeClass
(
""
);
$divs
.
eq
(
0
).
addClass
(
"test"
).
removeClass
(
valueObj
(
""
)
);
ok
(
$divs
.
eq
(
0
).
is
(
'.test'
),
"Empty string passed to removeClass"
);
ok
(
$divs
.
eq
(
0
).
is
(
'.test'
),
"Empty string passed to removeClass"
);
// using contents will get regular, text, and comment nodes
// using contents will get regular, text, and comment nodes
var
j
=
jQuery
(
"#nonnodes"
).
contents
();
var
j
=
jQuery
(
"#nonnodes"
).
contents
();
j
.
removeClass
(
"asdf"
);
j
.
removeClass
(
valueObj
(
"asdf"
)
);
ok
(
!
j
.
hasClass
(
"asdf"
),
"Check node,textnode,comment for removeClass"
);
ok
(
!
j
.
hasClass
(
"asdf"
),
"Check node,textnode,comment for removeClass"
);
};
test
(
"removeClass(String) - simple"
,
function
()
{
testRemoveClass
(
bareObj
);
});
});
test
(
"toggleClass(String|boolean|undefined[, boolean])"
,
function
()
{
test
(
"removeClass(Function) - simple"
,
function
()
{
testRemoveClass
(
functionReturningObj
);
});
var
testToggleClass
=
function
(
valueObj
)
{
expect
(
17
);
expect
(
17
);
var
e
=
jQuery
(
"#firstp"
);
var
e
=
jQuery
(
"#firstp"
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
e
.
toggleClass
(
"test"
);
e
.
toggleClass
(
valueObj
(
"test"
)
);
ok
(
e
.
is
(
".test"
),
"Assert class present"
);
ok
(
e
.
is
(
".test"
),
"Assert class present"
);
e
.
toggleClass
(
"test"
);
e
.
toggleClass
(
valueObj
(
"test"
)
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
// class name with a boolean
// class name with a boolean
e
.
toggleClass
(
"test"
,
false
);
e
.
toggleClass
(
valueObj
(
"test"
),
false
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
e
.
toggleClass
(
"test"
,
true
);
e
.
toggleClass
(
valueObj
(
"test"
),
true
);
ok
(
e
.
is
(
".test"
),
"Assert class present"
);
ok
(
e
.
is
(
".test"
),
"Assert class present"
);
e
.
toggleClass
(
"test"
,
false
);
e
.
toggleClass
(
valueObj
(
"test"
),
false
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
ok
(
!
e
.
is
(
".test"
),
"Assert class not present"
);
// multiple class names
// multiple class names
e
.
addClass
(
"testA testB"
);
e
.
addClass
(
"testA testB"
);
ok
(
(
e
.
is
(
".testA.testB"
)),
"Assert 2 different classes present"
);
ok
(
(
e
.
is
(
".testA.testB"
)),
"Assert 2 different classes present"
);
e
.
toggleClass
(
"testB testC"
);
e
.
toggleClass
(
valueObj
(
"testB testC"
)
);
ok
(
(
e
.
is
(
".testA.testC"
)
&&
!
e
.
is
(
".testB"
)),
"Assert 1 class added, 1 class removed, and 1 class kept"
);
ok
(
(
e
.
is
(
".testA.testC"
)
&&
!
e
.
is
(
".testB"
)),
"Assert 1 class added, 1 class removed, and 1 class kept"
);
e
.
toggleClass
(
"testA testC"
);
e
.
toggleClass
(
valueObj
(
"testA testC"
)
);
ok
(
(
!
e
.
is
(
".testA"
)
&&
!
e
.
is
(
".testB"
)
&&
!
e
.
is
(
".testC"
)),
"Assert no class present"
);
ok
(
(
!
e
.
is
(
".testA"
)
&&
!
e
.
is
(
".testB"
)
&&
!
e
.
is
(
".testC"
)),
"Assert no class present"
);
// toggleClass storage
// toggleClass storage
...
@@ -393,11 +412,27 @@ test("toggleClass(String|boolean|undefined[, boolean])", function() {
...
@@ -393,11 +412,27 @@ test("toggleClass(String|boolean|undefined[, boolean])", function() {
// Cleanup
// Cleanup
e
.
removeClass
(
"testD"
);
e
.
removeClass
(
"testD"
);
e
.
removeData
(
'__className__'
);
e
.
removeData
(
'__className__'
);
};
test
(
"toggleClass(String|boolean|undefined[, boolean])"
,
function
()
{
testToggleClass
(
bareObj
);
});
});
test
(
"removeAttr(String"
,
function
()
{
test
(
"toggleClass(Function[, boolean])"
,
function
()
{
testToggleClass
(
functionReturningObj
);
});
var
testRemoveAttr
=
function
(
valueObj
)
{
expect
(
1
);
expect
(
1
);
equals
(
jQuery
(
'#mark'
).
removeAttr
(
"class"
)[
0
].
className
,
""
,
"remove class"
);
equals
(
jQuery
(
'#mark'
).
removeAttr
(
valueObj
(
"class"
)
)[
0
].
className
,
""
,
"remove class"
);
};
test
(
"removeAttr(String)"
,
function
()
{
testRemoveAttr
(
bareObj
);
});
test
(
"removeAttr(Function)"
,
function
()
{
testRemoveAttr
(
functionReturningObj
);
});
});
test
(
"addClass, removeClass, hasClass"
,
function
()
{
test
(
"addClass, removeClass, hasClass"
,
function
()
{
...
...
test/unit/manipulation.js
View file @
b3cee018
...
@@ -739,15 +739,16 @@ test("html(String)", function() {
...
@@ -739,15 +739,16 @@ test("html(String)", function() {
test
(
"html(Function)"
,
function
()
{
test
(
"html(Function)"
,
function
()
{
testHtml
(
functionReturningObj
);
testHtml
(
functionReturningObj
);
})
})
;
var
testText
=
function
(
valueObj
)
{
var
testText
=
function
(
valueObj
)
{
expect
(
4
);
expect
(
4
);
equals
(
jQuery
(
"#foo"
).
text
(
"<div><b>Hello</b> cruel world!</div>"
)[
0
].
innerHTML
.
replace
(
/>/g
,
">"
),
"<div><b>Hello</b> cruel world!</div>"
,
"Check escaped text"
);
var
val
=
valueObj
(
"<div><b>Hello</b> cruel world!</div>"
);
equals
(
jQuery
(
"#foo"
).
text
(
val
)[
0
].
innerHTML
.
replace
(
/>/g
,
">"
),
"<div><b>Hello</b> cruel world!</div>"
,
"Check escaped text"
);
// using contents will get comments regular, text, and comment nodes
// using contents will get comments regular, text, and comment nodes
var
j
=
jQuery
(
"#nonnodes"
).
contents
();
var
j
=
jQuery
(
"#nonnodes"
).
contents
();
j
.
text
(
"hi!"
);
j
.
text
(
valueObj
(
"hi!"
)
);
equals
(
jQuery
(
j
[
0
]).
text
(),
"hi!"
,
"Check node,textnode,comment with text()"
);
equals
(
jQuery
(
j
[
0
]).
text
(),
"hi!"
,
"Check node,textnode,comment with text()"
);
equals
(
j
[
1
].
nodeValue
,
" there "
,
"Check node,textnode,comment with text()"
);
equals
(
j
[
1
].
nodeValue
,
" there "
,
"Check node,textnode,comment with text()"
);
equals
(
j
[
2
].
nodeType
,
8
,
"Check node,textnode,comment with text()"
);
equals
(
j
[
2
].
nodeType
,
8
,
"Check node,textnode,comment with text()"
);
...
...
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