DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
(m *M, dataURI []byte)
| 53 | |
| 54 | // DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt. |
| 55 | func DataURI(m *M, dataURI []byte) []byte { |
| 56 | origData := parse.Copy(dataURI) |
| 57 | mediatype, data, err := parse.DataURI(dataURI) |
| 58 | if err != nil { |
| 59 | return dataURI |
| 60 | } |
| 61 | |
| 62 | data, _ = m.Bytes(string(mediatype), data) |
| 63 | base64Len := len(";base64") + base64.StdEncoding.EncodedLen(len(data)) |
| 64 | asciiLen := len(data) |
| 65 | for _, c := range data { |
| 66 | if parse.DataURIEncodingTable[c] { |
| 67 | asciiLen += 2 |
| 68 | } |
| 69 | if asciiLen > base64Len { |
| 70 | break |
| 71 | } |
| 72 | } |
| 73 | if len(origData) < base64Len && len(origData) < asciiLen { |
| 74 | return origData |
| 75 | } |
| 76 | if base64Len < asciiLen { |
| 77 | encoded := make([]byte, base64Len-len(";base64")) |
| 78 | base64.StdEncoding.Encode(encoded, data) |
| 79 | data = encoded |
| 80 | mediatype = append(mediatype, base64Bytes...) |
| 81 | } else { |
| 82 | data = parse.EncodeURL(data, parse.DataURIEncodingTable) |
| 83 | } |
| 84 | if len("text/plain") <= len(mediatype) && parse.EqualFold(mediatype[:len("text/plain")], textMimeBytes) { |
| 85 | mediatype = mediatype[len("text/plain"):] |
| 86 | } |
| 87 | for i := 0; i+len(";charset=us-ascii") <= len(mediatype); i++ { |
| 88 | // must start with semicolon and be followed by end of mediatype or semicolon |
| 89 | if mediatype[i] == ';' && parse.EqualFold(mediatype[i+1:i+len(";charset=us-ascii")], charsetASCIIBytes) && (i+len(";charset=us-ascii") >= len(mediatype) || mediatype[i+len(";charset=us-ascii")] == ';') { |
| 90 | mediatype = append(mediatype[:i], mediatype[i+len(";charset=us-ascii"):]...) |
| 91 | break |
| 92 | } |
| 93 | } |
| 94 | return append(append(append(dataBytes, mediatype...), ','), data...) |
| 95 | } |
| 96 | |
| 97 | // MaxInt is the maximum value of int. |
| 98 | const MaxInt = int(^uint(0) >> 1) |
searching dependent graphs…