I discovered this little beauty awhile ago, but used it once then forgot about it. I rediscovered it today while working on an awesome WordPress theme framework that I’m developing. However I ran into some issues with it and wanted to document them here for future reference, and to help others avoid the debugging grief I just went through.

Accessing Hyphenated Attributes

The documentation states that to access the attribute of an element, use the arrow operator. This works fine in the example:

// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;

However it doesn’t work so well when the attribute we’re trying to access is data-role or data-inset, for obvious reasons. Turns out there’s an attr[] array that stores these values as well, and is much friendlier for these types of attributes. So to access the data-role attribute we’d just use:

$value = $e->attr['data-role'];

Bug When Adding Attributes

It seems that when we add an attribute that didn’t already exist on the element using the aforementioned attr[] array, it fails to add a leading space. We end up with <uldata-role="listview"data-inset="true">, which isn’t really what we’re looking for. The fix is quite easy. Just add the leading space when you assign the attribute, though doing some sanity checks doesn’t hurt:

if ( ! $ul->attr['data-role'] )
	// Add leading space on attribute name
	$ul->attr[' data-role'] = 'listview';
	$ul->attr['data-role'] = 'listview';

That’s all I’ve discovered so far.  I do highly recommend this helper class as it lets you manipulate the DOM quite well. I’m normally not opposed to JavaScript, but most of the things I do these days tend to be fairly JS-heavy, so getting some of this lifting done on the server (and caching it) will definitely help with page load times.

PHP Simple HTML DOM Parser on Sourceforge