#include #include #include #include #include using namespace std; struct Element { string nodeName; string nodeValue; vector childNodes; string innerHTML() { 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::childNodes.empty() ) { for ( size_t i = 0; i < childNodes.size(); ++i ) { if ( childNodes[i].nodeName != "#text" ) { temp += childNodes[i].innerHTML(); } else { temp += childNodes[i].nodeValue; } } temp += ""; } return temp; } map attributes; void setAttribute( const string& name, const string& value ) { attributes[ name ] = value; } string getAttribute( const string& name ) { return attributes[name]; } void appendChild( Element e ) { Element::childNodes.push_back( e ); } }; static struct Document { Element documentElement; Element body; Element createElement(const string& e ) { Element temp; temp.nodeName = e; return temp; } Element createTextNode(const string& t ) { Element temp; temp.nodeName = "#text"; temp.nodeValue = t; return temp; } } document; static struct Navigator { string userAgent; } navigator; static struct Window { Navigator navigator; Document document; } window; void alert ( const string& s ) { cout << s << endl; } typedef Element TextNode; int main() { /* generating the document */ navigator.userAgent = "Opera"; Element documentElement = document.createElement("html"); documentElement.setAttribute("lang", "en"); Element head = document.createElement("head"); Element title = document.createElement("title"); title.appendChild( document.createTextNode("C++ HTML") ); head.appendChild( title ); documentElement.appendChild( head ); Element body = document.createElement("body"); Element div = document.createElement("div"); div.setAttribute("id", "main"); div.appendChild( document.createTextNode( "This the first child text node of the div" ) ); Element div2 = document.createElement("div"); div2.appendChild( document.createTextNode( "This a child text node of the nested div" ) ); Element button = document.createElement("button"); button.appendChild( document.createTextNode("Go") ); div2.appendChild( button ); div.appendChild( div2 ); div.appendChild( document.createTextNode( "This is the last child, which is a text node, of the div") ); Element p = document.createElement("p"); p.appendChild( document.createTextNode("This is a paragraph") ); body.appendChild( div ); body.appendChild( p ); Element input = document.createElement("input"); input.setAttribute("type", "text"); input.setAttribute("value", "45"); body.appendChild( input ); documentElement.appendChild( body ); document.documentElement = documentElement; window.navigator = navigator; window.document = document; document.body = document.documentElement.childNodes[1]; /* done generating */ //alert( document.documentElement.childNodes[1].childNodes[0].childNodes[0].nodeValue ); //alert( document.body.nodeName ); alert( document.documentElement.innerHTML() ); //alert( document.documentElement.getAttribute("lang") ); //alert( document.documentElement.attributes["lang"] ); //alert( document.documentElement.attributes.begin()->first ); //alert( document.documentElement.attributes.begin()->second ); }