Skip to content

Commit 2fb673d

Browse files
committed
Create and use new rspec matcher 'match_html'
This introduces match_html, which converts actual and expected HTML strings into "canonical" HTML using Nokogiri with NOBLANKS and COMPACT, and then does a simple equality comparison. This eliminates whitespace differences introduced by library changes (e.g. the libxml2 2.9.0 change). pretty_text_spec.rb has been changed to use match_html where appropriate. and all tests pass under libxml2 2.7.6, 2.8.0 or 2.9.0
1 parent 9b48795 commit 2fb673d

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

spec/components/pretty_text_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,72 @@
77
it "should support github style code blocks" do
88
PrettyText.cook("```
99
test
10-
```").should == "<pre><code class=\"lang-auto\">test \n</code></pre>"
10+
```").should match_html "<pre><code class=\"lang-auto\">test \n</code></pre>"
1111
end
1212

1313
it "should support quoting [] " do
1414
PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"][sam][/quote]").should =~ /\[sam\]/
1515
end
1616

1717
it "produces a quote even with new lines in it" do
18-
PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd\n[/quote]").should == "<p></p><aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img width=\"20\" height=\"20\" src=\"/users/eviltrout/avatar/40?__ws=http%3A%2F%2Ftest.localhost\" class=\"avatar \" title=\"\">\n EvilTrout\n said:\n </div>\n <blockquote>ddd</blockquote>\n</aside><p> </p>"
18+
PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd\n[/quote]").should match_html "<p></p><aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img width=\"20\" height=\"20\" src=\"/users/eviltrout/avatar/40?__ws=http%3A%2F%2Ftest.localhost\" class=\"avatar \" title=\"\">\n EvilTrout\n said:\n </div>\n <blockquote>ddd</blockquote>\n</aside><p> </p>"
1919
end
2020

2121
it "should produce a quote" do
22-
PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd[/quote]").should == "<p></p><aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img width=\"20\" height=\"20\" src=\"/users/eviltrout/avatar/40?__ws=http%3A%2F%2Ftest.localhost\" class=\"avatar \" title=\"\">\n EvilTrout\n said:\n </div>\n <blockquote>ddd</blockquote>\n</aside><p> </p>"
22+
PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd[/quote]").should match_html "<p></p><aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img width=\"20\" height=\"20\" src=\"/users/eviltrout/avatar/40?__ws=http%3A%2F%2Ftest.localhost\" class=\"avatar \" title=\"\">\n EvilTrout\n said:\n </div>\n <blockquote>ddd</blockquote>\n</aside><p> </p>"
2323
end
2424

2525
it "trims spaces on quote params" do
26-
PrettyText.cook("[quote=\"EvilTrout, post:555, topic: 666\"]ddd[/quote]").should == "<p></p><aside class=\"quote\" data-post=\"555\" data-topic=\"666\"><div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img width=\"20\" height=\"20\" src=\"/users/eviltrout/avatar/40?__ws=http%3A%2F%2Ftest.localhost\" class=\"avatar \" title=\"\">\n EvilTrout\n said:\n </div>\n <blockquote>ddd</blockquote>\n</aside><p> </p>"
26+
PrettyText.cook("[quote=\"EvilTrout, post:555, topic: 666\"]ddd[/quote]").should match_html "<p></p><aside class=\"quote\" data-post=\"555\" data-topic=\"666\"><div class=\"title\">\n <div class=\"quote-controls\"></div>\n <img width=\"20\" height=\"20\" src=\"/users/eviltrout/avatar/40?__ws=http%3A%2F%2Ftest.localhost\" class=\"avatar \" title=\"\">\n EvilTrout\n said:\n </div>\n <blockquote>ddd</blockquote>\n</aside><p> </p>"
2727
end
2828

2929

