Skip to content

Commit

Permalink
test: add a test where bbox is null
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed May 12, 2022
1 parent 298b87b commit c4845d7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
7 changes: 7 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ test('should get svg bbox(rect)', async (t) => {
t.is(result.getHeight(), 100)
})

test('should bbox value is null', (t) => {
const svg = `<svg width="300px" height="300px" viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg"></svg>`
const resvg = new Resvg(svg)
t.is(resvg.getBBox(), null)
t.is(resvg.innerBBox(), null)
})

test('should throw because invalid SVG attribute (width attribute is 0)', (t) => {
const error = t.throws(
() => {
Expand Down
7 changes: 7 additions & 0 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ test('should get svg bbox(rect)', async (t) => {
t.is(result.getHeight(), 100)
})

test('should bbox value is null', (t) => {
const svg = `<svg width="300px" height="300px" viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg"></svg>`
const resvg = new Resvg(svg)
t.is(resvg.getBBox(), null)
t.is(resvg.innerBBox(), null)
})

// throws
test('should throw because invalid SVG (blank string)', (t) => {
const error = t.throws(
Expand Down
2 changes: 1 addition & 1 deletion example/bbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function main() {

// const bbox = resvg.innerBBox()
const bbox = resvg.getBBox()
resvg.cropByBBox(bbox)
if (bbox) resvg.cropByBBox(bbox)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()

Expand Down
4 changes: 2 additions & 2 deletions js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class Resvg {
*
* Note: path bounding box are approx values.
*/
innerBBox(): BBox
innerBBox(): BBox | null
/**
* Calculate a maximum bounding box of all visible elements in this SVG.
* This will first apply transform.
* Similar to `SVGGraphicsElement.getBBox()` DOM API.
*/
getBBox(): BBox
getBBox(): BBox | null
/**
* Use a given `BBox` to crop the svg. Currently this method simply changes
* the viewbox/size of the svg and do not move the elements for simplicity
Expand Down
12 changes: 5 additions & 7 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,11 @@
const resvgJS = new resvg.Resvg(svg, opts)
document.querySelector('#svg-info').textContent = 'Original SVG size: ' + resvgJS.width + ' x ' + resvgJS.height + ' px'

const bbox = resvgJS.innerBBox()
const bbox2 = resvgJS.getBBox()
console.log('SVG innerBBox', bbox)
console.log('SVG getBBox', bbox2)
if (hasCrop) {
resvgJS.cropByBBox(bbox2)
}
const innerBBox = resvgJS.innerBBox()
const bbox = resvgJS.getBBox()
console.log('SVG innerBBox', innerBBox)
console.log('SVG getBBox', bbox)
if (hasCrop && bbox) resvgJS.cropByBBox(bbox)
const pngData = resvgJS.render()
const pngBuffer = pngData.asPng()

Expand Down

0 comments on commit c4845d7

Please sign in to comment.