<#{tag_name}>/*</#{tag_name}><img src onerror=alert(1)>*/")
expect(content).to eq("")
end
end
end
context "when given an tag with a relative src" do
it "converts the src value to an absolute URL" do
content = sanitize_value(field, " ")
expect(content).to eq("
")
end
end
context "when given an tag with an absolute src" do
it "doesn't modify the src value" do
content = sanitize_value(field, " ")
expect(content).to eq("
")
end
end
end
end
ArchiveConfig.FIELDS_ALLOWING_HTML.each do |field|
it "preserves ruby-annotated HTML in #{field}" do
result = sanitize_value(field, "BigText( small_text ) ")
expect(result).to include("BigText( small_text ) ")
end
it "preserves ruby-annotated HTML without rp in #{field}" do
result = sanitize_value(field, "BigTextsmall_text ")
expect(result).to include("BigTextsmall_text ")
end
it "transforms open attribute's value when present on details element in #{field}" do
html = <<~HTML
Automated Status: Operational
Velocity: 12m/s
Direction: North
HTML
result = sanitize_value(field, html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./details/@open").to_s.strip).to eq("open")
end
it "does not require details to have an 'open' attribute in #{field}" do
html = <<~HTML
Automated Status: Operational
Velocity: 12m/s
Direction: North
HTML
result = sanitize_value(field, html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./details[@open]")).to be_empty
end
end
end
describe "fix_bad_characters" do
it "should not touch normal text" do
expect(fix_bad_characters("normal text")).to eq("normal text")
end
it "should not touch normal text with valid unicode chars" do
expect(fix_bad_characters("„‚nörmäl’—téxt‘“")).to eq("„‚nörmäl’—téxt‘“")
end
it "does not touch zero-width non-joiner" do
string = ["A".ord, 0x200C, "A".ord] # "A[zwnj]A"
expect(fix_bad_characters(string.pack("U*")).unpack("U*")).to eq(string)
end
it "does not touch zero-width joiner" do
string = ["A".ord, 0x200D, "A".ord] # "A[zwj]A"
expect(fix_bad_characters(string.pack("U*")).unpack("U*")).to eq(string)
end
it "does not touch word joiner" do
string = ["A".ord, 0x2060, "A".ord] # "A[wj]A"
expect(fix_bad_characters(string.pack("U*")).unpack("U*")).to eq(string)
end
it "should remove invalid unicode chars" do
bad_string = [65, 150, 65].pack("C*") # => "A\226A"
expect(fix_bad_characters(bad_string)).to eq("AA")
end
it "should escape <3" do
expect(fix_bad_characters("normal <3 text")).to eq("normal <3 text")
end
it "should convert \\r\\n to \\n" do
expect(fix_bad_characters("normal\r\ntext")).to eq("normal\ntext")
end
it "should remove the spacer" do
expect(fix_bad_characters("A____spacer____A")).to eq("AA")
end
end
describe "add_paragraphs_to_text" do
%w[a abbr acronym address].each do |tag|
it "does not add extraneous paragraph breaks after #{tag} tags" do
result = add_paragraphs_to_text("<#{tag}>quack#{tag}> quack")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath(".//p").size).to eq(1)
expect(doc.xpath(".//br")).to be_empty
end
end
it "leaves audio tags alone" do
html = "\n "
result = add_paragraphs_to_text(html)
expect(result).not_to match("")
expect(result).not_to match(" ")
expect(result).not_to match(" name")
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./p/a/img").size).to eq(1)
end
it "does not convert linebreaks after p tags" do
result = add_paragraphs_to_text("
A
\nB
\n\nC
\n\n\n")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p").size).to eq(3)
expect(doc.xpath(".//br")).to be_empty
end
it "does not convert linebreaks after tables" do
result = add_paragraphs_to_text("#{one_cell_table('A')}\n#{one_cell_table('A')}\n\n#{one_cell_table('A')}\n\n\n")
expect(result).not_to match("")
expect(result).not_to match(" A#{tag}>\n<#{tag}>B#{tag}>\n\n<#{tag}>C#{tag}>\n\n\n")
expect(result).not_to match("
")
expect(result).not_to match(" A#{tag}>\n<#{tag}>B#{tag}>\n\n<#{tag}>C#{tag}>\n\n\n")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./#{tag}/p").size).to eq(3)
expect(doc.xpath(".//br")).to be_empty
end
it "does not wrap #{tag} tag with a paragraph" do
result = add_paragraphs_to_text("<#{tag}>A#{tag}>\n
B
")
# This needs XML parsing because HTML5 parser might hide failures
# by reinterpreting
_
as
_
doc = Nokogiri::XML.fragment(result)
expect(doc.xpath("./p").size).to eq(1)
expect(doc.xpath("./p/#{tag}").size).to eq(0)
end
it "wraps content inside of nested #{tag} tags with a paragraph" do
result = add_paragraphs_to_text("<#{tag}><#{tag}>A#{tag}>#{tag}>")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath(".//p").size).to eq(1)
expect(doc.xpath("./#{tag}/#{tag}/p/node()").to_s).to eq("A")
end
it "does not wrap paragraphs inside of nested #{tag} tags" do
result = add_paragraphs_to_text("<#{tag}><#{tag}><#{tag}>A
#{tag}>B
#{tag}>#{tag}>")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath(".//p").size).to eq(2)
expect(doc.xpath("./#{tag}/#{tag}/#{tag}/p/node()").to_s).to eq("A")
expect(doc.xpath("./#{tag}/#{tag}/p/node()").to_s).to eq("B")
end
it "does not add paragraphs between a #{tag} tag and a paragraph" do
result = add_paragraphs_to_text("<#{tag}>A#{tag}>\n\nB
")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./#{tag}/following-sibling::p/node()").to_s).to eq("B")
end
end
it "does not convert linebreaks after br tags" do
result = add_paragraphs_to_text("A B \n\nC \n\n\n")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p").size).to eq(1)
expect(doc.xpath(".//br").size).to eq(3)
end
it "does not convert linebreaks after hr tags" do
result = add_paragraphs_to_text("A B \n\nC \n\n\n")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p").size).to eq(3)
expect(doc.xpath(".//br")).to be_empty
end
it "does not wrap table in p tags" do
result = add_paragraphs_to_text("aa #{one_cell_table('foo')} bb")
# This needs XML parsing because HTML5 parser might hide issues:
# Nokogiri::HTML5.fragment('aa
bb').to_s
# "aa
bb
"
doc = Nokogiri::XML.fragment(result)
expect(doc.xpath(".//p").size).to eq(2)
expect(doc.xpath("./table").size).to eq(1)
end
%w[figure dl h1 h2 h3 h4 h5 h6 ol pre summary ul].each do |tag|
it "does not wrap #{tag} in p tags" do
result = add_paragraphs_to_text("aa <#{tag}>foo#{tag}> bb")
# This needs XML parsing because HTML5 parser might hide failures
# by reinterpreting
_ as
_
doc = Nokogiri::XML.fragment(result)
expect(doc.xpath(".//p").size).to eq(2)
expect(doc.xpath("./#{tag}/node()").to_s.strip).to eq("foo")
end
end
it "does not wrap details in p tags" do
html = <<~HTML
aa
Automated Status: Operational
Velocity: 12m/s
Direction: North
bb
HTML
result = add_paragraphs_to_text(html)
# This needs XML parsing because HTML5 parser might hide failures
# by reinterpreting _
as
_
doc = Nokogiri::XML.fragment(result)
# aa, velocity..., direction..., bb
expect(doc.xpath(".//p").size).to eq(4)
expect(doc.xpath("./p/details").size).to eq(0)
expect(doc.xpath("./details/p").size).to eq(2)
expect(doc.xpath("./p").size).to eq(2)
expect(doc.xpath("./p[1]/text()").to_s).to eq("aa")
expect(doc.xpath("./p[2]/text()").to_s).to eq("bb")
end
%w[ol ul].each do |tag|
it "does not convert linebreaks inside #{tag} lists" do
html = <<~HTML
<#{tag}>
A
B
#{tag}>
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./#{tag}/li[1]/node()").to_s.strip).to eq("A")
expect(doc.xpath("./#{tag}/li[2]/node()").to_s.strip).to eq("B")
expect(doc.xpath(".//br")).to be_empty
end
end
it "does not convert linebreaks inside tables" do
html = <<~TABLE
TABLE
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./table/tbody/tr[1]/th[1]/node()").to_s.strip).to eq("A")
expect(doc.xpath("./table/tbody/tr[1]/th[2]/node()").to_s.strip).to eq("B")
expect(doc.xpath("./table/tbody/tr[2]/td[1]/node()").to_s.strip).to eq("C")
expect(doc.xpath("./table/tbody/tr[2]/td[2]/node()").to_s.strip).to eq("D")
expect(doc.xpath(".//br")).to be_empty
end
it "does not convert linebreaks inside definition lists" do
html = <<~HTML
A
aaa
B
bbb
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./dl/dt[1]/node()").to_s.strip).to eq("A")
expect(doc.xpath("./dl/dd[1]/node()").to_s.strip).to eq("aaa")
expect(doc.xpath("./dl/dt[2]/node()").to_s.strip).to eq("B")
expect(doc.xpath("./dl/dd[2]/node()").to_s.strip).to eq("bbb")
expect(doc.xpath(".//br")).to be_empty
end
it "does not add paragraphs inside summary" do
html = <<~HTML
Automated
Status:
Operational
Velocity: 12m/s
Direction: North
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./summary/p")).to be_empty
end
it "does not add paragraphs inside figure" do
html = <<~HTML
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./figure/p")).to be_empty
end
it "allows alt and title attributes on elements inside figure" do
html = <<~HTML
Take picture here
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./figure/img/@alt").to_s.strip).to eq("camera icon")
expect(doc.xpath("./figure/figcaption/@title").to_s.strip).to eq("here is title")
end
it "allows other HTML elements inside figcaption" do
html = <<~HTML
Take picture here
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./figure/figcaption/em/text()").to_s.strip).to eq("Take picture")
expect(doc.xpath("./figure/figcaption/em/a/text()").to_s.strip).to eq("here")
expect(doc.xpath("./figure/figcaption/em/a/@href").to_s.strip).to eq("http://example.com/link")
end
it "allows other HTML elements inside summary" do
html = <<~HTML
Automated Status: Operational
Velocity: 12m/s
Direction: North
HTML
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./details/summary/em/text()").to_s.strip).to eq("Automated Status:")
expect(doc.xpath("./details/summary/em/a/text()").to_s.strip).to eq("Operational")
expect(doc.xpath("./details/summary/em/a/@href").to_s.strip).to eq("http://example.com/link")
end
%w[address h1 h2 h3 h4 h5 h6 p pre].each do |tag|
it "does not wrap in p and not convert linebreaks inside #{tag} tags" do
result = add_paragraphs_to_text("<#{tag}>A\nB\n\nC\n\n\nD#{tag}>")
# This needs XML parsing because HTML5 parser might hide failures
# by reinterpreting
_ as
_
doc = Nokogiri::XML.fragment(result)
expect(doc.xpath("./#{tag}[1]/node()").to_s.strip).to eq("A\nB\n\nC\n\n\nD")
end
end
%w[a abbr acronym].each do |tag|
it "wraps in p and not convert linebreaks inside #{tag} tags" do
result = add_paragraphs_to_text("<#{tag}>A\nB\n\nC\n\n\nD#{tag}>")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p/#{tag}[1]/node()").to_s.strip).to eq("A\nB\n\nC\n\n\nD")
end
end
it "wraps plain text in p tags" do
result = add_paragraphs_to_text("some text")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("some text")
end
it "converts single linebreak to br" do
result = add_paragraphs_to_text("some\ntext")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to match(%r{some \ntext})
end
it "converts double linebreaks to paragraph break" do
result = add_paragraphs_to_text("some\n\ntext")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/node()").to_s.strip).to eq("text")
end
it "converts triple linebreaks into blank paragraph" do
result = add_paragraphs_to_text("some\n\n\ntext")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/node()").to_s.strip).to eq(" ")
expect(doc.xpath("./p[3]/node()").to_s.strip).to eq("text")
end
it "converts double br tags into paragraph break" do
result = add_paragraphs_to_text("some text")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/node()").to_s.strip).to eq("text")
expect(doc.xpath(".//br")).to be_empty
end
it "converts triple br tags into blank paragraph" do
result = add_paragraphs_to_text("some text")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/node()").to_s.strip).to eq(" ")
expect(doc.xpath("./p[3]/node()").to_s.strip).to eq("text")
end
it "does not convert double br tags inside p tags" do
result = add_paragraphs_to_text("some \n text
")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath(".//p").size).to eq(1)
expect(doc.xpath(".//br").size).to eq(2)
end
it "does not convert triple br tags inside p tags" do
result = add_paragraphs_to_text("some \n \n text
")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath(".//p").size).to eq(1)
expect(doc.xpath(".//br").size).to eq(3)
end
%w[b big cite code del dfn em i ins kbd q s samp
small span strike strong sub sup tt u var].each do |tag|
it "handles #{tag} inline tags spanning double line breaks" do
result = add_paragraphs_to_text("<#{tag}>some\n\ntext#{tag}>")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/#{tag}/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/#{tag}/node()").to_s.strip).to eq("text")
end
it "handles #{tag} with an unclosed br tag in it" do
result = add_paragraphs_to_text("<#{tag}>some text#{tag}>")
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./p[1]/#{tag}[1]").children.to_s.strip).to match(%r{some text})
end
end
it "handles inline tags spanning double line breaks" do
result = add_paragraphs_to_text("have some\n\ntext yay ")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/i/node()").to_s.strip).to match(/\Ahave/)
expect(doc.xpath("./p[1]/i/b/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/i/b/node()").to_s.strip).to eq("text")
expect(doc.xpath("./p[2]/i/node()").to_s.strip).to match(/ yay\Z/)
end
it "handles nested inline tags spanning double line breaks" do
result = add_paragraphs_to_text("have some\n\ntext yay")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to match(/\Ahave/)
expect(doc.xpath("./p[1]/em/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/em/node()").to_s.strip).to eq("text")
expect(doc.xpath("./p[2]/node()").to_s.strip).to match(/ yay\Z/)
end
%w[blockquote center div details].each do |tag|
it "converts double linebreaks inside #{tag} tag" do
result = add_paragraphs_to_text("<#{tag}>some\n\ntext#{tag}>")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./#{tag}/p[1]/node()").to_s.strip).to eq("some")
expect(doc.xpath("./#{tag}/p[2]/node()").to_s.strip).to eq("text")
end
it "doesn't insert extra
tags before the #{tag} tag" do
result = add_paragraphs_to_text("before
<#{tag}>during
#{tag}>")
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath(".//p").size).to eq(2)
expect(doc.xpath("./p[1]").children.to_s.strip).to eq("before")
expect(doc.xpath("./#{tag}/p[1]").children.to_s.strip).to eq("during")
end
it "creates a paragraph for text immediately following the #{tag} tag" do
result = add_paragraphs_to_text("<#{tag}>during#{tag}>after")
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath(".//p").size).to eq(2)
expect(doc.xpath("./#{tag}/p[1]").children.to_s.strip).to eq("during")
expect(doc.xpath("./p[1]").children.to_s.strip).to eq("after")
end
end
it "wraps text in p before and after existing p tag" do
result = add_paragraphs_to_text("boom\n\nda
\n\nyadda")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("boom")
expect(doc.xpath("./p[2]/node()").to_s.strip).to eq("da")
expect(doc.xpath("./p[3]/node()").to_s.strip).to eq("yadda")
end
it "wraps ruby-annotated text in p tags" do
result = add_paragraphs_to_text("text with ルビ ( RUBY ) ")
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./p[1]").children.to_s.strip).to eq("text with ルビ ( RUBY ) ")
end
it "keeps attributes of block elements" do
result = add_paragraphs_to_text("some\n\ntext
")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./div[@class='foo']/p[1]/node()").to_s.strip).to eq("some")
expect(doc.xpath("./div[@class='foo']/p[2]/node()").to_s.strip).to eq("text")
end
it "keeps attributes of inline elements across paragraphs" do
result = add_paragraphs_to_text("some\n\ntext ")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/span[@class='foo']/node()").to_s.strip).to eq("some")
expect(doc.xpath("./p[2]/span[@class='foo']/node()").to_s.strip).to eq("text")
end
it "handles two classes" do
result = add_paragraphs_to_text('foobar
')
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[contains(@class, 'foo')]/node()").to_s.strip).to eq("foobar")
expect(doc.xpath("./p[contains(@class, 'bar')]/node()").to_s.strip).to eq("foobar")
end
it "closes unclosed tag within other tag" do
html = "unclosed "
doc = Nokogiri::HTML5.fragment(add_paragraphs_to_text(html))
expect(doc.xpath("./p/strong/em/node()").to_s.strip).to eq("unclosed")
end
it "closes unclosed rt tags" do
html = "big textsmall text "
result = add_paragraphs_to_text(html)
expect(result).to include("big textsmall text ")
end
it "closes unclosed rp tag" do
html = "big text( small text ) "
result = add_paragraphs_to_text(html)
expect(result).to include("big text( small text ) ")
end
it "re-nests mis-nested tags" do
html = "some text "
doc = Nokogiri::HTML5.fragment(add_paragraphs_to_text(html))
expect(doc.xpath("./p[1]/em/strong/node()").to_s.strip).to eq("text")
end
it "handles mixed uppercase/lowecase html tags" do
result = add_paragraphs_to_text("mixed stuff ")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/em[1]/node()").to_s.strip).to eq("mixed")
expect(doc.xpath("./p[1]/em[2]/node()").to_s.strip).to eq("stuff")
end
%w[b big cite code del dfn em i ins kbd q s samp
small span strike strong sub sup tt u var].each do |tag|
it "wraps consecutive #{tag} inline tags in one paragraph " do
result = add_paragraphs_to_text("<#{tag}>hey#{tag}> <#{tag}>ho#{tag}>")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p[1]/#{tag}[1]/node()").to_s.strip).to eq("hey")
expect(doc.xpath("./p[1]/#{tag}[2]/node()").to_s.strip).to eq("ho")
expect(doc.xpath("./p[1]/text()").to_s).to eq(" ")
end
end
%w[> < &].each do |entity|
it "leaves #{entity} alone" do
result = add_paragraphs_to_text(entity)
expect(result).to eq("#{entity}
")
end
end
it "does not add empty p tags" do
result = add_paragraphs_to_text("AB
C
")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./p").size).to eq(3)
expect(doc.xpath("./p[1]/node()").to_s.strip).to eq("A")
expect(doc.xpath("./p[2]/node()").to_s.strip).to eq("B")
expect(doc.xpath("./p[3]/node()").to_s.strip).to eq("C")
end
it "does not leave p inside i" do
result = add_paragraphs_to_text("foo
bar
")
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath(".//i/p")).to be_empty
end
it "handles table tags that don't need closing" do
html = <<~TABLE
TABLE
result = add_paragraphs_to_text(html)
doc = Nokogiri::HTML5.fragment(result)
expect(doc.xpath("./table/colgroup[@align='left']/col[@width='20']").size).to eq(1)
expect(doc.xpath("./table/colgroup[@align='right']").size).to eq(1)
expect(doc.xpath("./table/tbody/tr[1]/th[1]/node()").to_s.strip).to eq("A")
expect(doc.xpath("./table/tbody/tr[1]/th[2]/node()").to_s.strip).to eq("B")
expect(doc.xpath("./table/tbody/tr[2]/td[1]/node()").to_s.strip).to eq("C")
expect(doc.xpath("./table/tbody/tr[2]/td[2]/node()").to_s.strip).to eq("D")
end
it "doesn't break when an attribute includes a single quote" do
result = add_paragraphs_to_text(<<~HTML)
Cause I'm having a good time
HTML
doc = Nokogiri::HTML.fragment(result)
node = doc.xpath(".//span").first
expect(node.attribute("title").value).to eq("Don't stop me now")
end
it "doesn't unescape escaped text when processing newlines" do
result = add_paragraphs_to_text(<<~HTML.strip)
<span>
<div>
HTML
doc = Nokogiri::HTML.fragment(result)
expect(doc.xpath("./p[1]").children.to_s.strip).to eq("<span>")
expect(doc.xpath("./p[2]").children.to_s.strip).to eq("<div>")
end
it "fails gracefully for missing ending quotation marks" do
pending "Opened enhancement request with Nokogiri"
result = add_paragraphs_to_text("mylink ')
doc = Nokogiri::HTML5.fragment(result)
node = doc.xpath(".//a").first
expect(node.attribute("href").value).to eq('ao3.org"')
expect(node.text.strip).to eq("mylink")
end
end
describe "add_break_between_paragraphs" do
it "adds between paragraphs" do
original = "Hi!
I need more space.
"
result = "Hi!
I need more space.
"
expect(add_break_between_paragraphs(original)).to eq(result)
end
it "removes any blank spaces before, between, and after the paragraph marks" do
original = "bla. Bla"
result = "bla.
Bla"
expect(add_break_between_paragraphs(original)).to eq(result)
end
end
describe "strip_images" do
let(:result) { "Hi! Bye" }
context "without keep_src" do
it "removes the img tag entirely when the src uses double quotes" do
string = 'Hi! Bye'
expect(strip_images(string)).to eq(result)
end
it "removes the img tag entirely when the src uses single quotes" do
string = "Hi! Bye"
expect(strip_images(string)).to eq(result)
end
it "removes the img tag entirely when the src uses mismatched quotes" do
string = "Hi! Bye"
expect(strip_images(string)).to eq(result)
end
it "removes the img tag entirely when the src is missing" do
string = 'Hi! Bye'
expect(strip_images(string)).to eq(result)
end
it "removes the img tag entirely when the src is missing a closing quotation mark" do
string = 'Hi! Bye"
expect(strip_images(string, keep_src: false)).to eq(result)
end
it "removes the img tag entirely when the src is missing" do
string = 'Hi! Bye'
expect(strip_images(string, keep_src: false)).to eq(result)
end
it "removes the img tag entirely when the src is missing a closing quotation mark" do
string = 'Hi! Bye'
result = 'Hi! img src="http://example.org/image.png" alt=\'something\' Bye'
expect(strip_images(string, keep_src: true)).to eq(result)
end
it "does not keep tag trailing slash without a space" do
string = 'Hi! Bye'
result = 'Hi! img src="http://example.org/image.png" alt=\'something\' Bye'
expect(strip_images(string, keep_src: true)).to eq(result)
end
it "does not keep tag trailing slash with a space" do
string = 'Hi! Bye'
result = 'Hi! img src="http://example.org/image.png" alt=\'something\' Bye'
expect(strip_images(string, keep_src: true)).to eq(result)
end
end
end
end