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
ee9687d4
Commit
ee9687d4
authored
Oct 16, 2012
by
James Huston
Committed by
Dave Methvin
Oct 17, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix #12751. Ensure parseJson throws in the same situations as JSON.parse. Close gh-993.
parent
c31539c8
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
17 deletions
+88
-17
AUTHORS.txt
AUTHORS.txt
+2
-1
core.js
src/core.js
+18
-13
ajax.js
test/unit/ajax.js
+14
-0
core.js
test/unit/core.js
+54
-3
No files found.
AUTHORS.txt
View file @
ee9687d4
...
@@ -134,3 +134,4 @@ Nikita Govorov <nikita.govorov@gmail.com>
...
@@ -134,3 +134,4 @@ Nikita Govorov <nikita.govorov@gmail.com>
Michael Pennisi <mike@mikepennisi.com>
Michael Pennisi <mike@mikepennisi.com>
Markus Staab <markus.staab@redaxo.de>
Markus Staab <markus.staab@redaxo.de>
Daniel Gálvez <dgalvez@editablething.com>
Daniel Gálvez <dgalvez@editablething.com>
James Huston <james@jameshuston.net>
\ No newline at end of file
src/core.js
View file @
ee9687d4
...
@@ -488,18 +488,21 @@ jQuery.extend({
...
@@ -488,18 +488,21 @@ jQuery.extend({
},
},
parseJSON
:
function
(
data
)
{
parseJSON
:
function
(
data
)
{
if
(
!
data
||
typeof
data
!==
"string"
)
{
return
null
;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data
=
jQuery
.
trim
(
data
);
// Attempt to parse using the native JSON parser first
// Attempt to parse using the native JSON parser first
if
(
window
.
JSON
&&
window
.
JSON
.
parse
)
{
if
(
window
.
JSON
&&
window
.
JSON
.
parse
)
{
return
window
.
JSON
.
parse
(
data
);
return
window
.
JSON
.
parse
(
data
);
}
}
if
(
data
===
null
)
{
return
data
;
}
if
(
typeof
data
===
"string"
)
{
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data
=
jQuery
.
trim
(
data
);
if
(
data
)
{
// Make sure the incoming data is actual JSON
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
// Logic borrowed from http://json.org/json2.js
if
(
rvalidchars
.
test
(
data
.
replace
(
rvalidescape
,
"@"
)
if
(
rvalidchars
.
test
(
data
.
replace
(
rvalidescape
,
"@"
)
...
@@ -507,8 +510,10 @@ jQuery.extend({
...
@@ -507,8 +510,10 @@ jQuery.extend({
.
replace
(
rvalidbraces
,
""
))
)
{
.
replace
(
rvalidbraces
,
""
))
)
{
return
(
new
Function
(
"return "
+
data
)
)();
return
(
new
Function
(
"return "
+
data
)
)();
}
}
}
}
jQuery
.
error
(
"Invalid JSON: "
+
data
);
jQuery
.
error
(
"Invalid JSON: "
+
data
);
},
},
...
...
test/unit/ajax.js
View file @
ee9687d4
...
@@ -2759,4 +2759,18 @@ if ( jQuery.ajax && ( !isLocal || hasPHP ) ) {
...
@@ -2759,4 +2759,18 @@ if ( jQuery.ajax && ( !isLocal || hasPHP ) ) {
start
();
start
();
});
});
});
});
test
(
"jQuery.ajax - empty json gets to error callback instead of success callback."
,
function
()
{
expect
(
1
);
stop
();
jQuery
.
ajax
(
url
(
"data/echoData.php"
),
{
error
:
function
(
_
,
__
,
error
)
{
equal
(
typeof
error
===
"object"
,
true
,
"Didn't get back error object for empty json response"
);
start
();
},
dataType
:
"json"
});
});
}
}
test/unit/core.js
View file @
ee9687d4
...
@@ -1150,9 +1150,13 @@ test("jQuery.parseHTML", function() {
...
@@ -1150,9 +1150,13 @@ test("jQuery.parseHTML", function() {
test
(
"jQuery.parseJSON"
,
function
(){
test
(
"jQuery.parseJSON"
,
function
(){
expect
(
8
);
expect
(
8
);
equal
(
jQuery
.
parseJSON
(),
null
,
"Nothing in, null out."
);
raises
(
function
()
{
equal
(
jQuery
.
parseJSON
(
null
),
null
,
"Nothing in, null out."
);
jQuery
.
parseJSON
();
equal
(
jQuery
.
parseJSON
(
""
),
null
,
"Nothing in, null out."
);
},
null
,
"parseJson now matches JSON.parse for empty input."
);
equal
(
jQuery
.
parseJSON
(
null
),
null
,
"parseJson now matches JSON.parse on null input."
);
raises
(
function
()
{
jQuery
.
parseJSON
(
""
);
},
null
,
"parseJson now matches JSON.parse for empty strings."
);
deepEqual
(
jQuery
.
parseJSON
(
"{}"
),
{},
"Plain object parsing."
);
deepEqual
(
jQuery
.
parseJSON
(
"{}"
),
{},
"Plain object parsing."
);
deepEqual
(
jQuery
.
parseJSON
(
"{
\"
test
\"
:1}"
),
{
"test"
:
1
},
"Plain object parsing."
);
deepEqual
(
jQuery
.
parseJSON
(
"{
\"
test
\"
:1}"
),
{
"test"
:
1
},
"Plain object parsing."
);
...
@@ -1346,3 +1350,50 @@ test("jQuery.camelCase()", function() {
...
@@ -1346,3 +1350,50 @@ test("jQuery.camelCase()", function() {
equal
(
jQuery
.
camelCase
(
key
),
val
,
"Converts: "
+
key
+
" => "
+
val
);
equal
(
jQuery
.
camelCase
(
key
),
val
,
"Converts: "
+
key
+
" => "
+
val
);
});
});
});
});
test
(
"JQuery.parseJSON() test internal parseJson (using fallback) to make sure that it throws like JSON.parse"
,
function
()
{
expect
(
10
);
var
jsonParse
=
window
.
JSON
;
window
.
JSON
=
null
;
raises
(
function
()
{
jsonParse
.
parse
(
"''"
);
});
raises
(
function
()
{
jQuery
.
parseJSON
(
"''"
);
});
raises
(
function
()
{
jsonParse
.
parse
(
""
);
});
raises
(
function
()
{
jQuery
.
parseJSON
(
""
);
});
raises
(
function
()
{
jsonParse
.
parse
({});
});
raises
(
function
()
{
jQuery
.
parseJSON
({});
});
var
parsedValue
=
jsonParse
.
parse
(
null
);
equal
(
parsedValue
,
null
);
parsedValue
=
jQuery
.
parseJSON
(
null
);
equal
(
parsedValue
,
null
);
parsedValue
=
jsonParse
.
parse
(
"{}"
);
equal
(
(
typeof
parsedValue
===
"object"
),
true
);
parsedValue
=
jQuery
.
parseJSON
(
"{}"
);
equal
(
(
typeof
parsedValue
===
"object"
),
true
);
window
.
JSON
=
jsonParse
;
}
);
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