Skip to content

Commit 07fe0c3

Browse files
committed
🐛 Fix TypeError for color on nested blocks
Reading a `color` property on a row or column block with a nested text block lead to a TypeError: > TypeError: Invalid value for "definition/content/0/rows/0/color": > Expected valid color, got: {type: 'RGB', red: 0, green: 0.3, blue: > 0.5} This happened because the `readColor()` function transforms the input and did not accept its own result type as input. For nested blocks, the inherited `color` property is parsed twice if not overwritten. As a quick fix, this commit changes the `readColor()` function to accept its own result type, i.e. an object with type `RGB`. Fixes #95
1 parent 87ef775 commit 07fe0c3

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ describe('readColor', () => {
2222
expect(readColor('blue')).toEqual({ type: 'RGB', red: 0, green: 0, blue: 1 });
2323
});
2424

25+
it('accepts its own output', () => {
26+
// See https://github.com/eclipsesource/pdf-maker/issues/95
27+
expect(readColor(readColor('red'))).toEqual({ type: 'RGB', red: 1, green: 0, blue: 0 });
28+
});
29+
2530
it('throws on unsupported named color', () => {
2631
expect(() => readColor('' as any)).toThrowError("Expected valid color name, got: ''");
2732
expect(() => readColor('salmon' as any)).toThrowError(

src/read-color.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { type Color, rgb } from 'pdf-lib';
22

33
import { namedColors } from './api/colors.ts';
4-
import { typeError } from './types.ts';
4+
import { isObject, typeError } from './types.ts';
55

66
export { type Color };
77

88
export function readColor(input: unknown): Color {
9+
if (isObject(input) && input.type === 'RGB') {
10+
return input as unknown as Color;
11+
}
912
if (typeof input === 'string') {
1013
if (/^#[0-9a-f]{6}$/.test(input)) {
1114
const r = parseInt(input.slice(1, 3), 16) / 255;

0 commit comments

Comments
 (0)