Skip to content

Commit 572ee42

Browse files
fix: Boolean/number props not getting parsed correctly (#362)
* Fixed prop boolean/number props not getting parsed correctly * Updated to also work with floats * Small fix * Small fix * Added test for copy/pasting block with number type props
1 parent db0124d commit 572ee42

File tree

3 files changed

+215
-1
lines changed

3 files changed

+215
-1
lines changed

packages/core/src/extensions/Blocks/api/block.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,39 @@ export function propsToAttributes<
3838
// Props are displayed in kebab-case as HTML attributes. If a prop's
3939
// value is the same as its default, we don't display an HTML
4040
// attribute for it.
41-
parseHTML: (element) => element.getAttribute(camelToDataKebab(name)),
41+
parseHTML: (element) => {
42+
const value = element.getAttribute(camelToDataKebab(name));
43+
44+
if (value === null) {
45+
return null;
46+
}
47+
48+
if (typeof spec.default === "boolean") {
49+
if (value === "true") {
50+
return true;
51+
}
52+
53+
if (value === "false") {
54+
return false;
55+
}
56+
57+
return null;
58+
}
59+
60+
if (typeof spec.default === "number") {
61+
const asNumber = parseFloat(value);
62+
const isNumeric =
63+
!Number.isNaN(asNumber) && Number.isFinite(asNumber);
64+
65+
if (isNumeric) {
66+
return asNumber;
67+
}
68+
69+
return null;
70+
}
71+
72+
return value;
73+
},
4274
renderHTML: (attributes) =>
4375
attributes[name] !== spec.default
4476
? {

tests/end-to-end/copypaste/copypaste.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
insertNestedListItems,
1010
insertParagraph,
1111
} from "../../utils/copypaste";
12+
import { executeSlashCommand } from "../../utils/slashmenu";
1213

1314
test.describe.configure({ mode: "serial" });
1415

@@ -135,4 +136,59 @@ test.describe("Check Copy/Paste Functionality", () => {
135136

136137
await compareDocToSnapshot(page, "nestedOrderedLists.json");
137138
});
139+
140+
test("Images should keep props", async ({ page, browserName }) => {
141+
test.skip(
142+
browserName === "firefox" || browserName === "webkit",
143+
"Firefox doesn't yet support the async clipboard API. Webkit copy/paste stopped working after updating to Playwright 1.33."
144+
);
145+
146+
await focusOnEditor(page);
147+
await page.keyboard.type("paragraph");
148+
149+
const IMAGE_EMBED_URL =
150+
"https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg";
151+
await executeSlashCommand(page, "image");
152+
153+
await page.click(`[data-test="embed-tab"]`);
154+
await page.click(`[data-test="embed-input"]`);
155+
await page.keyboard.type(IMAGE_EMBED_URL);
156+
await page.click(`[data-test="embed-input-button"]`);
157+
await page.waitForSelector(`img[src="${IMAGE_EMBED_URL}"]`);
158+
159+
await page.click(`img`);
160+
161+
await page.waitForSelector(`[class*="resizeHandle"][style*="right"]`);
162+
const resizeHandle = page.locator(
163+
`[class*="resizeHandle"][style*="right"]`
164+
);
165+
const resizeHandleBoundingBox = await resizeHandle.boundingBox();
166+
await page.mouse.move(
167+
resizeHandleBoundingBox.x + resizeHandleBoundingBox.width / 2,
168+
resizeHandleBoundingBox.y + resizeHandleBoundingBox.height / 2,
169+
{
170+
steps: 5,
171+
}
172+
);
173+
await page.mouse.down();
174+
175+
await page.mouse.move(
176+
resizeHandleBoundingBox.x + resizeHandleBoundingBox.width / 2 - 50,
177+
resizeHandleBoundingBox.y + resizeHandleBoundingBox.height / 2,
178+
{
179+
steps: 5,
180+
}
181+
);
182+
183+
await page.mouse.up();
184+
185+
await page.click(`img`);
186+
await page.keyboard.press("ArrowDown");
187+
await page.pause();
188+
189+
await copyPasteAll(page);
190+
await page.pause();
191+
192+
await compareDocToSnapshot(page, "images.json");
193+
});
138194
});
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"type": "doc",
3+
"content": [
4+
{
5+
"type": "blockGroup",
6+
"content": [
7+
{
8+
"type": "blockContainer",
9+
"attrs": {
10+
"id": "0",
11+
"textColor": "default",
12+
"backgroundColor": "default"
13+
},
14+
"content": [
15+
{
16+
"type": "paragraph",
17+
"attrs": {
18+
"textAlignment": "left"
19+
},
20+
"content": [
21+
{
22+
"type": "text",
23+
"text": "paragraph"
24+
}
25+
]
26+
}
27+
]
28+
},
29+
{
30+
"type": "blockContainer",
31+
"attrs": {
32+
"id": "2",
33+
"textColor": "default",
34+
"backgroundColor": "default"
35+
},
36+
"content": [
37+
{
38+
"type": "image",
39+
"attrs": {
40+
"textAlignment": "left",
41+
"backgroundColor": "default",
42+
"url": "https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg",
43+
"caption": "",
44+
"width": 462
45+
}
46+
}
47+
]
48+
},
49+
{
50+
"type": "blockContainer",
51+
"attrs": {
52+
"id": "1",
53+
"textColor": "default",
54+
"backgroundColor": "default"
55+
},
56+
"content": [
57+
{
58+
"type": "paragraph",
59+
"attrs": {
60+
"textAlignment": "left"
61+
}
62+
}
63+
]
64+
},
65+
{
66+
"type": "blockContainer",
67+
"attrs": {
68+
"id": "4",
69+
"textColor": "default",
70+
"backgroundColor": "default"
71+
},
72+
"content": [
73+
{
74+
"type": "paragraph",
75+
"attrs": {
76+
"textAlignment": "left"
77+
},
78+
"content": [
79+
{
80+
"type": "text",
81+
"text": "paragraph"
82+
}
83+
]
84+
}
85+
]
86+
},
87+
{
88+
"type": "blockContainer",
89+
"attrs": {
90+
"id": "5",
91+
"textColor": "default",
92+
"backgroundColor": "default"
93+
},
94+
"content": [
95+
{
96+
"type": "image",
97+
"attrs": {
98+
"textAlignment": "left",
99+
"backgroundColor": "default",
100+
"url": "https://www.pulsecarshalton.co.uk/wp-content/uploads/2016/08/jk-placeholder-image.jpg",
101+
"caption": "",
102+
"width": 462
103+
}
104+
}
105+
]
106+
},
107+
{
108+
"type": "blockContainer",
109+
"attrs": {
110+
"id": "6",
111+
"textColor": "default",
112+
"backgroundColor": "default"
113+
},
114+
"content": [
115+
{
116+
"type": "paragraph",
117+
"attrs": {
118+
"textAlignment": "left"
119+
}
120+
}
121+
]
122+
}
123+
]
124+
}
125+
]
126+
}

0 commit comments

Comments
 (0)