From 18fd30380d4bf6ac1a04b4c1dca3a120009d94e0 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Thu, 28 Jan 2016 10:15:18 +0100 Subject: [PATCH] Minify numbers in meta viewport Fixes #69, numbers are minified using the CSS specs --- html/html.go | 15 +++++++++++++++ html/html_test.go | 1 + 2 files changed, 16 insertions(+) diff --git a/html/html.go b/html/html.go index efb538c59c..6ba6877bda 100644 --- a/html/html.go +++ b/html/html.go @@ -285,6 +285,21 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st content.AttrVal = bytes.Replace(content.AttrVal, []byte(", "), []byte(","), -1) } else if parse.EqualFold(name.AttrVal, []byte("viewport")) { content.AttrVal = bytes.Replace(content.AttrVal, []byte(" "), []byte(""), -1) + for i := 0; i < len(content.AttrVal); i++ { + if content.AttrVal[i] == '=' && i+2 < len(content.AttrVal) { + i++ + if n := parse.Number(content.AttrVal[i:]); n > 0 { + minNum := minify.Number(content.AttrVal[i : i+n]) + if len(minNum) < n { + copy(content.AttrVal[i:i+len(minNum)], minNum) + copy(content.AttrVal[i+len(minNum):], content.AttrVal[i+n:]) + content.AttrVal = content.AttrVal[:len(content.AttrVal)+len(minNum)-n] + } + i += len(minNum) + } + i-- // mitigate for-loop increase + } + } } } } diff --git a/html/html_test.go b/html/html_test.go index c2715a8726..8b6151b515 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -53,6 +53,7 @@ func TestHTML(t *testing.T) { {``, ``}, {``, ``}, {``, ``}, + {``, ``}, // increase coverage {``, ``},