defmodule MusicLibraryWeb.MarkdownTest do use ExUnit.Case, async: true alias MusicLibraryWeb.Markdown doctest MusicLibraryWeb.Markdown describe "streaming" do test "new document renders empty string" do doc = Markdown.new_streaming_doc() assert Markdown.streaming_to_html(doc) == "" end test "incremental chunks produce valid HTML" do doc = Markdown.new_streaming_doc() doc = MDEx.Document.put_markdown(doc, "**bold") html = Markdown.streaming_to_html(doc) assert html =~ "bold" doc = MDEx.Document.put_markdown(doc, " text**") html = Markdown.streaming_to_html(doc) assert html =~ "bold text" end test "sanitizes script tags" do doc = Markdown.new_streaming_doc() doc = MDEx.Document.put_markdown(doc, "") html = Markdown.streaming_to_html(doc) refute html =~ "") refute result =~ "" end test "strips event handlers from tags" do result = Markdown.to_html("") refute result =~ "onerror" refute result =~ "alert" end test "preserves normal markdown formatting" do result = Markdown.to_html("**bold**") assert result =~ "bold" end test "preserves links" do result = Markdown.to_html("[link](http://example.com)") assert result =~ "" end test "does not add target attribute by default" do result = Markdown.to_html("[link](http://example.com)") refute result =~ "target=" end end describe "to_html/2 with link_target" do test "adds target and rel attributes to links" do result = Markdown.to_html("[link](http://example.com)", link_target: "_blank") assert result =~ ~s(target="_blank") assert result =~ ~s(rel="noopener noreferrer") assert result =~ ~s(href="http://example.com") assert result =~ ">link" end test "adds target to autolinked URLs" do result = Markdown.to_html("Visit http://example.com for more", link_target: "_blank") assert result =~ ~s(target="_blank") assert result =~ ~s(href="http://example.com") end test "does not add target to internal links" do result = Markdown.to_html("Check [[Porcupine Tree]]", link_target: "_blank") refute result =~ ~s(target="_blank") assert result =~ ~s(href="/collection?query=Porcupine+Tree") assert result =~ "Porcupine Tree" end test "preserves link title" do result = Markdown.to_html(~s|[link](http://example.com "My Title")|, link_target: "_blank") assert result =~ ~s(title="My Title") assert result =~ ~s(target="_blank") end end end