outaTiME

at devel days

DOMDom: More DOM, Less DOM

leave a comment »

Zach Leatherman has created another DOM creation class, DOMDom, (with support for HTML Fragments) that uses
the CSS query language. But instead of using the language to query nodes, it is used to create nodes.

To make <div style=”width:100%;border:1px solid blue” class=”testClass”><a href=”http://www.google.com/”><span>Google</span></a></div>, you’d have to do the following in other packages:

DomHelper:

JAVASCRIPT:

  1.  
  2. Ext.DomHelper.append( myDiv, {
  3.    tag: ‘div’,
  4.    style: ‘width:100%;border:1px solid blue;’,
  5.    cls: ‘testClass’,
  6.    children: [{
  7.        tag: ‘a’,
  8.        href: ‘http://www.google.com/’,
  9.        children: [{
  10.            tag: ’span’,
  11.            html: ‘Google’
  12.        }]
  13.    }]
  14. } );
  15.  

jQuery’s FlyDom:

JAVASCRIPT:

  1.  
  2. $( myDiv ).createAppend(
  3.    ‘div’, { style: ‘width:100%;border:1px solid blue;’, class: ‘testClass’ }, [
  4.        ‘a’, { href: ‘http://www.google.com/’ }, [
  5.            ’span’, {}, ‘Google’
  6.        ]
  7.    ]
  8. );
  9.  

This can be achieved in DOMDom, with comparable speed and code size with the following descriptor:

JAVASCRIPT:

  1.  
  2. DOMDom.append( { ‘div[style="width:100%;border:1px solid
  3. blue",class="testClass"] a[href="http://www.google.com/"] span’:
  4. ‘#Google’ }, myDiv );
  5.  

Currently works with YUI, but it will probably be ported to jQuery and other libraries.

(Via Ajaxian.)

Written by outaTiME

July 12, 2007 at 1:40 pm

Posted in Development, Javascript

Leave a Reply