From 4c07b324d1e9e987725de1f3126f06fc2dcc066a Mon Sep 17 00:00:00 2001 From: Trey Griffith Date: Mon, 28 Apr 2014 01:18:16 -0700 Subject: [PATCH 1/3] add test exercising unsetting a css value --- test/css.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/css.js b/test/css.js index 9646e7c..7ce469f 100644 --- a/test/css.js +++ b/test/css.js @@ -223,4 +223,14 @@ describe('css(prop)', function() { assert('1000' == css(div, 'z-index')); el.removeChild(div); }); + + it('should unset styles', function() { + var div = domify('
'); + el.appendChild(div); + css(div, 'display', 'none'); + css(div, 'display', ''); + + assert('' === css(div, 'display')); + el.removeChild(div); + }); }); From 2998e487328201fcbf5d861e3a3438555e94e737 Mon Sep 17 00:00:00 2001 From: Trey Griffith Date: Mon, 28 Apr 2014 02:19:15 -0700 Subject: [PATCH 2/3] put get() into ./computed where it was used, and set empty strings as valid styles --- lib/computed.js | 30 ++++++++++++++++++++++++++++-- lib/style.js | 27 +-------------------------- test/css.js | 3 +-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/computed.js b/lib/computed.js index 921ad62..9f70f9e 100644 --- a/lib/computed.js +++ b/lib/computed.js @@ -5,6 +5,8 @@ var debug = require('debug')('css:computed'); var withinDocument = require('within-document'); var styles = require('./styles'); +var camelcase = require('to-camel-case'); +var hooks = require('./hooks'); /** * Expose `computed` @@ -36,8 +38,7 @@ function computed(el, prop, precomputed) { if ('' === ret && !withinDocument(el)) { debug('element not within document, try finding from style attribute'); - var style = require('./style'); - ret = style(el, prop); + ret = get(el, prop); } debug('computed value of %s: %s', prop, ret); @@ -46,3 +47,28 @@ function computed(el, prop, precomputed) { // IE returns zIndex value as an integer. return undefined === ret ? ret : ret + ''; } + +/** + * Get the style + * + * @param {Element} el + * @param {String} prop + * @param {Mixed} extra + * @return {String} + */ + +function get(el, prop, extra) { + var style = el.style; + var hook = hooks[prop] || hooks[orig]; + var orig = camelcase(prop); + var ret; + + if (hook && hook.get && undefined !== (ret = hook.get(el, false, extra))) { + debug('get hook defined, returning: %s', ret); + return ret; + } + + ret = style[prop]; + debug('getting %s', ret); + return ret; +} diff --git a/lib/style.js b/lib/style.js index 8e87c5e..35a3f52 100644 --- a/lib/style.js +++ b/lib/style.js @@ -50,7 +50,7 @@ function style(el, prop, val, extra) { var style = el.style; var type = typeof val; - if (!val) return get(el, prop, orig, extra); + if (!val && val !== '') return; prop = property(prop, style); @@ -79,28 +79,3 @@ function style(el, prop, val, extra) { } } - -/** - * Get the style - * - * @param {Element} el - * @param {String} prop - * @param {String} orig - * @param {Mixed} extra - * @return {String} - */ - -function get(el, prop, orig, extra) { - var style = el.style; - var hook = hooks[prop] || hooks[orig]; - var ret; - - if (hook && hook.get && undefined !== (ret = hook.get(el, false, extra))) { - debug('get hook defined, returning: %s', ret); - return ret; - } - - ret = style[prop]; - debug('getting %s', ret); - return ret; -} diff --git a/test/css.js b/test/css.js index 7ce469f..cb10103 100644 --- a/test/css.js +++ b/test/css.js @@ -229,8 +229,7 @@ describe('css(prop)', function() { el.appendChild(div); css(div, 'display', 'none'); css(div, 'display', ''); - - assert('' === css(div, 'display')); + assert('block' == css(div, 'display')); el.removeChild(div); }); }); From e9a998e3ef812fceebeb0cc9f1dd2cdff221de91 Mon Sep 17 00:00:00 2001 From: Trey Griffith Date: Mon, 28 Apr 2014 02:20:16 -0700 Subject: [PATCH 3/3] patch version bump for bugfix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a54cb38..c326036 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "component-css", "description": "DOM element css helper", - "version": "0.0.5", + "version": "0.0.6", "keywords": [], "dependencies": { "debug": "*",