all files / src/ nodes.js

100% Statements 8/8
100% Branches 4/4
100% Functions 4/4
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                   
/**
 * 节点操作
 */
import {setCollection} from './collection'
import $ from './index'
 
/**
 * 设置或获取文字
 * @param {string|undefined} text 替换的字符串或空
 * @return {$|string} 返回的字符串或集合
 */
export function text (text) {
  return replaceNodes.call(this, 'innerText', text)
}
 
/**
 * 设置或获取节点
 * @param {string|undefined} html 替换的节点或空
 * @return {$|string} 返回的节点或集合
 */
export function html (html) {
  return replaceNodes.call(this, 'innerHTML', html)
}
 
/**
 * 根据传入的类型和内容 替换节点
 * @param {string} type 类型
 * @param {string} content 传入的内容
 * @return {$|string} 返回的集合或内容
 */
function replaceNodes (type, content) {
  let contents = ''
  this.map(elem => {
    if (content) {
      elem.innerHTML = content
    } else {
      contents += elem[type]
    }
  })
 
  return content ? this : contents
}