Browse Source

Compress inline JS too

Volodymyr Tkach 2 years ago
parent
commit
92794fb987
2 changed files with 27 additions and 10 deletions
  1. 20 9
      utils/http/helpers/helpers.go
  2. 7 1
      utils/http/helpers/helpers_test.go

+ 20 - 9
utils/http/helpers/helpers.go

@@ -34,6 +34,10 @@ import (
 var mHtml = regexp.MustCompile(`>[\n\t\r]+<`)
 var mHtml = regexp.MustCompile(`>[\n\t\r]+<`)
 var mHtmlLeft = regexp.MustCompile(`>[\n\t\r]+`)
 var mHtmlLeft = regexp.MustCompile(`>[\n\t\r]+`)
 var mHtmlRight = regexp.MustCompile(`[\n\t\r]+<`)
 var mHtmlRight = regexp.MustCompile(`[\n\t\r]+<`)
+var mScript = regexp.MustCompile(`<script>([^<]*)</script>`)
+var mScriptLine = regexp.MustCompile(`[\n\t\r]+`)
+var mScriptEqual = regexp.MustCompile(`[\n\t\r\s]+=[\n\t\r\s]+`)
+var mScriptDots = regexp.MustCompile(`:[\n\t\r\s]+"`)
 
 
 func ClientIP(r *http.Request) string {
 func ClientIP(r *http.Request) string {
 	ips := ClientIPs(r)
 	ips := ClientIPs(r)
@@ -139,6 +143,13 @@ func HandleTextXml(data string) http.Handler {
 }
 }
 
 
 func MinifyHtmlCode(str string) string {
 func MinifyHtmlCode(str string) string {
+	str = mScript.ReplaceAllStringFunc(str, func(m string) string {
+		s := strings.TrimSuffix(strings.TrimPrefix(m, "<script>"), "</script>")
+		s = mScriptLine.ReplaceAllString(s, "")
+		s = mScriptEqual.ReplaceAllString(s, "=")
+		s = mScriptDots.ReplaceAllString(s, ":\"")
+		return `<script>` + s + `</script>`
+	})
 	str = mHtml.ReplaceAllString(str, "><")
 	str = mHtml.ReplaceAllString(str, "><")
 	str = mHtmlLeft.ReplaceAllString(str, ">")
 	str = mHtmlLeft.ReplaceAllString(str, ">")
 	str = mHtmlRight.ReplaceAllString(str, "<")
 	str = mHtmlRight.ReplaceAllString(str, "<")
@@ -188,7 +199,7 @@ func RespondAsMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
 // if err != nil && !errors.Is(err, os.ErrNotExist) {
 // if err != nil && !errors.Is(err, os.ErrNotExist) {
 //
 //
 //	helpers.RespondAsBadRequest(w, r, err)
 //	helpers.RespondAsBadRequest(w, r, err)
-// 	return
+//	return
 //
 //
 // }
 // }
 //
 //
@@ -203,15 +214,15 @@ func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, err
 
 
 // Example:
 // Example:
 //
 //
-// if err = r.ParseForm(); err != nil {
-// 	helpers.RespondAsBadRequest(w, r, err)
-// 	return
-// }
+//	if err = r.ParseForm(); err != nil {
+//		helpers.RespondAsBadRequest(w, r, err)
+//		return
+//	}
 //
 //
-// if err = helpers.SetLanguageCookie(w, r); err != nil {
-// 		helpers.RespondAsBadRequest(w, r, err)
-// 		return
-// }
+//	if err = helpers.SetLanguageCookie(w, r); err != nil {
+//		helpers.RespondAsBadRequest(w, r, err)
+//		return
+//	}
 func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error {
 func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error {
 	var clang string
 	var clang string
 	if c, err := r.Cookie("lang"); err == nil {
 	if c, err := r.Cookie("lang"); err == nil {

+ 7 - 1
utils/http/helpers/helpers_test.go

@@ -377,12 +377,18 @@ var _ = Describe("helpers", func() {
 				<html lang="uk">
 				<html lang="uk">
 					<head>
 					<head>
 						<meta charset="utf-8" />
 						<meta charset="utf-8" />
+						<script>
+							var LangTexts = {
+								"empty": "Empty",
+								"full": "Full"
+							};
+						</script>
 					</head>
 					</head>
 					<body>
 					<body>
 						Index
 						Index
 					</body>
 					</body>
 				</html>
 				</html>
-			`)).To(Equal(`<!doctype html><html lang="uk"><head><meta charset="utf-8" /></head><body>Index</body></html>`))
+			`)).To(Equal(`<!doctype html><html lang="uk"><head><meta charset="utf-8" /><script>var LangTexts={"empty":"Empty","full":"Full"};</script></head><body>Index</body></html>`))
 
 
 			Expect(helpers.MinifyHtmlCode(`
 			Expect(helpers.MinifyHtmlCode(`
 				<div>
 				<div>