PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rdom/node_test.rb

https://gitlab.com/Blueprint-Marketing/rdom
Ruby | 390 lines | 301 code | 60 blank | 29 comment | 15 complexity | 57512957d756de74aa939ffe742d1ffb MD5 | raw file
  1. require File.expand_path('../../test_helper', __FILE__)
  2. class NodeTest < Test::Unit::TestCase
  3. attr_reader :window, :document, :body, :div
  4. def setup
  5. @window = RDom::Window.new('<html><body><div id="foo">FOO</div><p class="bar">BAR</p></body></html>')
  6. @document = window.document
  7. @body = document.getElementsByTagName('//body').first
  8. @div = document.getElementsByTagName('//div').first
  9. window.evaluate <<-js
  10. var body = document.body
  11. var div = document.getElementsByTagName('div')[0]
  12. function nodeNamesOf(nodes) {
  13. var names = [];
  14. for(i = 0; i < nodes.length; i++) { names.push(nodes[i].nodeName); }
  15. return names;
  16. }
  17. js
  18. end
  19. # test "ruby: identical nodes obtained from various methods are the same objects", :ruby, :implementation do
  20. # assert_equal document.object_id, body.ownerDocument.object_id
  21. # assert_equal document.object_id, div.parentNode.ownerDocument.object_id
  22. #
  23. # assert_equal body.object_id, div.parentNode.object_id
  24. # assert_equal body.object_id, div.parentNode.lastChild.parentNode.object_id
  25. # end
  26. # DOM-Level-1-Core
  27. # http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html
  28. test "ruby: Node.nodeName returns the node's name (#document for document)", :ruby, :dom_1_core do
  29. assert_equal 'DIV', div.nodeName
  30. end
  31. test "js: Node.nodeName returns the node's name (#document for document)", :js, :dom_1_core do
  32. assert_equal 'DIV', window.evaluate("div.nodeName")
  33. end
  34. test "ruby: Node.nodeValue returns the node's value (null for document)", :ruby, :dom_1_core do
  35. window.load <<-html
  36. <html>
  37. <body>
  38. <div>Foo</div>
  39. <input name="input" value="value">
  40. <!-- comment -->
  41. </body>
  42. </html>
  43. html
  44. document = window.document
  45. input = document.getElementsByTagName('input')[0]
  46. div = document.getElementsByTagName('div')[0]
  47. comment = document.body.lastChild
  48. assert_nil document.nodeValue
  49. assert_nil div.nodeValue
  50. assert_nil input.nodeValue
  51. assert_equal 'input', input.getAttributeNode('name').nodeValue
  52. assert_equal ' comment ', comment.nodeValue
  53. end
  54. test "js: Node.nodeValue returns the node's value (null for document)", :js, :dom_1_core do
  55. window.load <<-html
  56. <html>
  57. <body>
  58. <div>Foo</div>
  59. <input name="input" value="value">
  60. <!-- comment -->
  61. </body>
  62. </html>
  63. html
  64. window.evaluate <<-js
  65. document = window.document
  66. input = document.getElementsByTagName('input')[0]
  67. div = document.getElementsByTagName('div')[0]
  68. comment = document.body.lastChild
  69. js
  70. assert_nil window.evaluate("document.nodeValue")
  71. assert_nil window.evaluate("div.nodeValue")
  72. assert_nil window.evaluate("input.nodeValue")
  73. assert_equal 'input', window.evaluate("input.getAttributeNode('name').nodeValue")
  74. assert_equal ' comment ', window.evaluate("comment.nodeValue")
  75. end
  76. test "ruby: Node.nodeType returns a node type constant (9 for document)", :ruby, :dom_1_core do
  77. assert_equal 1, Nokogiri::XML::Node::ELEMENT_NODE
  78. assert_equal 2, Nokogiri::XML::Node::ATTRIBUTE_NODE
  79. assert_equal 3, Nokogiri::XML::Node::TEXT_NODE
  80. assert_equal 4, Nokogiri::XML::Node::CDATA_SECTION_NODE
  81. assert_equal 5, Nokogiri::XML::Node::ENTITY_REF_NODE
  82. assert_equal 6, Nokogiri::XML::Node::ENTITY_NODE
  83. assert_equal 7, Nokogiri::XML::Node::PI_NODE
  84. assert_equal 8, Nokogiri::XML::Node::COMMENT_NODE
  85. assert_equal 9, Nokogiri::XML::Node::DOCUMENT_NODE
  86. assert_equal 10, Nokogiri::XML::Node::DOCUMENT_TYPE_NODE
  87. assert_equal 11, Nokogiri::XML::Node::DOCUMENT_FRAG_NODE
  88. assert_equal 12, Nokogiri::XML::Node::NOTATION_NODE
  89. assert_equal 1, div.nodeType
  90. assert_equal 2, div.getAttributeNode('id').nodeType
  91. assert_equal 3, div.firstChild.nodeType
  92. assert_equal 8, document.createComment('comment').nodeType
  93. assert_equal 9, document.nodeType
  94. assert_equal 11, document.createDocumentFragment.nodeType
  95. end
  96. test "js: Node.nodeType returns a node type constant (9 for document)", :js, :dom_1_core do
  97. assert_equal 1, window.evaluate("div.nodeType")
  98. assert_equal 2, window.evaluate("div.getAttributeNode('id').nodeType")
  99. assert_equal 3, window.evaluate("div.firstChild.nodeType")
  100. assert_equal 8, window.evaluate("document.createComment('comment').nodeType")
  101. assert_equal 9, window.evaluate("document.nodeType")
  102. assert_equal 11, window.evaluate("document.createDocumentFragment().nodeType")
  103. end
  104. test "ruby: Node.parentNode returns the parent of the specified node in the DOM tree (null for document)", :ruby, :dom_1_core do
  105. assert_equal 'BODY', div.parentNode.nodeName
  106. end
  107. test "js: Node.parentNode returns the parent of the specified node in the DOM tree (null for document)", :js, :dom_1_core do
  108. assert_equal 'BODY', window.evaluate("div.parentNode.nodeName")
  109. end
  110. test "ruby: Node.childNodes returns a collection of child nodes of the given node NodeList", :ruby, :dom_1_core do
  111. assert body.childNodes.respond_to?(:length)
  112. assert_equal 2, body.childNodes.length
  113. end
  114. test "js: Node.childNodes returns a collection of child nodes of the given node NodeList", :js, :dom_1_core do
  115. assert_equal 2, window.evaluate("body.childNodes.length")
  116. end
  117. test "ruby: Node.firstChild returns the first node in the list of direct children of the document", :ruby, :dom_1_core do
  118. assert_equal 'DIV', body.firstChild.nodeName
  119. end
  120. test "js: Node.firstChild returns the first node in the list of direct children of the document", :js, :dom_1_core do
  121. assert_equal 'DIV', window.evaluate("body.firstChild.nodeName")
  122. end
  123. test "ruby: Node.lastChild returns the last child of a node Node", :ruby, :dom_1_core do
  124. assert_equal 'P', body.lastChild.nodeName
  125. end
  126. test "js: Node.lastChild returns the last child of a node Node", :js, :dom_1_core do
  127. assert_equal 'P', window.evaluate("body.lastChild.nodeName")
  128. end
  129. test "ruby: Node.previousSibling returns the node immediately preceding the specified one in its parent's childNodes list, null if the specified node is the first in that list (null for document)", :ruby, :dom_1_core do
  130. assert_equal 'DIV', body.lastChild.previousSibling.nodeName
  131. end
  132. test "js: Node.previousSibling returns the node immediately preceding the specified one in its parent's childNodes list, null if the specified node is the first in that list (null for document)", :js, :dom_1_core do
  133. assert_equal 'DIV', window.evaluate("body.lastChild.previousSibling.nodeName")
  134. end
  135. test "ruby: Node.nextSibling returns the node immediately following the specified one in its parent's childNodes list, or null if the specified node is the last node in that list (null for documents)", :ruby, :dom_1_core do
  136. assert_equal 'P', body.firstChild.nextSibling.nodeName
  137. end
  138. test "js: Node.nextSibling returns the node immediately following the specified one in its parent's childNodes list, or null if the specified node is the last node in that list (null for documents)", :js, :dom_1_core do
  139. assert_equal 'P', window.evaluate("body.firstChild.nextSibling.nodeName")
  140. end
  141. test "ruby: Node.attributes returns a collection of attributes of the given element", :ruby, :dom_1_core do
  142. assert_equal 'id', div.attributes['id'].name
  143. assert_equal 'foo', div.attributes['id'].value
  144. end
  145. test "js: Node.attributes returns a collection of attributes of the given element", :js, :dom_1_core do
  146. assert_equal 'id', window.evaluate("div.attributes['id'].name")
  147. assert_equal 'foo', window.evaluate("div.attributes['id'].value")
  148. end
  149. test "ruby: Node.ownerDocument returns the top-level document object for this node (null if already is the document)", :ruby, :dom_1_core do
  150. assert_equal document.object_id, div.ownerDocument.object_id
  151. end
  152. test "js: Node.ownerDocument returns the top-level document object for this node (null if already is the document)", :js, :dom_1_core do
  153. assert_equal document.object_id, window.evaluate("div.ownerDocument").object_id
  154. end
  155. test "ruby: Node.insertBefore inserts the specified node before a reference node as a child of the current node", :ruby, :dom_1_core do
  156. body.insertBefore(document.createElement('span'), body.lastChild)
  157. assert_equal %w(DIV SPAN P), body.childNodes.map { |child| child.nodeName }
  158. body.insertBefore(document.createElement('span'), nil)
  159. assert_equal %w(DIV SPAN P SPAN), body.childNodes.map { |child| child.nodeName }
  160. end
  161. def assert_node_names(code, expected_names)
  162. # assert_equal expected_names, window.evaluate("nodeNamesOf(#{code})")
  163. assert_equal expected_names, window.evaluate("body.childNodes").map { |node| node.nodeName }
  164. end
  165. test "js: Node.insertBefore inserts the specified node before a reference node as a child of the current node", :js, :dom_1_core do
  166. window.evaluate("body.insertBefore(document.createElement('span'), body.lastChild)")
  167. assert_node_names "body.childNodes", %w(DIV SPAN P)
  168. end
  169. test "ruby: Node.replaceChild replaces one child node of the specified node with another", :ruby, :dom_1_core do
  170. body.replaceChild(document.createElement('span'), div)
  171. assert_equal %w(SPAN P), body.childNodes.map { |child| child.nodeName }
  172. end
  173. test "js: Node.replaceChild replaces one child node of the specified node with another", :js, :dom_1_core do
  174. window.evaluate("body.replaceChild(document.createElement('span'), div)")
  175. assert_node_names "body.childNodes", %w(SPAN P)
  176. end
  177. test "ruby: Node.appendChild adds a node to the end of the list of children of a specified parent node", :ruby, :dom_1_core do
  178. body.appendChild(document.createElement('span'))
  179. assert_node_names "body.childNodes", %w(DIV P SPAN)
  180. end
  181. test "js: Node.appendChild adds a node to the end of the list of children of a specified parent node", :js, :dom_1_core do
  182. window.evaluate("body.appendChild(document.createElement('span'))")
  183. assert_node_names "body.childNodes", %w(DIV P SPAN)
  184. end
  185. test "ruby: Node.removeChild removes an existing child node from the DOM", :ruby, :dom_1_core do
  186. body.removeChild(div)
  187. assert_equal %w(P), body.childNodes.map { |child| child.nodeName }
  188. end
  189. test "js: Node.removeChild removes an existing child node from the DOM", :js, :dom_1_core do
  190. window.evaluate("body.removeChild(div)")
  191. assert_node_names "body.childNodes", %w(P)
  192. end
  193. test "ruby: Node.removeChild removes a new child node from the DOM", :ruby, :dom_1_core do
  194. a = document.createElement('a')
  195. document.body.appendChild(a)
  196. document.body.removeChild(a)
  197. assert_equal %w(DIV P), body.childNodes.map { |child| child.nodeName }
  198. end
  199. test "js: Node.removeChild removes a new child node from the DOM", :js, :dom_1_core do
  200. window.evaluate <<-js
  201. a = document.createElement('a')
  202. document.body.appendChild(a)
  203. document.body.removeChild(a)
  204. js
  205. assert_equal %w(DIV P), window.evaluate("body.childNodes").map { |node| node.nodeName }
  206. # assert_equal %w(DIV P), window.evaluate("nodeNamesOf(body.childNodes)").to_a
  207. end
  208. test "ruby: Node.hasChildNodes returns a Boolean value indicating whether the current element has child nodes or not", :ruby, :dom_1_core do
  209. assert body.hasChildNodes
  210. assert !div.firstChild.hasChildNodes
  211. end
  212. test "js: Node.hasChildNodes returns a Boolean value indicating whether the current element has child nodes or not", :js, :dom_1_core do
  213. assert window.evaluate("body.hasChildNodes()")
  214. assert window.evaluate("!div.firstChild.hasChildNodes()")
  215. end
  216. test "ruby: Node.cloneNode makes a copy of a node or document", :ruby, :dom_1_core do
  217. assert_equal '<body></body>', body.cloneNode.to_s
  218. assert_equal 'FOO', body.cloneNode(true).firstChild.textContent
  219. assert div.object_id != div.cloneNode.object_id
  220. end
  221. test "js: Node.cloneNode makes a copy of a node or document", :js, :dom_1_core do
  222. assert_equal 'FOO', window.evaluate("body.cloneNode(true).firstChild.textContent")
  223. end
  224. # DOM-Level-2-Core
  225. # http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html
  226. # test "ruby: Node.normalize normalizes the node or document", :ruby, :dom_2_core do
  227. # end
  228. #
  229. # test "ruby: Node.isSupported tests whether the DOM implementation implements a specific feature and that feature is supported by this node or document", :ruby, :dom_2_core do
  230. # end
  231. #
  232. # test "ruby: Node.namespaceURI returns the XML namespace of the current document", :ruby, :dom_2_core do
  233. # end
  234. #
  235. # test "ruby: Node.prefix returns the namespace prefix of the specified node, or null if no prefix is specified", :ruby, :dom_2_core do
  236. # end
  237. #
  238. # test "ruby: Node.localName returns the local part of the qualified name of this node (null for a document)", :ruby, :dom_2_core do
  239. # end
  240. test "ruby: Node.hasAttributes indicates whether the node possesses attributes", :ruby, :dom_2_core do
  241. assert div.hasAttributes
  242. assert !body.hasAttributes
  243. end
  244. test "js: Node.hasAttributes indicates whether the node possesses attributes", :js, :dom_2_core do
  245. assert window.evaluate("div.hasAttributes()")
  246. assert !window.evaluate("body.hasAttributes()")
  247. end
  248. # http://www.quirksmode.org/dom/tests/basics.html
  249. test 'ruby: compareDocumentPosition quirksmode test' do
  250. window.load <<-html
  251. <p id="test" class="testClass">Test <code>&lt;p&gt;</code> with <code>id="test"</code>
  252. and <code>class="testClass"</code>. It contains a
  253. <b id="testB"><code>&lt;b&gt;</code> with id="testB"</b>.</p>
  254. <p id="test2" class="nonsense testClass"><code>&lt;p&gt;</code> with id="test2" and
  255. class="nonsense testClass"</p>
  256. html
  257. x = window.document.getElementById('test')
  258. y = window.document.getElementById('test2')
  259. z = window.document.getElementById('testB')
  260. assert_equal 4, x.compareDocumentPosition(y)
  261. assert_equal 20, x.compareDocumentPosition(z)
  262. end
  263. # http://plugins.jquery.com/project/compareDocumentPosition
  264. test 'ruby: compareDocumentPosition jquery plugin test' do
  265. window.load <<-html
  266. <div id="div1"><p id="p1">Hello, world</p><p id="p2"></p></div><p id="p3"></p>
  267. html
  268. div1 = window.document.getElementById('div1')
  269. p1 = window.document.getElementById('p1')
  270. p2 = window.document.getElementById('p2')
  271. p3 = window.document.getElementById('p3')
  272. assert_equal 20, div1.compareDocumentPosition(p1)
  273. assert_equal 10, p1.compareDocumentPosition(div1)
  274. assert_equal 4, p1.compareDocumentPosition(p2)
  275. assert_equal 2, p3.compareDocumentPosition(p1)
  276. assert_equal 0, p1.compareDocumentPosition(p1)
  277. assert_equal 10, p1.firstChild.compareDocumentPosition(div1)
  278. end
  279. test "ruby: Nokogiri <=>" do
  280. window.load <<-html
  281. <html>
  282. <body>
  283. <div id='a'>
  284. <div id='aa'><div id='aaa'></div></div>
  285. <div id='ab'></div>
  286. </div>
  287. <div id='b'>
  288. <div id='ba'><div id='baa'></div></div>
  289. <div id='bb'></div>
  290. </div>
  291. </body>
  292. </html>
  293. html
  294. node = lambda { |id| window.document.getElementById(id) }
  295. assert node.call('a') <=> node.call('aa')
  296. assert node.call('a') <=> node.call('aaa')
  297. assert node.call('a') <=> node.call('ab')
  298. assert node.call('a') <=> node.call('b')
  299. assert node.call('a') <=> node.call('ba')
  300. assert node.call('a') <=> node.call('baa')
  301. assert node.call('a') <=> node.call('bb')
  302. assert node.call('aa') <=> node.call('aaa')
  303. assert node.call('aa') <=> node.call('ab')
  304. assert node.call('aa') <=> node.call('b')
  305. assert node.call('aa') <=> node.call('ba')
  306. assert node.call('aa') <=> node.call('baa')
  307. assert node.call('aa') <=> node.call('bb')
  308. assert node.call('aaa') <=> node.call('ab')
  309. assert node.call('aaa') <=> node.call('b')
  310. assert node.call('aaa') <=> node.call('ba')
  311. assert node.call('aaa') <=> node.call('baa')
  312. assert node.call('aaa') <=> node.call('bb')
  313. assert node.call('ab') <=> node.call('b')
  314. assert node.call('ab') <=> node.call('ba')
  315. assert node.call('ab') <=> node.call('baa')
  316. assert node.call('ab') <=> node.call('bb')
  317. assert node.call('b') <=> node.call('ba')
  318. assert node.call('b') <=> node.call('baa')
  319. assert node.call('b') <=> node.call('bb')
  320. assert node.call('ba') <=> node.call('baa')
  321. assert node.call('ba') <=> node.call('bb')
  322. assert node.call('baa') <=> node.call('bb')
  323. end
  324. end