|
4 | 4 | import * as htmlparser2 from "htmlparser2"; |
5 | 5 | import * as domhandler from "domhandler"; |
6 | 6 | 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"; |
74 | 8 |
|
75 | 9 | function renderOptionsFromDoc(document: globalThis.Document): RenderOptions { |
76 | 10 | return { |
|
0 commit comments