index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var dom = require("../../lib/jsdom/level2/core").dom.level2.core;
  2. var browser = require("../../lib/jsdom/browser/index").browserAugmentation(dom);
  3. exports.tests = {
  4. notfound_getelementsbyclassname: function(test) {
  5. var doc = new browser.Document();
  6. var html = doc.createElement("html");
  7. doc.appendChild(html);
  8. var body = doc.createElement("body");
  9. html.appendChild(body);
  10. var p = doc.createElement("p");
  11. p.className = "unknown";
  12. body.appendChild(p);
  13. var elements = doc.getElementsByClassName("first-p");
  14. test.equal(elements.length, 0, "no results");
  15. test.done();
  16. },
  17. basic_getelementsbyclassname: function(test) {
  18. var doc = new browser.Document();
  19. var html = doc.createElement("html");
  20. doc.appendChild(html);
  21. var body = doc.createElement("body");
  22. html.appendChild(body);
  23. var p = doc.createElement("p");
  24. p.className = "first-p";
  25. body.appendChild(p);
  26. var elements = doc.getElementsByClassName("first-p");
  27. test.equal(elements.item(0), p, 'p and first-p');
  28. test.done();
  29. },
  30. multiple_getelementsbyclassname: function(test) {
  31. var doc = new browser.Document();
  32. var html = doc.createElement("html");
  33. doc.appendChild(html);
  34. var body = doc.createElement("body");
  35. html.appendChild(body);
  36. var p = doc.createElement("p");
  37. p.className = "first-p second third";
  38. body.appendChild(p);
  39. var first = doc.getElementsByClassName("first-p").item(0);
  40. var second = doc.getElementsByClassName("second").item(0);
  41. var third = doc.getElementsByClassName("third").item(0);
  42. test.equal(first, p, 'p and first-p');
  43. test.equal(second, p, 'p and second');
  44. test.equal(third, p, 'p and third');
  45. test.done();
  46. },
  47. testclassnameworksasexpected: function(test) {
  48. var doc = new browser.Document();
  49. var p = doc.createElement("p");
  50. p.setAttribute("class", "first-p");
  51. test.equal(p.className, 'first-p', 'class attribute is same as className');
  52. p.className += " second";
  53. test.equal(p.className, 'first-p second', 'className getter/setter');
  54. test.done();
  55. },
  56. basic_getelementbyid: function(test) {
  57. var doc = new browser.Document();
  58. var html = doc.createElement("html");
  59. doc.appendChild(html);
  60. var body = doc.createElement("body");
  61. html.appendChild(body);
  62. var p = doc.createElement("p");
  63. p.id = "theid";
  64. body.appendChild(p);
  65. var element = doc.getElementById("theid");
  66. test.equal(element, p, "p and #theid");
  67. test.done();
  68. },
  69. nonexistant_getelementbyid: function(test) {
  70. var doc = new browser.Document();
  71. var html = doc.createElement("html");
  72. doc.appendChild(html);
  73. var body = doc.createElement("body");
  74. html.appendChild(body);
  75. var p = doc.createElement("p");
  76. p.id = "theid";
  77. body.appendChild(p);
  78. var element = doc.getElementById("non-existant-id");
  79. test.equal(element, null, "p and #theid");
  80. test.done();
  81. },
  82. remove_nonexistantattribute: function(test) {
  83. var doc = new browser.Document();
  84. var html = doc.createElement("html");
  85. doc.appendChild(html);
  86. var body = doc.createElement("body");
  87. html.appendChild(body);
  88. test.doesNotThrow(function(){ body.removeAttribute("non-existant"); }), 'setValue_throws_NO_MODIFICATION_ERR';
  89. test.done();
  90. },
  91. render_singletag: function(test) {
  92. var doc = new browser.Document();
  93. var p = doc.createElement("p");
  94. var img = doc.createElement("img");
  95. p.appendChild(img);
  96. var out = p.outerHTML;
  97. test.equal(out.match(/<\/img>/), null, 'end tag not included in output')
  98. test.done();
  99. },
  100. parse_scripttags: function(test) {
  101. var doc = new browser.Document();
  102. var head = doc.createElement("head");
  103. var scriptHtml = '<script>alert("hello world")</script>';
  104. head.innerHTML = scriptHtml;
  105. test.equal(scriptHtml, head.innerHTML, "original and processed");
  106. test.done();
  107. },
  108. parse_styletags: function(test) {
  109. var doc = new browser.Document();
  110. var head = doc.createElement("head");
  111. var styleHtml = '<style>body: {color: #fff;}</style>';
  112. head.innerHTML = styleHtml;
  113. test.equal(styleHtml, head.innerHTML, "original and processed");
  114. test.done();
  115. },
  116. parse_doublespacetags: function(test) {
  117. var doc = new browser.Document();
  118. var html = '<html><body class="testing" /></html>';
  119. test.doesNotThrow(function(){ doc.innerHTML = html; }), 'setValue_throws_INVALID_CHARACTER_ERR';
  120. test.done();
  121. },
  122. serialize_styleattribute: function(test) {
  123. var doc = new browser.Document();
  124. var domToHtml = require('../../lib/jsdom/browser/domtohtml');
  125. doc.appendChild(doc.createElement('html'));
  126. doc.documentElement.style.color = 'black';
  127. doc.documentElement.style.backgroundColor = 'white';
  128. test.equal(domToHtml.domToHtml(doc), '<html style="color: black; background-color: white"></html>\n', '');
  129. test.done();
  130. },
  131. innerhtml_removeallchildren: function(test) {
  132. var doc = new browser.HTMLDocument();
  133. doc.write('<html><body><p></p><p></p></body></html>');
  134. doc.body.innerHTML = '';
  135. test.equal(doc.body.childNodes.length, 0, 'still has children');
  136. test.done();
  137. },
  138. serialize_html5_doctype: function(test) {
  139. var dom = new browser.DOMImplementation();
  140. var doctype = dom.createDocumentType('html');
  141. var document = dom.createDocument(null, null, doctype);
  142. var regexp = /^\s*<!DOCTYPE html>/;
  143. test.ok(regexp.test(document.outerHTML), 'HTML 5 doctype did not serialize correctly');
  144. test.done();
  145. },
  146. serialize_html4_strict_doctype: function(test) {
  147. var dom = new browser.DOMImplementation();
  148. var doctype = dom.createDocumentType('html', '-//W3C//DTD HTML 4.01//EN', 'http://www.w3.org/TR/html4/strict.dtd');
  149. var document = dom.createDocument(null, null, doctype);
  150. var regexp = /^\s*<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD HTML 4.01\/\/EN" "http:\/\/www.w3.org\/TR\/html4\/strict.dtd">/;
  151. test.ok(regexp.test(document.outerHTML), 'HTML 4 strict doctype did not serialize correctly');
  152. test.done();
  153. },
  154. serialize_system_doctype: function(test) {
  155. var dom = new browser.DOMImplementation();
  156. var doctype = dom.createDocumentType('foo', null, 'foo.dtd');
  157. var document = dom.createDocument(null, null, doctype);
  158. var regexp = /^\s*<!DOCTYPE foo SYSTEM "foo.dtd">/;
  159. test.ok(regexp.test(document.outerHTML), 'Doctype did not serialize correctly');
  160. test.done();
  161. },
  162. serialize_doctype_containing_quotes: function(test) {
  163. var dom = new browser.DOMImplementation();
  164. var doctype = dom.createDocumentType('foo', null, 'foo "bar".dtd');
  165. var document = dom.createDocument(null, null, doctype);
  166. var regexp = /^\s*<!DOCTYPE foo SYSTEM \'foo "bar".dtd\'>/;
  167. test.ok(regexp.test(document.outerHTML), 'Doctype did not serialize correctly');
  168. test.done();
  169. },
  170. parse_doctype_containing_newline : function(test) {
  171. var html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n \
  172. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html></html>',
  173. doc = new browser.Document();
  174. doc.innerHTML = html;
  175. test.ok(!!doc.doctype, 'doctype should not be falsy');
  176. test.done();
  177. },
  178. basic_nodelist_indexOf : function(test) {
  179. var doc = new browser.Document();
  180. var html = doc.createElement("html");
  181. doc.appendChild(html);
  182. var body = doc.createElement("body");
  183. html.appendChild(body);
  184. var p = doc.createElement("p");
  185. body.appendChild(p);
  186. var div = doc.createElement("div");
  187. body.appendChild(div);
  188. var span = doc.createElement("span");
  189. body.appendChild(span);
  190. var index = body.childNodes.indexOf(span);
  191. test.equal(index, 2, "indexOf 'span' in childNodes")
  192. test.done();
  193. },
  194. nonexistant_nodelist_indexOf : function(test) {
  195. var doc = new browser.Document();
  196. var html = doc.createElement("html");
  197. doc.appendChild(html);
  198. var body = doc.createElement("body");
  199. html.appendChild(body);
  200. var p = doc.createElement("p");
  201. body.appendChild(p);
  202. var div = doc.createElement("div");
  203. p.appendChild(div);
  204. var index = body.childNodes.indexOf(div);
  205. test.equal(index, -1, "indexOf 'span' in childNodes")
  206. test.done();
  207. }
  208. };