1 Star 0 Fork 10

hutubaba/dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
wrap.js 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
import isFunction from './utils/types/isFunction'
import isString from './utils/types/isString'
import isHTML from './utils/types/isHTML'
import isElement from './isElement'
import isCollection from './isCollection'
import getEl from './getEl'
import build from './build'
import clone from './clone'
import replace from './replace'
/**
* 给 NodeList 中的每个 DOM 节点包裹上指定 HTML 标签
* ========================================================================
* @method wrap
* @since 1.1.0
* @param {HTMLCollection|NodeList} collection
* @param {String|HTMLElement|Function} wrapElement
*/
const wrap = (collection, wrapElement) => {
let $collection = []
let $temp
let strHTML
let $return
/* istanbul ignore else */
if (
!isCollection(collection) ||
(!isHTML(wrapElement) &&
!isString(wrapElement) &&
!isElement(wrapElement) &&
!isFunction(wrapElement))
) {
return false
}
$collection = [...collection]
if ($collection.length < 1) {
return false
}
$collection.forEach(($node, i) => {
if (isHTML(wrapElement)) {
// HTML 字符串
strHTML = build(wrapElement).outerHTML
} else if (isString(wrapElement) && isElement(getEl(wrapElement))) {
// DOM 元素的选择器
strHTML = clone(getEl(wrapElement), true).outerHTML
} else if (isElement(wrapElement)) {
// DOM 元素
strHTML = clone(wrapElement, true).outerHTML
} else {
$return = wrapElement($node, i)
// Function 返回 DOM 元素
if (isElement($return)) {
strHTML = clone($return, true).outerHTML
} else {
// Function 返回(HTML)字符串
strHTML = build(wrapElement($node, i)).outerHTML
}
}
strHTML = strHTML.replace(
/>(?:\s*)<\//,
`>${clone($node, true).outerHTML}</`
)
$temp = build(strHTML)
/* istanbul ignore else */
if ($temp) {
replace($temp, $node)
}
})
}
export default wrap
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/hutu88/dom.js.git
git@gitee.com:hutu88/dom.js.git
hutu88
dom.js
dom.js
main

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385