Files
danbooru/test/unit/d_text_test.rb
evazion 33f2725ae7 Fix #4112: Colorize tags in DText.
DText is processed in three phases: a preprocessing phase, the regular
parsing phases, and a postprocessing phase.

In the preprocessing phase we extract all the wiki links from all the
dtext messages on the page (more precisely, we do this in forum threads
and on comment pages, because these are the main places with lots of
dtext). This is so we can lookup all the tags and wiki pages in one
query, which is necessary because in the worst case (in certain forum
threads and in certain list_of_* wiki pages) there can be hundreds of
tags per page.

In the postprocessing phase we fixup the html generated by the ragel
parser to add CSS classes to wiki links. We do this in a postprocessing
step because it's easier than doing it in the ragel parser itself.
2019-10-11 18:45:55 -05:00

61 lines
2.3 KiB
Ruby

require "test_helper"
class DTextTest < ActiveSupport::TestCase
def assert_strip_dtext(expected, dtext)
assert_equal(expected, DText.strip_dtext(dtext))
end
context "DText" do
context "#strip_dtext" do
should "strip dtext markup from the input" do
assert_strip_dtext("x", "[b]x[/b]")
assert_strip_dtext("x", "[i]x[/i]")
assert_strip_dtext("x", "[tn]x[/tn]")
assert_strip_dtext("x", "[spoilers]x[/spoilers]")
assert_strip_dtext("post #123", "post #123")
assert_strip_dtext("pixiv #123", "pixiv #123")
assert_strip_dtext("bkub", "{{bkub}}")
assert_strip_dtext("bkub", "[[bkub]]")
assert_strip_dtext("Bkub", "[[bkub|Bkub]]")
assert_strip_dtext("http://www.example.com", "http://www.example.com")
assert_strip_dtext("http://www.example.com", "<http://www.example.com>")
assert_strip_dtext("x", '"x":/posts')
assert_strip_dtext("x", '"x":[/posts]')
assert_strip_dtext("@bkub", "@bkub")
assert_strip_dtext("@bkub", "<@bkub>")
assert_strip_dtext("x", "h1. x")
assert_strip_dtext("x", "h2. [i]x[/i]")
assert_strip_dtext("* one\n* two", "* [b]one[/b]\n* [[two]]")
assert_strip_dtext("okay", "[expand][u]okay[/u][/expand]")
assert_strip_dtext("> chen said:\n> \n> honk honk", "[quote]chen said:\n\nhonk honk[/quote]")
assert_strip_dtext("one two three\nfour\n\nfive six", "one [b]two[/b] three\nfour\n\nfive six")
end
end
context "#format_text" do
should "add tag types to wiki links" do
create(:tag, name: "bkub", category: Tag.categories.artist, post_count: 42)
assert_match(/tag-type-#{Tag.categories.artist}/, DText.format_text("[[bkub]]"))
end
should "mark links to nonexistent tags or wikis" do
create(:tag, name: "no_wiki", post_count: 42)
create(:tag, name: "empty_tag", post_count: 0)
assert_match(/dtext-wiki-does-not-exist/, DText.format_text("[[no wiki]]"))
assert_match(/dtext-tag-does-not-exist/, DText.format_text("[[no tag]]"))
assert_match(/dtext-tag-empty/, DText.format_text("[[empty tag]]"))
refute_match(/dtext-tag-does-not-exist/, DText.format_text("[[help:nothing]]"))
end
end
end
end