api.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var htmlparser2 = require(".."),
  2. assert = require("assert");
  3. describe("API", function(){
  4. it("should load all modules", function(){
  5. var Stream = require("../lib/Stream.js");
  6. assert.strictEqual(htmlparser2.Stream, Stream, "should load module");
  7. assert.strictEqual(htmlparser2.Stream, Stream, "should load it again (cache)");
  8. var ProxyHandler = require("../lib/ProxyHandler.js");
  9. assert.strictEqual(htmlparser2.ProxyHandler, ProxyHandler, "should load module");
  10. assert.strictEqual(htmlparser2.ProxyHandler, ProxyHandler, "should load it again (cache)");
  11. });
  12. it("should work without callbacks", function(){
  13. var p = new htmlparser2.Parser(null, {xmlMode: true, lowerCaseAttributeNames: true});
  14. p.end("<a foo><bar></a><!-- --><![CDATA[]]]><?foo?><!bar><boo/>boohay");
  15. p.write("foo");
  16. //check for an error
  17. p.end();
  18. var err = false;
  19. p._cbs.onerror = function(){ err = true; };
  20. p.write("foo");
  21. assert(err);
  22. err = false;
  23. p.end();
  24. assert(err);
  25. p.reset();
  26. //remove method
  27. p._cbs.onopentag = function(){};
  28. p.write("<a foo");
  29. p._cbs.onopentag = null;
  30. p.write(">");
  31. //pause/resume
  32. var processed = false;
  33. p._cbs.ontext = function(t){
  34. assert.equal(t, "foo");
  35. processed = true;
  36. };
  37. p.pause();
  38. p.write("foo");
  39. assert(!processed);
  40. p.resume();
  41. assert(processed);
  42. processed = false;
  43. p.pause();
  44. assert(!processed);
  45. p.resume();
  46. assert(!processed);
  47. p.pause();
  48. p.end("foo");
  49. assert(!processed);
  50. p.resume();
  51. assert(processed);
  52. });
  53. it("should update the position", function(){
  54. var p = new htmlparser2.Parser(null);
  55. p.write("foo");
  56. assert.equal(p.startIndex, 0);
  57. p.write("<bar>");
  58. assert.equal(p.startIndex, 3);
  59. });
  60. });