3 class RichTextTest < ActiveSupport::TestCase
 
   4   include Rails::Dom::Testing::Assertions::SelectorAssertions
 
   7     r = RichText.new("html", "foo http://example.com/ bar")
 
  10       assert_select "a[href='http://example.com/']", 1
 
  11       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
  14     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
 
  17       assert_select "a[href='http://example.com/']", 1
 
  18       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
  21     r = RichText.new("html", "foo <a rel='junk me trash' href='http://example.com/'>bar</a> baz")
 
  24       assert_select "a[href='http://example.com/']", 1
 
  25       assert_select "a[rel='me nofollow noopener noreferrer']", 1
 
  28     r = RichText.new("html", "foo example@example.com bar")
 
  33     r = RichText.new("html", "foo <a href='mailto:example@example.com'>bar</a> baz")
 
  36       assert_select "a[href='mailto:example@example.com']", 1
 
  37       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
  40     r = RichText.new("html", "foo <div>bar</div> baz")
 
  42       assert_select "div", false
 
  43       assert_select "p", /^foo *bar *baz$/
 
  46     r = RichText.new("html", "foo <script>bar = 1;</script> baz")
 
  48       assert_select "script", false
 
  49       assert_select "p", /^foo *baz$/
 
  52     r = RichText.new("html", "foo <style>div { display: none; }</style> baz")
 
  54       assert_select "style", false
 
  55       assert_select "p", /^foo *baz$/
 
  58     r = RichText.new("html", "<table><tr><td>column</td></tr></table>")
 
  60       assert_select "table[class='table table-sm w-auto']"
 
  63     r = RichText.new("html", "<p class='btn btn-warning'>Click Me</p>")
 
  65       assert_select "p[class='btn btn-warning']", false
 
  66       assert_select "p", /^Click Me$/
 
  69     r = RichText.new("html", "<p style='color:red'>Danger</p>")
 
  71       assert_select "p[style='color:red']", false
 
  72       assert_select "p", /^Danger$/
 
  77     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
 
  78     assert_equal "foo <a href='http://example.com/'>bar</a> baz", r.to_text
 
  81   def test_html_spam_score
 
  82     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
 
  83     assert_equal 55, r.spam_score.round
 
  86   def test_markdown_to_html
 
  87     r = RichText.new("markdown", "foo http://example.com/ bar")
 
  90       assert_select "a[href='http://example.com/']", 1
 
  91       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
  94     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
 
  97       assert_select "a[href='http://example.com/']", 1
 
  98       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
 101     r = RichText.new("markdown", "foo <a rel='junk me trash' href='http://example.com/'>bar</a>) baz")
 
 104       assert_select "a[href='http://example.com/']", 1
 
 105       assert_select "a[rel='me nofollow noopener noreferrer']", 1
 
 108     r = RichText.new("markdown", "foo example@example.com bar")
 
 111       assert_select "a[href='mailto:example@example.com']", 1
 
 112       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
 115     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
 
 118       assert_select "a[href='mailto:example@example.com']", 1
 
 119       assert_select "a[rel='nofollow noopener noreferrer']", 1
 
 122     r = RichText.new("markdown", "foo  bar")
 
 124       assert_select "img", 1
 
 125       assert_select "img[alt='bar']", 1
 
 126       assert_select "img[src='http://example.com/example.png']", 1
 
 129     r = RichText.new("markdown", "# foo bar baz")
 
 131       assert_select "h1", "foo bar baz"
 
 134     r = RichText.new("markdown", "## foo bar baz")
 
 136       assert_select "h2", "foo bar baz"
 
 139     r = RichText.new("markdown", "### foo bar baz")
 
 141       assert_select "h3", "foo bar baz"
 
 144     r = RichText.new("markdown", "* foo bar baz")
 
 146       assert_select "ul" do
 
 147         assert_select "li", "foo bar baz"
 
 151     r = RichText.new("markdown", "1. foo bar baz")
 
 153       assert_select "ol" do
 
 154         assert_select "li", "foo bar baz"
 
 158     r = RichText.new("markdown", "foo *bar* _baz_ qux")
 
 160       assert_select "em", "bar"
 
 161       assert_select "em", "baz"
 
 164     r = RichText.new("markdown", "foo **bar** __baz__ qux")
 
 166       assert_select "strong", "bar"
 
 167       assert_select "strong", "baz"
 
 170     r = RichText.new("markdown", "foo `bar` baz")
 
 172       assert_select "code", "bar"
 
 175     r = RichText.new("markdown", "    foo bar baz")
 
 177       assert_select "pre", /^\s*foo bar baz\s*$/
 
 180     r = RichText.new("markdown", "|column|column")
 
 182       assert_select "table[class='table table-sm w-auto']"
 
 185     r = RichText.new("markdown", "Click Me\n{:.btn.btn-warning}")
 
 187       assert_select "p[class='btn btn-warning']", false
 
 188       assert_select "p", /^Click Me$/
 
 191     r = RichText.new("markdown", "<p style='color:red'>Danger</p>")
 
 193       assert_select "p[style='color:red']", false
 
 194       assert_select "p", /^Danger$/
 
 198   def test_markdown_table_alignment
 
 199     # Ensure that kramdown table alignment styles are converted to bootstrap classes
 
 200     markdown_table = <<~MARKDOWN
 
 205     r = RichText.new("markdown", markdown_table)
 
 207       assert_select "td[style='text-align:center']", false
 
 208       assert_select "td[class='text-center']", true
 
 209       assert_select "td[style='text-align:right']", false
 
 210       assert_select "td[class='text-end']", true
 
 214   def test_markdown_to_text
 
 215     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
 
 216     assert_equal "foo [bar](http://example.com/) baz", r.to_text
 
 219   def test_markdown_spam_score
 
 220     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
 
 221     assert_equal 50, r.spam_score.round
 
 224   def test_text_to_html_linkify
 
 225     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do
 
 226       r = RichText.new("text", "foo http://example.com/ bar")
 
 228         assert_dom "a", :count => 1, :text => "http://example.com/" do
 
 229           assert_dom "> @href", "http://example.com/"
 
 230           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 236   def test_text_to_html_linkify_replace
 
 237     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do
 
 238       r = RichText.new("text", "foo https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12 bar")
 
 240         assert_dom "a", :count => 1, :text => "repl.example.com/some/path?query=te<st&limit=20>10#result12" do
 
 241           assert_dom "> @href", "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
 
 242           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 248   def test_text_to_html_linkify_replace_other_scheme
 
 249     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do
 
 250       r = RichText.new("text", "foo ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12 bar")
 
 252         assert_dom "a", :count => 1, :text => "ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12" do
 
 253           assert_dom "> @href", "ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
 
 254           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 260   def test_text_to_html_linkify_replace_undefined
 
 261     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => nil) do
 
 262       r = RichText.new("text", "foo https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12 bar")
 
 264         assert_dom "a", :count => 1, :text => "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12" do
 
 265           assert_dom "> @href", "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
 
 266           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 272   def test_text_to_html_linkify_wiki_replace_prefix
 
 273     with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com",
 
 274                   :linkify_wiki_optional_path_prefix => "^/wiki(?=/[A-Z])") do
 
 275       r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal bar")
 
 277         assert_dom "a", :count => 1, :text => "wiki.example.com/Tag:surface%3Dmetal" do
 
 278           assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal"
 
 279           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 285   def test_text_to_html_linkify_wiki_replace_prefix_undefined
 
 286     with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com",
 
 287                   :linkify_wiki_optional_path_prefix => nil) do
 
 288       r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal bar")
 
 290         assert_dom "a", :count => 1, :text => "wiki.example.com/wiki/Tag:surface%3Dmetal" do
 
 291           assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal"
 
 292           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 298   def test_text_to_html_linkify_wiki_replace_undefined_prefix
 
 299     with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => nil,
 
 300                   :linkify_wiki_optional_path_prefix => "^/wiki(?=/[A-Z])") do
 
 301       r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal bar")
 
 303         assert_dom "a", :count => 1, :text => "https://replace-me-wiki.example.com/Tag:surface%3Dmetal" do
 
 304           assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal"
 
 305           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 311   def test_text_to_html_linkify_wiki_replace_prefix_no_match
 
 312     with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com",
 
 313                   :linkify_wiki_optional_path_prefix => "^/wiki(?=/[A-Z])") do
 
 314       r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/w bar")
 
 316         assert_dom "a", :count => 1, :text => "wiki.example.com/wiki/w" do
 
 317           assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/w"
 
 318           assert_dom "> @rel", "nofollow noopener noreferrer"
 
 324   def test_text_to_html_email
 
 325     r = RichText.new("text", "foo example@example.com bar")
 
 331   def test_text_to_html_escape
 
 332     r = RichText.new("text", "foo < bar & baz > qux")
 
 334       assert_select "p", "foo < bar & baz > qux"
 
 338   def test_text_to_text
 
 339     r = RichText.new("text", "foo http://example.com/ bar")
 
 340     assert_equal "foo http://example.com/ bar", r.to_text
 
 343   def test_text_spam_score
 
 344     r = RichText.new("text", "foo http://example.com/ bar")
 
 345     assert_equal 141, r.spam_score.round
 
 348   def test_text_no_opengraph_properties
 
 349     r = RichText.new("text", "foo https://example.com/ bar")
 
 351     assert_nil r.image_alt
 
 352     assert_nil r.description
 
 355   def test_html_no_opengraph_properties
 
 356     r = RichText.new("html", "foo <a href='https://example.com/'>bar</a> baz")
 
 358     assert_nil r.image_alt
 
 359     assert_nil r.description
 
 362   def test_markdown_no_image
 
 363     r = RichText.new("markdown", "foo [bar](https://example.com/) baz")
 
 365     assert_nil r.image_alt
 
 368   def test_markdown_image
 
 369     r = RichText.new("markdown", "foo  baz")
 
 370     assert_equal "https://example.com/image.jpg", r.image
 
 371     assert_equal "bar", r.image_alt
 
 374   def test_markdown_first_image
 
 375     r = RichText.new("markdown", "foo  baz\nfoo  baz")
 
 376     assert_equal "https://example.com/image1.jpg", r.image
 
 377     assert_equal "bar1", r.image_alt
 
 380   def test_markdown_image_with_empty_src
 
 381     r = RichText.new("markdown", "![invalid]()")
 
 383     assert_nil r.image_alt
 
 386   def test_markdown_skip_image_with_empty_src
 
 387     r = RichText.new("markdown", "![invalid]() ")
 
 388     assert_equal "https://example.com/valid.gif", r.image
 
 389     assert_equal "valid", r.image_alt
 
 392   def test_markdown_html_image
 
 393     r = RichText.new("markdown", "<img src='https://example.com/img_element.png' alt='alt text here'>")
 
 394     assert_equal "https://example.com/img_element.png", r.image
 
 395     assert_equal "alt text here", r.image_alt
 
 398   def test_markdown_html_image_without_alt
 
 399     r = RichText.new("markdown", "<img src='https://example.com/img_element.png'>")
 
 400     assert_equal "https://example.com/img_element.png", r.image
 
 401     assert_nil r.image_alt
 
 404   def test_markdown_html_image_with_empty_src
 
 405     r = RichText.new("markdown", "<img src='' alt='forgot src'>")
 
 407     assert_nil r.image_alt
 
 410   def test_markdown_skip_html_image_with_empty_src
 
 411     r = RichText.new("markdown", "<img src='' alt='forgot src'> <img src='https://example.com/next_img_element.png' alt='have src'>")
 
 412     assert_equal "https://example.com/next_img_element.png", r.image
 
 413     assert_equal "have src", r.image_alt
 
 416   def test_markdown_html_image_without_src
 
 417     r = RichText.new("markdown", "<img alt='totally forgot src'>")
 
 419     assert_nil r.image_alt
 
 422   def test_markdown_skip_html_image_without_src
 
 423     r = RichText.new("markdown", "<img alt='totally forgot src'> <img src='https://example.com/next_img_element.png' alt='have src'>")
 
 424     assert_equal "https://example.com/next_img_element.png", r.image
 
 425     assert_equal "have src", r.image_alt
 
 428   def test_markdown_no_description
 
 429     r = RichText.new("markdown", "#Nope")
 
 430     assert_nil r.description
 
 433   def test_markdown_description
 
 434     r = RichText.new("markdown", "This is an article about something.")
 
 435     assert_equal "This is an article about something.", r.description
 
 438   def test_markdown_description_after_heading
 
 439     r = RichText.new("markdown", "#Heading\n\nHere starts the text.")
 
 440     assert_equal "Here starts the text.", r.description
 
 443   def test_markdown_description_after_image
 
 444     r = RichText.new("markdown", "\n\nThis is below the image.")
 
 445     assert_equal "This is below the image.", r.description
 
 448   def test_markdown_description_only_first_paragraph
 
 449     r = RichText.new("markdown", "This thing.\n\nMaybe also that thing.")
 
 450     assert_equal "This thing.", r.description
 
 453   def test_markdown_description_elements
 
 454     r = RichText.new("markdown", "*Something* **important** [here](https://example.com/).")
 
 455     assert_equal "Something important here.", r.description
 
 458   def test_markdown_html_description
 
 459     r = RichText.new("markdown", "<p>Can use HTML tags.</p>")
 
 460     assert_equal "Can use HTML tags.", r.description
 
 463   def test_markdown_description_max_length
 
 464     r = RichText.new("markdown", "x" * RichText::MAX_DESCRIPTION_LENGTH)
 
 465     assert_equal "x" * RichText::MAX_DESCRIPTION_LENGTH, r.description
 
 467     r = RichText.new("markdown", "y" * (RichText::MAX_DESCRIPTION_LENGTH + 1))
 
 468     assert_equal "#{'y' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description
 
 470     r = RichText.new("markdown", "*zzzzzzzzz*z" * ((RichText::MAX_DESCRIPTION_LENGTH + 1) / 10.0).ceil)
 
 471     assert_equal "#{'z' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description
 
 476   def assert_html(richtext, &block)
 
 477     html = richtext.to_html
 
 478     assert_predicate html, :html_safe?
 
 479     root = Nokogiri::HTML::DocumentFragment.parse(html)
 
 480     assert_select root, "*" do