Spanish Total.js
English
Documentation For AI agents
Contents

HTML and XML parser

Parse HTML and XML on the server into a tree of elements that you can search and change.

.md GitHub

String.prototype.parseHTML([trim]) parses HTML into a tree of elements that you can search and change, a small DOM on the server. parseXML2() does the same for XML (tag names keep their prefixes, such as soap:Body).

var dom = '<div class="list"><p id="a">Hello <b>world</b></p><p data-id="2">Bye</p></div>'.parseHTML();

dom.find('p').length;                     // 2
dom.find('div > p[data-id="2"]').text();  // 'Bye'

var p = dom.find('#a')[0];
p.aclass('big').css('color', 'red').attr('title', 'Greeting');
p.find('b')[0].remove();

dom.html(); // '<div class="list"><p id="a" class="big" style="color:red" title="Greeting">Hello </p>...'

find() accepts tag names, #id, .class, [attr="value"], [attr] (the attribute exists), descendants (div p), children (div > p) and several selectors separated by commas. It returns an array of elements with the same methods for all of them (aclass(), rclass(), attr(), find(), html(), text()...). Each element has tagName (upper case), attrs, children and parentNode; text is in nodes with tagName TEXT and textContent.

Made in Europe