Skip to content

Commit

Permalink
feat: 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchang committed Dec 6, 2023
1 parent 86df9bd commit 5ea247b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.0.2 - 2023-12-06

### Bug Fixes

* use xlink:href additionally to href in img


## 1.0.1 - 2023-12-06

### Features
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pnpm/npm/yarn i dom2svg
## Usage

```js
import { documentToSVG, elementToSVG, inlineResources, formatXML } from 'dom2svg'
import { documentToSVG, elementToSVG, formatXML, inlineResources } from 'dom2svg'

// Capture the whole document
const svgDocument = documentToSVG(document)
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pnpm/npm/yarn i dom2svg
## 使用

```js
import { documentToSVG, elementToSVG, inlineResources, formatXML } from 'dom2svg'
import { documentToSVG, elementToSVG, formatXML, inlineResources } from 'dom2svg'

// 捕获整个文档
const svgDocument = documentToSVG(document)
Expand Down
6 changes: 4 additions & 2 deletions lib/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export async function inlineResources(element: Element): Promise<void> {
...[...element.children].map(inlineResources),
(async () => {
if (isSVGImageElement(element)) {
const blob = await withTimeout(10000, `Timeout fetching ${element.href.baseVal}`, () =>
fetchResource(element.href.baseVal),
const elementHref = element.getAttribute('href') || element.getAttribute('xlink:href')
assert(elementHref, 'Expected <image> element to have an href or xlink:href attribute')
const blob = await withTimeout(10000, `Timeout fetching ${elementHref}`, () =>
fetchResource(elementHref),
)
if (blob.type === 'image/svg+xml') {
// If the image is an SVG, inline it into the output SVG.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dom2svg",
"type": "module",
"version": "1.0.1",
"version": "1.0.2",
"description": "Take SVG screenshots of DOM elements",
"author": "xiashui",
"license": "MIT",
Expand Down
14 changes: 11 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './style.css'
import { elementToSVG } from '../lib/index'
import { elementToSVG, inlineResources } from '../lib/index'
import typescriptLogo from './typescript.svg'
import viteLogo from './vite.svg'

Expand All @@ -19,9 +19,17 @@ document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
</div>
`

document.querySelector('#export')?.addEventListener('click', () => {
document.querySelector('#export')?.addEventListener('click', async () => {
const svg = elementToSVG(document.querySelector('body')!)
const svgString = new XMLSerializer().serializeToString(svg)
const svgRootElement = svg.documentElement
document.querySelector('body')!.prepend(svgRootElement)
try {
await inlineResources(svgRootElement)
}
finally {
svgRootElement.remove()
}
const svgString = new XMLSerializer().serializeToString(svgRootElement)
const blob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' })
window.open(URL.createObjectURL(blob))
})

0 comments on commit 5ea247b

Please sign in to comment.