require "test_helper" require "govspeak_test_helper" require "ostruct" class GovspeakTest < Minitest::Test include GovspeakTestHelper test "simple smoke-test" do rendered = Govspeak::Document.new("*this is markdown*").to_html assert_equal "
this is markdown
\n", rendered end test "simple smoke-test for simplified API" do rendered = Govspeak::Document.to_html("*this is markdown*") assert_equal "this is markdown
\n", rendered end test "strips forbidden unicode characters" do rendered = Govspeak::Document.new( "this is text with forbidden characters \u0008\u000b\ufffe\u{2ffff}\u{5fffe}", ).to_html assert_equal "this is text with forbidden characters
\n", rendered end test "highlight-answer block extension" do rendered = Govspeak::Document.new("this \n{::highlight-answer}Lead in to *BIG TEXT*\n{:/highlight-answer}").to_html assert_equal %(this
\n\nLead in to BIG TEXT
\nthis
\n\n\n), rendered end test "extracts headers with text, level and generated id" do document = Govspeak::Document.new %( # Big title ### Small subtitle ## Medium title ) assert_equal [ Govspeak::Header.new("Big title", 1, "big-title"), Govspeak::Header.new("Small subtitle", 3, "small-subtitle"), Govspeak::Header.new("Medium title", 2, "medium-title"), ], document.headers end test "extracts different ids for duplicate headers" do document = Govspeak::Document.new("## Duplicate header\n\n## Duplicate header") assert_equal [ Govspeak::Header.new("Duplicate header", 2, "duplicate-header"), Govspeak::Header.new("Duplicate header", 2, "duplicate-header-1"), ], document.headers end test "extracts headers when nested inside blocks" do document = Govspeak::Document.new %( # First title\n123 Test Street
Testcase Cliffs
Teston
0123 456 7890 \n
col | \n
---|
val | \n
col | \n
---|
val | \n
Paragraph1
\n\n\n123 Test Street
Testcase Cliffs
Teston
0123 456 7890 \n
I am very informational
The following is very informational
I am very informational
I am very informational
I am very important
The following is very important
I am very important
I am very helpful
The following is very helpful
I am very helpful
I am very helpful
I am very helpful
This is a link isn’t it?
' assert_text_output "This is a link isn’t it?" end test_given_govspeak "This is a [link with an at sign in it](http://www.gov.uk/@dg/@this) isn't it?" do assert_html_output 'This is a link with an at sign in it isn’t it?
' assert_text_output "This is a link with an at sign in it isn’t it?" end test_given_govspeak " HTML *[HTML]: Hyper Text Markup Language" do assert_html_output %(HTML
) assert_text_output "HTML" end test_given_govspeak "x[a link](http://rubyforge.org)x" do assert_html_output '' assert_text_output "a link" end test_given_govspeak "x[an xx link](http://x.com)x" do assert_html_output '' end test_given_govspeak "[internal link](http://www.gov.uk)" do assert_html_output '' end test_given_govspeak "[link with no host is assumed to be internal](/)" do assert_html_output 'link with no host is assumed to be internal
' end test_given_govspeak "[internal link with rel attribute keeps it](http://www.gov.uk){:rel='next'}" do assert_html_output 'internal link with rel attribute keeps it
' end test_given_govspeak "[external link without x markers](http://www.google.com)" do assert_html_output 'external link without x markers
' end # Based on Kramdown inline attribute list (IAL) test: # https://github.com/gettalong/kramdown/blob/627978525cf5ee5b290d8a1b8675aae9cc9e2934/test/testcases/span/01_link/link_defs_with_ial.text test_given_govspeak "External link definitions with [attr] and [attr 2] and [attr 3] and [attr before]\n\n[attr]: http://example.com 'title'\n{: hreflang=\"en\" .test}\n\n[attr 2]: http://example.com 'title'\n{: hreflang=\"en\"}\n{: .test}\n\n[attr 3]: http://example.com\n{: .test}\ntest\n\n{: hreflang=\"en\"}\n{: .test}\n[attr before]: http://example.com" do assert_html_output "External link definitions with attr and attr 2 and attr 3 and attr before
\n\ntest
" end test_given_govspeak "External link with [inline attribute list] (IAL)\n\n[inline attribute list]: http://example.com 'title'\n{: hreflang=\"en\" .test}" do assert_html_output 'External link with inline attribute list (IAL)
' end test_given_govspeak "[external link with rel attribute](http://www.google.com){:rel='next'}" do assert_html_output 'external link with rel attribute
' end test_given_govspeak "Text before [an external link](http://www.google.com)" do assert_html_output 'Text before an external link
' end test_given_govspeak "[An external link](http://www.google.com) with text afterwards" do assert_html_output 'An external link with text afterwards
' end test_given_govspeak "Text before [an external link](http://www.google.com) and text afterwards" do assert_html_output 'Text before an external link and text afterwards
' end test_given_govspeak "![image with external url](http://www.example.com/image.jpg)" do assert_html_output '' end test "should be able to override default 'document_domains' option" do html = Govspeak::Document.new("[internal link](http://www.not-external.com)", document_domains: %w[www.not-external.com]).to_html refute html.include?('rel="external"'), "should not consider www.not-external.com as an external url" end test "should be able to supply multiple domains for 'document_domains' option" do html = Govspeak::Document.new("[internal link](http://www.not-external-either.com)", document_domains: %w[www.not-external.com www.not-external-either.com]).to_html refute html.include?('rel="external"'), "should not consider www.not-external-either.com as an external url" end test "should be able to override default 'input' option" do html = Govspeak::Document.new("[external link](http://www.external.com)", input: "kramdown").to_html refute html.include?('rel="external"'), "should not automatically add rel external attribute" end test "should not be able to override default 'entity output' option" do html = Govspeak::Document.new(">", entity_output: :numeric).to_html assert html.include?(">") end test "should assume a link with an invalid uri is internal" do html = Govspeak::Document.new("[link](:invalid-uri)").to_html refute html.include?('rel="external"') end test "should treat a mailto as internal" do html = Govspeak::Document.new("[link](mailto:a@b.com)").to_html refute html.include?('rel="external"') assert_equal %(\n), deobfuscate_mailto(html) end test "permits mailto:// URI" do html = Govspeak::Document.new("[link](mailto://a@b.com)").to_html assert_equal %(\n), deobfuscate_mailto(html) end test "permits dud mailto: URI" do html = Govspeak::Document.new("[link](mailto:)").to_html assert_equal %(\n), deobfuscate_mailto(html) end test "permits trailing whitespace in an URI" do Govspeak::Document.new("[link](http://example.com/%20)").to_html end # Regression test - the surrounded_by helper doesn't require the closing x # so 'xaa' was getting picked up by the external link helper above # TODO: review whether we should require closing symbols for these extensions # need to check all existing content. test_given_govspeak "xaa" do assert_html_output "xaa
" assert_text_output "xaa" end test_given_govspeak " $! rainbow $!" do assert_html_output %(rainbow
help, send cake
street
road
help
\ncan you tell me how to get to…
Click here to start the tool
Here is some text
Click here to start the tool
Click here to start the tool
Here is some text
Click here to start the tool
zippy
bungle
george
step
list
1.0 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis ante, lobortis non quam sit amet, tempus interdum justo.
Pellentesque quam enim, egestas sit amet congue sit amet, ultrices vitae arcu. fringilla, metus dui scelerisque est.
a) A list item
b) Another list item
1.1 Second entry Curabitur pretium pharetra sapien, a feugiat arcu euismod eget. Nunc luctus ornare varius. Nulla scelerisque, justo dictum dapibus
The quick brown $LegislativeList * 1. fox jumps over
" end test_given_govspeak " The quick brown fox $LegislativeList * 1. jumps over the lazy dog $EndLegislativeList " do assert_html_output %(The quick brown fox
This bit of text
Zippy, Bungle and George did not qualify for the tax exemption in s428. They filled in their tax return accordingly.
) end test_given_govspeak ":scotland: I am very devolved\n and very scottish \n:scotland:" do assert_html_output 'This section applies to Scotland
I am very devolved and very scottish
Message with a link
This is a paragraph $PriorityList:3 * List item 1 * List item 2 * List item 3 * List item 4 * List item 5
") end end test "Priority list placed correctly" do govspeak = " This is a paragraph $PriorityList:3 * List item 1 * List item 2 * List item 3 * List item 4 * List item 5" given_govspeak(govspeak) do assert_html_output %(This is a paragraph
He said:
I’m not sure what you mean!
Or so we thought.
) end end test "should add class to last paragraph of blockquote" do govspeak = " > first line > > last line" given_govspeak(govspeak) do assert_html_output %() end end endfirst line
last line