Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <gumbo.h>
  4.  
  5. int main()
  6. {
  7. // Result is the same if there is no doctype, or if some of the nodes are missing
  8.  
  9. const char *data = "<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body>\n</body>\n</html>";
  10. GumboOutput *output = gumbo_parse(data);
  11.  
  12.  
  13. // Following is just getting to the problematic node:
  14.  
  15. assert(output->root->type == GUMBO_NODE_ELEMENT);
  16. const GumboElement &htmlNode = output->root->v.element;
  17. assert(htmlNode.tag == GUMBO_TAG_HTML);
  18. std::cout << "Root element is HTML and has " << htmlNode.children.length << " children" << std::endl;
  19.  
  20. assert(htmlNode.children.length > 2);
  21. GumboNode *bodyNodeCont = static_cast<GumboNode*>(htmlNode.children.data[2]);
  22. assert(bodyNodeCont->type == GUMBO_NODE_ELEMENT);
  23. const GumboElement &bodyNode = bodyNodeCont->v.element;
  24. assert(bodyNode.tag == GUMBO_TAG_BODY);
  25. std::cout << "3rd of them is BODY which has " << bodyNode.children.length << " children" << std::endl;
  26.  
  27. assert(bodyNode.children.length > 0);
  28. GumboNode *whitespaceCont = static_cast<GumboNode*>(bodyNode.children.data[0]);
  29. assert(whitespaceCont->type == GUMBO_NODE_WHITESPACE);
  30. const GumboText &whitespace = whitespaceCont->v.text;
  31.  
  32.  
  33. // ...and now the problem itself:
  34.  
  35. std::cout << "1st of them is WHITESPACE which looks like this: \""
  36. << whitespace.text << "\", and original is "
  37. << whitespace.original_text.length << " bytes long and looks like this: \""
  38. << std::string(whitespace.original_text.data, whitespace.original_text.length)
  39. << "\"" << std::endl;
  40.  
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement