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
e2497c68
Commit
e2497c68
authored
Jun 21, 2012
by
timmywil
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add parseHTML for explicitly parsing strings into html. Fixes #11617.
parent
7ff3da18
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
17 deletions
+60
-17
core.js
src/core.js
+31
-17
core.js
test/unit/core.js
+29
-0
No files found.
src/core.js
View file @
e2497c68
...
@@ -104,7 +104,6 @@ jQuery.fn = jQuery.prototype = {
...
@@ -104,7 +104,6 @@ jQuery.fn = jQuery.prototype = {
// Handle HTML strings
// Handle HTML strings
if
(
typeof
selector
===
"string"
)
{
if
(
typeof
selector
===
"string"
)
{
// Are we dealing with HTML string or an ID?
if
(
selector
.
charAt
(
0
)
===
"<"
&&
selector
.
charAt
(
selector
.
length
-
1
)
===
">"
&&
selector
.
length
>=
3
)
{
if
(
selector
.
charAt
(
0
)
===
"<"
&&
selector
.
charAt
(
selector
.
length
-
1
)
===
">"
&&
selector
.
length
>=
3
)
{
// Assume that strings that start and end with <> are HTML and skip the regex check
// Assume that strings that start and end with <> are HTML and skip the regex check
match
=
[
null
,
selector
,
null
];
match
=
[
null
,
selector
,
null
];
...
@@ -118,19 +117,10 @@ jQuery.fn = jQuery.prototype = {
...
@@ -118,19 +117,10 @@ jQuery.fn = jQuery.prototype = {
context
=
context
instanceof
jQuery
?
context
[
0
]
:
context
;
context
=
context
instanceof
jQuery
?
context
[
0
]
:
context
;
doc
=
(
context
&&
context
.
nodeType
?
context
.
ownerDocument
||
context
:
document
);
doc
=
(
context
&&
context
.
nodeType
?
context
.
ownerDocument
||
context
:
document
);
// If a single string is passed in and it's a single tag
// scripts is true for back-compat
// just do a createElement and skip the rest
selector
=
jQuery
.
parseHTML
(
match
[
1
],
doc
,
true
);
ret
=
rsingleTag
.
exec
(
selector
);
if
(
rsingleTag
.
test
(
match
[
1
]
)
&&
jQuery
.
isPlainObject
(
context
)
)
{
this
.
attr
.
call
(
selector
,
context
,
true
);
if
(
ret
)
{
selector
=
[
doc
.
createElement
(
ret
[
1
]
)
];
if
(
jQuery
.
isPlainObject
(
context
)
)
{
this
.
attr
.
call
(
selector
,
context
,
true
);
}
}
else
{
ret
=
jQuery
.
buildFragment
(
[
match
[
1
]
],
doc
);
selector
=
(
ret
.
cacheable
?
jQuery
.
clone
(
ret
.
fragment
)
:
ret
.
fragment
).
childNodes
;
}
}
return
jQuery
.
merge
(
this
,
selector
);
return
jQuery
.
merge
(
this
,
selector
);
...
@@ -459,8 +449,32 @@ jQuery.extend({
...
@@ -459,8 +449,32 @@ jQuery.extend({
throw
new
Error
(
msg
);
throw
new
Error
(
msg
);
},
},
// data: string of html
// context (optional): If specified, the fragment will be created in this context, defaults to document
// scripts (optional): If true, will include scripts passed in the html string
parseHTML
:
function
(
data
,
context
,
scripts
)
{
var
parsed
;
if
(
!
data
||
typeof
data
!==
"string"
)
{
return
null
;
}
if
(
typeof
context
===
"boolean"
)
{
scripts
=
context
;
context
=
0
;
}
context
=
context
||
document
;
// Single tag
if
(
(
parsed
=
rsingleTag
.
exec
(
data
))
)
{
return
[
context
.
createElement
(
parsed
[
1
]
)
];
}
parsed
=
jQuery
.
buildFragment
(
[
data
],
context
,
scripts
?
null
:
[]
);
return
jQuery
.
merge
(
[],
(
parsed
.
cacheable
?
jQuery
.
clone
(
parsed
.
fragment
)
:
parsed
.
fragment
).
childNodes
);
},
parseJSON
:
function
(
data
)
{
parseJSON
:
function
(
data
)
{
if
(
typeof
data
!==
"string"
||
!
data
)
{
if
(
!
data
||
typeof
data
!==
"string"
)
{
return
null
;
return
null
;
}
}
...
@@ -486,10 +500,10 @@ jQuery.extend({
...
@@ -486,10 +500,10 @@ jQuery.extend({
// Cross-browser xml parsing
// Cross-browser xml parsing
parseXML
:
function
(
data
)
{
parseXML
:
function
(
data
)
{
if
(
typeof
data
!==
"string"
||
!
data
)
{
var
xml
,
tmp
;
if
(
!
data
||
typeof
data
!==
"string"
)
{
return
null
;
return
null
;
}
}
var
xml
,
tmp
;
try
{
try
{
if
(
window
.
DOMParser
)
{
// Standard
if
(
window
.
DOMParser
)
{
// Standard
tmp
=
new
DOMParser
();
tmp
=
new
DOMParser
();
...
...
test/unit/core.js
View file @
e2497c68
...
@@ -1120,6 +1120,35 @@ test("jQuery.proxy", function(){
...
@@ -1120,6 +1120,35 @@ test("jQuery.proxy", function(){
jQuery
.
proxy
(
test4
,
"meth"
)(
"boom"
);
jQuery
.
proxy
(
test4
,
"meth"
)(
"boom"
);
});
});
test
(
"jQuery.parseHTML"
,
function
()
{
expect
(
11
);
equal
(
jQuery
.
parseHTML
(),
null
,
"Nothing in, null out."
);
equal
(
jQuery
.
parseHTML
(
null
),
null
,
"Nothing in, null out."
);
equal
(
jQuery
.
parseHTML
(
""
),
null
,
"Nothing in, null out."
);
raises
(
function
()
{
jQuery
.
parseHTML
(
"<div>"
,
document
.
getElementById
(
"form"
)
);
},
"Passing an element as the context raises an exception (context should be a document)"
);
var
elems
=
jQuery
.
parseHTML
(
jQuery
(
"body"
).
html
()
);
ok
(
elems
.
length
>
10
,
"Parse a large html string"
);
equal
(
jQuery
.
type
(
elems
),
"array"
,
"parseHTML returns an array rather than a nodelist"
);
var
script
=
"<script>undefined()</script>"
;
equal
(
jQuery
.
parseHTML
(
script
).
length
,
0
,
"Passing a script is not allowed by default"
);
raises
(
function
()
{
jQuery
(
jQuery
.
parseHTML
(
script
,
true
)).
appendTo
(
"#qunit-fixture"
);
},
"Passing a script is allowed if allowScripts is true"
);
var
html
=
script
+
"<div></div>"
;
equal
(
jQuery
.
parseHTML
(
html
)[
0
].
nodeName
.
toLowerCase
(),
"div"
,
"Ignore scripts by default"
);
raises
(
function
()
{
jQuery
(
jQuery
.
parseHTML
(
html
,
true
)).
appendTo
(
"#qunit-fixture"
);
},
"Passing a script is allowed if allowScripts is true"
);
equal
(
jQuery
.
parseHTML
(
"text"
)[
0
].
nodeType
,
3
,
"Parsing text returns a text node"
);
});
test
(
"jQuery.parseJSON"
,
function
(){
test
(
"jQuery.parseJSON"
,
function
(){
expect
(
8
);
expect
(
8
);
...
...
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