3030
it "should handle 3 mentions in a row" do
31-
PrettyText.cook('@hello @hello @hello').should == "<p><span class=\"mention\">@hello</span> <span class=\"mention\">@hello</span> <span class=\"mention\">@hello</span></p>"
31+
PrettyText.cook('@hello @hello @hello').should match_html "<p><span class=\"mention\">@hello</span> <span class=\"mention\">@hello</span> <span class=\"mention\">@hello</span></p>"
3232
end
3333

3434
it "should not do weird @ mention stuff inside a pre block" do
3535

3636
PrettyText.cook("```
3737
a @test
38-
```").should == "<pre><code class=\"lang-auto\">a @test \n</code></pre>"
38+
```").should match_html "<pre><code class=\"lang-auto\">a @test \n</code></pre>"
3939

4040
end
4141

4242
it "should sanitize the html" do
43-
PrettyText.cook("<script>alert(42)</script>").should == "alert(42)"
43+
PrettyText.cook("<script>alert(42)</script>").should match_html "alert(42)"
4444
end
4545

4646
it "should escape html within the code block" do
4747

4848
PrettyText.cook("```text
4949
<header>hello</header>
50-
```").should == "<pre><code class=\"text\">&lt;header&gt;hello&lt;/header&gt; \n</code></pre>"
50+
```").should match_html "<pre><code class=\"text\">&lt;header&gt;hello&lt;/header&gt; \n</code></pre>"
5151
end
5252

5353
it "should support language choices" do
5454

5555
PrettyText.cook("```ruby
5656
test
57-
```").should == "<pre><code class=\"ruby\">test \n</code></pre>"
57+
```").should match_html "<pre><code class=\"ruby\">test \n</code></pre>"
5858
end
5959

6060
it 'should decorate @mentions' do
61-
PrettyText.cook("Hello @eviltrout").should == "<p>Hello <span class=\"mention\">@eviltrout</span></p>"
61+
PrettyText.cook("Hello @eviltrout").should match_html "<p>Hello <span class=\"mention\">@eviltrout</span></p>"
6262
end
6363

6464
it 'should allow for @mentions to have punctuation' do
65-
PrettyText.cook("hello @bob's @bob,@bob; @bob\"").should ==
66-
"<p>hello <span class=\"mention\">@bob</span>'s <span class=\"mention\">@bob</span>,<span class=\"mention\">@bob</span>; <span class=\"mention\">@bob</span>\"</p>"
65+
PrettyText.cook("hello @bob's @bob,@bob; @bob\"").should
66+
match_html "<p>hello <span class=\"mention\">@bob</span>'s <span class=\"mention\">@bob</span>,<span class=\"mention\">@bob</span>; <span class=\"mention\">@bob</span>\"</p>"
6767
end
6868

6969
it 'should add spoiler tags' do
70-
PrettyText.cook("[spoiler]hello[/spoiler]").should == "<p><span class=\"spoiler\">hello</span></p>"
70+
PrettyText.cook("[spoiler]hello[/spoiler]").should match_html "<p><span class=\"spoiler\">hello</span></p>"
7171
end
7272

7373
it "should only detect ``` at the begining of lines" do
7474
PrettyText.cook(" ```\n hello\n ```")
75-
.should == "<pre><code>```\nhello\n```\n</code></pre>"
75+
.should match_html "<pre><code>```\nhello\n```\n</code></pre>"
7676
end
7777
end
7878

spec/support/match_html_matcher.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'nokogiri/xml/parse_options'
2+
RSpec::Matchers.define :match_html do |expected|
3+
match do |actual|
4+
a = make_canonical_html expected
5+
b = make_canonical_html actual
6+
a.to_html == b.to_html
7+
end
8+
9+
failure_message_for_should do |actual|
10+
"after sanitizing for extra white space and compactness, expected #{actual} to match #{expected}"
11+
end
12+
13+
failure_message_for_should_not do |actual|
14+
"after sanitizing for extra white space and compactness, expected #{actual} not to match #{expected}"
15+
end
16+
17+
def make_canonical_html(html)
18+
Nokogiri::HTML(html) { |config| config.options = Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::COMPACT }
19+
end
20+
21+
end

0 commit comments

Comments
 (0)