svg-visitor
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

svg-visitor

visit svg element use visitor pattern

Install

npm install svg-visitor

Usage

import { svgVisitor } from 'svg-visitor';

const svgDom = document.getElementByTagName('svg')[0]

svgVisitor(svgDom, {
  rectVisitor: function (rectDom) {
    const fill = rectDom.getAttribute('fill')
    if (fill === '#f00') {
      rectDom.setAttribute('fill', "#0f0")
    }
  }
})

before

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full" width="40" height="40" >
  <rect width="40" height="40" x="0" y="0" id="0" fill="#f00"></rect>
</svg>

after

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full" width="40" height="40" >
  <rect width="40" height="40" x="0" y="0" id="0" fill="#0f0"></rect>
</svg>

API

import { svgVisitor } from 'svg-visitor';

const svgDom = document.getElementByTagName('svg')[0]

svgVisitor(svgDom, {
  // visit all svg element under svgDom
  visitor: function(dom){},
  // visit <rect>...</rect>
  rectVisitor: function (rectDom) {},
  // visit <circle>...</circle>
  circleVisitor: function (circleDom) {},
  ...
})

Test

npm run-script test

Readme

Keywords

Package Sidebar

Install

npm i svg-visitor

Weekly Downloads

0

Version

0.3.0

License

MIT

Unpacked Size

6.08 kB

Total Files

10

Last publish

Collaborators

  • psz