Skip to content

Commit 468c3c3

Browse files
committed
Remove casting of invalid values
Previously, known boolean values were casted to `true`, and known numerical values were casted to a number. Even if those values were invalid, which resulted in `NaN`. Now, this is reverted and invalid booleans (not *overloaded* booleans) are only casted to `true` when they have a valid value (empty or the attribute’s name), and numeric values are only casted if they result in a valid number.
1 parent 859a3d3 commit 468c3c3

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ function parsePrimitive(info, name, value) {
4747
return result;
4848
}
4949

50-
if (info.boolean) {
51-
result = true;
52-
} else if (info.numeric || info.positiveNumeric) {
53-
result = Number(result);
54-
} else if (info.overloadedBoolean) {
50+
if (info.numeric || info.positiveNumeric) {
51+
if (!isNaN(result)) {
52+
result = Number(result);
53+
}
54+
} else if (info.boolean || info.overloadedBoolean) {
5555
/*
5656
* Accept `boolean` and `string`.
5757
*/

test.js

+47-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ test('hastscript', function (t) {
248248

249249
st.deepEqual(
250250
h(null, {
251-
'allowFullScreen': 'yup'
251+
'allowFullScreen': ''
252252
}),
253253
{
254254
'type': 'element',
@@ -258,7 +258,52 @@ test('hastscript', function (t) {
258258
},
259259
'children': []
260260
},
261-
'should cast known `boolean` values'
261+
'should cast valid known `boolean` values'
262+
);
263+
264+
st.deepEqual(
265+
h(null, {
266+
'allowFullScreen': 'yup'
267+
}),
268+
{
269+
'type': 'element',
270+
'tagName': 'div',
271+
'properties': {
272+
'allowFullScreen': 'yup'
273+
},
274+
'children': []
275+
},
276+
'should not cast invalid known `boolean` values'
277+
);
278+
279+
st.deepEqual(
280+
h(null, {
281+
'volume': '0.1'
282+
}),
283+
{
284+
'type': 'element',
285+
'tagName': 'div',
286+
'properties': {
287+
'volume': 0.1
288+
},
289+
'children': []
290+
},
291+
'should cast valid known `numeric` values'
292+
);
293+
294+
st.deepEqual(
295+
h(null, {
296+
'volume': 'one'
297+
}),
298+
{
299+
'type': 'element',
300+
'tagName': 'div',
301+
'properties': {
302+
'volume': 'one'
303+
},
304+
'children': []
305+
},
306+
'should not cast invalid known `numeric` values'
262307
);
263308

264309
st.deepEqual(

0 commit comments

Comments
 (0)