diff --git a/common/parser.go b/common/parser.go index d75fc87e..473d025b 100644 --- a/common/parser.go +++ b/common/parser.go @@ -6,6 +6,7 @@ import ( "regexp" "strconv" "strings" + "unicode/utf8" ) var SpaceGap = []byte(" ") @@ -170,19 +171,29 @@ type TagToAction struct { PartialMode bool } -// TODO: Write tests for this // TODO: Preparse Markdown and normalize it into HTML? func PreparseMessage(msg string) string { + //fmt.Println("initial msg: ", msg) + //fmt.Println("initial []byte(msg): ", []byte(msg)) + // TODO: Kick this check down a level into SanitiseBody? + if !utf8.ValidString(msg) { + return "" + } msg = strings.Replace(msg, "


", "\n\n", -1) msg = strings.Replace(msg, "

", "\n\n", -1) msg = strings.Replace(msg, "

", "", -1) + // TODO: Make this looser by moving it to the reverse HTML parser? msg = strings.Replace(msg, "
", "\n\n", -1) + msg = strings.Replace(msg, "
", "\n\n", -1) // XHTML style + msg = strings.Replace(msg, " ", "", -1) + msg = strings.Replace(msg, "\r", "", -1) // Windows artifact + //msg = strings.Replace(msg, "\n\n\n\n", "\n\n\n", -1) msg = RunSshook("preparse_preassign", msg) // There are a few useful cases for having spaces, but I'd like to stop the WYSIWYG from inserting random lines here and there msg = SanitiseBody(msg) - msg = strings.Replace(msg, " ", "", -1) //fmt.Println("before msg: ", msg) + //fmt.Println("before []byte(msg): ", []byte(msg)) var runes = []rune(msg) msg = "" var stepForward = func(i int, step int, runes []rune) int { @@ -348,7 +359,7 @@ func PreparseMessage(msg string) string { } //fmt.Println("msg: ", msg) - return shortcodeToUnicode(msg) + return strings.TrimSpace(shortcodeToUnicode(msg)) } // TODO: Test this diff --git a/misc_test.go b/misc_test.go index 35be362c..5253cc92 100644 --- a/misc_test.go +++ b/misc_test.go @@ -956,49 +956,100 @@ func passwordTest(t *testing.T, realPassword string, hashedPassword string) { expect(t, err != nil, "The two shouldn't match!") } +type METri struct { + Name string // Optional, this is here for tests involving invisible characters so we know what's going in + Msg string + Expects string +} + +func addMETri(msgList []METri, args ...string) []METri { + if len(args) < 2 { + panic("need 2 or more args") + } + if len(args) > 2 { + return append(msgList, METri{args[0], args[1], args[2]}) + } + return append(msgList, METri{"", args[0], args[1]}) +} + func TestPreparser(t *testing.T) { var res string - var msgList []MEPair + var msgList []METri // 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, "", "</>") + msgList = addMETri(msgList, "", "") + msgList = addMETri(msgList, " ", "") + msgList = addMETri(msgList, " hi", "hi") + msgList = addMETri(msgList, "hi ", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, ":grinning:", "😀") + msgList = addMETri(msgList, "😀", "😀") + msgList = addMETri(msgList, " ", "") + msgList = addMETri(msgList, "

", "") + msgList = addMETri(msgList, "

", "") + msgList = addMETri(msgList, "

", "") + + // Note: strings.TrimSpace strips newlines, if there's nothing before or after them + msgList = addMETri(msgList, "
", "") + msgList = addMETri(msgList, "
", "") + msgList = addMETri(msgList, "\\n", "\n", "") + msgList = addMETri(msgList, "\\n\\n", "\n\n", "") + msgList = addMETri(msgList, "\\n\\n\\n", "\n\n\n", "") + msgList = addMETri(msgList, "\\r\\n", "\r\n", "") // Windows style line ending + msgList = addMETri(msgList, "\\n\\r", "\n\r", "") + + msgList = addMETri(msgList, "ho
ho", "ho\n\nho") + msgList = addMETri(msgList, "ho
ho", "ho\n\nho") + msgList = addMETri(msgList, "ho\\nho", "ho\nho", "ho\nho") + msgList = addMETri(msgList, "ho\\n\\nho", "ho\n\nho", "ho\n\nho") + //msgList = addMETri(msgList, "ho\\n\\n\\n\\nho", "ho\n\n\n\nho", "ho\n\n\nho") + msgList = addMETri(msgList, "ho\\r\\nho", "ho\r\nho", "ho\nho") // Windows style line ending + msgList = addMETri(msgList, "ho\\n\\rho", "ho\n\rho", "ho\nho") + + msgList = addMETri(msgList, "", "") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "
hi
", "<div>hi</div>") + msgList = addMETri(msgList, "hi", "hi") // This is stripped since the editor (Trumbowyg) likes blasting useless spans + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, ">hi", ">hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi</b>") + msgList = addMETri(msgList, "
", "</b>") + msgList = addMETri(msgList, "", "</del>") + msgList = addMETri(msgList, "", "</strong>") + msgList = addMETri(msgList, "", "") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "hi", "hi") + msgList = addMETri(msgList, "", "") + msgList = addMETri(msgList, "", "") + msgList = addMETri(msgList, "", "") + msgList = addMETri(msgList, "<>", "<></>") + msgList = addMETri(msgList, "<>", "</><>") + msgList = addMETri(msgList, "<>", "<>") + msgList = addMETri(msgList, "", "</>") + //msgList = addMETri(msgList, "byte 0", string([]byte{0}), "") + msgList = addMETri(msgList, "byte 'a'", string([]byte{'a'}), "a") + //msgList = addMETri(msgList, "byte 255", string([]byte{255}), "") + //msgList = addMETri(msgList, "rune 0", string([]rune{0}), "") + // TODO: Do a test with invalid UTF-8 input for _, item := range msgList { res = common.PreparseMessage(item.Msg) if res != item.Expects { + if item.Name != "" { + t.Error("Name: ", item.Name) + } t.Error("Testing string '" + item.Msg + "'") t.Error("Bad output:", "'"+res+"'") //t.Error("Ouput in bytes:", []byte(res)) diff --git a/panel_routes.go b/panel_routes.go index 1a476438..5cd1c0ba 100644 --- a/panel_routes.go +++ b/panel_routes.go @@ -122,7 +122,10 @@ func routePanelDashboard(w http.ResponseWriter, r *http.Request, user common.Use // TODO: Localise these var gridElements = []common.GridElement{ - common.GridElement{"dash-version", "v" + version.String(), 0, "grid_istat stat_green", "", "", "Gosora is up-to-date :)"}, + // TODO: Implement a check for new versions of Gosora + //common.GridElement{"dash-version", "v" + version.String(), 0, "grid_istat stat_green", "", "", "Gosora is up-to-date :)"}, + common.GridElement{"dash-version", "v" + version.String(), 0, "grid_istat", "", "", ""}, + common.GridElement{"dash-cpu", "CPU: " + cpustr, 1, "grid_istat " + cpuColour, "", "", "The global CPU usage of this server"}, common.GridElement{"dash-ram", "RAM: " + ramstr, 2, "grid_istat " + ramColour, "", "", "The global RAM usage of this server"}, }