1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package xml implements a simple XML 1.0 parser that
6 // understands XML name spaces.
10 // Annotated XML spec: http://www.xml.com/axml/testaxml.htm
11 // XML name spaces: http://www.w3.org/TR/REC-xml-names/
14 // Test error handling.
28 // A SyntaxError represents a syntax error in the XML input stream.
29 type SyntaxError struct {
34 func (e *SyntaxError) Error() string {
35 return "XML syntax error on line " + strconv.Itoa(e.Line) + ": " + e.Msg
38 // A Name represents an XML name (Local) annotated with a name space
39 // identifier (Space). In tokens returned by Decoder.Token, the Space
40 // identifier is given as a canonical URL, not the short prefix used in
41 // the document being parsed.
43 // As a special case, XML namespace declarations will use the literal
44 // string "xmlns" for the Space field instead of the fully resolved URL.
45 // See Encoder.EncodeToken for more information on namespace encoding
51 // isNamespace reports whether the name is a namespace-defining name.
52 func (name Name) isNamespace() bool {
53 return name.Local == "xmlns" || name.Space == "xmlns"
56 // An Attr represents an attribute in an XML element (Name=Value).
62 // A Token is an interface holding one of the token types:
63 // StartElement, EndElement, CharData, Comment, ProcInst, or Directive.
64 type Token interface{}
66 // A StartElement represents an XML start element.
67 type StartElement struct {
72 func (e StartElement) Copy() StartElement {
73 attrs := make([]Attr, len(e.Attr))
79 // End returns the corresponding XML end element.
80 func (e StartElement) End() EndElement {
81 return EndElement{e.Name}
84 // setDefaultNamespace sets the namespace of the element
85 // as the default for all elements contained within it.
86 func (e *StartElement) setDefaultNamespace() {
87 if e.Name.Space == "" {
88 // If there's no namespace on the element, don't
89 // set the default. Strictly speaking this might be wrong, as
90 // we can't tell if the element had no namespace set
91 // or was just using the default namespace.
94 // Don't add a default name space if there's already one set.
95 for _, attr := range e.Attr {
96 if attr.Name.Space == "" && attr.Name.Local == "xmlns" {
100 e.Attr = append(e.Attr, Attr{
108 // An EndElement represents an XML end element.
109 type EndElement struct {
113 // A CharData represents XML character data (raw text),
114 // in which XML escape sequences have been replaced by
115 // the characters they represent.
118 func makeCopy(b []byte) []byte {
119 b1 := make([]byte, len(b))
124 func (c CharData) Copy() CharData { return CharData(makeCopy(c)) }
126 // A Comment represents an XML comment of the form <!--comment-->.
127 // The bytes do not include the <!-- and --> comment markers.
130 func (c Comment) Copy() Comment { return Comment(makeCopy(c)) }
132 // A ProcInst represents an XML processing instruction of the form <?target inst?>
133 type ProcInst struct {
138 func (p ProcInst) Copy() ProcInst {
139 p.Inst = makeCopy(p.Inst)
143 // A Directive represents an XML directive of the form <!text>.
144 // The bytes do not include the <! and > markers.
145 type Directive []byte
147 func (d Directive) Copy() Directive { return Directive(makeCopy(d)) }
149 // CopyToken returns a copy of a Token.
150 func CopyToken(t Token) Token {
151 switch v := t.(type) {
166 // A Decoder represents an XML parser reading a particular input stream.
167 // The parser assumes that its input is encoded in UTF-8.
168 type Decoder struct {
169 // Strict defaults to true, enforcing the requirements
170 // of the XML specification.
171 // If set to false, the parser allows input containing common
173 // * If an element is missing an end tag, the parser invents
174 // end tags as necessary to keep the return values from Token
175 // properly balanced.
176 // * In attribute values and character data, unknown or malformed
177 // character entities (sequences beginning with &) are left alone.
182 // d.AutoClose = HTMLAutoClose;
183 // d.Entity = HTMLEntity
185 // creates a parser that can handle typical HTML.
187 // Strict mode does not enforce the requirements of the XML name spaces TR.
188 // In particular it does not reject name space tags using undefined prefixes.
189 // Such tags are recorded with the unknown prefix as the name space URL.
192 // When Strict == false, AutoClose indicates a set of elements to
193 // consider closed immediately after they are opened, regardless
194 // of whether an end element is present.
197 // Entity can be used to map non-standard entity names to string replacements.
198 // The parser behaves as if these standard mappings are present in the map,
199 // regardless of the actual map content:
206 Entity map[string]string
208 // CharsetReader, if non-nil, defines a function to generate
209 // charset-conversion readers, converting from the provided
210 // non-UTF-8 charset into UTF-8. If CharsetReader is nil or
211 // returns an error, parsing stops with an error. One of the
212 // the CharsetReader's result values must be non-nil.
213 CharsetReader func(charset string, input io.Reader) (io.Reader, error)
215 // DefaultSpace sets the default name space used for unadorned tags,
216 // as if the entire XML stream were wrapped in an element containing
217 // the attribute xmlns="DefaultSpace".
236 // NewDecoder creates a new XML parser reading from r.
237 // If r does not implement io.ByteReader, NewDecoder will
238 // do its own buffering.
239 func NewDecoder(r io.Reader) *Decoder {
241 ns: make(map[string]string),
250 // Token returns the next XML token in the input stream.
251 // At the end of the input stream, Token returns nil, io.EOF.
253 // Slices of bytes in the returned token data refer to the
254 // parser's internal buffer and remain valid only until the next
255 // call to Token. To acquire a copy of the bytes, call CopyToken
256 // or the token's Copy method.
258 // Token expands self-closing elements such as <br/>
259 // into separate start and end elements returned by successive calls.
261 // Token guarantees that the StartElement and EndElement
262 // tokens it returns are properly nested and matched:
263 // if Token encounters an unexpected end element,
264 // it will return an error.
266 // Token implements XML name spaces as described by
267 // http://www.w3.org/TR/REC-xml-names/. Each of the
268 // Name structures contained in the Token has the Space
269 // set to the URL identifying its name space when known.
270 // If Token encounters an unrecognized name space prefix,
271 // it uses the prefix as the Space rather than report an error.
272 func (d *Decoder) Token() (t Token, err error) {
273 if d.stk != nil && d.stk.kind == stkEOF {
277 if d.nextToken != nil {
280 } else if t, err = d.rawToken(); err != nil {
285 if t1, ok := d.autoClose(t); ok {
290 switch t1 := t.(type) {
292 // In XML name spaces, the translations listed in the
293 // attributes apply to the element name and
294 // to the other attribute names, so process
295 // the translations first.
296 for _, a := range t1.Attr {
297 if a.Name.Space == "xmlns" {
298 v, ok := d.ns[a.Name.Local]
299 d.pushNs(a.Name.Local, v, ok)
300 d.ns[a.Name.Local] = a.Value
302 if a.Name.Space == "" && a.Name.Local == "xmlns" {
303 // Default space for untagged names
310 d.translate(&t1.Name, true)
311 for i := range t1.Attr {
312 d.translate(&t1.Attr[i].Name, false)
314 d.pushElement(t1.Name)
318 d.translate(&t1.Name, true)
319 if !d.popElement(&t1) {
327 const xmlURL = "http://www.w3.org/XML/1998/namespace"
329 // Apply name space translation to name n.
330 // The default name space (for Space=="")
331 // applies only to element names, not to attribute names.
332 func (d *Decoder) translate(n *Name, isElementName bool) {
334 case n.Space == "xmlns":
336 case n.Space == "" && !isElementName:
338 case n.Space == "xml":
340 case n.Space == "" && n.Local == "xmlns":
343 if v, ok := d.ns[n.Space]; ok {
345 } else if n.Space == "" {
346 n.Space = d.DefaultSpace
350 func (d *Decoder) switchToReader(r io.Reader) {
351 // Get efficient byte at a time reader.
352 // Assume that if reader has its own
353 // ReadByte, it's efficient enough.
354 // Otherwise, use bufio.
355 if rb, ok := r.(io.ByteReader); ok {
358 d.r = bufio.NewReader(r)
362 // Parsing state - stack holds old name space translations
363 // and the current set of open elements. The translations to pop when
364 // ending a given tag are *below* it on the stack, which is
365 // more work but forced on us by XML.
379 func (d *Decoder) push(kind int) *stack {
392 func (d *Decoder) pop() *stack {
402 // Record that after the current element is finished
403 // (that element is already pushed on the stack)
404 // Token should return EOF until popEOF is called.
405 func (d *Decoder) pushEOF() {
406 // Walk down stack to find Start.
407 // It might not be the top, because there might be stkNs
410 for start.kind != stkStart {
413 // The stkNs entries below a start are associated with that
414 // element too; skip over them.
415 for start.next != nil && start.next.kind == stkNs {
430 // The element must have been finished, so the EOF should be at the top of the stack.
431 func (d *Decoder) popEOF() bool {
432 if d.stk == nil || d.stk.kind != stkEOF {
439 // Record that we are starting an element with the given name.
440 func (d *Decoder) pushElement(name Name) {
441 s := d.push(stkStart)
445 // Record that we are changing the value of ns[local].
446 // The old value is url, ok.
447 func (d *Decoder) pushNs(local string, url string, ok bool) {
454 // Creates a SyntaxError with the current line number.
455 func (d *Decoder) syntaxError(msg string) error {
456 return &SyntaxError{Msg: msg, Line: d.line}
459 // Record that we are ending an element with the given name.
460 // The name must match the record at the top of the stack,
461 // which must be a pushElement record.
462 // After popping the element, apply any undo records from
463 // the stack to restore the name translations that existed
464 // before we saw this element.
465 func (d *Decoder) popElement(t *EndElement) bool {
469 case s == nil || s.kind != stkStart:
470 d.err = d.syntaxError("unexpected end element </" + name.Local + ">")
472 case s.name.Local != name.Local:
479 d.err = d.syntaxError("element <" + s.name.Local + "> closed by </" + name.Local + ">")
481 case s.name.Space != name.Space:
482 d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space +
483 "closed by </" + name.Local + "> in space " + name.Space)
487 // Pop stack until a Start or EOF is on the top, undoing the
488 // translations that were associated with the element we just closed.
489 for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF {
492 d.ns[s.name.Local] = s.name.Space
494 delete(d.ns, s.name.Local)
501 // If the top element on the stack is autoclosing and
502 // t is not the end tag, invent the end tag.
503 func (d *Decoder) autoClose(t Token) (Token, bool) {
504 if d.stk == nil || d.stk.kind != stkStart {
507 name := strings.ToLower(d.stk.name.Local)
508 for _, s := range d.AutoClose {
509 if strings.ToLower(s) == name {
510 // This one should be auto closed if t doesn't close it.
511 et, ok := t.(EndElement)
512 if !ok || et.Name.Local != name {
513 return EndElement{d.stk.name}, true
521 var errRawToken = errors.New("xml: cannot use RawToken from UnmarshalXML method")
523 // RawToken is like Token but does not verify that
524 // start and end elements match and does not translate
525 // name space prefixes to their corresponding URLs.
526 func (d *Decoder) RawToken() (Token, error) {
527 if d.unmarshalDepth > 0 {
528 return nil, errRawToken
533 func (d *Decoder) rawToken() (Token, error) {
538 // The last element we read was self-closing and
539 // we returned just the StartElement half.
540 // Return the EndElement half now.
542 return EndElement{d.toClose}, nil
553 data := d.text(-1, false)
557 return CharData(data), nil
560 if b, ok = d.mustgetc(); !ok {
567 if name, ok = d.nsname(); !ok {
569 d.err = d.syntaxError("expected element name after </")
574 if b, ok = d.mustgetc(); !ok {
578 d.err = d.syntaxError("invalid characters between </" + name.Local + " and >")
581 return EndElement{name}, nil
584 // <?: Processing instruction.
586 if target, ok = d.name(); !ok {
588 d.err = d.syntaxError("expected target name after <?")
596 if b, ok = d.mustgetc(); !ok {
600 if b0 == '?' && b == '>' {
605 data := d.buf.Bytes()
606 data = data[0 : len(data)-2] // chop ?>
609 content := string(data)
610 ver := procInst("version", content)
611 if ver != "" && ver != "1.0" {
612 d.err = fmt.Errorf("xml: unsupported version %q; only version 1.0 is supported", ver)
615 enc := procInst("encoding", content)
616 if enc != "" && enc != "utf-8" && enc != "UTF-8" {
617 if d.CharsetReader == nil {
618 d.err = fmt.Errorf("xml: encoding %q declared but Decoder.CharsetReader is nil", enc)
621 newr, err := d.CharsetReader(enc, d.r.(io.Reader))
623 d.err = fmt.Errorf("xml: opening charset %q: %v", enc, err)
627 panic("CharsetReader returned a nil Reader for charset " + enc)
629 d.switchToReader(newr)
632 return ProcInst{target, data}, nil
635 // <!: Maybe comment, maybe CDATA.
636 if b, ok = d.mustgetc(); !ok {
641 // Probably <!-- for a comment.
642 if b, ok = d.mustgetc(); !ok {
646 d.err = d.syntaxError("invalid sequence <!- not part of <!--")
649 // Look for terminator.
653 if b, ok = d.mustgetc(); !ok {
657 if b0 == '-' && b1 == '-' && b == '>' {
662 data := d.buf.Bytes()
663 data = data[0 : len(data)-3] // chop -->
664 return Comment(data), nil
667 // Probably <![CDATA[.
668 for i := 0; i < 6; i++ {
669 if b, ok = d.mustgetc(); !ok {
672 if b != "CDATA["[i] {
673 d.err = d.syntaxError("invalid <![ sequence")
677 // Have <![CDATA[. Read text until ]]>.
678 data := d.text(-1, true)
682 return CharData(data), nil
685 // Probably a directive: <!DOCTYPE ...>, <!ENTITY ...>, etc.
686 // We don't care, but accumulate for caller. Quoted angle
687 // brackets do not count for nesting.
693 if b, ok = d.mustgetc(); !ok {
696 if inquote == 0 && b == '>' && depth == 0 {
706 // in quotes, no special action
708 case b == '\'' || b == '"':
711 case b == '>' && inquote == 0:
714 case b == '<' && inquote == 0:
715 // Look for <!-- to begin comment.
717 for i := 0; i < len(s); i++ {
718 if b, ok = d.mustgetc(); !ok {
722 for j := 0; j < i; j++ {
723 d.buf.WriteByte(s[j])
730 // Remove < that was written above.
731 d.buf.Truncate(d.buf.Len() - 1)
733 // Look for terminator.
736 if b, ok = d.mustgetc(); !ok {
739 if b0 == '-' && b1 == '-' && b == '>' {
746 return Directive(d.buf.Bytes()), nil
749 // Must be an open element like <a href="foo">
757 if name, ok = d.nsname(); !ok {
759 d.err = d.syntaxError("expected element name after <")
767 if b, ok = d.mustgetc(); !ok {
772 if b, ok = d.mustgetc(); !ok {
776 d.err = d.syntaxError("expected /> in element")
788 nCap := 2 * cap(attr)
792 nattr := make([]Attr, n, nCap)
798 if a.Name, ok = d.nsname(); !ok {
800 d.err = d.syntaxError("expected attribute name in element")
805 if b, ok = d.mustgetc(); !ok {
810 d.err = d.syntaxError("attribute name without = in element")
814 a.Value = a.Name.Local
822 a.Value = string(data)
829 return StartElement{name, attr}, nil
832 func (d *Decoder) attrval() []byte {
833 b, ok := d.mustgetc()
837 // Handle quoted attribute values
838 if b == '"' || b == '\'' {
839 return d.text(int(b), false)
841 // Handle unquoted attribute values for strict parsers
843 d.err = d.syntaxError("unquoted or missing attribute value in element")
846 // Handle unquoted attribute values for unstrict parsers
854 // http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2
855 if 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' ||
856 '0' <= b && b <= '9' || b == '_' || b == ':' || b == '-' {
866 // Skip spaces if any
867 func (d *Decoder) space() {
874 case ' ', '\r', '\n', '\t':
882 // Read a single byte.
883 // If there is no byte to read, return ok==false
884 // and leave the error in d.err.
885 // Maintain line number.
886 func (d *Decoder) getc() (b byte, ok bool) {
894 b, d.err = d.r.ReadByte()
909 // InputOffset returns the input stream byte offset of the current decoder position.
910 // The offset gives the location of the end of the most recently returned token
911 // and the beginning of the next token.
912 func (d *Decoder) InputOffset() int64 {
916 // Return saved offset.
917 // If we did ungetc (nextByte >= 0), have to back up one.
918 func (d *Decoder) savedOffset() int {
926 // Must read a single byte.
927 // If there is no byte to read,
928 // set d.err to SyntaxError("unexpected EOF")
929 // and return ok==false
930 func (d *Decoder) mustgetc() (b byte, ok bool) {
931 if b, ok = d.getc(); !ok {
933 d.err = d.syntaxError("unexpected EOF")
939 // Unread a single byte.
940 func (d *Decoder) ungetc(b byte) {
948 var entity = map[string]int{
956 // Read plain text section (XML calls it character data).
957 // If quote >= 0, we are in a quoted string and need to find the matching quote.
958 // If cdata == true, we are in a <![CDATA[ section and need to find ]]>.
959 // On failure return nil and leave the error in d.err.
960 func (d *Decoder) text(quote int, cdata bool) []byte {
970 d.err = d.syntaxError("unexpected EOF in CDATA section")
977 // <![CDATA[ section ends with ]]>.
978 // It is an error for ]]> to appear in ordinary text.
979 if b0 == ']' && b1 == ']' && b == '>' {
984 d.err = d.syntaxError("unescaped ]]> not in CDATA section")
988 // Stop reading text if we see a <.
989 if b == '<' && !cdata {
991 d.err = d.syntaxError("unescaped < inside quoted string")
997 if quote >= 0 && b == byte(quote) {
1000 if b == '&' && !cdata {
1001 // Read escaped character expression up to semicolon.
1002 // XML in all its glory allows a document to define and use
1003 // its own character names with <!ENTITY ...> directives.
1004 // Parsers are required to recognize lt, gt, amp, apos, and quot
1005 // even if they have not been declared.
1006 before := d.buf.Len()
1007 d.buf.WriteByte('&')
1011 if b, ok = d.mustgetc(); !ok {
1016 if b, ok = d.mustgetc(); !ok {
1023 if b, ok = d.mustgetc(); !ok {
1027 start := d.buf.Len()
1028 for '0' <= b && b <= '9' ||
1029 base == 16 && 'a' <= b && b <= 'f' ||
1030 base == 16 && 'A' <= b && b <= 'F' {
1032 if b, ok = d.mustgetc(); !ok {
1039 s := string(d.buf.Bytes()[start:])
1040 d.buf.WriteByte(';')
1041 n, err := strconv.ParseUint(s, base, 64)
1042 if err == nil && n <= unicode.MaxRune {
1055 if b, ok = d.mustgetc(); !ok {
1061 name := d.buf.Bytes()[before+1:]
1062 d.buf.WriteByte(';')
1065 if r, ok := entity[s]; ok {
1068 } else if d.Entity != nil {
1069 text, haveText = d.Entity[s]
1076 d.buf.Truncate(before)
1077 d.buf.Write([]byte(text))
1085 ent := string(d.buf.Bytes()[before:])
1086 if ent[len(ent)-1] != ';' {
1087 ent += " (no semicolon)"
1089 d.err = d.syntaxError("invalid character entity " + ent)
1093 // We must rewrite unescaped \r and \r\n into \n.
1095 d.buf.WriteByte('\n')
1096 } else if b1 == '\r' && b == '\n' {
1097 // Skip \r\n--we already wrote \n.
1104 data := d.buf.Bytes()
1105 data = data[0 : len(data)-trunc]
1107 // Inspect each rune for being a disallowed character.
1110 r, size := utf8.DecodeRune(buf)
1111 if r == utf8.RuneError && size == 1 {
1112 d.err = d.syntaxError("invalid UTF-8")
1116 if !isInCharacterRange(r) {
1117 d.err = d.syntaxError(fmt.Sprintf("illegal character code %U", r))
1125 // Decide whether the given rune is in the XML Character Range, per
1126 // the Char production of http://www.xml.com/axml/testaxml.htm,
1127 // Section 2.2 Characters.
1128 func isInCharacterRange(r rune) (inrange bool) {
1132 r >= 0x20 && r <= 0xDF77 ||
1133 r >= 0xE000 && r <= 0xFFFD ||
1134 r >= 0x10000 && r <= 0x10FFFF
1137 // Get name space name: name with a : stuck in the middle.
1138 // The part before the : is the name space identifier.
1139 func (d *Decoder) nsname() (name Name, ok bool) {
1144 i := strings.Index(s, ":")
1149 name.Local = s[i+1:]
1154 // Get name: /first(first|second)*/
1155 // Do not set d.err if the name is missing (unless unexpected EOF is received):
1156 // let the caller provide better context.
1157 func (d *Decoder) name() (s string, ok bool) {
1163 // Now we check the characters.
1166 d.err = d.syntaxError("invalid XML name: " + string(b))
1169 return string(b), true
1172 // Read a name and append its bytes to d.buf.
1173 // The name is delimited by any single-byte character not valid in names.
1174 // All multi-byte characters are accepted; the caller must check their validity.
1175 func (d *Decoder) readName() (ok bool) {
1177 if b, ok = d.mustgetc(); !ok {
1180 if b < utf8.RuneSelf && !isNameByte(b) {
1187 if b, ok = d.mustgetc(); !ok {
1190 if b < utf8.RuneSelf && !isNameByte(b) {
1199 func isNameByte(c byte) bool {
1200 return 'A' <= c && c <= 'Z' ||
1201 'a' <= c && c <= 'z' ||
1202 '0' <= c && c <= '9' ||
1203 c == '_' || c == ':' || c == '.' || c == '-'
1206 func isName(s []byte) bool {
1210 c, n := utf8.DecodeRune(s)
1211 if c == utf8.RuneError && n == 1 {
1214 if !unicode.Is(first, c) {
1219 c, n = utf8.DecodeRune(s)
1220 if c == utf8.RuneError && n == 1 {
1223 if !unicode.Is(first, c) && !unicode.Is(second, c) {
1230 func isNameString(s string) bool {
1234 c, n := utf8.DecodeRuneInString(s)
1235 if c == utf8.RuneError && n == 1 {
1238 if !unicode.Is(first, c) {
1243 c, n = utf8.DecodeRuneInString(s)
1244 if c == utf8.RuneError && n == 1 {
1247 if !unicode.Is(first, c) && !unicode.Is(second, c) {
1254 // These tables were generated by cut and paste from Appendix B of
1255 // the XML spec at http://www.xml.com/axml/testaxml.htm
1256 // and then reformatting. First corresponds to (Letter | '_' | ':')
1257 // and second corresponds to NameChar.
1259 var first = &unicode.RangeTable{
1260 R16: []unicode.Range16{
1261 {0x003A, 0x003A, 1},
1262 {0x0041, 0x005A, 1},
1263 {0x005F, 0x005F, 1},
1264 {0x0061, 0x007A, 1},
1265 {0x00C0, 0x00D6, 1},
1266 {0x00D8, 0x00F6, 1},
1267 {0x00F8, 0x00FF, 1},
1268 {0x0100, 0x0131, 1},
1269 {0x0134, 0x013E, 1},
1270 {0x0141, 0x0148, 1},
1271 {0x014A, 0x017E, 1},
1272 {0x0180, 0x01C3, 1},
1273 {0x01CD, 0x01F0, 1},
1274 {0x01F4, 0x01F5, 1},
1275 {0x01FA, 0x0217, 1},
1276 {0x0250, 0x02A8, 1},
1277 {0x02BB, 0x02C1, 1},
1278 {0x0386, 0x0386, 1},
1279 {0x0388, 0x038A, 1},
1280 {0x038C, 0x038C, 1},
1281 {0x038E, 0x03A1, 1},
1282 {0x03A3, 0x03CE, 1},
1283 {0x03D0, 0x03D6, 1},
1284 {0x03DA, 0x03E0, 2},
1285 {0x03E2, 0x03F3, 1},
1286 {0x0401, 0x040C, 1},
1287 {0x040E, 0x044F, 1},
1288 {0x0451, 0x045C, 1},
1289 {0x045E, 0x0481, 1},
1290 {0x0490, 0x04C4, 1},
1291 {0x04C7, 0x04C8, 1},
1292 {0x04CB, 0x04CC, 1},
1293 {0x04D0, 0x04EB, 1},
1294 {0x04EE, 0x04F5, 1},
1295 {0x04F8, 0x04F9, 1},
1296 {0x0531, 0x0556, 1},
1297 {0x0559, 0x0559, 1},
1298 {0x0561, 0x0586, 1},
1299 {0x05D0, 0x05EA, 1},
1300 {0x05F0, 0x05F2, 1},
1301 {0x0621, 0x063A, 1},
1302 {0x0641, 0x064A, 1},
1303 {0x0671, 0x06B7, 1},
1304 {0x06BA, 0x06BE, 1},
1305 {0x06C0, 0x06CE, 1},
1306 {0x06D0, 0x06D3, 1},
1307 {0x06D5, 0x06D5, 1},
1308 {0x06E5, 0x06E6, 1},
1309 {0x0905, 0x0939, 1},
1310 {0x093D, 0x093D, 1},
1311 {0x0958, 0x0961, 1},
1312 {0x0985, 0x098C, 1},
1313 {0x098F, 0x0990, 1},
1314 {0x0993, 0x09A8, 1},
1315 {0x09AA, 0x09B0, 1},
1316 {0x09B2, 0x09B2, 1},
1317 {0x09B6, 0x09B9, 1},
1318 {0x09DC, 0x09DD, 1},
1319 {0x09DF, 0x09E1, 1},
1320 {0x09F0, 0x09F1, 1},
1321 {0x0A05, 0x0A0A, 1},
1322 {0x0A0F, 0x0A10, 1},
1323 {0x0A13, 0x0A28, 1},
1324 {0x0A2A, 0x0A30, 1},
1325 {0x0A32, 0x0A33, 1},
1326 {0x0A35, 0x0A36, 1},
1327 {0x0A38, 0x0A39, 1},
1328 {0x0A59, 0x0A5C, 1},
1329 {0x0A5E, 0x0A5E, 1},
1330 {0x0A72, 0x0A74, 1},
1331 {0x0A85, 0x0A8B, 1},
1332 {0x0A8D, 0x0A8D, 1},
1333 {0x0A8F, 0x0A91, 1},
1334 {0x0A93, 0x0AA8, 1},
1335 {0x0AAA, 0x0AB0, 1},
1336 {0x0AB2, 0x0AB3, 1},
1337 {0x0AB5, 0x0AB9, 1},
1338 {0x0ABD, 0x0AE0, 0x23},
1339 {0x0B05, 0x0B0C, 1},
1340 {0x0B0F, 0x0B10, 1},
1341 {0x0B13, 0x0B28, 1},
1342 {0x0B2A, 0x0B30, 1},
1343 {0x0B32, 0x0B33, 1},
1344 {0x0B36, 0x0B39, 1},
1345 {0x0B3D, 0x0B3D, 1},
1346 {0x0B5C, 0x0B5D, 1},
1347 {0x0B5F, 0x0B61, 1},
1348 {0x0B85, 0x0B8A, 1},
1349 {0x0B8E, 0x0B90, 1},
1350 {0x0B92, 0x0B95, 1},
1351 {0x0B99, 0x0B9A, 1},
1352 {0x0B9C, 0x0B9C, 1},
1353 {0x0B9E, 0x0B9F, 1},
1354 {0x0BA3, 0x0BA4, 1},
1355 {0x0BA8, 0x0BAA, 1},
1356 {0x0BAE, 0x0BB5, 1},
1357 {0x0BB7, 0x0BB9, 1},
1358 {0x0C05, 0x0C0C, 1},
1359 {0x0C0E, 0x0C10, 1},
1360 {0x0C12, 0x0C28, 1},
1361 {0x0C2A, 0x0C33, 1},
1362 {0x0C35, 0x0C39, 1},
1363 {0x0C60, 0x0C61, 1},
1364 {0x0C85, 0x0C8C, 1},
1365 {0x0C8E, 0x0C90, 1},
1366 {0x0C92, 0x0CA8, 1},
1367 {0x0CAA, 0x0CB3, 1},
1368 {0x0CB5, 0x0CB9, 1},
1369 {0x0CDE, 0x0CDE, 1},
1370 {0x0CE0, 0x0CE1, 1},
1371 {0x0D05, 0x0D0C, 1},
1372 {0x0D0E, 0x0D10, 1},
1373 {0x0D12, 0x0D28, 1},
1374 {0x0D2A, 0x0D39, 1},
1375 {0x0D60, 0x0D61, 1},
1376 {0x0E01, 0x0E2E, 1},
1377 {0x0E30, 0x0E30, 1},
1378 {0x0E32, 0x0E33, 1},
1379 {0x0E40, 0x0E45, 1},
1380 {0x0E81, 0x0E82, 1},
1381 {0x0E84, 0x0E84, 1},
1382 {0x0E87, 0x0E88, 1},
1383 {0x0E8A, 0x0E8D, 3},
1384 {0x0E94, 0x0E97, 1},
1385 {0x0E99, 0x0E9F, 1},
1386 {0x0EA1, 0x0EA3, 1},
1387 {0x0EA5, 0x0EA7, 2},
1388 {0x0EAA, 0x0EAB, 1},
1389 {0x0EAD, 0x0EAE, 1},
1390 {0x0EB0, 0x0EB0, 1},
1391 {0x0EB2, 0x0EB3, 1},
1392 {0x0EBD, 0x0EBD, 1},
1393 {0x0EC0, 0x0EC4, 1},
1394 {0x0F40, 0x0F47, 1},
1395 {0x0F49, 0x0F69, 1},
1396 {0x10A0, 0x10C5, 1},
1397 {0x10D0, 0x10F6, 1},
1398 {0x1100, 0x1100, 1},
1399 {0x1102, 0x1103, 1},
1400 {0x1105, 0x1107, 1},
1401 {0x1109, 0x1109, 1},
1402 {0x110B, 0x110C, 1},
1403 {0x110E, 0x1112, 1},
1404 {0x113C, 0x1140, 2},
1405 {0x114C, 0x1150, 2},
1406 {0x1154, 0x1155, 1},
1407 {0x1159, 0x1159, 1},
1408 {0x115F, 0x1161, 1},
1409 {0x1163, 0x1169, 2},
1410 {0x116D, 0x116E, 1},
1411 {0x1172, 0x1173, 1},
1412 {0x1175, 0x119E, 0x119E - 0x1175},
1413 {0x11A8, 0x11AB, 0x11AB - 0x11A8},
1414 {0x11AE, 0x11AF, 1},
1415 {0x11B7, 0x11B8, 1},
1416 {0x11BA, 0x11BA, 1},
1417 {0x11BC, 0x11C2, 1},
1418 {0x11EB, 0x11F0, 0x11F0 - 0x11EB},
1419 {0x11F9, 0x11F9, 1},
1420 {0x1E00, 0x1E9B, 1},
1421 {0x1EA0, 0x1EF9, 1},
1422 {0x1F00, 0x1F15, 1},
1423 {0x1F18, 0x1F1D, 1},
1424 {0x1F20, 0x1F45, 1},
1425 {0x1F48, 0x1F4D, 1},
1426 {0x1F50, 0x1F57, 1},
1427 {0x1F59, 0x1F5B, 0x1F5B - 0x1F59},
1428 {0x1F5D, 0x1F5D, 1},
1429 {0x1F5F, 0x1F7D, 1},
1430 {0x1F80, 0x1FB4, 1},
1431 {0x1FB6, 0x1FBC, 1},
1432 {0x1FBE, 0x1FBE, 1},
1433 {0x1FC2, 0x1FC4, 1},
1434 {0x1FC6, 0x1FCC, 1},
1435 {0x1FD0, 0x1FD3, 1},
1436 {0x1FD6, 0x1FDB, 1},
1437 {0x1FE0, 0x1FEC, 1},
1438 {0x1FF2, 0x1FF4, 1},
1439 {0x1FF6, 0x1FFC, 1},
1440 {0x2126, 0x2126, 1},
1441 {0x212A, 0x212B, 1},
1442 {0x212E, 0x212E, 1},
1443 {0x2180, 0x2182, 1},
1444 {0x3007, 0x3007, 1},
1445 {0x3021, 0x3029, 1},
1446 {0x3041, 0x3094, 1},
1447 {0x30A1, 0x30FA, 1},
1448 {0x3105, 0x312C, 1},
1449 {0x4E00, 0x9FA5, 1},
1450 {0xAC00, 0xD7A3, 1},
1454 var second = &unicode.RangeTable{
1455 R16: []unicode.Range16{
1456 {0x002D, 0x002E, 1},
1457 {0x0030, 0x0039, 1},
1458 {0x00B7, 0x00B7, 1},
1459 {0x02D0, 0x02D1, 1},
1460 {0x0300, 0x0345, 1},
1461 {0x0360, 0x0361, 1},
1462 {0x0387, 0x0387, 1},
1463 {0x0483, 0x0486, 1},
1464 {0x0591, 0x05A1, 1},
1465 {0x05A3, 0x05B9, 1},
1466 {0x05BB, 0x05BD, 1},
1467 {0x05BF, 0x05BF, 1},
1468 {0x05C1, 0x05C2, 1},
1469 {0x05C4, 0x0640, 0x0640 - 0x05C4},
1470 {0x064B, 0x0652, 1},
1471 {0x0660, 0x0669, 1},
1472 {0x0670, 0x0670, 1},
1473 {0x06D6, 0x06DC, 1},
1474 {0x06DD, 0x06DF, 1},
1475 {0x06E0, 0x06E4, 1},
1476 {0x06E7, 0x06E8, 1},
1477 {0x06EA, 0x06ED, 1},
1478 {0x06F0, 0x06F9, 1},
1479 {0x0901, 0x0903, 1},
1480 {0x093C, 0x093C, 1},
1481 {0x093E, 0x094C, 1},
1482 {0x094D, 0x094D, 1},
1483 {0x0951, 0x0954, 1},
1484 {0x0962, 0x0963, 1},
1485 {0x0966, 0x096F, 1},
1486 {0x0981, 0x0983, 1},
1487 {0x09BC, 0x09BC, 1},
1488 {0x09BE, 0x09BF, 1},
1489 {0x09C0, 0x09C4, 1},
1490 {0x09C7, 0x09C8, 1},
1491 {0x09CB, 0x09CD, 1},
1492 {0x09D7, 0x09D7, 1},
1493 {0x09E2, 0x09E3, 1},
1494 {0x09E6, 0x09EF, 1},
1495 {0x0A02, 0x0A3C, 0x3A},
1496 {0x0A3E, 0x0A3F, 1},
1497 {0x0A40, 0x0A42, 1},
1498 {0x0A47, 0x0A48, 1},
1499 {0x0A4B, 0x0A4D, 1},
1500 {0x0A66, 0x0A6F, 1},
1501 {0x0A70, 0x0A71, 1},
1502 {0x0A81, 0x0A83, 1},
1503 {0x0ABC, 0x0ABC, 1},
1504 {0x0ABE, 0x0AC5, 1},
1505 {0x0AC7, 0x0AC9, 1},
1506 {0x0ACB, 0x0ACD, 1},
1507 {0x0AE6, 0x0AEF, 1},
1508 {0x0B01, 0x0B03, 1},
1509 {0x0B3C, 0x0B3C, 1},
1510 {0x0B3E, 0x0B43, 1},
1511 {0x0B47, 0x0B48, 1},
1512 {0x0B4B, 0x0B4D, 1},
1513 {0x0B56, 0x0B57, 1},
1514 {0x0B66, 0x0B6F, 1},
1515 {0x0B82, 0x0B83, 1},
1516 {0x0BBE, 0x0BC2, 1},
1517 {0x0BC6, 0x0BC8, 1},
1518 {0x0BCA, 0x0BCD, 1},
1519 {0x0BD7, 0x0BD7, 1},
1520 {0x0BE7, 0x0BEF, 1},
1521 {0x0C01, 0x0C03, 1},
1522 {0x0C3E, 0x0C44, 1},
1523 {0x0C46, 0x0C48, 1},
1524 {0x0C4A, 0x0C4D, 1},
1525 {0x0C55, 0x0C56, 1},
1526 {0x0C66, 0x0C6F, 1},
1527 {0x0C82, 0x0C83, 1},
1528 {0x0CBE, 0x0CC4, 1},
1529 {0x0CC6, 0x0CC8, 1},
1530 {0x0CCA, 0x0CCD, 1},
1531 {0x0CD5, 0x0CD6, 1},
1532 {0x0CE6, 0x0CEF, 1},
1533 {0x0D02, 0x0D03, 1},
1534 {0x0D3E, 0x0D43, 1},
1535 {0x0D46, 0x0D48, 1},
1536 {0x0D4A, 0x0D4D, 1},
1537 {0x0D57, 0x0D57, 1},
1538 {0x0D66, 0x0D6F, 1},
1539 {0x0E31, 0x0E31, 1},
1540 {0x0E34, 0x0E3A, 1},
1541 {0x0E46, 0x0E46, 1},
1542 {0x0E47, 0x0E4E, 1},
1543 {0x0E50, 0x0E59, 1},
1544 {0x0EB1, 0x0EB1, 1},
1545 {0x0EB4, 0x0EB9, 1},
1546 {0x0EBB, 0x0EBC, 1},
1547 {0x0EC6, 0x0EC6, 1},
1548 {0x0EC8, 0x0ECD, 1},
1549 {0x0ED0, 0x0ED9, 1},
1550 {0x0F18, 0x0F19, 1},
1551 {0x0F20, 0x0F29, 1},
1552 {0x0F35, 0x0F39, 2},
1553 {0x0F3E, 0x0F3F, 1},
1554 {0x0F71, 0x0F84, 1},
1555 {0x0F86, 0x0F8B, 1},
1556 {0x0F90, 0x0F95, 1},
1557 {0x0F97, 0x0F97, 1},
1558 {0x0F99, 0x0FAD, 1},
1559 {0x0FB1, 0x0FB7, 1},
1560 {0x0FB9, 0x0FB9, 1},
1561 {0x20D0, 0x20DC, 1},
1562 {0x20E1, 0x3005, 0x3005 - 0x20E1},
1563 {0x302A, 0x302F, 1},
1564 {0x3031, 0x3035, 1},
1565 {0x3099, 0x309A, 1},
1566 {0x309D, 0x309E, 1},
1567 {0x30FC, 0x30FE, 1},
1571 // HTMLEntity is an entity map containing translations for the
1572 // standard HTML entity characters.
1573 var HTMLEntity = htmlEntity
1575 var htmlEntity = map[string]string{
1577 hget http://www.w3.org/TR/html4/sgml/entities.html |
1579 ,y /\>/ x/\<(.|\n)+/ s/\n/ /g
1580 ,x v/^\<!ENTITY/d
1581 ,s/\<!ENTITY ([^ ]+) .*U\+([0-9A-F][0-9A-F][0-9A-F][0-9A-F]) .+/ "\1": "\\u\2",/g
1685 "Epsilon": "\u0395",
1695 "Omicron": "\u039F",
1700 "Upsilon": "\u03A5",
1709 "epsilon": "\u03B5",
1719 "omicron": "\u03BF",
1725 "upsilon": "\u03C5",
1730 "thetasym": "\u03D1",
1743 "alefsym": "\u2135",
1838 // HTMLAutoClose is the set of HTML elements that
1839 // should be considered to close automatically.
1840 var HTMLAutoClose = htmlAutoClose
1842 var htmlAutoClose = []string{
1844 hget http://www.w3.org/TR/html4/loose.dtd |
1845 9 sed -n 's/<!ELEMENT ([^ ]*) +- O EMPTY.+/ "\1",/p' | tr A-Z a-z
1863 esc_quot = []byte(""") // shorter than """
1864 esc_apos = []byte("'") // shorter than "'"
1865 esc_amp = []byte("&")
1866 esc_lt = []byte("<")
1867 esc_gt = []byte(">")
1868 esc_tab = []byte("	")
1869 esc_nl = []byte("
")
1870 esc_cr = []byte("
")
1871 esc_fffd = []byte("\uFFFD") // Unicode replacement character
1874 // EscapeText writes to w the properly escaped XML equivalent
1875 // of the plain text data s.
1876 func EscapeText(w io.Writer, s []byte) error {
1877 return escapeText(w, s, true)
1880 // escapeText writes to w the properly escaped XML equivalent
1881 // of the plain text data s. If escapeNewline is true, newline
1882 // characters will be escaped.
1883 func escapeText(w io.Writer, s []byte, escapeNewline bool) error {
1886 for i := 0; i < len(s); {
1887 r, width := utf8.DecodeRune(s[i:])
1910 if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
1916 if _, err := w.Write(s[last : i-width]); err != nil {
1919 if _, err := w.Write(esc); err != nil {
1924 if _, err := w.Write(s[last:]); err != nil {
1930 // EscapeString writes to p the properly escaped XML equivalent
1931 // of the plain text data s.
1932 func (p *printer) EscapeString(s string) {
1935 for i := 0; i < len(s); {
1936 r, width := utf8.DecodeRuneInString(s[i:])
1956 if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
1962 p.WriteString(s[last : i-width])
1966 p.WriteString(s[last:])
1969 // Escape is like EscapeText but omits the error return value.
1970 // It is provided for backwards compatibility with Go 1.0.
1971 // Code targeting Go 1.1 or later should use EscapeText.
1972 func Escape(w io.Writer, s []byte) {
1976 // procInst parses the `param="..."` or `param='...'`
1977 // value out of the provided string, returning "" if not found.
1978 func procInst(param, s string) string {
1979 // TODO: this parsing is somewhat lame and not exact.
1980 // It works for all actual cases, though.
1982 idx := strings.Index(s, param)
1986 v := s[idx+len(param):]
1990 if v[0] != '\'' && v[0] != '"' {
1993 idx = strings.IndexRune(v[1:], rune(v[0]))