77 * @typedef {import('../state.js').State } State
88 */
99
10+ /**
11+ * @typedef {Record<string, string> } Style
12+ */
13+
1014import { stringify as commas } from 'comma-separated-tokens'
1115import { svg , find , hastToReact } from 'property-information'
1216import { stringify as spaces } from 'space-separated-tokens'
@@ -17,6 +21,8 @@ import {
1721import styleToObject from 'style-to-object'
1822
1923const own = { } . hasOwnProperty
24+ const cap = / [ A - Z ] / g
25+ const dashSomething = / - ( [ a - z ] ) / g
2026
2127/**
2228 * Turn a hast element into an estree node.
@@ -77,26 +83,30 @@ export function element(node, state) {
7783 }
7884
7985 if ( prop === 'style' ) {
80- /** @type {Record<string, string> } */
81- // @ts -expect-error Assume `value` is an object otherwise.
82- const styleValue =
83- typeof value === 'string' ? parseStyle ( value , node . tagName ) : value
86+ let styleObject =
87+ typeof value === 'object'
88+ ? value
89+ : parseStyle ( String ( value ) , node . tagName )
90+
91+ if ( state . stylePropertyNameCase === 'css' ) {
92+ styleObject = transformStyleToCssCasing ( styleObject )
93+ }
8494
8595 /** @type {Array<Property> } */
8696 const cssProperties = [ ]
8797 /** @type {string } */
8898 let cssProp
8999
90- for ( cssProp in styleValue ) {
100+ for ( cssProp in styleObject ) {
91101 // eslint-disable-next-line max-depth
92- if ( own . call ( styleValue , cssProp ) ) {
102+ if ( own . call ( styleObject , cssProp ) ) {
93103 cssProperties . push ( {
94104 type : 'Property' ,
95105 method : false ,
96106 shorthand : false ,
97107 computed : false ,
98108 key : { type : 'Identifier' , name : cssProp } ,
99- value : { type : 'Literal' , value : String ( styleValue [ cssProp ] ) } ,
109+ value : { type : 'Literal' , value : String ( styleObject [ cssProp ] ) } ,
100110 kind : 'init'
101111 } )
102112 }
@@ -174,11 +184,11 @@ export function element(node, state) {
174184 * CSS text.
175185 * @param {string } tagName
176186 * Element name.
177- * @returns {Record<string, string> }
187+ * @returns {Style }
178188 * Props.
179189 */
180190function parseStyle ( value , tagName ) {
181- /** @type {Record<string, string> } */
191+ /** @type {Style } */
182192 const result = { }
183193
184194 try {
@@ -203,25 +213,68 @@ function parseStyle(value, tagName) {
203213 * Nothing.
204214 */
205215 function iterator ( name , value ) {
206- if ( name . slice ( 0 , 4 ) === '-ms-' ) name = 'ms-' + name . slice ( 4 )
207- result [ name . replace ( / - ( [ a - z ] ) / g, styleReplacer ) ] = value
216+ let key = name
217+
218+ if ( key . slice ( 0 , 2 ) !== '--' ) {
219+ // See: <https://alanhogan.com/code/vendor-prefixed-css-property-names-in-javascript>
220+ if ( key . slice ( 0 , 4 ) === '-ms-' ) key = 'ms-' + key . slice ( 4 )
221+ key = key . replace ( dashSomething , toCamel )
222+ }
223+
224+ result [ key ] = value
225+ }
226+ }
227+
228+ /**
229+ * Transform a DOM casing style object to a CSS casing style object.
230+ *
231+ * @param {Style } domCasing
232+ * @returns {Style }
233+ */
234+ function transformStyleToCssCasing ( domCasing ) {
235+ /** @type {Style } */
236+ const cssCasing = { }
237+ /** @type {string } */
238+ let from
239+
240+ for ( from in domCasing ) {
241+ if ( own . call ( domCasing , from ) ) {
242+ let to = from . replace ( cap , toDash )
243+ // Handle `ms-xxx` -> `-ms-xxx`.
244+ if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
245+ cssCasing [ to ] = domCasing [ from ]
246+ }
208247 }
248+
249+ return cssCasing
209250}
210251
211252/**
212- * Uppercase `$1`.
253+ * Make `$1` capitalized .
213254 *
214255 * @param {string } _
215256 * Whatever.
216257 * @param {string } $1
217- * an ASCII alphabetic .
258+ * Single ASCII alphabetical .
218259 * @returns {string }
219- * Uppercased `$1`.
260+ * Capitalized `$1`.
220261 */
221- function styleReplacer ( _ , $1 ) {
262+ function toCamel ( _ , $1 ) {
222263 return $1 . toUpperCase ( )
223264}
224265
266+ /**
267+ * Make `$0` dash cased.
268+ *
269+ * @param {string } $0
270+ * Capitalized ASCII leter.
271+ * @returns {string }
272+ * Dash and lower letter.
273+ */
274+ function toDash ( $0 ) {
275+ return '-' + $0 . toLowerCase ( )
276+ }
277+
225278/**
226279 * Checks if the given string is a valid identifier name.
227280 *
0 commit comments