Added i as an alias for em in the HTML parser.

b is now an alias for strong in the HTML parser rather than being it's own thing.
Fixed a bug where tags weren't being closed.
Fixed a bug where the right entities weren't being written all the time.

Added tests for the preparser.
This commit is contained in:
Azareal 2018-06-26 12:30:29 +10:00
parent 1f28ecb804
commit 6eb3429c25
2 changed files with 71 additions and 3 deletions

View File

@ -201,6 +201,7 @@ func PreparseMessage(msg string) string {
'd': []string{"el"},
'u': []string{""},
'b': []string{""},
'i': []string{""},
}
var buildLitMatch = func(tag string) func(*TagToAction, bool, int, []rune) (int, string) {
return func(action *TagToAction, open bool, _ int, _ []rune) (int, string) {
@ -242,7 +243,8 @@ func PreparseMessage(msg string) string {
},
'd': []*TagToAction{&TagToAction{"el", buildLitMatch("del"), 0, false}},
'u': []*TagToAction{&TagToAction{"", buildLitMatch("u"), 0, false}},
'b': []*TagToAction{&TagToAction{"", buildLitMatch("b"), 0, false}},
'b': []*TagToAction{&TagToAction{"", buildLitMatch("strong"), 0, false}},
'i': []*TagToAction{&TagToAction{"", buildLitMatch("em"), 0, false}},
}
// TODO: Implement a less literal parser
for i := 0; i < len(runes); i++ {
@ -255,6 +257,7 @@ func PreparseMessage(msg string) string {
//fmt.Println("string(char): ", string(char))
if int(char) >= len(allowedTags) {
//fmt.Println("sentinel char out of bounds")
msg += "&"
i -= 4
continue
}
@ -271,8 +274,11 @@ func PreparseMessage(msg string) string {
if len(tags) == 0 {
//fmt.Println("couldn't find char in allowedTags")
if closeTag {
//msg += "&lt;/"
msg += "&"
i -= 5
} else {
msg += "&"
i -= 4
}
continue
@ -292,18 +298,21 @@ func PreparseMessage(msg string) string {
newI, out = toAction.Do(toAction, !closeTag, i, runes)
//fmt.Println("newI: ", newI)
//fmt.Println("i: ", i)
//fmt.Println("string(runes[i]): ", string(runes[i]))
if newI != -1 {
i = newI
} else {
} else if out != "" {
i += len(toAction.Suffix + "&gt;")
}
//fmt.Println("i: ", i)
//fmt.Println("string(runes[i]): ", string(runes[i]))
//fmt.Println("out: ", out)
break
}
}
if out == "" {
//fmt.Println("no out")
msg += "&"
if closeTag {
i -= 5
} else {
@ -317,11 +326,19 @@ func PreparseMessage(msg string) string {
}
}
//fmt.Println("running autoclosers")
//fmt.Println("msg: ", msg)
for _, actionList := range tagToAction {
//if len(actionList) > 0 {
// fmt.Println("actionList: ", actionList)
//}
for _, toAction := range actionList {
if toAction.Depth > 1 {
//fmt.Printf("toAction: %+v\n", toAction)
if toAction.Depth > 0 {
//fmt.Println("autoclosing")
for ; toAction.Depth > 0; toAction.Depth-- {
_, out := toAction.Do(toAction, false, len(runes), runes)
//fmt.Println("out: ", out)
if out != "" {
msg += out
}

View File

@ -955,3 +955,54 @@ func passwordTest(t *testing.T, realPassword string, hashedPassword string) {
expect(t, err != common.ErrPasswordTooLong, "CheckPassword thinks the password is too long")
expect(t, err != nil, "The two shouldn't match!")
}
func TestPreparser(t *testing.T) {
var res string
var msgList []MEPair
// Note: The open tag is evaluated without knowledge of the close tag for efficiency and simplicity, so the parser autofills the associated close tag when it finds an open tag without a partner
msgList = addMEPair(msgList, "", "")
msgList = addMEPair(msgList, "hi", "hi")
msgList = addMEPair(msgList, "<b></b>", "<strong></strong>")
msgList = addMEPair(msgList, "<b>hi</b>", "<strong>hi</strong>")
msgList = addMEPair(msgList, "<s>hi</s>", "<del>hi</del>")
msgList = addMEPair(msgList, "<del>hi</del>", "<del>hi</del>")
msgList = addMEPair(msgList, "<u>hi</u>", "<u>hi</u>")
msgList = addMEPair(msgList, "<em>hi</em>", "<em>hi</em>")
msgList = addMEPair(msgList, "<i>hi</i>", "<em>hi</em>")
msgList = addMEPair(msgList, "<strong>hi</strong>", "<strong>hi</strong>")
msgList = addMEPair(msgList, "<b><i>hi</i></b>", "<strong><em>hi</em></strong>")
msgList = addMEPair(msgList, "<strong><em>hi</em></strong>", "<strong><em>hi</em></strong>")
msgList = addMEPair(msgList, "<b><i><b>hi</b></i></b>", "<strong><em><strong>hi</strong></em></strong>")
msgList = addMEPair(msgList, "<strong><em><strong>hi</strong></em></strong>", "<strong><em><strong>hi</strong></em></strong>")
msgList = addMEPair(msgList, "<div>hi</div>", "&lt;div&gt;hi&lt;/div&gt;")
msgList = addMEPair(msgList, "<span>hi</span>", "hi") // This is stripped since the editor (Trumbowyg) likes blasting useless spans
msgList = addMEPair(msgList, "<span >hi</span>", "hi")
msgList = addMEPair(msgList, "<span style='background-color: yellow;'>hi</span>", "hi")
msgList = addMEPair(msgList, "<span style='background-color: yellow;'>>hi</span>", "&gt;hi")
msgList = addMEPair(msgList, "<b>hi", "<strong>hi</strong>")
msgList = addMEPair(msgList, "hi</b>", "hi&lt;/b&gt;")
msgList = addMEPair(msgList, "</b>", "&lt;/b&gt;")
msgList = addMEPair(msgList, "</del>", "&lt;/del&gt;")
msgList = addMEPair(msgList, "</strong>", "&lt;/strong&gt;")
msgList = addMEPair(msgList, "<b>", "<strong></strong>")
msgList = addMEPair(msgList, "<span style='background-color: yellow;'>hi", "hi")
msgList = addMEPair(msgList, "hi</span>", "hi")
msgList = addMEPair(msgList, "</span>", "")
msgList = addMEPair(msgList, "<span></span>", "")
msgList = addMEPair(msgList, "<span ></span>", "")
msgList = addMEPair(msgList, "<></>", "&lt;&gt;&lt;/&gt;")
msgList = addMEPair(msgList, "</><>", "&lt;/&gt;&lt;&gt;")
msgList = addMEPair(msgList, "<>", "&lt;&gt;")
msgList = addMEPair(msgList, "</>", "&lt;/&gt;")
for _, item := range msgList {
res = common.PreparseMessage(item.Msg)
if res != item.Expects {
t.Error("Testing string '" + item.Msg + "'")
t.Error("Bad output:", "'"+res+"'")
//t.Error("Ouput in bytes:", []byte(res))
t.Error("Expected:", "'"+item.Expects+"'")
}
}
}