From 3cec3de4f9e186328ce9f18220185c7f17bb3007 Mon Sep 17 00:00:00 2001 From: Sergei Z Date: Sat, 12 Jul 2014 02:47:09 +0300 Subject: [PATCH] fix(jqLite): .css() retrieves computed style also --- src/jqLite.js | 15 ++++++++++++++- test/jqLiteSpec.js | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/jqLite.js b/src/jqLite.js index 771c4312432d..826e2f3d72f8 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -512,7 +512,20 @@ forEach({ if (val === '') val = 'auto'; } - val = val || element.style[name]; + var contains = function (a, b){ + // jshint bitwise: false + return a.contains ? + a != b && a.contains(b) : + !!(a.compareDocumentPosition(b) & 16); + }; + + var elementWindow = element.ownerDocument.defaultView || element.ownerDocument.parentWindow; + // if browser supports getComputedStyle and element attached to body + if (isDefined(elementWindow.getComputedStyle) && contains(element.ownerDocument, element)) { + val = val || elementWindow.getComputedStyle(element, null)[name]; + } else { + val = val || element.style[name]; + } if (msie <= 8) { // jquery weirdness :-/ diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 8bab591ae173..68fb9f43750c 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -825,6 +825,19 @@ describe('jqLite', function() { } }); + it('should get computed styles', function() { + if (window.getComputedStyle) { // browser should support of computed styles + a.id = 'elementA'; + var jqA = jqLite(a); + jqLite(document).find('body').append(jqA); + + var style = document.createElement('style'); + style.appendChild(document.createTextNode('#elementA { width: 25px; }')); + jqLite(document).find('head').append(style); + + expect(jqA.css('width')).toBe('25px'); + } + }); it('should set a bunch of css properties specified via an object', function() { if (msie <= 8) {