Skip to content

Commit 571122d

Browse files
committed
dedupe styleObjectToCssString function
1 parent 50def12 commit 571122d

File tree

2 files changed

+4
-68
lines changed

2 files changed

+4
-68
lines changed

packages/html/src/render.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ const listen = (
327327
* @param styleObject - The style object with React-style camelCase properties
328328
* @returns A CSS string suitable for the style attribute
329329
*/
330-
const styleObjectToCssString = (styleObject: Record<string, any>): string => {
330+
export const styleObjectToCssString = (
331+
styleObject: Record<string, any>,
332+
): string => {
331333
return Object.entries(styleObject)
332334
.map(([key, value]) => {
333335
// Skip if value is null or undefined

packages/html/src/utils.ts

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,7 @@
44
import * as htmlparser2 from "htmlparser2";
55
import * as domhandler from "domhandler";
66
import * as domserializer from "dom-serializer";
7-
import { RenderOptions } from "./render.ts";
8-
9-
/**
10-
* Converts a React-style CSS object to a CSS string.
11-
* Supports vendor prefixes, pixel value shorthand, and comprehensive CSS properties.
12-
* @param styleObject - The style object with React-style camelCase properties
13-
* @returns A CSS string suitable for the style attribute
14-
*/
15-
const styleObjectToCssString = (styleObject: Record<string, any>): string => {
16-
return Object.entries(styleObject)
17-
.map(([key, value]) => {
18-
// Skip if value is null or undefined
19-
if (value == null) return "";
20-
21-
// Convert camelCase to kebab-case, handling vendor prefixes
22-
let cssKey = key;
23-
24-
// CSS custom properties (--*) are case-sensitive and should not be transformed
25-
if (!key.startsWith("--")) {
26-
// Handle vendor prefixes (WebkitTransform -> -webkit-transform)
27-
if (/^(webkit|moz|ms|o)[A-Z]/.test(key)) {
28-
cssKey = "-" + key;
29-
}
30-
31-
// Convert camelCase to kebab-case
32-
cssKey = cssKey.replace(/([A-Z])/g, "-$1").toLowerCase();
33-
}
34-
35-
// Convert value to string
36-
let cssValue = value;
37-
38-
// Add 'px' suffix to numeric values for properties that need it
39-
// Exceptions: properties that accept unitless numbers
40-
const unitlessProperties = new Set([
41-
"animation-iteration-count",
42-
"column-count",
43-
"fill-opacity",
44-
"flex",
45-
"flex-grow",
46-
"flex-shrink",
47-
"font-weight",
48-
"line-height",
49-
"opacity",
50-
"order",
51-
"orphans",
52-
"stroke-opacity",
53-
"widows",
54-
"z-index",
55-
"zoom",
56-
]);
57-
58-
if (
59-
typeof value === "number" &&
60-
!cssKey.startsWith("--") && // CSS custom properties should never get px
61-
!unitlessProperties.has(cssKey) &&
62-
value !== 0
63-
) {
64-
cssValue = `${value}px`;
65-
} else {
66-
cssValue = String(value);
67-
}
68-
69-
return `${cssKey}: ${cssValue}`;
70-
})
71-
.filter((s) => s !== "")
72-
.join("; ");
73-
};
7+
import { RenderOptions, styleObjectToCssString } from "./render.ts";
748

759
function renderOptionsFromDoc(document: globalThis.Document): RenderOptions {
7610
return {

0 commit comments

Comments
 (0)