Skip to content

Use style-to-js #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 2 additions & 44 deletions lib/handlers/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import {stringify as commas} from 'comma-separated-tokens'
import {name as identifierName} from 'estree-util-is-identifier-name'
import {find, hastToReact, svg} from 'property-information'
import {stringify as spaces} from 'space-separated-tokens'
import styleToObject from 'style-to-object'
import styleToJs from 'style-to-js'

const own = {}.hasOwnProperty
const cap = /[A-Z]/g
const dashSomething = /-([a-z])/g

const tableCellElement = new Set(['td', 'th'])

Expand Down Expand Up @@ -230,11 +229,8 @@ export function element(node, state) {
* Properties.
*/
function parseStyle(value, tagName) {
/** @type {Record<string, string>} */
const result = {}

try {
styleToObject(value, iterator)
return styleToJs(value, {reactCompat: true})
} catch (error) {
const cause = /** @type {Error} */ (error)
const exception = new Error(
Expand All @@ -243,30 +239,6 @@ function parseStyle(value, tagName) {
)
throw exception
}

return result

/**
* Add `name`, as a CSS property, to `result`.
*
* @param {string} name
* Key.
* @param {string} value
* Value.
* @returns {undefined}
* Nothing.
*/
function iterator(name, value) {
let key = name

if (key.slice(0, 2) !== '--') {
// See: <https://alanhogan.com/code/vendor-prefixed-css-property-names-in-javascript>
if (key.slice(0, 4) === '-ms-') key = 'ms-' + key.slice(4)
key = key.replace(dashSomething, toCamel)
}

result[key] = value
}
}

/**
Expand Down Expand Up @@ -303,20 +275,6 @@ function transformStyleToCssCasing(from) {
return to
}

/**
* Make `$1` capitalized.
*
* @param {string} _
* Whatever.
* @param {string} $1
* Single ASCII alphabetical.
* @returns {string}
* Capitalized `$1`.
*/
function toCamel(_, $1) {
return $1.toUpperCase()
}

/**
* Make `$0` dash cased.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"mdast-util-mdxjs-esm": "^2.0.0",
"property-information": "^7.0.0",
"space-separated-tokens": "^2.0.0",
"style-to-object": "^1.0.0",
"style-to-js": "^1.0.0",
"unist-util-position": "^5.0.0",
"zwitch": "^2.0.0"
},
Expand Down