-
Notifications
You must be signed in to change notification settings - Fork 50
/
dom-to-svg.ts
20 lines (19 loc) · 1 KB
/
dom-to-svg.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { Context } from '../context'
import type { Options } from '../options'
import { orCreateContext } from '../create-context'
import { createSvg, svgToDataUrl } from '../utils'
import { domToDataUrl } from './dom-to-data-url'
export async function domToSvg<T extends Node>(node: T, options?: Options): Promise<string>
export async function domToSvg<T extends Node>(context: Context<T>): Promise<string>
export async function domToSvg(node: any, options?: any): Promise<string> {
const context = await orCreateContext(node, options)
const { width, height, ownerDocument } = context
const dataUrl = await domToDataUrl(context)
const svg = createSvg(width, height, ownerDocument)
const svgImage = svg.ownerDocument.createElementNS(svg.namespaceURI, 'image')
svgImage.setAttributeNS(null, 'href', dataUrl)
svgImage.setAttributeNS(null, 'height', '100%')
svgImage.setAttributeNS(null, 'width', '100%')
svg.appendChild(svgImage)
return svgToDataUrl(svg, context.isEnable('removeControlCharacter'))
}