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
63c1414a
Commit
63c1414a
authored
May 05, 2015
by
Timmy Willison
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Manipulation: privatize buildFragment() function
Fixes gh-2224
parent
b9b5c23f
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
265 additions
and
202 deletions
+265
-202
parseHTML.js
src/core/parseHTML.js
+4
-6
manipulation.js
src/manipulation.js
+17
-196
buildFragment.js
src/manipulation/buildFragment.js
+118
-0
createSafeFragment.js
src/manipulation/createSafeFragment.js
+20
-0
getAll.js
src/manipulation/getAll.js
+33
-0
setGlobalEval.js
src/manipulation/setGlobalEval.js
+19
-0
nodeNames.js
src/manipulation/var/nodeNames.js
+5
-0
rleadingWhitespace.js
src/manipulation/var/rleadingWhitespace.js
+3
-0
rscriptType.js
src/manipulation/var/rscriptType.js
+3
-0
rtagName.js
src/manipulation/var/rtagName.js
+3
-0
wrapMap.js
src/manipulation/wrapMap.js
+40
-0
No files found.
src/core/parseHTML.js
View file @
63c1414a
define
([
define
([
"../core"
,
"../core"
,
"./var/rsingleTag"
,
"./var/rsingleTag"
,
"../manipulation/buildFragment"
,
// This is the only module that needs core/support
// This is the only module that needs core/support
"./support"
,
"./support"
],
function
(
jQuery
,
rsingleTag
,
buildFragment
,
support
)
{
// buildFragment
"../manipulation"
],
function
(
jQuery
,
rsingleTag
,
support
)
{
// data: string of html
// data: string of html
// context (optional): If specified, the fragment will be created in this context,
// context (optional): If specified, the fragment will be created in this context,
...
@@ -35,7 +33,7 @@ jQuery.parseHTML = function( data, context, keepScripts ) {
...
@@ -35,7 +33,7 @@ jQuery.parseHTML = function( data, context, keepScripts ) {
return
[
context
.
createElement
(
parsed
[
1
]
)
];
return
[
context
.
createElement
(
parsed
[
1
]
)
];
}
}
parsed
=
jQuery
.
buildFragment
(
[
data
],
context
,
scripts
);
parsed
=
buildFragment
(
[
data
],
context
,
scripts
);
if
(
scripts
&&
scripts
.
length
)
{
if
(
scripts
&&
scripts
.
length
)
{
jQuery
(
scripts
).
remove
();
jQuery
(
scripts
).
remove
();
...
...
src/manipulation.js
View file @
63c1414a
...
@@ -4,7 +4,17 @@ define([
...
@@ -4,7 +4,17 @@ define([
"./var/push"
,
"./var/push"
,
"./var/deletedIds"
,
"./var/deletedIds"
,
"./core/access"
,
"./core/access"
,
"./manipulation/var/rcheckableType"
,
"./manipulation/var/rcheckableType"
,
"./manipulation/var/rtagName"
,
"./manipulation/var/rscriptType"
,
"./manipulation/var/rleadingWhitespace"
,
"./manipulation/var/nodeNames"
,
"./manipulation/createSafeFragment"
,
"./manipulation/wrapMap"
,
"./manipulation/getAll"
,
"./manipulation/setGlobalEval"
,
"./manipulation/buildFragment"
,
"./manipulation/support"
,
"./manipulation/support"
,
"./core/init"
,
"./core/init"
,
...
@@ -12,96 +22,22 @@ define([
...
@@ -12,96 +22,22 @@ define([
"./traversing"
,
"./traversing"
,
"./selector"
,
"./selector"
,
"./event"
"./event"
],
function
(
jQuery
,
concat
,
push
,
deletedIds
,
access
,
rcheckableType
,
support
)
{
],
function
(
jQuery
,
concat
,
push
,
deletedIds
,
access
,
rcheckableType
,
rtagName
,
rscriptType
,
rleadingWhitespace
,
nodeNames
,
function
createSafeFragment
(
document
)
{
createSafeFragment
,
wrapMap
,
getAll
,
setGlobalEval
,
var
list
=
nodeNames
.
split
(
"|"
),
buildFragment
,
support
)
{
safeFrag
=
document
.
createDocumentFragment
();
if
(
safeFrag
.
createElement
)
{
var
rinlinejQuery
=
/ jQuery
\d
+="
(?:
null|
\d
+
)
"/g
,
while
(
list
.
length
)
{
rnoshimcache
=
new
RegExp
(
"<(?:"
+
nodeNames
+
")[
\\
s/>]"
,
"i"
),
safeFrag
.
createElement
(
list
.
pop
()
);
}
}
return
safeFrag
;
}
var
nodeNames
=
"abbr|article|aside|audio|bdi|canvas|data|datalist|"
+
"details|dialog|figcaption|figure|footer|header|hgroup|main|"
+
"mark|meter|nav|output|picture|progress|section|summary|template|time|video"
,
rinlinejQuery
=
/ jQuery
\d
+="
(?:
null|
\d
+
)
"/g
,
rnoshimcache
=
new
RegExp
(
"<(?:"
+
nodeNames
+
")[
\\
s/>]"
,
"i"
),
rleadingWhitespace
=
/^
\s
+/
,
rxhtmlTag
=
/<
(?!
area|br|col|embed|hr|img|input|link|meta|param
)(([\w
:-
]
+
)[^
>
]
*
)\/
>/gi
,
rxhtmlTag
=
/<
(?!
area|br|col|embed|hr|img|input|link|meta|param
)(([\w
:-
]
+
)[^
>
]
*
)\/
>/gi
,
rtagName
=
/<
([\w
:-
]
+
)
/
,
rhtml
=
/<|&#
?\w
+;/
,
rnoInnerhtml
=
/<
(?:
script|style|link
)
/i
,
rnoInnerhtml
=
/<
(?:
script|style|link
)
/i
,
// checked="checked" or checked
// checked="checked" or checked
rchecked
=
/checked
\s
*
(?:[^
=
]
|=
\s
*.checked.
)
/i
,
rchecked
=
/checked
\s
*
(?:[^
=
]
|=
\s
*.checked.
)
/i
,
rscriptType
=
/^$|
\/(?:
java|ecma
)
script/i
,
rscriptTypeMasked
=
/^true
\/(
.*
)
/
,
rscriptTypeMasked
=
/^true
\/(
.*
)
/
,
rcleanScript
=
/^
\s
*<!
(?:\[
CDATA
\[
|--
)
|
(?:\]\]
|--
)
>
\s
*$/g
,
rcleanScript
=
/^
\s
*<!
(?:\[
CDATA
\[
|--
)
|
(?:\]\]
|--
)
>
\s
*$/g
,
// We have to close these tags to support XHTML (#13200)
wrapMap
=
{
option
:
[
1
,
"<select multiple='multiple'>"
,
"</select>"
],
// Support: IE8
param
:
[
1
,
"<object>"
,
"</object>"
],
thead
:
[
1
,
"<table>"
,
"</table>"
],
// Some of the following wrappers are not fully defined, because
// their parent elements (except for "table" element) could be omitted
// since browser parsers are smart enough to auto-insert them
// Support: Android 2.3
// Android browser doesn't auto-insert colgroup
col
:
[
2
,
"<table><colgroup>"
,
"</colgroup></table>"
],
// Auto-insert "tbody" element
tr
:
[
2
,
"<table>"
,
"</table>"
],
// Auto-insert "tbody" and "tr" elements
td
:
[
3
,
"<table>"
,
"</table>"
],
// IE8 can't serialize link, script, style, or any html5 (NoScope) tags,
// unless wrapped in a div with non-breaking characters in front of it.
_default
:
support
.
htmlSerialize
?
[
0
,
""
,
""
]
:
[
1
,
"X<div>"
,
"</div>"
]
},
safeFragment
=
createSafeFragment
(
document
),
safeFragment
=
createSafeFragment
(
document
),
fragmentDiv
=
safeFragment
.
appendChild
(
document
.
createElement
(
"div"
)
);
fragmentDiv
=
safeFragment
.
appendChild
(
document
.
createElement
(
"div"
)
);
wrapMap
.
optgroup
=
wrapMap
.
option
;
wrapMap
.
tbody
=
wrapMap
.
tfoot
=
wrapMap
.
colgroup
=
wrapMap
.
caption
=
wrapMap
.
thead
;
wrapMap
.
th
=
wrapMap
.
td
;
function
getAll
(
context
,
tag
)
{
var
elems
,
elem
,
i
=
0
,
found
=
typeof
context
.
getElementsByTagName
!==
"undefined"
?
context
.
getElementsByTagName
(
tag
||
"*"
)
:
typeof
context
.
querySelectorAll
!==
"undefined"
?
context
.
querySelectorAll
(
tag
||
"*"
)
:
undefined
;
if
(
!
found
)
{
for
(
found
=
[],
elems
=
context
.
childNodes
||
context
;
(
elem
=
elems
[
i
])
!=
null
;
i
++
)
{
if
(
!
tag
||
jQuery
.
nodeName
(
elem
,
tag
)
)
{
found
.
push
(
elem
);
}
else
{
jQuery
.
merge
(
found
,
getAll
(
elem
,
tag
)
);
}
}
}
return
tag
===
undefined
||
tag
&&
jQuery
.
nodeName
(
context
,
tag
)
?
jQuery
.
merge
(
[
context
],
found
)
:
found
;
}
// Manipulating tables requires a tbody
// Manipulating tables requires a tbody
function
manipulationTarget
(
elem
,
content
)
{
function
manipulationTarget
(
elem
,
content
)
{
if
(
jQuery
.
nodeName
(
elem
,
"table"
)
&&
if
(
jQuery
.
nodeName
(
elem
,
"table"
)
&&
...
@@ -128,21 +64,7 @@ function restoreScript( elem ) {
...
@@ -128,21 +64,7 @@ function restoreScript( elem ) {
return
elem
;
return
elem
;
}
}
// Mark scripts as having already been evaluated
function
setGlobalEval
(
elems
,
refElements
)
{
var
elem
,
i
=
0
;
for
(
;
(
elem
=
elems
[
i
])
!=
null
;
i
++
)
{
jQuery
.
_data
(
elem
,
"globalEval"
,
!
refElements
||
jQuery
.
_data
(
refElements
[
i
],
"globalEval"
)
);
}
}
function
cloneCopyEvent
(
src
,
dest
)
{
function
cloneCopyEvent
(
src
,
dest
)
{
if
(
dest
.
nodeType
!==
1
||
!
jQuery
.
hasData
(
src
)
)
{
if
(
dest
.
nodeType
!==
1
||
!
jQuery
.
hasData
(
src
)
)
{
return
;
return
;
}
}
...
@@ -278,107 +200,6 @@ jQuery.extend({
...
@@ -278,107 +200,6 @@ jQuery.extend({
return
clone
;
return
clone
;
},
},
buildFragment
:
function
(
elems
,
context
,
scripts
,
selection
,
ignored
)
{
var
j
,
elem
,
contains
,
tmp
,
tag
,
wrap
,
l
=
elems
.
length
,
// Ensure a safe fragment
safe
=
createSafeFragment
(
context
),
nodes
=
[],
i
=
0
;
for
(
;
i
<
l
;
i
++
)
{
elem
=
elems
[
i
];
if
(
elem
||
elem
===
0
)
{
// Add nodes directly
if
(
jQuery
.
type
(
elem
)
===
"object"
)
{
jQuery
.
merge
(
nodes
,
elem
.
nodeType
?
[
elem
]
:
elem
);
// Convert non-html into a text node
}
else
if
(
!
rhtml
.
test
(
elem
)
)
{
nodes
.
push
(
context
.
createTextNode
(
elem
)
);
// Convert html into DOM nodes
}
else
{
tmp
=
tmp
||
safe
.
appendChild
(
context
.
createElement
(
"div"
)
);
// Deserialize a standard representation
tag
=
(
rtagName
.
exec
(
elem
)
||
[
""
,
""
])[
1
].
toLowerCase
();
wrap
=
wrapMap
[
tag
]
||
wrapMap
.
_default
;
tmp
.
innerHTML
=
wrap
[
1
]
+
jQuery
.
htmlPrefilter
(
elem
)
+
wrap
[
2
];
// Descend through wrappers to the right content
j
=
wrap
[
0
];
while
(
j
--
)
{
tmp
=
tmp
.
lastChild
;
}
// Manually add leading whitespace removed by IE
if
(
!
support
.
leadingWhitespace
&&
rleadingWhitespace
.
test
(
elem
)
)
{
nodes
.
push
(
context
.
createTextNode
(
rleadingWhitespace
.
exec
(
elem
)[
0
]
)
);
}
jQuery
.
merge
(
nodes
,
tmp
.
childNodes
);
// Fix #12392 for WebKit and IE > 9
tmp
.
textContent
=
""
;
// Fix #12392 for oldIE
while
(
tmp
.
firstChild
)
{
tmp
.
removeChild
(
tmp
.
firstChild
);
}
// Remember the top-level container for proper cleanup
tmp
=
safe
.
lastChild
;
}
}
}
// Fix #11356: Clear elements from fragment
if
(
tmp
)
{
safe
.
removeChild
(
tmp
);
}
i
=
0
;
while
(
(
elem
=
nodes
[
i
++
])
)
{
// Skip elements already in the context collection (trac-4087)
if
(
selection
&&
jQuery
.
inArray
(
elem
,
selection
)
>
-
1
)
{
if
(
ignored
)
{
ignored
.
push
(
elem
);
}
continue
;
}
contains
=
jQuery
.
contains
(
elem
.
ownerDocument
,
elem
);
// Append to fragment
tmp
=
getAll
(
safe
.
appendChild
(
elem
),
"script"
);
// Preserve script evaluation history
if
(
contains
)
{
setGlobalEval
(
tmp
);
}
// Capture executables
if
(
scripts
)
{
j
=
0
;
while
(
(
elem
=
tmp
[
j
++
])
)
{
if
(
rscriptType
.
test
(
elem
.
type
||
""
)
)
{
scripts
.
push
(
elem
);
}
}
}
}
tmp
=
null
;
return
safe
;
},
cleanData
:
function
(
elems
,
/* internal */
acceptData
)
{
cleanData
:
function
(
elems
,
/* internal */
acceptData
)
{
var
elem
,
type
,
id
,
data
,
var
elem
,
type
,
id
,
data
,
i
=
0
,
i
=
0
,
...
@@ -626,7 +447,7 @@ jQuery.fn.extend({
...
@@ -626,7 +447,7 @@ jQuery.fn.extend({
}
}
if
(
l
)
{
if
(
l
)
{
fragment
=
jQuery
.
buildFragment
(
args
,
this
[
0
].
ownerDocument
,
false
,
this
,
ignored
);
fragment
=
buildFragment
(
args
,
this
[
0
].
ownerDocument
,
false
,
this
,
ignored
);
first
=
fragment
.
firstChild
;
first
=
fragment
.
firstChild
;
if
(
fragment
.
childNodes
.
length
===
1
)
{
if
(
fragment
.
childNodes
.
length
===
1
)
{
...
...
src/manipulation/buildFragment.js
0 → 100644
View file @
63c1414a
define
([
"../core"
,
"./var/rtagName"
,
"./var/rscriptType"
,
"./var/rleadingWhitespace"
,
"./createSafeFragment"
,
"./wrapMap"
,
"./getAll"
,
"./setGlobalEval"
,
"./support"
],
function
(
jQuery
,
rtagName
,
rscriptType
,
rleadingWhitespace
,
createSafeFragment
,
wrapMap
,
getAll
,
setGlobalEval
,
support
)
{
var
rhtml
=
/<|&#
?\w
+;/
;
function
buildFragment
(
elems
,
context
,
scripts
,
selection
,
ignored
)
{
var
j
,
elem
,
contains
,
tmp
,
tag
,
wrap
,
l
=
elems
.
length
,
// Ensure a safe fragment
safe
=
createSafeFragment
(
context
),
nodes
=
[],
i
=
0
;
for
(
;
i
<
l
;
i
++
)
{
elem
=
elems
[
i
];
if
(
elem
||
elem
===
0
)
{
// Add nodes directly
if
(
jQuery
.
type
(
elem
)
===
"object"
)
{
jQuery
.
merge
(
nodes
,
elem
.
nodeType
?
[
elem
]
:
elem
);
// Convert non-html into a text node
}
else
if
(
!
rhtml
.
test
(
elem
)
)
{
nodes
.
push
(
context
.
createTextNode
(
elem
)
);
// Convert html into DOM nodes
}
else
{
tmp
=
tmp
||
safe
.
appendChild
(
context
.
createElement
(
"div"
)
);
// Deserialize a standard representation
tag
=
(
rtagName
.
exec
(
elem
)
||
[
""
,
""
])[
1
].
toLowerCase
();
wrap
=
wrapMap
[
tag
]
||
wrapMap
.
_default
;
tmp
.
innerHTML
=
wrap
[
1
]
+
jQuery
.
htmlPrefilter
(
elem
)
+
wrap
[
2
];
// Descend through wrappers to the right content
j
=
wrap
[
0
];
while
(
j
--
)
{
tmp
=
tmp
.
lastChild
;
}
// Manually add leading whitespace removed by IE
if
(
!
support
.
leadingWhitespace
&&
rleadingWhitespace
.
test
(
elem
)
)
{
nodes
.
push
(
context
.
createTextNode
(
rleadingWhitespace
.
exec
(
elem
)[
0
]
)
);
}
jQuery
.
merge
(
nodes
,
tmp
.
childNodes
);
// Fix #12392 for WebKit and IE > 9
tmp
.
textContent
=
""
;
// Fix #12392 for oldIE
while
(
tmp
.
firstChild
)
{
tmp
.
removeChild
(
tmp
.
firstChild
);
}
// Remember the top-level container for proper cleanup
tmp
=
safe
.
lastChild
;
}
}
}
// Fix #11356: Clear elements from fragment
if
(
tmp
)
{
safe
.
removeChild
(
tmp
);
}
i
=
0
;
while
(
(
elem
=
nodes
[
i
++
])
)
{
// Skip elements already in the context collection (trac-4087)
if
(
selection
&&
jQuery
.
inArray
(
elem
,
selection
)
>
-
1
)
{
if
(
ignored
)
{
ignored
.
push
(
elem
);
}
continue
;
}
contains
=
jQuery
.
contains
(
elem
.
ownerDocument
,
elem
);
// Append to fragment
tmp
=
getAll
(
safe
.
appendChild
(
elem
),
"script"
);
// Preserve script evaluation history
if
(
contains
)
{
setGlobalEval
(
tmp
);
}
// Capture executables
if
(
scripts
)
{
j
=
0
;
while
(
(
elem
=
tmp
[
j
++
])
)
{
if
(
rscriptType
.
test
(
elem
.
type
||
""
)
)
{
scripts
.
push
(
elem
);
}
}
}
}
tmp
=
null
;
return
safe
;
}
return
buildFragment
;
});
src/manipulation/createSafeFragment.js
0 → 100644
View file @
63c1414a
define
([
"./var/nodeNames"
],
function
(
nodeNames
)
{
function
createSafeFragment
(
document
)
{
var
list
=
nodeNames
.
split
(
"|"
),
safeFrag
=
document
.
createDocumentFragment
();
if
(
safeFrag
.
createElement
)
{
while
(
list
.
length
)
{
safeFrag
.
createElement
(
list
.
pop
()
);
}
}
return
safeFrag
;
}
return
createSafeFragment
;
});
src/manipulation/getAll.js
0 → 100644
View file @
63c1414a
define
([
"../core"
],
function
(
jQuery
)
{
function
getAll
(
context
,
tag
)
{
var
elems
,
elem
,
i
=
0
,
found
=
typeof
context
.
getElementsByTagName
!==
"undefined"
?
context
.
getElementsByTagName
(
tag
||
"*"
)
:
typeof
context
.
querySelectorAll
!==
"undefined"
?
context
.
querySelectorAll
(
tag
||
"*"
)
:
undefined
;
if
(
!
found
)
{
for
(
found
=
[],
elems
=
context
.
childNodes
||
context
;
(
elem
=
elems
[
i
]
)
!=
null
;
i
++
)
{
if
(
!
tag
||
jQuery
.
nodeName
(
elem
,
tag
)
)
{
found
.
push
(
elem
);
}
else
{
jQuery
.
merge
(
found
,
getAll
(
elem
,
tag
)
);
}
}
}
return
tag
===
undefined
||
tag
&&
jQuery
.
nodeName
(
context
,
tag
)
?
jQuery
.
merge
(
[
context
],
found
)
:
found
;
}
return
getAll
;
});
src/manipulation/setGlobalEval.js
0 → 100644
View file @
63c1414a
define
([
"../core"
],
function
(
jQuery
)
{
// Mark scripts as having already been evaluated
function
setGlobalEval
(
elems
,
refElements
)
{
var
elem
,
i
=
0
;
for
(
;
(
elem
=
elems
[
i
]
)
!=
null
;
i
++
)
{
jQuery
.
_data
(
elem
,
"globalEval"
,
!
refElements
||
jQuery
.
_data
(
refElements
[
i
],
"globalEval"
)
);
}
}
return
setGlobalEval
;
});
src/manipulation/var/nodeNames.js
0 → 100644
View file @
63c1414a
define
(
function
()
{
return
"abbr|article|aside|audio|bdi|canvas|data|datalist|"
+
"details|dialog|figcaption|figure|footer|header|hgroup|main|"
+
"mark|meter|nav|output|picture|progress|section|summary|template|time|video"
;
});
src/manipulation/var/rleadingWhitespace.js
0 → 100644
View file @
63c1414a
define
(
function
()
{
return
(
/^
\s
+/
);
});
src/manipulation/var/rscriptType.js
0 → 100644
View file @
63c1414a
define
(
function
()
{
return
(
/^$|
\/(?:
java|ecma
)
script/i
);
});
src/manipulation/var/rtagName.js
0 → 100644
View file @
63c1414a
define
(
function
()
{
return
(
/<
([\w
:-
]
+
)
/
);
});
src/manipulation/wrapMap.js
0 → 100644
View file @
63c1414a
define
([
"./support"
],
function
(
support
)
{
// We have to close these tags to support XHTML (#13200)
var
wrapMap
=
{
option
:
[
1
,
"<select multiple='multiple'>"
,
"</select>"
],
// Support: IE8
param
:
[
1
,
"<object>"
,
"</object>"
],
thead
:
[
1
,
"<table>"
,
"</table>"
],
// Some of the following wrappers are not fully defined, because
// their parent elements (except for "table" element) could be omitted
// since browser parsers are smart enough to auto-insert them
// Support: Android 2.3
// Android browser doesn't auto-insert colgroup
col
:
[
2
,
"<table><colgroup>"
,
"</colgroup></table>"
],
// Auto-insert "tbody" element
tr
:
[
2
,
"<table>"
,
"</table>"
],
// Auto-insert "tbody" and "tr" elements
td
:
[
3
,
"<table>"
,
"</table>"
],
// IE8 can't serialize link, script, style, or any html5 (NoScope) tags,
// unless wrapped in a div with non-breaking characters in front of it.
_default
:
support
.
htmlSerialize
?
[
0
,
""
,
""
]
:
[
1
,
"X<div>"
,
"</div>"
]
};
// Support: IE8-IE9
wrapMap
.
optgroup
=
wrapMap
.
option
;
wrapMap
.
tbody
=
wrapMap
.
tfoot
=
wrapMap
.
colgroup
=
wrapMap
.
caption
=
wrapMap
.
thead
;
wrapMap
.
th
=
wrapMap
.
td
;
return
wrapMap
;
});
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