ember-fastboot / simple-dom

207 stars 33 forks source link

Add childNodes.length property #3

Closed dszlachta closed 8 years ago

dszlachta commented 9 years ago

In the current master, Node.childNodes has no length property. That's not compliant with DOM Core (http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-203510337) and can be a pain if writing code to check for element children. For example, this simple code won't work:

var hasChildren = !!someNode.childNodes.length

Using Object.defineGetter seems the simplest and most elegant solution here. It's supported by Chrome, FF, Opera 9.5, Safari 3 and IE 9: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

Cheers, Dawid

fivetanley commented 9 years ago

Is there a fallback we can use for IE8?

dszlachta commented 9 years ago

Unfortunately, defining anything similar to getter is impossible in IE8. But we could manipulate Node.childNodes.length after every append/remove. That's not so bullet-proof, but IE8 compatible. I have it here: https://github.com/dszlachta/simple-dom/tree/childNode-length-ie8

krisselden commented 9 years ago

@dszlachta WebCore starts maintaining the length on add/remove lazily when you access childNodes.length but like I said, childNodes is still an array like 'view' of the DOM graph, it is not naturally an array, and I'm very tempted to remove childNodes altogether, and replace with hooks for people who want to add support for it or move it to another build.

krisselden commented 9 years ago

I would accept a PR that cached and maintained childNodes.length on mutations.

stefanpenner commented 8 years ago

I would accept a PR that cached and maintained childNodes.length on mutations.

Will gladly reopen, if @krisselden feedback is addressed.