Permalink
Browse files

Fix specs and refactoring.

Fixed a bug .get('html') bug introduced in the last commit.
Fixed a legacy bug that always tried to fix innerHTML for some elements.
This required a bit of refactoring of the code, so please review
carefully.

PASSED: IE6-9; FFx 3-5, 8, 10; Chrome latest; Safari 5; Opera 11
  • Loading branch information...
ibolmo committed Dec 11, 2011
1 parent 626aae4 commit b317c97a42e1a9e6af6271d32b31125d7e4185da
Showing with 55 additions and 34 deletions.
  1. +55 −34 Source/Element/Element.js
View
@@ -518,6 +518,7 @@ Array.forEach([
properties[property.toLowerCase()] = property;
});
+properties.html = 'innerHTML';
properties.text = (document.createElement('div').textContent == null) ? 'innerText': 'textContent';
Object.forEach(properties, function(real, key){
@@ -906,56 +907,76 @@ Element.Properties.tag = {
};
+Element.Properties.html = {
+
+ set: function(html){
+ if (typeOf(html) == 'array') html = html.join('');
+ else if (html == null) html = '';
+ this.innerHTML = html;
+ },
+
+ erase: function(){
+ this.set('');
+ }
+
+};
+
/*<ltIE9>*/
// technique by jdbarlett - http://jdbartlett.com/innershiv/
var div = document.createElement('div');
div.innerHTML = '<nav></nav>';
-var supportsHTML5Elements = div.childNodes.length == 1;
+var supportsHTML5Elements = (div.childNodes.length == 1);
if (!supportsHTML5Elements){
var tags = 'abbr article aside audio canvas datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video'.split(' '),
fragment = document.createDocumentFragment(), l = tags.length;
while (l--) fragment.createElement(tags[l]);
- fragment.appendChild(div);
}
div = null;
/*</ltIE9>*/
-/*<!webkit>*/
-
-var translations = {
- table: [1, '<table>', '</table>'],
- select: [1, '<select>', '</select>'],
- tbody: [2, '<table><tbody>', '</tbody></table>'],
- tr: [3, '<table><tbody><tr>', '</tr></tbody></table>']
-};
-translations.thead = translations.tfoot = translations.tbody;
-
-var tableTest = Function.attempt(function(){
+/*<IE>*/
+var supportsTableInnerHTML = Function.attempt(function(){
var table = document.createElement('table');
table.innerHTML = '<tr><td></td></tr>';
+ return true;
});
-var html;
-Element.Properties.html = html = {set: function(html){
- if (typeOf(html) == 'array') html = html.join('');
- else if (html == null) html = '';
-
- var wrap = (!tableTest && translations[this.get('tag')]);
- /*<ltIE9>*/
- if (!wrap && !supportsHTML5Elements) wrap = [0, '', ''];
- /*</ltIE9>*/
- if (wrap){
- var first = document.createElement('div');
- first.innerHTML = wrap[1] + html + wrap[2];
- for (var i = wrap[0]; i--;) first = first.firstChild;
- this.empty().adopt(first.childNodes);
- first = null;
- } else {
- this.innerHTML = html;
- }
-}};
-html.erase = html.set;
-/*</!webkit>*/
+/*<ltFFx4>*/
+var tr = document.createElement('tr'), html = '<td></td>';
+tr.innerHTML = html;
+var supportsTRInnerHTML = (tr.innerHTML == html);
+/*</ltFFx4>*/
+
+if (!supportsTableInnerHTML || !supportsTRInnerHTML){
+
+ Element.Properties.html.set = (function(original){
+
+ var translations = {
+ table: [1, '<table>', '</table>'],
+ select: [1, '<select>', '</select>'],
+ tbody: [2, '<table><tbody>', '</tbody></table>'],
+ tr: [3, '<table><tbody><tr>', '</tr></tbody></table>']
+ };
+
+ translations.thead = translations.tfoot = translations.tbody;
+
+ return function(html){
+ var wrap = translations[this.get('tag')];
+ if (!wrap && !supportsHTML5Elements) wrap = [0, '', ''];
+ if (!wrap) return original.call(this, html);
+
+ var level = wrap[0], wrapper = document.createElement('div'), target = wrapper;
+ if (!supportsHTML5Elements) fragment.appendChild(wrapper);
+ wrapper.innerHTML = [wrap[1], html, wrap[2]].flatten().join('');
+ while (level--) target = target.firstChild;
+ this.empty().adopt(target.childNodes);
+ if (!supportsHTML5Elements) fragment.removeChild(wrapper);
+ wrapper = null;
+ };
+
+ })(Element.Properties.html.set);
+}
+/*</IE>*/
/*<ltIE9>*/
var testForm = document.createElement('form');

0 comments on commit b317c97

Please sign in to comment.