test-gumbo.nut 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. local input = [==[
  2. <TITlE>Test Document</TITLe>
  3. <h1>Test Heading</h1>
  4. <p><a href=foobar.html>Quux</a></p>
  5. <iNValID foo="bar">abc</invalid>
  6. <p class=empty></p>
  7. ]==];
  8. //local document = assert(gumbo.parse(input))
  9. local document = gumbo.parse(input);
  10. foreach(k,v in document) print(k,v);
  11. //local root = assert(document.root)
  12. print(table_len(document.root));
  13. local root = document.root
  14. local head = root.children[0];
  15. local body = root.children[1];
  16. assert(root.tag == "html")
  17. assert(head.tag == "head")
  18. assert(body.tag == "body")
  19. print("head.children[0].tag", head.children[0].tag);
  20. assert(head.children[0].tag == "title")
  21. assert(head.children[0].children[0] == "Test Document")
  22. assert(body.children[0].tag == "h1")
  23. assert(body.children[0].children[0] == "Test Heading")
  24. assert(body.children[1].tag == "p")
  25. assert(body.children[1].children[0].attr.href == "foobar.html")
  26. assert(body.children[2].tag == "iNValID")
  27. assert(body.children[2].attr.foo == "bar")
  28. assert(body.children[2].children[0] == "abc")
  29. assert(body.children[3].children.len() == 0)
  30. assert(body.children[3].attr["class"] == "empty")
  31. assert(gumbo.parse("<h1>Hello</h1>").root.children[1].children[0].children[0] == "Hello")
  32. //assert(! gumbo.parse_file( "non-existent-file"))
  33. document = gumbo.parse_file( "/home/mingo/Downloads/Components · Bootstrap-3.0.html");
  34. foreach(k,v in document) print(k,v);
  35. root = document.root
  36. head = root.children[0];
  37. foreach(k,v in head.children) print(k,v);
  38. foreach(k,v in head) print(k,v);
  39. body = root.children[1];
  40. foreach(k,v in body) print(k,v);
  41. foreach(k,v in body.children) print(k,v, v ? table_rawget(v, "tag", "?") : "??");