rails/rails-dom-testing

What is equal?

kwerle opened this issue · 1 comments

kwerle commented
compare_doms(Nokogiri.HTML5("<foo a=1 b=2/>"), Nokogiri.HTML5("<Foo b=2 a=1/>"), nil)
    ==>false

Is that really the case? Are dom attributes order meaningful?

DOM attributes are not meaningful:

#! /usr/bin/env ruby

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "rails-dom-testing", path: "."
end

require "minitest/autorun"
require "rails/dom/testing"

Rails::Dom::Testing.default_html_version = :html5

class Foo < Minitest::Test
  include Rails::Dom::Testing::Assertions

  def test_attribute_order_does_not_matter
    assert_dom_equal("<foo a='1' b='2'></foo>", "<foo b='2' a='1'></foo>")
  end
end

outputs

# Running:

.

Finished in 0.003215s, 311.0529 runs/s, 311.0529 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

The issue you're running into is how <foo a=1/> is parsed, let's look at the actual DOM objects created by Nokogiri:

pp Nokogiri::HTML5.fragment("<foo a=1/>")
# => #(DocumentFragment:0x488 {
#      name = "#document-fragment",
#      children = [ #(Element:0x49c { name = "foo", attribute_nodes = [ #(Attr:0x4b0 { name = "a", value = "1/" })] })]
#      })

Note that the attribute "a"'s value is "1/". So in the case you've originally posted, the two DOMs are not equal because the values of the attributes are different, not because they're in a different order.

Hope this helps.