Skip to content

Commit 1edc4d2

Browse files
author
Illia Obukhau
authored
[WC-1561]: Update style regex to handle zero spaces (#185)
2 parents 5d3318f + d9c44bd commit 1edc4d2

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/pluggableWidgets/html-element-web/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010

1111
- We fixed an issue with HTML Element widget producing errors in Studio Pro versions below 9.18.
1212

13+
- We fixed an issue with inline CSS styles parsing
14+
1315
## [1.0.0] - 2022-11-24
1416

1517
### Added

packages/pluggableWidgets/html-element-web/src/utils/__tests__/style-utils.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ describe("style-utils", () => {
99
borderRadius: "20px"
1010
});
1111
});
12+
it("converts properties with no space between props and names", () => {
13+
const style = convertInlineCssToReactStyle("background-color:#FF00FF");
14+
15+
expect(style).toEqual({
16+
backgroundColor: "#FF00FF"
17+
});
18+
});
19+
it("converts properties with multiple spaces and newlines between props and names", () => {
20+
const style = convertInlineCssToReactStyle("background-color \n\n : \n #FF00FF");
21+
22+
expect(style).toEqual({
23+
backgroundColor: "#FF00FF"
24+
});
25+
});
26+
it("converts properties with colons inside", () => {
27+
const style = convertInlineCssToReactStyle("background-image: url(http://localhost:8080/img.png)");
28+
29+
expect(style).toEqual({
30+
backgroundImage: "url(http://localhost:8080/img.png)"
31+
});
32+
});
1233
it("ignores broken properties", () => {
1334
const style = convertInlineCssToReactStyle("background-color: red; ; foo-bar");
1435

packages/pluggableWidgets/html-element-web/src/utils/style-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from "react";
22

33
// We need regexp to split rows in prop/value pairs
4-
// split by : not work for all cases, eg "background-image: url(http://localhost:8080);"
5-
const cssPropRegex = /(?<prop>[^:]+):\s+?(?<value>[^;]+);?/m;
4+
// split by : doesn't work for all cases, eg "background-image: url(http://localhost:8080);"
5+
const cssPropRegex = /(?<prop>[^:]+):(?<value>.+)/s;
66

77
export function convertInlineCssToReactStyle(inlineStyle: string): React.CSSProperties {
88
return Object.fromEntries(
@@ -11,7 +11,7 @@ export function convertInlineCssToReactStyle(inlineStyle: string): React.CSSProp
1111
.filter(r => r.length) // filter out empty
1212
.map(r => {
1313
const { prop = "", value = "" } = cssPropRegex.exec(r.trim())?.groups ?? {};
14-
return [prop, value];
14+
return [prop.trim(), value.trim()];
1515
})
1616
.filter(v => v.length === 2 && v[0].length && v[1].length) // filter out broken lines
1717
.map(([key, value]) => [convertStylePropNameToReactPropName(key), value] as [string, string])

0 commit comments

Comments
 (0)