From 2bff5c5c7e776a183ed9b429f2cb46cc044d9bc1 Mon Sep 17 00:00:00 2001 From: Ryan Seddon Date: Wed, 4 Jun 2014 20:50:20 +1000 Subject: [PATCH] Correctly trim strings for css properties * If a unitless value is passed with a space it'll return `1 px` --- .../ui/dom/__tests__/CSSPropertyOperations-test.js | 8 ++++++++ src/browser/ui/dom/dangerousStyleValue.js | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/browser/ui/dom/__tests__/CSSPropertyOperations-test.js b/src/browser/ui/dom/__tests__/CSSPropertyOperations-test.js index b40617f743..77e9e5f6cf 100644 --- a/src/browser/ui/dom/__tests__/CSSPropertyOperations-test.js +++ b/src/browser/ui/dom/__tests__/CSSPropertyOperations-test.js @@ -68,6 +68,14 @@ describe('CSSPropertyOperations', function() { })).toBe('left:0;margin:16px;opacity:0.5;padding:4px;'); }); + it('should trim values so `px` will be appended correctly', function() { + expect(CSSPropertyOperations.createMarkupForStyles({ + margin: '16 ', + opacity: 0.5, + padding: ' 4 ' + })).toBe('margin:16px;opacity:0.5;padding:4px;'); + }); + it('should not append `px` to styles that might need a number', function() { var CSSProperty = require('CSSProperty'); var unitlessProperties = Object.keys(CSSProperty.isUnitlessNumber); diff --git a/src/browser/ui/dom/dangerousStyleValue.js b/src/browser/ui/dom/dangerousStyleValue.js index 44cbe8166d..c1108ce23e 100644 --- a/src/browser/ui/dom/dangerousStyleValue.js +++ b/src/browser/ui/dom/dangerousStyleValue.js @@ -54,6 +54,9 @@ function dangerousStyleValue(name, value) { return '' + value; // cast to string } + if (typeof value === 'string') { + value = value.trim(); + } return value + 'px'; }