Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit b162369

Browse files
committed
Add proper CSS style parsing
Related to c593e58. Closes GH-13. Related to GH-12. Related to GH-11.
1 parent 9e747b4 commit b162369

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed

index.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ var svg = require('property-information/svg')
55
var find = require('property-information/find')
66
var spaces = require('space-separated-tokens')
77
var commas = require('comma-separated-tokens')
8+
var style = require('style-to-object')
89
var ns = require('web-namespaces')
910
var is = require('unist-util-is')
1011

1112
var dashes = /-([a-z])/g
12-
var ws = /^\s*|\s*$/g
1313

1414
module.exports = wrapper
1515

@@ -80,13 +80,13 @@ function toH(h, node, ctx) {
8080
var value
8181
var result
8282

83-
if (parentSchema.space === 'html' && lower(name) === 'svg') {
83+
if (parentSchema.space === 'html' && name.toLowerCase() === 'svg') {
8484
schema = svg
8585
ctx.schema = schema
8686
}
8787

8888
if (ctx.vdom === true && schema.space === 'html') {
89-
name = upper(name)
89+
name = name.toUpperCase()
9090
}
9191

9292
properties = node.properties
@@ -101,7 +101,7 @@ function toH(h, node, ctx) {
101101
(ctx.vdom === true || ctx.react === true)
102102
) {
103103
// VDOM and React accept `style` as object.
104-
attributes.style = parseStyle(attributes.style)
104+
attributes.style = parseStyle(attributes.style, name)
105105
}
106106

107107
if (ctx.prefix) {
@@ -202,25 +202,21 @@ function vdom(h) {
202202
return h && h('div').type === 'VirtualNode'
203203
}
204204

205-
function parseStyle(value) {
205+
function parseStyle(value, tagName) {
206206
var result = {}
207-
var declarations = value.split(';')
208-
var length = declarations.length
209-
var index = -1
210-
var declaration
211-
var prop
212-
var pos
213207

214-
while (++index < length) {
215-
declaration = declarations[index]
216-
pos = declaration.indexOf(':')
217-
if (pos !== -1) {
218-
prop = styleCase(trim(declaration.slice(0, pos)))
219-
result[prop] = trim(declaration.slice(pos + 1))
220-
}
208+
try {
209+
style(value, iterator)
210+
} catch (err) {
211+
err.message = tagName + '[style]' + err.message.slice('undefined'.length)
212+
throw err
221213
}
222214

223215
return result
216+
217+
function iterator(name, value) {
218+
result[styleCase(name)] = value
219+
}
224220
}
225221

226222
function styleCase(val) {
@@ -232,17 +228,5 @@ function styleCase(val) {
232228
}
233229

234230
function styleReplacer($0, $1) {
235-
return upper($1)
236-
}
237-
238-
function lower(value) {
239-
return value.toLowerCase()
240-
}
241-
242-
function upper(value) {
243-
return value.toUpperCase()
244-
}
245-
246-
function trim(value) {
247-
return value.replace(ws, '')
231+
return $1.toUpperCase()
248232
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"comma-separated-tokens": "^1.0.0",
2929
"property-information": "^4.0.0",
3030
"space-separated-tokens": "^1.0.0",
31+
"style-to-object": "^0.2.1",
3132
"unist-util-is": "^2.0.0",
3233
"web-namespaces": "^1.1.2"
3334
},

test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,16 +441,18 @@ test('hast-to-hyperscript', function(t) {
441441
'react: should parse vendor prefixed in style declarations'
442442
)
443443

444-
st.deepEqual(
445-
toH(
446-
r,
447-
u('element', {
448-
tagName: 'div',
449-
properties: {style: '; color; border: 1;'}
450-
})
451-
).props.style,
452-
{border: '1'},
453-
'react: should parse an invalid style declaration'
444+
st.throws(
445+
function() {
446+
toH(
447+
r,
448+
u('element', {
449+
tagName: 'div',
450+
properties: {style: '; color; border: 1;'}
451+
})
452+
)
453+
},
454+
/^Error: div\[style]:1:3: missing '\}'$/,
455+
'react: should throw on invalid style declarations'
454456
)
455457

456458
st.deepEqual(

0 commit comments

Comments
 (0)