OLD | NEW |
(Empty) | |
| 1 // Package charset provides common text encodings for HTML documents. |
| 2 // |
| 3 // The mapping from encoding labels to encodings is defined |
| 4 // at http://encoding.spec.whatwg.org. |
| 5 package charset |
| 6 |
| 7 import ( |
| 8 "errors" |
| 9 "strings" |
| 10 |
| 11 "code.google.com/p/go.text/encoding" |
| 12 "code.google.com/p/go.text/transform" |
| 13 ) |
| 14 |
| 15 // Encoding returns the encoding with the specified label, or nil if label is |
| 16 // not one of the standard encodings for HTML. Matching is case-insensitive |
| 17 // and ignores leading and trailing whitespace. |
| 18 func Encoding(label string) encoding.Encoding { |
| 19 label = strings.ToLower(strings.Trim(label, "\t\n\r\f ")) |
| 20 return encodings[label] |
| 21 } |
| 22 |
| 23 type utf8Encoding struct{} |
| 24 |
| 25 func (utf8Encoding) NewDecoder() transform.Transformer { |
| 26 return transform.Nop |
| 27 } |
| 28 |
| 29 func (utf8Encoding) NewEncoder() transform.Transformer { |
| 30 return transform.Nop |
| 31 } |
| 32 |
| 33 var ErrReplacementEncoding = errors.New("charset: ISO-2022-CN and ISO-2022-KR ar
e obsolete encodings for HTML") |
| 34 |
| 35 type errorTransformer struct { |
| 36 err error |
| 37 } |
| 38 |
| 39 func (t errorTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int
, err error) { |
| 40 return 0, 0, t.err |
| 41 } |
| 42 |
| 43 type replacementEncoding struct{} |
| 44 |
| 45 func (replacementEncoding) NewDecoder() transform.Transformer { |
| 46 return errorTransformer{ErrReplacementEncoding} |
| 47 } |
| 48 |
| 49 func (replacementEncoding) NewEncoder() transform.Transformer { |
| 50 return transform.Nop |
| 51 } |
OLD | NEW |