PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rdom/properties_test.rb

https://gitlab.com/Blueprint-Marketing/rdom
Ruby | 355 lines | 252 code | 68 blank | 35 comment | 7 complexity | 49e62a52868b98da5408c43b8fc884c3 MD5 | raw file
  1. require File.expand_path('../../test_helper', __FILE__)
  2. require 'core_ext/string/titleize'
  3. class PropertiesTest < Test::Unit::TestCase
  4. TEST_ALL_ELEMENTS_PROPERTIES = false
  5. attr_reader :window, :document, :links
  6. def setup
  7. stub_request(:any, /./).to_return { |request| { :body => '' } }
  8. @window = RDom::Window.new
  9. window.load(File.expand_path('../../fixtures/properties.html', __FILE__), :url => 'http://example.org')
  10. window.evaluate('function find_tag(tag_name) { return window.document.getElementsByTagName(tag_name)[0]; }')
  11. @links = window.evaluate('links = document.getElementsByTagName("a");')
  12. end
  13. def find_tag(tag_name)
  14. window.document.getElementsByTagName(tag_name)[0]
  15. end
  16. test "js: attribute values written using . show up in the markup" do
  17. window.evaluate('links[0].href = "bar"')
  18. assert_match /href="bar"/, window.evaluate('links[0]').to_s
  19. # assert_match /href="bar"/, window.evaluate('links[0].toString()') # TODO
  20. end
  21. test "js: attribute values written using [] show up in the markup" do
  22. window.evaluate('links[0]["href"] = "bar"')
  23. assert_match /href="bar"/, window.evaluate('links[0]').to_s
  24. # assert_match /href="bar"/, window.evaluate('links[0].toString()') # TODO
  25. end
  26. test "js: setting a non-standard property: can be read via method call syntax" do
  27. assert_equal 'bar', window.evaluate('links[0].bar = "bar"; links[0].bar')
  28. end
  29. test "js: setting a non-standard property: can be read via hash access syntax" do
  30. assert_equal 'bar', window.evaluate('links[0].bar = "bar"; links[0]["bar"]')
  31. end
  32. test "js: setting a non-standard property: does not push it into the markup" do
  33. assert window.evaluate('links[0].bar = "bar"; links[0]') !~ /bar=/
  34. end
  35. test "js: reading a non-standard property: can be read via method call syntax" do
  36. assert_nil window.evaluate('links[0].bar')
  37. end
  38. test "js: reading a non-standard property: can be read via hash access syntax" do
  39. assert_nil window.evaluate('links[0]["bar"]')
  40. end
  41. # js_property: nodeName
  42. test 'js: tag.nodeName returns "A" for <a title="foo"/>' do
  43. assert_equal 'A', window.evaluate('links[0].nodeName')
  44. end
  45. test 'js: tag["nodeName"] returns "A" for <a title="foo"/>' do
  46. assert_equal 'A', window.evaluate('links[0]["nodeName"]')
  47. end
  48. test 'js: tag.getAttribute("nodeName") returns nil' do
  49. assert_nil window.evaluate('links[0].getAttribute("nodeName")')
  50. end
  51. test 'js: tag.attributes.nodeName returns nil' do
  52. assert_nil window.evaluate('links[0].attributes.nodeName')
  53. end
  54. # standard html attributes
  55. def load_html_tag_with_attribute(tag, attribute = nil, value = nil)
  56. attribute = "#{attribute}=\"#{value}\"" if attribute
  57. html = case tag
  58. when 'head'
  59. "<html><head #{attribute}></head></html>"
  60. when 'body'
  61. "<html><body #{attribute}></body></html>"
  62. else
  63. "<html><body><#{tag} #{attribute} /></body></html>"
  64. end
  65. window.load(html)
  66. end
  67. SKIP_ATTRIBUTES = [:checked, :selected, :readOnly] # these behave differently
  68. ATTRIBUTE_MAP = {
  69. :className => 'class',
  70. :httpEquiv => 'http-equiv',
  71. :acceptCharset => 'accept-charset'
  72. }
  73. def self.test_tags(*tag_names)
  74. tag_names.each { |tag_name| test_tag(tag_name) }
  75. end
  76. def self.test_tag(tag_name)
  77. attributes = RDom::Element.const_get(tag_name.titleize).ancestors.uniq.map do |const|
  78. const.const_defined?(:HTML_ATTRIBUTES) ? const.const_get(:HTML_ATTRIBUTES) : []
  79. end.flatten.uniq
  80. attributes += [:className]
  81. attributes.each do |dom_name|
  82. unless SKIP_ATTRIBUTES.include?(dom_name)
  83. test_attribute(tag_name, dom_name.to_s, ATTRIBUTE_MAP[dom_name])
  84. end
  85. end
  86. end
  87. def self.test_attribute(tag_name, dom_name, html_name = nil)
  88. html_name ||= dom_name.to_s.downcase
  89. # TODO tons of exceptions. who the fuck came up with that mess called DOM?
  90. return if RDom::Attr.numeric?(dom_name) || RDom::Attr.boolean?(dom_name)
  91. return if dom_name == 'style'
  92. if %w(href src).include?(dom_name)
  93. html_value = "http://example.org/foo"
  94. expect_value = html_value
  95. empty_value = ''
  96. # elsif dom_name == 'style'
  97. # html_value = 'font-size: 1px;'
  98. # expect_value = RDom::Css::StyleDeclaration.new(nil, html_value)
  99. # empty_value = {}
  100. else
  101. html_value = 'foo'
  102. expect_value = 'foo'
  103. empty_value = ''
  104. end
  105. test "js: #{tag_name} - given the #{dom_name} attribute is set: element.#{dom_name} returns the string value" do
  106. load_html_tag_with_attribute(tag_name, html_name, html_value)
  107. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').#{dom_name}")
  108. end
  109. test "js: #{tag_name} - given the #{dom_name} attribute is set: element['#{dom_name}'] returns the string value" do
  110. load_html_tag_with_attribute(tag_name, html_name, html_value)
  111. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}')['#{dom_name}']")
  112. end
  113. test "js: #{tag_name} - given the #{dom_name} attribute is set: element.getAttribute('#{html_name}') returns the string value" do
  114. load_html_tag_with_attribute(tag_name, html_name, html_value)
  115. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').getAttribute('#{html_name}')")
  116. end
  117. test "js: #{tag_name} - given the #{dom_name} attribute is set: element.attributes.#{html_name} returns the attribute" do
  118. load_html_tag_with_attribute(tag_name, html_name, html_value)
  119. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes.#{html_name}").value
  120. end
  121. test "js: #{tag_name} - given the #{dom_name} attribute is set: element.attributes['#{html_name}'] returns the attribute" do
  122. load_html_tag_with_attribute(tag_name, html_name, html_value)
  123. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes['#{html_name}']").value
  124. end
  125. test "js: #{tag_name} - given the #{dom_name} attribute is an empty string: element.#{dom_name} returns an empty #{empty_value.ruby_class.name}" do
  126. load_html_tag_with_attribute(tag_name, html_name, '')
  127. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}').#{dom_name}")
  128. end
  129. test "js: #{tag_name} - given the #{dom_name} attribute is an empty string: element['#{dom_name}'] returns an empty #{empty_value.ruby_class.name}" do
  130. load_html_tag_with_attribute(tag_name, html_name, '')
  131. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}')['#{dom_name}']")
  132. end
  133. test "js: #{tag_name} - given the #{dom_name} attribute is an empty string: element.getAttribute('#{html_name}') returns an empty #{empty_value.ruby_class.name}" do
  134. load_html_tag_with_attribute(tag_name, html_name, '')
  135. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}').getAttribute('#{html_name}')")
  136. end
  137. test "js: #{tag_name} - given the #{dom_name} attribute is an empty string: element.attributes.#{html_name} returns the attribute" do
  138. load_html_tag_with_attribute(tag_name, html_name, '')
  139. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}').attributes.#{html_name}").value
  140. end
  141. test "js: #{tag_name} - given the #{dom_name} attribute is an empty string: element.attributes['#{html_name}'] returns the attribute" do
  142. load_html_tag_with_attribute(tag_name, html_name, '')
  143. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}').attributes['#{html_name}']").value
  144. end
  145. test "js: #{tag_name} - given the #{dom_name} attribute is not set: element.#{dom_name} returns an empty string" do
  146. load_html_tag_with_attribute(tag_name)
  147. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}').#{dom_name}")
  148. end
  149. test "js: #{tag_name} - given the #{dom_name} attribute is not set: element['#{dom_name}'] returns an empty string" do
  150. load_html_tag_with_attribute(tag_name)
  151. assert_equal empty_value, window.evaluate("find_tag('#{tag_name}')['#{dom_name}']")
  152. end
  153. test "js: #{tag_name} - given the #{dom_name} attribute is not set: element.getAttribute('#{html_name}') returns nil" do
  154. load_html_tag_with_attribute(tag_name)
  155. assert_nil window.evaluate("find_tag('#{tag_name}').getAttribute('#{html_name}')")
  156. end
  157. test "js: #{tag_name} - given the #{dom_name} attribute is not set: element.attributes.#{html_name} returns nil" do
  158. load_html_tag_with_attribute(tag_name)
  159. assert_nil window.evaluate("find_tag('#{tag_name}').attributes.#{html_name}")
  160. end
  161. test "js: #{tag_name} - given the #{dom_name} attribute is not set: element.attributes['#{html_name}'] returns nil" do
  162. load_html_tag_with_attribute(tag_name)
  163. assert_nil window.evaluate("find_tag('#{tag_name}').attributes['#{html_name}']")
  164. end
  165. # test "ruby: #{tag_name} - given the #{dom_name} attribute is set: element.attributes.#{html_name} returns the attribute" do
  166. # assert_equal expect_value, find_tag(tag_name).attributes.send(html_name).value
  167. # end
  168. unless dom_name == 'style'
  169. test "js: #{tag_name} - can set the #{dom_name} attribute using: element.#{dom_name} = #{html_value.inspect}" do
  170. load_html_tag_with_attribute(tag_name)
  171. window.evaluate("find_tag('#{tag_name}').#{dom_name} = #{html_value.inspect}")
  172. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').#{dom_name}")
  173. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}')['#{dom_name}']")
  174. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').getAttribute('#{html_name}')")
  175. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes.#{html_name}").value
  176. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes['#{html_name}']").value
  177. end
  178. test "js: #{tag_name} - can set the #{dom_name} attribute using: element['#{dom_name}'] = #{html_value.inspect}" do
  179. load_html_tag_with_attribute(tag_name)
  180. window.evaluate("find_tag('#{tag_name}')['#{dom_name}'] = #{html_value.inspect}")
  181. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').#{dom_name}")
  182. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}')['#{dom_name}']")
  183. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').getAttribute('#{html_name}')")
  184. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes.#{html_name}").value
  185. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes['#{html_name}']").value
  186. end
  187. test "js: #{tag_name} - can set the #{dom_name} attribute using: element.setAttribute('#{html_name}', #{html_value.inspect})" do
  188. load_html_tag_with_attribute(tag_name)
  189. window.evaluate("find_tag('#{tag_name}').setAttribute('#{html_name}', #{html_value.inspect})")
  190. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').#{dom_name}")
  191. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}')['#{dom_name}']")
  192. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').getAttribute('#{html_name}')")
  193. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes.#{html_name}").value
  194. assert_equal expect_value, window.evaluate("find_tag('#{tag_name}').attributes['#{html_name}']").value
  195. end
  196. end
  197. end
  198. if TEST_ALL_ELEMENTS_PROPERTIES
  199. const_names = RDom::Element.constants - %w(ATTRS_CORE ATTRS_I18N ATTRS_EVENTS HTML_ATTRIBUTES)
  200. test_tags *const_names.map { |name| name.downcase }
  201. else
  202. test_tag('a')
  203. end
  204. # test_attribute('script', 'src')
  205. # test_attribute('a', 'className', 'class')
  206. # test_attribute('a', 'style')
  207. # using a non-standard attribute
  208. test "js: a - given the foo attribute is set: element.foo returns nil" do
  209. load_html_tag_with_attribute('a', 'foo', 'foo')
  210. assert_nil window.evaluate("find_tag('a').foo")
  211. end
  212. test "js: a - given the foo attribute is set: element['foo'] returns nil" do
  213. load_html_tag_with_attribute('a', 'foo', 'foo')
  214. assert_nil window.evaluate("find_tag('a')['foo']")
  215. end
  216. test "js: a - given the foo attribute is set: element.getAttribute('foo') returns the string value" do
  217. load_html_tag_with_attribute('a', 'foo', 'foo')
  218. assert_equal "foo", window.evaluate("find_tag('a').getAttribute('foo')")
  219. end
  220. test "js: a - given the foo attribute is set: element.attributes.foo returns the attribute" do
  221. load_html_tag_with_attribute('a', 'foo', 'foo')
  222. assert_equal "foo", window.evaluate("find_tag('a').attributes.foo").value
  223. end
  224. test "js: a - given the foo attribute is set: element.attributes['foo'] returns the attribute" do
  225. load_html_tag_with_attribute('a', 'foo', 'foo')
  226. assert_equal "foo", window.evaluate("find_tag('a').attributes['foo']").value
  227. end
  228. test "js: a - given the foo attribute is an empty string: element.foo returns nil" do
  229. load_html_tag_with_attribute('a', 'foo', '')
  230. assert_nil window.evaluate("find_tag('a').foo")
  231. end
  232. test "js: a - given the foo attribute is an empty string: element['foo'] returns nil" do
  233. load_html_tag_with_attribute('a', 'foo', '')
  234. assert_nil window.evaluate("find_tag('a')['foo']")
  235. end
  236. test "js: a - given the foo attribute is an empty string: element.getAttribute('foo') returns an empty string" do
  237. load_html_tag_with_attribute('a', 'foo', '')
  238. assert_equal "", window.evaluate("find_tag('a').getAttribute('foo')")
  239. end
  240. test "js: a - given the foo attribute is an empty string: element.attributes.foo returns the attribute" do
  241. load_html_tag_with_attribute('a', 'foo', '')
  242. assert_equal "", window.evaluate("find_tag('a').attributes.foo").value
  243. end
  244. test "js: a - given the foo attribute is an empty string: element.attributes['foo'] returns the attribute" do
  245. load_html_tag_with_attribute('a', 'foo', '')
  246. assert_equal "", window.evaluate("find_tag('a').attributes['foo']").value
  247. end
  248. test "js: a - given the foo attribute is not set: element.foo returns nil" do
  249. load_html_tag_with_attribute('a')
  250. assert_nil window.evaluate("find_tag('a').foo")
  251. end
  252. test "js: a - given the foo attribute is not set: element['foo'] returns nil" do
  253. load_html_tag_with_attribute('a')
  254. assert_nil window.evaluate("find_tag('a')['foo']")
  255. end
  256. test "js: a - given the foo attribute is not set: getAttribute('foo') returns nil" do
  257. load_html_tag_with_attribute('a')
  258. assert_nil window.evaluate("find_tag('a').getAttribute('foo')")
  259. end
  260. test "js: a - given the foo attribute is not set: attributes.foo returns nil" do
  261. load_html_tag_with_attribute('a')
  262. assert_nil window.evaluate("find_tag('a').attributes.foo")
  263. end
  264. test "js: a - given the foo attribute is not set: attributes['foo'] returns nil" do
  265. load_html_tag_with_attribute('a')
  266. assert_nil window.evaluate("find_tag('a').attributes['foo']")
  267. end
  268. # special case href
  269. #
  270. # test 'js: anchor.href returns "http://example.org/index.html#foo" for <a href="#foo"/>' do
  271. # end
  272. #
  273. # test 'js: anchor["href"] returns "http://example.org/index.html#foo" for <a href="#foo"/>' do
  274. # end
  275. #
  276. # test 'js: anchor.href returns "http://example.org/index.html" for <a href=""/>' do
  277. # end
  278. #
  279. # test 'js: anchor["href"] returns "http://example.org/index.html" for <a href=""/>' do
  280. # end
  281. #
  282. # test 'js: anchor.href returns "" for <a/>' do
  283. # end
  284. #
  285. # test 'js: anchor["href"] returns "" for <a/>' do
  286. # end
  287. end