You can now escape whitelisted HTML elements with a backslash to make them render as text rather than HTML elements.

Added eight more parser tests.
Replaced an empty slice with nil to make it cleaner.
This commit is contained in:
Azareal 2019-03-27 16:36:14 +10:00
parent e1dfebf090
commit 4f7bb5320d
3 changed files with 18 additions and 3 deletions

View File

@ -219,6 +219,7 @@ func PreparseMessage(msg string) string {
'u': []string{""}, 'u': []string{""},
'b': []string{"", "lockquote"}, 'b': []string{"", "lockquote"},
'i': []string{""}, 'i': []string{""},
//'p': []string{""},
'g': []string{""}, // Quick and dirty fix for Grammarly 'g': []string{""}, // Quick and dirty fix for Grammarly
} }
var buildLitMatch = func(tag string) func(*TagToAction, bool, int, []rune) (int, string) { var buildLitMatch = func(tag string) func(*TagToAction, bool, int, []rune) (int, string) {
@ -266,6 +267,7 @@ func PreparseMessage(msg string) string {
&TagToAction{"lockquote", buildLitMatch("blockquote"), 0, false}, &TagToAction{"lockquote", buildLitMatch("blockquote"), 0, false},
}, },
'i': []*TagToAction{&TagToAction{"", buildLitMatch("em"), 0, false}}, 'i': []*TagToAction{&TagToAction{"", buildLitMatch("em"), 0, false}},
//'p': []*TagToAction{&TagToAction{"", buildLitMatch2("\n\n", ""), 0, false}},
'g': []*TagToAction{ 'g': []*TagToAction{
&TagToAction{"", func(act *TagToAction, open bool, i int, runes []rune) (int, string) { &TagToAction{"", func(act *TagToAction, open bool, i int, runes []rune) (int, string) {
if open { if open {
@ -290,7 +292,13 @@ func PreparseMessage(msg string) string {
// TODO: Implement a less literal parser // TODO: Implement a less literal parser
for i := 0; i < len(runes); i++ { for i := 0; i < len(runes); i++ {
char := runes[i] char := runes[i]
if char == '&' && peekMatch(i, "lt;", runes) { // TODO: Make the slashes escapable too in case someone means to use a literaly slash, maybe as an example of how to escape elements?
if char == '\\' {
if peekMatch(i, "&lt;", runes) {
msg += "&"
i++
}
} else if char == '&' && peekMatch(i, "lt;", runes) {
var ok bool var ok bool
i, ok = tryStepForward(i, 4, runes) i, ok = tryStepForward(i, 4, runes)
if !ok { if !ok {

View File

@ -113,8 +113,7 @@ func installGuilds(plugin *common.Plugin) error {
qgen.DBTableColumn{"rank", "int", 0, false, false, "0"}, /* 0: Member. 1: Mod. 2: Admin. */ qgen.DBTableColumn{"rank", "int", 0, false, false, "0"}, /* 0: Member. 1: Mod. 2: Admin. */
qgen.DBTableColumn{"posts", "int", 0, false, false, "0"}, /* Per-Group post count. Should we do some sort of score system? */ qgen.DBTableColumn{"posts", "int", 0, false, false, "0"}, /* Per-Group post count. Should we do some sort of score system? */
qgen.DBTableColumn{"joinedAt", "datetime", 0, false, false, ""}, qgen.DBTableColumn{"joinedAt", "datetime", 0, false, false, ""},
}, }, nil,
[]qgen.DBTableKey{},
) )
if err != nil { if err != nil {
return err return err

View File

@ -73,10 +73,18 @@ func TestPreparser(t *testing.T) {
msgList.Add("</span>", "") msgList.Add("</span>", "")
msgList.Add("<span></span>", "") msgList.Add("<span></span>", "")
msgList.Add("<span ></span>", "") msgList.Add("<span ></span>", "")
msgList.Add("<span><span></span></span>", "")
msgList.Add("<span><b></b></span>", "<strong></strong>")
msgList.Add("<></>", "&lt;&gt;&lt;/&gt;") msgList.Add("<></>", "&lt;&gt;&lt;/&gt;")
msgList.Add("</><>", "&lt;/&gt;&lt;&gt;") msgList.Add("</><>", "&lt;/&gt;&lt;&gt;")
msgList.Add("<>", "&lt;&gt;") msgList.Add("<>", "&lt;&gt;")
msgList.Add("</>", "&lt;/&gt;") msgList.Add("</>", "&lt;/&gt;")
msgList.Add("<p>hi</p>", "hi")
msgList.Add("<p></p>", "")
msgList.Add("<blockquote>hi</blockquote>", "<blockquote>hi</blockquote>")
msgList.Add("<blockquote><b>hi</b></blockquote>", "<blockquote><strong>hi</strong></blockquote>")
msgList.Add("<blockquote><meow>hi</meow></blockquote>", "<blockquote>&lt;meow&gt;hi&lt;/meow&gt;</blockquote>")
msgList.Add("\\<blockquote>hi</blockquote>", "&lt;blockquote&gt;hi&lt;/blockquote&gt;")
msgList.Add("@", "@") msgList.Add("@", "@")
msgList.Add("@Admin", "@1") msgList.Add("@Admin", "@1")
msgList.Add("@Bah", "@Bah") msgList.Add("@Bah", "@Bah")