From d0a6f2b5561a56bc8febcbb4f6e8273a09f51bc8 Mon Sep 17 00:00:00 2001 From: Wouter Groeneveld Date: Wed, 29 Jun 2011 10:24:32 +0200 Subject: [PATCH] hack envjs element css style parsing --- src/test/javascript/lib/env.utils.js | 29 ++++++++++++++-- src/test/javascript/specs/envUtilsSpec.js | 41 ++++++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/test/javascript/lib/env.utils.js b/src/test/javascript/lib/env.utils.js index c7f50a1..f8735f4 100755 --- a/src/test/javascript/lib/env.utils.js +++ b/src/test/javascript/lib/env.utils.js @@ -55,7 +55,8 @@ * Envjs specific hacks * 1) Fix Envjs relative path system to work with Windows path systems * 2) Fix window.setTimeout() using Rhino specific functions - * 3) Fix CSS2Properties support: all properties have the same objmaps, wtf? + * 3) Fix CSS2Properties support for parsing style attributes: get from raw node context. + * 4) Fix CSS2Properties support for setting values: all properties have the same objmaps, wtf? */ (function() { @@ -74,6 +75,29 @@ }); }; + (function(Element) { + + var style = "style"; + function lookupStyleInNodeAttributes(el) { + if(el.attributes) { + for(var i = 0; i < el.attributes.length; i++) { + if(el.attributes[i].nodeName === style) { + return el.attributes[i].nodeValue; + } + } + } + } + + var styleSetFn = Element.__lookupGetter__(style); + Element.__defineGetter__(style, function() { + if(!this.cssText) { + this.cssText = lookupStyleInNodeAttributes(this); + } + return styleSetFn.apply(this); + }); + + })(HTMLElement.prototype); + (function(css) { var setCssProperty = css.prototype.setProperty; @@ -82,9 +106,8 @@ if(Object.keys(Object.getPrototypeOf(this.styleIndex)).length === 0) { this.styleIndex = Object.create(this.styleIndex); } - + return setCssProperty.call(this, name, value); } })(CSS2Properties); - })(); diff --git a/src/test/javascript/specs/envUtilsSpec.js b/src/test/javascript/specs/envUtilsSpec.js index d0ff318..cd3e508 100755 --- a/src/test/javascript/specs/envUtilsSpec.js +++ b/src/test/javascript/specs/envUtilsSpec.js @@ -1,7 +1,46 @@ +importPackage(org.apache.commons.io); +importPackage(java.io); + +var loadFixtures = function() { + this.fixturesPath = "src/test/javascript/specs/fixtures"; + + var read = ""; + for(var i = 0; i < arguments.length; i++) { + read += FileUtils.readFileToString(new File(this.fixturesPath, arguments[i])); + } + + document.body.innerHTML = document.body.innerHTML + read; +} + +var cleanupFixtures = function() { + document.body.innerHTML = ""; +} + describe("envjs fixes", function() { - describe("CSS2 style property support", function() { + describe("CSS2 style property support for parsing style attributes", function() { + beforeEach(function() { + loadFixtures("styleAttributes.html"); + }); + + afterEach(cleanupFixtures); + + it("should get a style attribute from a static DOM element", function() { + var div = document.getElementById("div"); + expect(div.style.color).toBe("blue"); + }); + + it("should get a style attribute with dashes using camelCasing properties", function() { + var spanStyle = document.getElementById("span").style; + + expect(spanStyle.backgroundColor).toBe("green"); + expect(spanStyle.fontSize).toBe("8pt"); + expect(spanStyle.fontFamily).toBe("verdana"); + }); + }); + + describe("CSS2 style property support for setting values", function() { var someColor = "#FFFFFF"; var someFont = "12px 'Bitstream Vera Sans Mono','Courier',monospace";