kucaahbe/rspec-html-matchers

Comparing HTML text but ignore attribute order

Closed this issue · 5 comments

Is it possible to compare two HTML strings, but ignore the order of any attributes in tags? For example:

expected: '<pre><code class="language-ruby" data-meta="test">module Foo\n</code></pre>'
     got: '<pre><code data-meta="test" class="language-ruby">module Foo\n</code></pre>'

Sometimes the order in which attributes are added to generated HTML might change. The order doesn't actually matter, so I'd like the test to be robust enough to ignore attribute order.

The problem is the expected html is coming from a file, which can't be changed.

I believe it is the default behavior to not take the order of attributes into account

I guess what I'm looking for something like this:

expect("<pre><code class="language-ruby" data-meta="test">module Foo\n</code></pre>").to have_html("'<pre><code data-meta="test" class="language-ruby">module Foo\n</code></pre>"

comparing an html string to another html string, ignoring the order of attributes. The expected values are pulled from a file, which I can't really change. So I can't go checking for specific tags.

there is no have_html mechanics. But smth. like this could be functionally equivalent to what you (I guess) want:

expect("<pre><code class="language-ruby" data-meta="test">module Foo\n</code></pre>")
  .to have_tag 'pre code', with: {'data-meta': 'test', class: 'ruby'}

pls note this code is just illustrating the idea that you can write matcher for tag and its attributes. Use the documentation to adjust it for your needs

Yeah since the expected html is being provided as a list in a file, there is not really a way to target specific tags and attributes.

I appreciate your responses. I'm using have_tag for other tests, so I'll just work around this particular scenario.