Skip to content

Commit

Permalink
throw when image extension not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jjhbw committed Apr 19, 2020
1 parent 8b6ffb0 commit 149e66d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
8 changes: 3 additions & 5 deletions README.md
Expand Up @@ -279,11 +279,9 @@ The JS snippet must return an _image object_ or a Promise of an _image object_,

* `width` in cm
* `height` in cm
* `thumbnail` _[optional]_: path to png to use when svg is not supported or shown in preview (svg only)
* `data` _[optional]_: either an ArrayBuffer or a base64 string with the image data
* `extension` _[optional]_: e.g. `.png`

Either specify the `path` or `data` + `extension`.
* `thumbnail` _[optional]_: non-SVG image to use when SVG is not supported or shown in preview (only relevant when image is SVG). See example below.
* `data`: either an ArrayBuffer or a base64 string with the image data
* `extension`: e.g. `.png`

Images can also be SVG. These needs a thumbnail (fallback) to be specified along with the image data.

Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/images.test.ts
Expand Up @@ -61,4 +61,43 @@ import { Image } from '../types';
expect(result).toMatchSnapshot();
});
});

describe(sbStatus, () => {
it('002: throws when thumbnail is incorrectly provided when inserting an SVG', async () => {
const template = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'imagesSVG.docx')
);
const thumbnail = {
data: await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'sample.png')
),
// extension: '.png', extension is not given
};

const opts = {
noSandbox,
template,
data: {},
additionalJsContext: {
svgImgStr: () => {
const data = Buffer.from(
`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" height="100" width="100" style="stroke:#ff0000; fill: #0000ff"/>
</svg>`,
'utf-8'
);
return {
width: 6,
height: 6,
data,
extension: '.svg',
thumbnail,
};
},
},
};

return expect(createReport(opts)).rejects.toBeInstanceOf(Error);
});
});
});
5 changes: 5 additions & 0 deletions src/processTemplate.ts
Expand Up @@ -678,6 +678,11 @@ const processEndForIf = (
};

const imageToContext = (ctx: Context, img: Image) => {
if (!(typeof img.extension === 'string')) {
throw new Error(
'When providing an image, you need to specify the extension (e.g. `.png`).'
);
}
ctx.imageId += 1;
const id = String(ctx.imageId);
const relId = `img${id}`;
Expand Down

0 comments on commit 149e66d

Please sign in to comment.