From 866d91490344b146803d54e5fa10dfabd8373930 Mon Sep 17 00:00:00 2001 From: SergioCrisostomo Date: Thu, 3 Jul 2014 19:30:32 +0200 Subject: [PATCH] Fix IE7/8 txt setter for "style" element Setter and Getter for "style" element is special in old IE. This creates exceptions to take care of that. --- Source/Element/Element.js | 30 +++++++++++++++++++++++++++--- Specs/Element/Element.js | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Source/Element/Element.js b/Source/Element/Element.js index 49517fa3..8f33e98b 100644 --- a/Source/Element/Element.js +++ b/Source/Element/Element.js @@ -214,12 +214,33 @@ var escapeQuotes = function(html){ }; /**/ +/**/ +// #2479 - IE8 Cannot set HTML of style element +var canChangeStyleHTML = (function(){ + var div = document.createElement('style'), + flag = false; + try { + div.innerHTML = '#justTesing{margin: 0px;}'; + flag = !!div.innerHTML; + } catch(e){} + return flag; +})(); +/**/ + Document.implement({ newElement: function(tag, props){ if (props){ if (props.checked != null) props.defaultChecked = props.checked; if ((props.type == 'checkbox' || props.type == 'radio') && props.value == null) props.value = 'on'; + /**/ // IE needs the type to be set before changing content of style element + if (!canChangeStyleHTML && tag == 'style'){ + var styleElement = document.createElement('style'); + styleElement.setAttribute('type', 'text/css'); + props.type && delete props.type; + return this.id(styleElement).set(props); + } + /**/ /**/// Fix for readonly name and type properties in IE < 8 if (createElementAcceptsHTML){ tag = '<' + tag; @@ -531,9 +552,12 @@ properties.text = (document.createElement('div').textContent == null) ? 'innerTe Object.forEach(properties, function(real, key){ propertySetters[key] = function(node, value){ - node[real] = value; + /**/ + if (node.get('tag') == 'style' && node.styleSheet) node.set('html', value); + else /**/node[real] = value; }; propertyGetters[key] = function(node){ + if (node.get('tag') == 'style' && node.styleSheet) return node.innerHTML; return node[real]; }; }); @@ -599,7 +623,7 @@ el = null; /**/ // #2479 - IE8 Cannot set HTML of style element -var canChangeStyleHtml = (function(){ +var canChangeStyleHTML = (function(){ var div = document.createElement('style'), flag = false; try { @@ -1034,7 +1058,7 @@ Element.Properties.html = { else if (typeOf(html) == 'array') html = html.join(''); /**/ - if (this.styleSheet && !canChangeStyleHtml) this.styleSheet.cssText = html; + if (this.styleSheet && !canChangeStyleHTML) this.styleSheet.cssText = html; else /**/this.innerHTML = html; }, erase: function(){ diff --git a/Specs/Element/Element.js b/Specs/Element/Element.js index 320195b9..9e5fcc26 100644 --- a/Specs/Element/Element.js +++ b/Specs/Element/Element.js @@ -1866,6 +1866,29 @@ describe('Element.set("html")', function(){ styleElement.destroy(); }); + it('should set the text of a style Element', function(){ + + var docHead = $(document.head); + var styleElement = new Element('style', {type: 'text/css'}).inject(docHead); + var definition = [ + '.pos-abs-left {', + 'position: absolute;', + 'width: 200px;', + 'height: 200px;', + 'left: 10%;', + 'background: red;', + '}' + ].join(''); + styleElement.set('text', definition); + var returned = styleElement.get('text').toLowerCase(); + expect(returned.indexOf('position: absolute')).not.toEqual(-1); + expect(returned.indexOf('width: 200px')).not.toEqual(-1); + expect(returned.indexOf('height: 200px')).not.toEqual(-1); + expect(returned.indexOf('left: 10%')).not.toEqual(-1); + expect(returned.indexOf('background: red')).not.toEqual(-1); + styleElement.destroy(); + }); + }); describe('Elements.empty', function(){ -- 2.11.4.GIT