document_spec.cr 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require "./spec_helper"
  2. describe Luce::Document do
  3. it "prevents less than and ampersand escaping in encode_html" do
  4. document = Luce::Document.new(nil, nil, nil, encode_html: false)
  5. result = document.parse_inline("< &")
  6. result.size.should eq 1
  7. result[0].should be_a Luce::Text
  8. (result[0].as Luce::Text).text.should eq "< &"
  9. end
  10. describe "with encode_html enabled" do
  11. document = Luce::Document.new(nil, nil, nil)
  12. it "encodes HTML with an inline code snippet" do
  13. result = document.parse_inline("``<p>Hello <em>Markdown</em></p>``")
  14. code_snippet = result.shift.as Luce::Element
  15. code_snippet.text_content.should eq "&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;"
  16. end
  17. it "encodes HTML in a fenced block" do
  18. lines = "```\n<p>Hello <em>Markdown</em></p>\n```\n".to_lines
  19. result = document.parse_line_list(lines)
  20. code_block = result.shift.as Luce::Element
  21. code_block.text_content.should eq "&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;\n"
  22. end
  23. it "encodes HTML in an indented code block" do
  24. lines = " <p>Hello <em>Markdown</em></p>\n".to_lines
  25. result = document.parse_line_list(lines)
  26. code_block = result.shift.as Luce::Element
  27. code_block.text_content.should eq "&lt;p&gt;Hello &lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;\n"
  28. end
  29. it "encodeHtml spaces are preserved in text" do
  30. # Example to get a <p> tag rendered before a text node.
  31. contents = "Sample\n\n<pre>\n A\n B\n</pre>"
  32. document = Luce::Document.new(nil, nil, nil)
  33. nodes = Luce::BlockParser.new(contents.to_lines, document).parse_lines
  34. result = Luce::HTMLRenderer.new.render(nodes)
  35. result.should eq "<p>\n</p>\n<pre>\n A\n B\n</pre>"
  36. end
  37. it "encode double quotes, greater than, and less than when escaped" do
  38. contents = ">\\\"< Hello"
  39. document = Luce::Document.new(nil, nil, nil)
  40. nodes = document.parse_inline contents
  41. nodes.size.should eq 1
  42. nodes[0].responds_to?(:text).should be_true
  43. (nodes[0].as Luce::Text).text.should eq "&gt;&quot;&lt; Hello"
  44. end
  45. end
  46. describe "with encodeHtml disabled" do
  47. document = Luce::Document.new(nil, nil, nil, encode_html: false)
  48. it "leaves HTML alone, in a code snippet" do
  49. result = document.parse_inline "```<p>Hello <em>Markdown</em></p>```"
  50. code_snippet = result.shift.as Luce::Element
  51. code_snippet.text_content.should eq "<p>Hello <em>Markdown</em></p>"
  52. end
  53. it "leaves HTML alone, in a fenced code block" do
  54. lines = "```\n<p>Hello <em>Markdown</em></p>\n```\n".to_lines
  55. result = document.parse_line_list(lines)
  56. code_block = result.shift.as Luce::Element
  57. code_block.text_content.should eq "<p>Hello <em>Markdown</em></p>\n"
  58. end
  59. it "leaves HTML alone, in an indented code block" do
  60. lines = " <p>Hello <em>Markdown</em></p>\n".to_lines
  61. result = document.parse_line_list(lines)
  62. code_block = result.shift.as Luce::Element
  63. code_block.text_content.should eq "<p>Hello <em>Markdown</em></p>\n"
  64. end
  65. it "leave double quotes, greater than, and less than when escaped" do
  66. contents = %{>"< Hello}
  67. document = Luce::Document.new(nil, nil, nil, encode_html: false)
  68. nodes = document.parse_inline contents
  69. nodes.size.should eq 1
  70. nodes[0].responds_to?(:text).should be_true
  71. (nodes[0].as Luce::Text).text.should eq ">\"< Hello"
  72. end
  73. end
  74. end