-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure elements returned from create() are detached from its parent e…
…lement
- Loading branch information
Showing
3 changed files
with
77 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Ported from jQuery's html() | ||
* https://github.com/jquery/jquery/blob/c9aae3565edc840961ecbeee77fb0cb367c46702/src/manipulation/buildFragment.js | ||
*/ | ||
|
||
const wrapMap = { | ||
thead: [1, '<table>', '</table>'], | ||
col: [2, '<table><colgroup>', '</colgroup></table>'], | ||
tr: [2, '<table><tbody>', '</tbody></table>'], | ||
td: [3, '<table><tbody><tr>', '</tr></tbody></table>'], | ||
|
||
_: [0, '', ''] | ||
}; | ||
|
||
wrapMap.tbody = wrapMap.thead; | ||
wrapMap.tfoot = wrapMap.thead; | ||
wrapMap.colgroup = wrapMap.thead; | ||
wrapMap.caption = wrapMap.thead; | ||
|
||
wrapMap.th = wrapMap.td; | ||
|
||
export default function fragmentContainer(html) { | ||
if (html === '') { | ||
return document.createTextNode(' '); | ||
} | ||
|
||
const match = /<([a-z][^/\0>\u0020\t\r\n\f]*)/i.exec(html); | ||
|
||
const tag = match ? match[0].replace(/</g, '').replace(/>/g, '') : '_'; | ||
const map = wrapMap[tag] || wrapMap._; | ||
|
||
const fragment = document.createDocumentFragment(); | ||
let container = fragment.appendChild(document.createElement('div')); | ||
|
||
container.innerHTML = map[1] + html + map[2]; | ||
|
||
let j = map[0]; | ||
|
||
while (j--) { | ||
container = container.lastChild; | ||
} | ||
|
||
return container; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters