#include #include #include #include using namespace std; struct Element { string nodeName; string nodeValue; vector childNodes; map attributes; void appendChild(Element e ) { Element::childNodes.push_back( e ); } vector getElementsByTagName(const string& tag ) { /* doesn't support space-separated or * yet */ vector temp; for ( size_t i = 0; i < Element::childNodes.size(); ++i ) { if ( Element::childNodes[i].nodeName == tag ) { temp.push_back( &Element::childNodes[i] ); } } return temp; } bool hasChildNodes() const { if ( !Element::childNodes.empty() ) { return true; } return false; } void setAttribute( const string& name, const string& value ) { attributes[ name ] = value; } string getAttribute( const string& name ) { return attributes[name]; } string outerHTML() { string temp; temp += "<"; temp += Element::nodeName; if ( Element::nodeName != "#text") { for ( map::const_iterator i = Element::attributes.begin(); i != Element::attributes.end(); ++i ) { temp += " "; temp += i->first; temp += "=\""; temp += i->second; temp += "\""; } } temp += ">"; if ( Element::hasChildNodes() ) { for ( size_t i = 0; i < childNodes.size(); ++i ) { if ( childNodes[i].nodeName != "#text" ) { temp += childNodes[i].outerHTML(); } else { temp += childNodes[i].nodeValue; } } temp += ""; } return temp; } string innerHTML() { if ( Element::hasChildNodes() ) { return Element::childNodes[0].outerHTML(); } return ""; } }; struct Document { Element documentElement; Element* body; Element createElement(const string& tag) const { Element temp; temp.nodeName = tag; return temp; } Element createTextNode( const string& text ) const { Element temp; temp.nodeValue = text; temp.nodeName = "#text"; return temp; } } document; struct Window { Document* document; template void alert( const T& s ) const { cout << s << endl; } } window; #define alert window.alert typedef Element TextNode; typedef vector ElementArray; int main() { window.document = &document; document.documentElement = document.createElement("html"); document.documentElement.setAttribute("lang", "en"); document.documentElement.appendChild( document.createElement("body") ); document.body = document.documentElement.getElementsByTagName("body")[0]; TextNode x = document.createTextNode( "Hello, world!" ); Element p = document.createElement("p"); p.setAttribute("id", "blabla"); p.appendChild ( x ); document.body->appendChild( p ); alert( document.body->childNodes[0].childNodes[0].nodeValue ); alert( window.document->documentElement.nodeName ); alert( document.body->hasChildNodes() ); alert( document.documentElement.outerHTML() ); alert( document.body->outerHTML() ); alert( document.body->innerHTML() ); alert( document.documentElement.innerHTML() ); alert( document.documentElement.getAttribute("lang") ); alert( document.documentElement.attributes.begin()->second ); }