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
083edd60
Commit
083edd60
authored
Oct 11, 2013
by
Ronny Springer
Committed by
Richard Gibson
Oct 16, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ref
2263134b
: Better identify potential JSON. Close gh-1401.
parent
209f0a8a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
13 deletions
+46
-13
data.js
src/data.js
+1
-1
data.js
test/unit/data.js
+45
-12
No files found.
src/data.js
View file @
083edd60
...
@@ -17,7 +17,7 @@ define([
...
@@ -17,7 +17,7 @@ define([
5. Avoid exposing implementation details on user objects (eg. expando properties)
5. Avoid exposing implementation details on user objects (eg. expando properties)
6. Provide a clear path for implementation upgrade to WeakMap in 2014
6. Provide a clear path for implementation upgrade to WeakMap in 2014
*/
*/
var
rbrace
=
/
(?:\{[\w\W]
*
\}
|
\[[\w\W]
*
\])
$/
,
var
rbrace
=
/
^
(?:\{[\w\W]
*
\}
|
\[[\w\W]
*
\])
$/
,
rmultiDash
=
/
([
A-Z
])
/g
;
rmultiDash
=
/
([
A-Z
])
/g
;
function
dataAttr
(
elem
,
key
,
data
)
{
function
dataAttr
(
elem
,
key
,
data
)
{
...
...
test/unit/data.js
View file @
083edd60
...
@@ -257,9 +257,12 @@ test(".data(object) does not retain references. #13815", function() {
...
@@ -257,9 +257,12 @@ test(".data(object) does not retain references. #13815", function() {
});
});
test
(
"data-* attributes"
,
function
()
{
test
(
"data-* attributes"
,
function
()
{
expect
(
40
);
expect
(
43
);
var
prop
,
i
,
l
,
metadata
,
elem
,
var
prop
,
i
,
l
,
metadata
,
elem
,
obj
,
obj2
,
check
,
num
,
num2
,
obj
,
obj2
,
check
,
num
,
num2
,
parseJSON
=
jQuery
.
parseJSON
,
nativeParse
=
typeof
JSON
!==
"undefined"
&&
JSON
.
parse
,
div
=
jQuery
(
"<div>"
),
div
=
jQuery
(
"<div>"
),
child
=
jQuery
(
"<div data-myobj='old data' data-ignored=
\"
DOM
\"
data-other='test'></div>"
),
child
=
jQuery
(
"<div data-myobj='old data' data-ignored=
\"
DOM
\"
data-other='test'></div>"
),
dummy
=
jQuery
(
"<div data-myobj='old data' data-ignored=
\"
DOM
\"
data-other='test'></div>"
);
dummy
=
jQuery
(
"<div data-myobj='old data' data-ignored=
\"
DOM
\"
data-other='test'></div>"
);
...
@@ -315,6 +318,19 @@ test("data-* attributes", function() {
...
@@ -315,6 +318,19 @@ test("data-* attributes", function() {
equal
(
child
.
data
(
"other"
),
"test"
,
"Make sure value was pulled in properly from a .data()."
);
equal
(
child
.
data
(
"other"
),
"test"
,
"Make sure value was pulled in properly from a .data()."
);
// attribute parsing
i
=
l
=
0
;
jQuery
.
parseJSON
=
function
()
{
i
++
;
return
parseJSON
.
apply
(
this
,
arguments
);
};
if
(
nativeParse
)
{
JSON
.
parse
=
function
()
{
l
++
;
return
nativeParse
.
apply
(
this
,
arguments
);
};
}
child
child
.
attr
(
"data-true"
,
"true"
)
.
attr
(
"data-true"
,
"true"
)
.
attr
(
"data-false"
,
"false"
)
.
attr
(
"data-false"
,
"false"
)
...
@@ -328,6 +344,8 @@ test("data-* attributes", function() {
...
@@ -328,6 +344,8 @@ test("data-* attributes", function() {
.
attr
(
"data-bigassnum"
,
"123456789123456789123456789"
)
.
attr
(
"data-bigassnum"
,
"123456789123456789123456789"
)
.
attr
(
"data-badjson"
,
"{123}"
)
.
attr
(
"data-badjson"
,
"{123}"
)
.
attr
(
"data-badjson2"
,
"[abc]"
)
.
attr
(
"data-badjson2"
,
"[abc]"
)
.
attr
(
"data-notjson"
,
" {}"
)
.
attr
(
"data-notjson2"
,
"[] "
)
.
attr
(
"data-empty"
,
""
)
.
attr
(
"data-empty"
,
""
)
.
attr
(
"data-space"
,
" "
)
.
attr
(
"data-space"
,
" "
)
.
attr
(
"data-null"
,
"null"
)
.
attr
(
"data-null"
,
"null"
)
...
@@ -335,21 +353,36 @@ test("data-* attributes", function() {
...
@@ -335,21 +353,36 @@ test("data-* attributes", function() {
strictEqual
(
child
.
data
(
"true"
),
true
,
"Primitive true read from attribute"
);
strictEqual
(
child
.
data
(
"true"
),
true
,
"Primitive true read from attribute"
);
strictEqual
(
child
.
data
(
"false"
),
false
,
"Primitive false read from attribute"
);
strictEqual
(
child
.
data
(
"false"
),
false
,
"Primitive false read from attribute"
);
strictEqual
(
child
.
data
(
"five"
),
5
,
"Primitive number read from attribute"
);
strictEqual
(
child
.
data
(
"five"
),
5
,
"Integer read from attribute"
);
strictEqual
(
child
.
data
(
"point"
),
5.5
,
"Primitive number read from attribute"
);
strictEqual
(
child
.
data
(
"point"
),
5.5
,
"Floating-point number read from attribute"
);
strictEqual
(
child
.
data
(
"pointe"
),
"5.5E3"
,
"Floating point exponential number read from attribute"
);
strictEqual
(
child
.
data
(
"pointe"
),
"5.5E3"
,
strictEqual
(
child
.
data
(
"grande"
),
"5.574E9"
,
"Big exponential number read from attribute"
);
"Exponential-notation number read from attribute as string"
);
strictEqual
(
child
.
data
(
"hexadecimal"
),
"0x42"
,
"Hexadecimal number read from attribute"
);
strictEqual
(
child
.
data
(
"grande"
),
"5.574E9"
,
strictEqual
(
child
.
data
(
"pointbad"
),
"5..5"
,
"Bad number read from attribute"
);
"Big exponential-notation number read from attribute as string"
);
strictEqual
(
child
.
data
(
"pointbad2"
),
"-."
,
"Bad number read from attribute"
);
strictEqual
(
child
.
data
(
"hexadecimal"
),
"0x42"
,
strictEqual
(
child
.
data
(
"bigassnum"
),
"123456789123456789123456789"
,
"Bad bigass number read from attribute"
);
"Hexadecimal number read from attribute as string"
);
strictEqual
(
child
.
data
(
"badjson"
),
"{123}"
,
"Bad number read from attribute"
);
strictEqual
(
child
.
data
(
"pointbad"
),
"5..5"
,
strictEqual
(
child
.
data
(
"badjson2"
),
"[abc]"
,
"Bad number read from attribute"
);
"Extra-point non-number read from attribute as string"
);
strictEqual
(
child
.
data
(
"pointbad2"
),
"-."
,
"No-digit non-number read from attribute as string"
);
strictEqual
(
child
.
data
(
"bigassnum"
),
"123456789123456789123456789"
,
"Bad bigass number read from attribute as string"
);
strictEqual
(
child
.
data
(
"badjson"
),
"{123}"
,
"Bad JSON object read from attribute as string"
);
strictEqual
(
child
.
data
(
"badjson2"
),
"[abc]"
,
"Bad JSON array read from attribute as string"
);
strictEqual
(
child
.
data
(
"notjson"
),
" {}"
,
"JSON object with leading non-JSON read from attribute as string"
);
strictEqual
(
child
.
data
(
"notjson2"
),
"[] "
,
"JSON array with trailing non-JSON read from attribute as string"
);
strictEqual
(
child
.
data
(
"empty"
),
""
,
"Empty string read from attribute"
);
strictEqual
(
child
.
data
(
"empty"
),
""
,
"Empty string read from attribute"
);
strictEqual
(
child
.
data
(
"space"
),
" "
,
"
Empty
string read from attribute"
);
strictEqual
(
child
.
data
(
"space"
),
" "
,
"
Whitespace
string read from attribute"
);
strictEqual
(
child
.
data
(
"null"
),
null
,
"Primitive null read from attribute"
);
strictEqual
(
child
.
data
(
"null"
),
null
,
"Primitive null read from attribute"
);
strictEqual
(
child
.
data
(
"string"
),
"test"
,
"Typical string read from attribute"
);
strictEqual
(
child
.
data
(
"string"
),
"test"
,
"Typical string read from attribute"
);
equal
(
i
||
l
,
2
,
"Correct number of JSON parse attempts when reading from attributes"
);
jQuery
.
parseJSON
=
parseJSON
;
if
(
nativeParse
)
{
JSON
.
parse
=
nativeParse
;
}
child
.
remove
();
child
.
remove
();
// tests from metadata plugin
// tests from metadata plugin
...
...
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