MCPcopy
hub / github.com/tdewolff/minify / Minify

Method Minify

html/html.go:72–546  ·  view source on GitHub ↗

Minify minifies HTML data, it reads from r and writes to w.

(m *minify.M, w io.Writer, r io.Reader, _ map[string]string)

Source from the content-addressed store, hash-verified

70
71// Minify minifies HTML data, it reads from r and writes to w.
72func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
73 var rawTagHash Hash
74 var rawTagMediatype []byte
75
76 if o.KeepConditionalComments {
77 fmt.Println("DEPRECATED: KeepConditionalComments is replaced by KeepSpecialComments")
78 o.KeepSpecialComments = true
79 o.KeepConditionalComments = false // omit next warning
80 }
81
82 omitSpace := true // if true the next leading space is omitted
83 inPre, inTemplate := false, false
84
85 attrMinifyBuffer := buffer.NewWriter(make([]byte, 0, 64))
86 attrByteBuffer := make([]byte, 0, 64)
87
88 z := parse.NewInput(r)
89 defer z.Restore()
90
91 l := html.NewTemplateLexer(z, o.TemplateDelims)
92 tb := NewTokenBuffer(z, l)
93 for {
94 t := *tb.Shift()
95 switch t.TokenType {
96 case html.ErrorToken:
97 if _, err := w.Write(nil); err != nil {
98 return err
99 }
100 if l.Err() == io.EOF {
101 return nil
102 }
103 return l.Err()
104 case html.DoctypeToken:
105 w.Write(doctypeBytes)
106 case html.CommentToken:
107 if o.KeepComments {
108 w.Write(t.Data)
109 } else if o.KeepSpecialComments {
110 if 6 < len(t.Text) && (bytes.HasPrefix(t.Text, []byte("[if ")) || bytes.HasSuffix(t.Text, []byte("[endif]")) || bytes.HasSuffix(t.Text, []byte("[endif]--"))) {
111 // [if ...] is always 7 or more characters, [endif] is only encountered for downlevel-revealed
112 // see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax
113 if bytes.HasPrefix(t.Data, []byte("<!--[if ")) && bytes.HasSuffix(t.Data, []byte("<![endif]-->")) { // downlevel-hidden
114 begin := bytes.IndexByte(t.Data, '>') + 1
115 end := len(t.Data) - len("<![endif]-->")
116 if begin < end {
117 w.Write(t.Data[:begin])
118 if err := o.Minify(m, w, buffer.NewReader(t.Data[begin:end]), nil); err != nil {
119 return minify.UpdateErrorPosition(err, z, t.Offset)
120 }
121 w.Write(t.Data[end:])
122 } else {
123 w.Write(t.Data) // malformed
124 }
125 } else {
126 w.Write(t.Data) // downlevel-revealed or short downlevel-hidden
127 }
128 } else if 1 < len(t.Text) && t.Text[0] == '#' {
129 // SSI tags

Callers 6

TestHTMLKeepEndTagsFunction · 0.95
TestHTMLKeepWhitespaceFunction · 0.95
TestHTMLKeepQuotesFunction · 0.95
TestHTMLGoTemplatesFunction · 0.95
TestHTMLPHPTemplatesFunction · 0.95

Implementers 9

cmdMinifierminify.go
Mminify.go
DummyMinifierminify_test.go
Minifierjs/js.go
Minifierhtml/html.go
Minifiercss/css.go
Minifierxml/xml.go
Minifierjson/json.go
Minifiersvg/svg.go

Calls 11

ShiftMethod · 0.95
PeekMethod · 0.95
AttributesMethod · 0.95
lenFunction · 0.85
MinifyMimetypeMethod · 0.80
EqualMethod · 0.80
NewTokenBufferFunction · 0.70
copyFunction · 0.50
stringFunction · 0.50
WriteMethod · 0.45
BytesMethod · 0.45

Tested by 6

TestHTMLKeepEndTagsFunction · 0.76
TestHTMLKeepWhitespaceFunction · 0.76
TestHTMLKeepQuotesFunction · 0.76
TestHTMLGoTemplatesFunction · 0.76
TestHTMLPHPTemplatesFunction · 0.76