alohaeditor/Aloha-Editor — pull request #335 by laktek (2011-10-24 02:32:15+00:00)
Title: ECMA5 compatibility changes

Here are the ECMA5 compatibility changes I worked on.

All ECMA5 methods are exposed to the engine.js via a object called '$_'. Which means all you got to do is to wrap the calling objects with $_() to get ECMA5 compatibility.

eg.

" Hello ! ".trim();

now becomes

$_(" Hello ! ").trim();

Benefit of this notation is in future if jQuery implements these methods or Aloha decides to add underscore.js as a dependency, it would be trivial to change the existing code.

Also, with this object oriented style it's possible to do method chaining. In order to do this, you need to pass a 'true' as the last argument to the method call.

eg.
getAncestors(node).concat(node)
.filter(function(node) { return node.nodeType == $_.Node.ELEMENT_NODE })
.some(function(node) { return $_.getComputedStyle(node).display == "none" })) {
return false;
}

now becomes

$_( getAncestors(node).concat(node) )
.filter(function(node) { return node.nodeType == $_.Node.ELEMENT_NODE }, true)
.some(function(node) { return $_.getComputedStyle(node).display == "none" })) {
return false;
}

ECMA5 method calls which belongs to global context such as `Node`, `compareDocumentPosition` and `getComputedStyle` can now be invoked as `$_.Node`, `$_.compareDocumentPosition` and `$_.getComputedStyle`.

Also, if one prefers functional style over object oriented (maybe to shave off the number of method calls), he can use the ECMA5 methods in this way too.

$_.map.call([1, 2, 3, 4], function(o){ return o + 1})

instead of

$_([1, 2, 3, 4]).map(function(o){ return o + 1 })

Apart from the existing ECMA5 compatible methods, I had to implement 'hasAttribute' method as it was not supported in IE7.

With this implementation the status of delete/forwarddelete tests are as follows (most erros in IE is now due to range issues):

IE7 - delete - 152 tests of 213 passed, 61 failed.
IE7 - forwarddelete - 106 tests of 201 passed, 95 failed.

IE8 - delete - 186 tests of 213 passed, 27 failed
IE8 - forwarddelete - 114 tests of 201 passed, 87 failed.

Chrome - delete -196 tests of 223 passed, 27 failed.
Chrome - forwarddelete - 175 tests of 214 passed, 39 failed

I couldn't get them tested on IE9, but since it's ECMA5 compatible I hope its results would be similar to Chrome.