From 6eb3429c25ef7820f5a49752f6bdeb0db9080fa1 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 26 Jun 2018 12:30:29 +1000 Subject: [PATCH] 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. --- common/parser.go | 23 +++++++++++++++++++--- misc_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/common/parser.go b/common/parser.go index d276e571..d75fc87e 100644 --- a/common/parser.go +++ b/common/parser.go @@ -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 += "</" + 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 + ">") } //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 } diff --git a/misc_test.go b/misc_test.go index a43aed38..35be362c 100644 --- a/misc_test.go +++ b/misc_test.go @@ -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, "", "") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "
hi
", "<div>hi</div>") + msgList = addMEPair(msgList, "hi", "hi") // This is stripped since the editor (Trumbowyg) likes blasting useless spans + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, ">hi", ">hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi</b>") + msgList = addMEPair(msgList, "", "</b>") + msgList = addMEPair(msgList, "", "</del>") + msgList = addMEPair(msgList, "", "</strong>") + msgList = addMEPair(msgList, "", "") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "hi", "hi") + msgList = addMEPair(msgList, "", "") + msgList = addMEPair(msgList, "", "") + msgList = addMEPair(msgList, "", "") + msgList = addMEPair(msgList, "<>", "<></>") + msgList = addMEPair(msgList, "<>", "</><>") + msgList = addMEPair(msgList, "<>", "<>") + msgList = addMEPair(msgList, "", "</>") + + 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+"'") + } + } +}