Minify minifies HTML data, it reads from r and writes to w.
(m *minify.M, w io.Writer, r io.Reader, _ map[string]string)
| 70 | |
| 71 | // Minify minifies HTML data, it reads from r and writes to w. |
| 72 | func (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 |