All files / lib parse.js

41.3% Statements 19/46
15.38% Branches 2/13
44.44% Functions 4/9
42.22% Lines 19/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121  2x 2x 2x 2x 2x 2x     2x   2x 7x                   2x 7x                                                                           7x 7x       7x 7x           7x 7x                                     8x                                                   2x  
'use strict'
const xmldom = require('xmldom')
const DOMParser = xmldom.DOMParser // 解析为文档对象
const XMLSerializer = xmldom.XMLSerializer // XML序列化
const juice = require('juice')
const minify = require('html-minifier').minify
const { readToFile, SLICE } = require('./utils.js')
 
// 插件处理机制
const { Dispatcher } = require('./dispatcher.js')
 
const minifySvg = dataStr => {
  return minify(dataStr, {
    collapseWhitespace: true,
    removeComments: true,
  })
}
 
/**
 * 修正去除额外字段
 * minify 压缩 svg 引起的 viewBox 变成 viewbox 问题
 */
const fixExtraFiled = Doc => {
  Iif (Doc.documentElement.getAttribute('viewbox')) {
    Doc.documentElement.setAttribute(
      'viewBox',
      Doc.documentElement.getAttribute('viewbox')
    )
    Doc.documentElement.removeAttribute('viewbox')
  }
}
 
class SVGParser {
  /**
   * [parse svg 文件解析函数]
   * @param {*} filePath plugins [{}]
   */
  parse (filePath, confing = {}) {
    try {
      this.Doc = new DOMParser().parseFromString(
        juice(minifySvg(readToFile(filePath))),
        'application/xml'
      )
      fixExtraFiled(this.Doc)
      if (this.Doc.documentElement.tagName.toUpperCase() !== 'SVG') {
        console.log('解析失败')
      }
    } catch (err) {
      console.log('解析失败', err)
    }
 
    // 插件解析 svg
    Dispatcher(this.Doc, confing)
    return this
  }
 
  /**
   * [parse svg 字符串解析函数]
   * @param {*} dataStr
   */
  parseStr (dataStr, confing = {}) {
    try {
      this.Doc = new DOMParser().parseFromString(
        juice(minifySvg(dataStr)),
        'application/xml'
      )
      fixExtraFiled(this.Doc)
      Iif (this.Doc.documentElement.tagName.toUpperCase() !== 'SVG') {
        console.log('解析失败')
      }
    } catch (err) {
      console.log('解析失败', err)
    }
    Dispatcher(this.Doc, confing)
    return this
  }
 
  /**
   * [parse svg 节点解析函数]
   * @param {*} Node
   */
  parseNode (Node, confing = {}) {
    this.Doc = Node
    fixExtraFiled(this.Doc)
    Dispatcher(this.Doc, confing)
    return this
  }
 
  /**
   * [toSimpleSvg svg基本图形转换path]
   * @return {[type]} [description]
   */
  toSimpleSvg () {
    return new XMLSerializer().serializeToString(this.Doc)
  }
 
  /**
   * getPathAttributes 获取 svg 中 path 属性
   * @returns
   * @memberof SVGParser
   */
  getPathAttributes () {
    const Node = this.Doc
    const paths = []
    SLICE.call(Node.documentElement.childNodes).forEach((node, index) => {
      const pathObj = {}
      pathObj.pid = index
      if (node.nodeName.toUpperCase() === 'PATH') {
        SLICE.call(node.attributes).forEach(attr => {
          if (attr.value) pathObj[attr.nodeName.toLowerCase()] = attr.value
        })
        paths.push(pathObj)
      }
    })
 
    return paths
  }
}
 
module.exports = new SVGParser()