Browse Source

Optimize, escape js var value for modal

Vova Tkach 6 years ago
parent
commit
a49c288352
3 changed files with 14 additions and 6 deletions
  1. 2 3
      engine/wrapper/wrapper.go
  2. 3 3
      modules/modules.go
  3. 9 0
      utils/utils.go

+ 2 - 3
engine/wrapper/wrapper.go

@@ -8,7 +8,6 @@ import (
 	"html/template"
 	"net/http"
 	"os"
-	"strings"
 
 	"golang-fave/consts"
 	"golang-fave/logger"
@@ -144,13 +143,13 @@ func (this *Wrapper) Write(data string) {
 func (this *Wrapper) MsgSuccess(msg string) {
 	this.Write(fmt.Sprintf(
 		`fave.ShowMsgSuccess('Success!', '%s', false);`,
-		strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
+		utils.JavaScriptVarValue(msg)))
 }
 
 func (this *Wrapper) MsgError(msg string) {
 	this.Write(fmt.Sprintf(
 		`fave.ShowMsgError('Error!', '%s', true);`,
-		strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
+		utils.JavaScriptVarValue(msg)))
 }
 
 func (this *Wrapper) RenderToString(tcont []byte, data interface{}) string {

+ 3 - 3
modules/modules.go

@@ -365,9 +365,9 @@ func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
 				Caption:            "Fave " + consts.ServerVersion,
 				BodyClasses:        body_class,
 				UserId:             wrap.User.A_id,
-				UserFirstName:      wrap.User.A_first_name,
-				UserLastName:       wrap.User.A_last_name,
-				UserEmail:          wrap.User.A_email,
+				UserFirstName:      utils.JavaScriptVarValue(wrap.User.A_first_name),
+				UserLastName:       utils.JavaScriptVarValue(wrap.User.A_last_name),
+				UserEmail:          utils.JavaScriptVarValue(wrap.User.A_email),
 				UserPassword:       "",
 				UserAvatarLink:     "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
 				NavBarModules:      template.HTML(this.getNavMenuModules(wrap, false)),

+ 9 - 0
utils/utils.go

@@ -242,3 +242,12 @@ func ExtractGetParams(str string) string {
 	}
 	return "?" + str[i+1:]
 }
+
+func JavaScriptVarValue(str string) string {
+	return strings.Replace(
+		strings.Replace(str, `'`, `’`, -1),
+		`"`,
+		`”`,
+		-1,
+	)
+}