Rebase.
[official-gcc.git] / libgo / go / encoding / xml / xml.go
blobb473cb845847447de8f3947197751a31c700ef20
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.
7 package xml
9 // References:
10 // Annotated XML spec: http://www.xml.com/axml/testaxml.htm
11 // XML name spaces: http://www.w3.org/TR/REC-xml-names/
13 // TODO(rsc):
14 // Test error handling.
16 import (
17 "bufio"
18 "bytes"
19 "errors"
20 "fmt"
21 "io"
22 "strconv"
23 "strings"
24 "unicode"
25 "unicode/utf8"
28 // A SyntaxError represents a syntax error in the XML input stream.
29 type SyntaxError struct {
30 Msg string
31 Line int
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
39 // with a name space identifier (Space).
40 // In tokens returned by Decoder.Token, the Space identifier
41 // is given as a canonical URL, not the short prefix used
42 // in the document being parsed.
43 type Name struct {
44 Space, Local string
47 // An Attr represents an attribute in an XML element (Name=Value).
48 type Attr struct {
49 Name Name
50 Value string
53 // A Token is an interface holding one of the token types:
54 // StartElement, EndElement, CharData, Comment, ProcInst, or Directive.
55 type Token interface{}
57 // A StartElement represents an XML start element.
58 type StartElement struct {
59 Name Name
60 Attr []Attr
63 func (e StartElement) Copy() StartElement {
64 attrs := make([]Attr, len(e.Attr))
65 copy(attrs, e.Attr)
66 e.Attr = attrs
67 return e
70 // End returns the corresponding XML end element.
71 func (e StartElement) End() EndElement {
72 return EndElement{e.Name}
75 // An EndElement represents an XML end element.
76 type EndElement struct {
77 Name Name
80 // A CharData represents XML character data (raw text),
81 // in which XML escape sequences have been replaced by
82 // the characters they represent.
83 type CharData []byte
85 func makeCopy(b []byte) []byte {
86 b1 := make([]byte, len(b))
87 copy(b1, b)
88 return b1
91 func (c CharData) Copy() CharData { return CharData(makeCopy(c)) }
93 // A Comment represents an XML comment of the form <!--comment-->.
94 // The bytes do not include the <!-- and --> comment markers.
95 type Comment []byte
97 func (c Comment) Copy() Comment { return Comment(makeCopy(c)) }
99 // A ProcInst represents an XML processing instruction of the form <?target inst?>
100 type ProcInst struct {
101 Target string
102 Inst []byte
105 func (p ProcInst) Copy() ProcInst {
106 p.Inst = makeCopy(p.Inst)
107 return p
110 // A Directive represents an XML directive of the form <!text>.
111 // The bytes do not include the <! and > markers.
112 type Directive []byte
114 func (d Directive) Copy() Directive { return Directive(makeCopy(d)) }
116 // CopyToken returns a copy of a Token.
117 func CopyToken(t Token) Token {
118 switch v := t.(type) {
119 case CharData:
120 return v.Copy()
121 case Comment:
122 return v.Copy()
123 case Directive:
124 return v.Copy()
125 case ProcInst:
126 return v.Copy()
127 case StartElement:
128 return v.Copy()
130 return t
133 // A Decoder represents an XML parser reading a particular input stream.
134 // The parser assumes that its input is encoded in UTF-8.
135 type Decoder struct {
136 // Strict defaults to true, enforcing the requirements
137 // of the XML specification.
138 // If set to false, the parser allows input containing common
139 // mistakes:
140 // * If an element is missing an end tag, the parser invents
141 // end tags as necessary to keep the return values from Token
142 // properly balanced.
143 // * In attribute values and character data, unknown or malformed
144 // character entities (sequences beginning with &) are left alone.
146 // Setting:
148 // d.Strict = false;
149 // d.AutoClose = HTMLAutoClose;
150 // d.Entity = HTMLEntity
152 // creates a parser that can handle typical HTML.
154 // Strict mode does not enforce the requirements of the XML name spaces TR.
155 // In particular it does not reject name space tags using undefined prefixes.
156 // Such tags are recorded with the unknown prefix as the name space URL.
157 Strict bool
159 // When Strict == false, AutoClose indicates a set of elements to
160 // consider closed immediately after they are opened, regardless
161 // of whether an end element is present.
162 AutoClose []string
164 // Entity can be used to map non-standard entity names to string replacements.
165 // The parser behaves as if these standard mappings are present in the map,
166 // regardless of the actual map content:
168 // "lt": "<",
169 // "gt": ">",
170 // "amp": "&",
171 // "apos": "'",
172 // "quot": `"`,
173 Entity map[string]string
175 // CharsetReader, if non-nil, defines a function to generate
176 // charset-conversion readers, converting from the provided
177 // non-UTF-8 charset into UTF-8. If CharsetReader is nil or
178 // returns an error, parsing stops with an error. One of the
179 // the CharsetReader's result values must be non-nil.
180 CharsetReader func(charset string, input io.Reader) (io.Reader, error)
182 // DefaultSpace sets the default name space used for unadorned tags,
183 // as if the entire XML stream were wrapped in an element containing
184 // the attribute xmlns="DefaultSpace".
185 DefaultSpace string
187 r io.ByteReader
188 buf bytes.Buffer
189 saved *bytes.Buffer
190 stk *stack
191 free *stack
192 needClose bool
193 toClose Name
194 nextToken Token
195 nextByte int
196 ns map[string]string
197 err error
198 line int
199 unmarshalDepth int
202 // NewDecoder creates a new XML parser reading from r.
203 // If r does not implement io.ByteReader, NewDecoder will
204 // do its own buffering.
205 func NewDecoder(r io.Reader) *Decoder {
206 d := &Decoder{
207 ns: make(map[string]string),
208 nextByte: -1,
209 line: 1,
210 Strict: true,
212 d.switchToReader(r)
213 return d
216 // Token returns the next XML token in the input stream.
217 // At the end of the input stream, Token returns nil, io.EOF.
219 // Slices of bytes in the returned token data refer to the
220 // parser's internal buffer and remain valid only until the next
221 // call to Token. To acquire a copy of the bytes, call CopyToken
222 // or the token's Copy method.
224 // Token expands self-closing elements such as <br/>
225 // into separate start and end elements returned by successive calls.
227 // Token guarantees that the StartElement and EndElement
228 // tokens it returns are properly nested and matched:
229 // if Token encounters an unexpected end element,
230 // it will return an error.
232 // Token implements XML name spaces as described by
233 // http://www.w3.org/TR/REC-xml-names/. Each of the
234 // Name structures contained in the Token has the Space
235 // set to the URL identifying its name space when known.
236 // If Token encounters an unrecognized name space prefix,
237 // it uses the prefix as the Space rather than report an error.
238 func (d *Decoder) Token() (t Token, err error) {
239 if d.stk != nil && d.stk.kind == stkEOF {
240 err = io.EOF
241 return
243 if d.nextToken != nil {
244 t = d.nextToken
245 d.nextToken = nil
246 } else if t, err = d.rawToken(); err != nil {
247 return
250 if !d.Strict {
251 if t1, ok := d.autoClose(t); ok {
252 d.nextToken = t
253 t = t1
256 switch t1 := t.(type) {
257 case StartElement:
258 // In XML name spaces, the translations listed in the
259 // attributes apply to the element name and
260 // to the other attribute names, so process
261 // the translations first.
262 for _, a := range t1.Attr {
263 if a.Name.Space == "xmlns" {
264 v, ok := d.ns[a.Name.Local]
265 d.pushNs(a.Name.Local, v, ok)
266 d.ns[a.Name.Local] = a.Value
268 if a.Name.Space == "" && a.Name.Local == "xmlns" {
269 // Default space for untagged names
270 v, ok := d.ns[""]
271 d.pushNs("", v, ok)
272 d.ns[""] = a.Value
276 d.translate(&t1.Name, true)
277 for i := range t1.Attr {
278 d.translate(&t1.Attr[i].Name, false)
280 d.pushElement(t1.Name)
281 t = t1
283 case EndElement:
284 d.translate(&t1.Name, true)
285 if !d.popElement(&t1) {
286 return nil, d.err
288 t = t1
290 return
293 const xmlURL = "http://www.w3.org/XML/1998/namespace"
295 // Apply name space translation to name n.
296 // The default name space (for Space=="")
297 // applies only to element names, not to attribute names.
298 func (d *Decoder) translate(n *Name, isElementName bool) {
299 switch {
300 case n.Space == "xmlns":
301 return
302 case n.Space == "" && !isElementName:
303 return
304 case n.Space == "xml":
305 n.Space = xmlURL
306 case n.Space == "" && n.Local == "xmlns":
307 return
309 if v, ok := d.ns[n.Space]; ok {
310 n.Space = v
311 } else if n.Space == "" {
312 n.Space = d.DefaultSpace
316 func (d *Decoder) switchToReader(r io.Reader) {
317 // Get efficient byte at a time reader.
318 // Assume that if reader has its own
319 // ReadByte, it's efficient enough.
320 // Otherwise, use bufio.
321 if rb, ok := r.(io.ByteReader); ok {
322 d.r = rb
323 } else {
324 d.r = bufio.NewReader(r)
328 // Parsing state - stack holds old name space translations
329 // and the current set of open elements. The translations to pop when
330 // ending a given tag are *below* it on the stack, which is
331 // more work but forced on us by XML.
332 type stack struct {
333 next *stack
334 kind int
335 name Name
336 ok bool
339 const (
340 stkStart = iota
341 stkNs
342 stkEOF
345 func (d *Decoder) push(kind int) *stack {
346 s := d.free
347 if s != nil {
348 d.free = s.next
349 } else {
350 s = new(stack)
352 s.next = d.stk
353 s.kind = kind
354 d.stk = s
355 return s
358 func (d *Decoder) pop() *stack {
359 s := d.stk
360 if s != nil {
361 d.stk = s.next
362 s.next = d.free
363 d.free = s
365 return s
368 // Record that after the current element is finished
369 // (that element is already pushed on the stack)
370 // Token should return EOF until popEOF is called.
371 func (d *Decoder) pushEOF() {
372 // Walk down stack to find Start.
373 // It might not be the top, because there might be stkNs
374 // entries above it.
375 start := d.stk
376 for start.kind != stkStart {
377 start = start.next
379 // The stkNs entries below a start are associated with that
380 // element too; skip over them.
381 for start.next != nil && start.next.kind == stkNs {
382 start = start.next
384 s := d.free
385 if s != nil {
386 d.free = s.next
387 } else {
388 s = new(stack)
390 s.kind = stkEOF
391 s.next = start.next
392 start.next = s
395 // Undo a pushEOF.
396 // The element must have been finished, so the EOF should be at the top of the stack.
397 func (d *Decoder) popEOF() bool {
398 if d.stk == nil || d.stk.kind != stkEOF {
399 return false
401 d.pop()
402 return true
405 // Record that we are starting an element with the given name.
406 func (d *Decoder) pushElement(name Name) {
407 s := d.push(stkStart)
408 s.name = name
411 // Record that we are changing the value of ns[local].
412 // The old value is url, ok.
413 func (d *Decoder) pushNs(local string, url string, ok bool) {
414 s := d.push(stkNs)
415 s.name.Local = local
416 s.name.Space = url
417 s.ok = ok
420 // Creates a SyntaxError with the current line number.
421 func (d *Decoder) syntaxError(msg string) error {
422 return &SyntaxError{Msg: msg, Line: d.line}
425 // Record that we are ending an element with the given name.
426 // The name must match the record at the top of the stack,
427 // which must be a pushElement record.
428 // After popping the element, apply any undo records from
429 // the stack to restore the name translations that existed
430 // before we saw this element.
431 func (d *Decoder) popElement(t *EndElement) bool {
432 s := d.pop()
433 name := t.Name
434 switch {
435 case s == nil || s.kind != stkStart:
436 d.err = d.syntaxError("unexpected end element </" + name.Local + ">")
437 return false
438 case s.name.Local != name.Local:
439 if !d.Strict {
440 d.needClose = true
441 d.toClose = t.Name
442 t.Name = s.name
443 return true
445 d.err = d.syntaxError("element <" + s.name.Local + "> closed by </" + name.Local + ">")
446 return false
447 case s.name.Space != name.Space:
448 d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space +
449 "closed by </" + name.Local + "> in space " + name.Space)
450 return false
453 // Pop stack until a Start or EOF is on the top, undoing the
454 // translations that were associated with the element we just closed.
455 for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF {
456 s := d.pop()
457 if s.ok {
458 d.ns[s.name.Local] = s.name.Space
459 } else {
460 delete(d.ns, s.name.Local)
464 return true
467 // If the top element on the stack is autoclosing and
468 // t is not the end tag, invent the end tag.
469 func (d *Decoder) autoClose(t Token) (Token, bool) {
470 if d.stk == nil || d.stk.kind != stkStart {
471 return nil, false
473 name := strings.ToLower(d.stk.name.Local)
474 for _, s := range d.AutoClose {
475 if strings.ToLower(s) == name {
476 // This one should be auto closed if t doesn't close it.
477 et, ok := t.(EndElement)
478 if !ok || et.Name.Local != name {
479 return EndElement{d.stk.name}, true
481 break
484 return nil, false
487 var errRawToken = errors.New("xml: cannot use RawToken from UnmarshalXML method")
489 // RawToken is like Token but does not verify that
490 // start and end elements match and does not translate
491 // name space prefixes to their corresponding URLs.
492 func (d *Decoder) RawToken() (Token, error) {
493 if d.unmarshalDepth > 0 {
494 return nil, errRawToken
496 return d.rawToken()
499 func (d *Decoder) rawToken() (Token, error) {
500 if d.err != nil {
501 return nil, d.err
503 if d.needClose {
504 // The last element we read was self-closing and
505 // we returned just the StartElement half.
506 // Return the EndElement half now.
507 d.needClose = false
508 return EndElement{d.toClose}, nil
511 b, ok := d.getc()
512 if !ok {
513 return nil, d.err
516 if b != '<' {
517 // Text section.
518 d.ungetc(b)
519 data := d.text(-1, false)
520 if data == nil {
521 return nil, d.err
523 return CharData(data), nil
526 if b, ok = d.mustgetc(); !ok {
527 return nil, d.err
529 switch b {
530 case '/':
531 // </: End element
532 var name Name
533 if name, ok = d.nsname(); !ok {
534 if d.err == nil {
535 d.err = d.syntaxError("expected element name after </")
537 return nil, d.err
539 d.space()
540 if b, ok = d.mustgetc(); !ok {
541 return nil, d.err
543 if b != '>' {
544 d.err = d.syntaxError("invalid characters between </" + name.Local + " and >")
545 return nil, d.err
547 return EndElement{name}, nil
549 case '?':
550 // <?: Processing instruction.
551 // TODO(rsc): Should parse the <?xml declaration to make sure the version is 1.0.
552 var target string
553 if target, ok = d.name(); !ok {
554 if d.err == nil {
555 d.err = d.syntaxError("expected target name after <?")
557 return nil, d.err
559 d.space()
560 d.buf.Reset()
561 var b0 byte
562 for {
563 if b, ok = d.mustgetc(); !ok {
564 return nil, d.err
566 d.buf.WriteByte(b)
567 if b0 == '?' && b == '>' {
568 break
570 b0 = b
572 data := d.buf.Bytes()
573 data = data[0 : len(data)-2] // chop ?>
575 if target == "xml" {
576 enc := procInstEncoding(string(data))
577 if enc != "" && enc != "utf-8" && enc != "UTF-8" {
578 if d.CharsetReader == nil {
579 d.err = fmt.Errorf("xml: encoding %q declared but Decoder.CharsetReader is nil", enc)
580 return nil, d.err
582 newr, err := d.CharsetReader(enc, d.r.(io.Reader))
583 if err != nil {
584 d.err = fmt.Errorf("xml: opening charset %q: %v", enc, err)
585 return nil, d.err
587 if newr == nil {
588 panic("CharsetReader returned a nil Reader for charset " + enc)
590 d.switchToReader(newr)
593 return ProcInst{target, data}, nil
595 case '!':
596 // <!: Maybe comment, maybe CDATA.
597 if b, ok = d.mustgetc(); !ok {
598 return nil, d.err
600 switch b {
601 case '-': // <!-
602 // Probably <!-- for a comment.
603 if b, ok = d.mustgetc(); !ok {
604 return nil, d.err
606 if b != '-' {
607 d.err = d.syntaxError("invalid sequence <!- not part of <!--")
608 return nil, d.err
610 // Look for terminator.
611 d.buf.Reset()
612 var b0, b1 byte
613 for {
614 if b, ok = d.mustgetc(); !ok {
615 return nil, d.err
617 d.buf.WriteByte(b)
618 if b0 == '-' && b1 == '-' && b == '>' {
619 break
621 b0, b1 = b1, b
623 data := d.buf.Bytes()
624 data = data[0 : len(data)-3] // chop -->
625 return Comment(data), nil
627 case '[': // <![
628 // Probably <![CDATA[.
629 for i := 0; i < 6; i++ {
630 if b, ok = d.mustgetc(); !ok {
631 return nil, d.err
633 if b != "CDATA["[i] {
634 d.err = d.syntaxError("invalid <![ sequence")
635 return nil, d.err
638 // Have <![CDATA[. Read text until ]]>.
639 data := d.text(-1, true)
640 if data == nil {
641 return nil, d.err
643 return CharData(data), nil
646 // Probably a directive: <!DOCTYPE ...>, <!ENTITY ...>, etc.
647 // We don't care, but accumulate for caller. Quoted angle
648 // brackets do not count for nesting.
649 d.buf.Reset()
650 d.buf.WriteByte(b)
651 inquote := uint8(0)
652 depth := 0
653 for {
654 if b, ok = d.mustgetc(); !ok {
655 return nil, d.err
657 if inquote == 0 && b == '>' && depth == 0 {
658 break
660 HandleB:
661 d.buf.WriteByte(b)
662 switch {
663 case b == inquote:
664 inquote = 0
666 case inquote != 0:
667 // in quotes, no special action
669 case b == '\'' || b == '"':
670 inquote = b
672 case b == '>' && inquote == 0:
673 depth--
675 case b == '<' && inquote == 0:
676 // Look for <!-- to begin comment.
677 s := "!--"
678 for i := 0; i < len(s); i++ {
679 if b, ok = d.mustgetc(); !ok {
680 return nil, d.err
682 if b != s[i] {
683 for j := 0; j < i; j++ {
684 d.buf.WriteByte(s[j])
686 depth++
687 goto HandleB
691 // Remove < that was written above.
692 d.buf.Truncate(d.buf.Len() - 1)
694 // Look for terminator.
695 var b0, b1 byte
696 for {
697 if b, ok = d.mustgetc(); !ok {
698 return nil, d.err
700 if b0 == '-' && b1 == '-' && b == '>' {
701 break
703 b0, b1 = b1, b
707 return Directive(d.buf.Bytes()), nil
710 // Must be an open element like <a href="foo">
711 d.ungetc(b)
713 var (
714 name Name
715 empty bool
716 attr []Attr
718 if name, ok = d.nsname(); !ok {
719 if d.err == nil {
720 d.err = d.syntaxError("expected element name after <")
722 return nil, d.err
725 attr = make([]Attr, 0, 4)
726 for {
727 d.space()
728 if b, ok = d.mustgetc(); !ok {
729 return nil, d.err
731 if b == '/' {
732 empty = true
733 if b, ok = d.mustgetc(); !ok {
734 return nil, d.err
736 if b != '>' {
737 d.err = d.syntaxError("expected /> in element")
738 return nil, d.err
740 break
742 if b == '>' {
743 break
745 d.ungetc(b)
747 n := len(attr)
748 if n >= cap(attr) {
749 nattr := make([]Attr, n, 2*cap(attr))
750 copy(nattr, attr)
751 attr = nattr
753 attr = attr[0 : n+1]
754 a := &attr[n]
755 if a.Name, ok = d.nsname(); !ok {
756 if d.err == nil {
757 d.err = d.syntaxError("expected attribute name in element")
759 return nil, d.err
761 d.space()
762 if b, ok = d.mustgetc(); !ok {
763 return nil, d.err
765 if b != '=' {
766 if d.Strict {
767 d.err = d.syntaxError("attribute name without = in element")
768 return nil, d.err
769 } else {
770 d.ungetc(b)
771 a.Value = a.Name.Local
773 } else {
774 d.space()
775 data := d.attrval()
776 if data == nil {
777 return nil, d.err
779 a.Value = string(data)
782 if empty {
783 d.needClose = true
784 d.toClose = name
786 return StartElement{name, attr}, nil
789 func (d *Decoder) attrval() []byte {
790 b, ok := d.mustgetc()
791 if !ok {
792 return nil
794 // Handle quoted attribute values
795 if b == '"' || b == '\'' {
796 return d.text(int(b), false)
798 // Handle unquoted attribute values for strict parsers
799 if d.Strict {
800 d.err = d.syntaxError("unquoted or missing attribute value in element")
801 return nil
803 // Handle unquoted attribute values for unstrict parsers
804 d.ungetc(b)
805 d.buf.Reset()
806 for {
807 b, ok = d.mustgetc()
808 if !ok {
809 return nil
811 // http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2
812 if 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' ||
813 '0' <= b && b <= '9' || b == '_' || b == ':' || b == '-' {
814 d.buf.WriteByte(b)
815 } else {
816 d.ungetc(b)
817 break
820 return d.buf.Bytes()
823 // Skip spaces if any
824 func (d *Decoder) space() {
825 for {
826 b, ok := d.getc()
827 if !ok {
828 return
830 switch b {
831 case ' ', '\r', '\n', '\t':
832 default:
833 d.ungetc(b)
834 return
839 // Read a single byte.
840 // If there is no byte to read, return ok==false
841 // and leave the error in d.err.
842 // Maintain line number.
843 func (d *Decoder) getc() (b byte, ok bool) {
844 if d.err != nil {
845 return 0, false
847 if d.nextByte >= 0 {
848 b = byte(d.nextByte)
849 d.nextByte = -1
850 } else {
851 b, d.err = d.r.ReadByte()
852 if d.err != nil {
853 return 0, false
855 if d.saved != nil {
856 d.saved.WriteByte(b)
859 if b == '\n' {
860 d.line++
862 return b, true
865 // Return saved offset.
866 // If we did ungetc (nextByte >= 0), have to back up one.
867 func (d *Decoder) savedOffset() int {
868 n := d.saved.Len()
869 if d.nextByte >= 0 {
872 return n
875 // Must read a single byte.
876 // If there is no byte to read,
877 // set d.err to SyntaxError("unexpected EOF")
878 // and return ok==false
879 func (d *Decoder) mustgetc() (b byte, ok bool) {
880 if b, ok = d.getc(); !ok {
881 if d.err == io.EOF {
882 d.err = d.syntaxError("unexpected EOF")
885 return
888 // Unread a single byte.
889 func (d *Decoder) ungetc(b byte) {
890 if b == '\n' {
891 d.line--
893 d.nextByte = int(b)
896 var entity = map[string]int{
897 "lt": '<',
898 "gt": '>',
899 "amp": '&',
900 "apos": '\'',
901 "quot": '"',
904 // Read plain text section (XML calls it character data).
905 // If quote >= 0, we are in a quoted string and need to find the matching quote.
906 // If cdata == true, we are in a <![CDATA[ section and need to find ]]>.
907 // On failure return nil and leave the error in d.err.
908 func (d *Decoder) text(quote int, cdata bool) []byte {
909 var b0, b1 byte
910 var trunc int
911 d.buf.Reset()
912 Input:
913 for {
914 b, ok := d.getc()
915 if !ok {
916 if cdata {
917 if d.err == io.EOF {
918 d.err = d.syntaxError("unexpected EOF in CDATA section")
920 return nil
922 break Input
925 // <![CDATA[ section ends with ]]>.
926 // It is an error for ]]> to appear in ordinary text.
927 if b0 == ']' && b1 == ']' && b == '>' {
928 if cdata {
929 trunc = 2
930 break Input
932 d.err = d.syntaxError("unescaped ]]> not in CDATA section")
933 return nil
936 // Stop reading text if we see a <.
937 if b == '<' && !cdata {
938 if quote >= 0 {
939 d.err = d.syntaxError("unescaped < inside quoted string")
940 return nil
942 d.ungetc('<')
943 break Input
945 if quote >= 0 && b == byte(quote) {
946 break Input
948 if b == '&' && !cdata {
949 // Read escaped character expression up to semicolon.
950 // XML in all its glory allows a document to define and use
951 // its own character names with <!ENTITY ...> directives.
952 // Parsers are required to recognize lt, gt, amp, apos, and quot
953 // even if they have not been declared.
954 before := d.buf.Len()
955 d.buf.WriteByte('&')
956 var ok bool
957 var text string
958 var haveText bool
959 if b, ok = d.mustgetc(); !ok {
960 return nil
962 if b == '#' {
963 d.buf.WriteByte(b)
964 if b, ok = d.mustgetc(); !ok {
965 return nil
967 base := 10
968 if b == 'x' {
969 base = 16
970 d.buf.WriteByte(b)
971 if b, ok = d.mustgetc(); !ok {
972 return nil
975 start := d.buf.Len()
976 for '0' <= b && b <= '9' ||
977 base == 16 && 'a' <= b && b <= 'f' ||
978 base == 16 && 'A' <= b && b <= 'F' {
979 d.buf.WriteByte(b)
980 if b, ok = d.mustgetc(); !ok {
981 return nil
984 if b != ';' {
985 d.ungetc(b)
986 } else {
987 s := string(d.buf.Bytes()[start:])
988 d.buf.WriteByte(';')
989 n, err := strconv.ParseUint(s, base, 64)
990 if err == nil && n <= unicode.MaxRune {
991 text = string(n)
992 haveText = true
995 } else {
996 d.ungetc(b)
997 if !d.readName() {
998 if d.err != nil {
999 return nil
1001 ok = false
1003 if b, ok = d.mustgetc(); !ok {
1004 return nil
1006 if b != ';' {
1007 d.ungetc(b)
1008 } else {
1009 name := d.buf.Bytes()[before+1:]
1010 d.buf.WriteByte(';')
1011 if isName(name) {
1012 s := string(name)
1013 if r, ok := entity[s]; ok {
1014 text = string(r)
1015 haveText = true
1016 } else if d.Entity != nil {
1017 text, haveText = d.Entity[s]
1023 if haveText {
1024 d.buf.Truncate(before)
1025 d.buf.Write([]byte(text))
1026 b0, b1 = 0, 0
1027 continue Input
1029 if !d.Strict {
1030 b0, b1 = 0, 0
1031 continue Input
1033 ent := string(d.buf.Bytes()[before:])
1034 if ent[len(ent)-1] != ';' {
1035 ent += " (no semicolon)"
1037 d.err = d.syntaxError("invalid character entity " + ent)
1038 return nil
1041 // We must rewrite unescaped \r and \r\n into \n.
1042 if b == '\r' {
1043 d.buf.WriteByte('\n')
1044 } else if b1 == '\r' && b == '\n' {
1045 // Skip \r\n--we already wrote \n.
1046 } else {
1047 d.buf.WriteByte(b)
1050 b0, b1 = b1, b
1052 data := d.buf.Bytes()
1053 data = data[0 : len(data)-trunc]
1055 // Inspect each rune for being a disallowed character.
1056 buf := data
1057 for len(buf) > 0 {
1058 r, size := utf8.DecodeRune(buf)
1059 if r == utf8.RuneError && size == 1 {
1060 d.err = d.syntaxError("invalid UTF-8")
1061 return nil
1063 buf = buf[size:]
1064 if !isInCharacterRange(r) {
1065 d.err = d.syntaxError(fmt.Sprintf("illegal character code %U", r))
1066 return nil
1070 return data
1073 // Decide whether the given rune is in the XML Character Range, per
1074 // the Char production of http://www.xml.com/axml/testaxml.htm,
1075 // Section 2.2 Characters.
1076 func isInCharacterRange(r rune) (inrange bool) {
1077 return r == 0x09 ||
1078 r == 0x0A ||
1079 r == 0x0D ||
1080 r >= 0x20 && r <= 0xDF77 ||
1081 r >= 0xE000 && r <= 0xFFFD ||
1082 r >= 0x10000 && r <= 0x10FFFF
1085 // Get name space name: name with a : stuck in the middle.
1086 // The part before the : is the name space identifier.
1087 func (d *Decoder) nsname() (name Name, ok bool) {
1088 s, ok := d.name()
1089 if !ok {
1090 return
1092 i := strings.Index(s, ":")
1093 if i < 0 {
1094 name.Local = s
1095 } else {
1096 name.Space = s[0:i]
1097 name.Local = s[i+1:]
1099 return name, true
1102 // Get name: /first(first|second)*/
1103 // Do not set d.err if the name is missing (unless unexpected EOF is received):
1104 // let the caller provide better context.
1105 func (d *Decoder) name() (s string, ok bool) {
1106 d.buf.Reset()
1107 if !d.readName() {
1108 return "", false
1111 // Now we check the characters.
1112 s = d.buf.String()
1113 if !isName([]byte(s)) {
1114 d.err = d.syntaxError("invalid XML name: " + s)
1115 return "", false
1117 return s, true
1120 // Read a name and append its bytes to d.buf.
1121 // The name is delimited by any single-byte character not valid in names.
1122 // All multi-byte characters are accepted; the caller must check their validity.
1123 func (d *Decoder) readName() (ok bool) {
1124 var b byte
1125 if b, ok = d.mustgetc(); !ok {
1126 return
1128 if b < utf8.RuneSelf && !isNameByte(b) {
1129 d.ungetc(b)
1130 return false
1132 d.buf.WriteByte(b)
1134 for {
1135 if b, ok = d.mustgetc(); !ok {
1136 return
1138 if b < utf8.RuneSelf && !isNameByte(b) {
1139 d.ungetc(b)
1140 break
1142 d.buf.WriteByte(b)
1144 return true
1147 func isNameByte(c byte) bool {
1148 return 'A' <= c && c <= 'Z' ||
1149 'a' <= c && c <= 'z' ||
1150 '0' <= c && c <= '9' ||
1151 c == '_' || c == ':' || c == '.' || c == '-'
1154 func isName(s []byte) bool {
1155 if len(s) == 0 {
1156 return false
1158 c, n := utf8.DecodeRune(s)
1159 if c == utf8.RuneError && n == 1 {
1160 return false
1162 if !unicode.Is(first, c) {
1163 return false
1165 for n < len(s) {
1166 s = s[n:]
1167 c, n = utf8.DecodeRune(s)
1168 if c == utf8.RuneError && n == 1 {
1169 return false
1171 if !unicode.Is(first, c) && !unicode.Is(second, c) {
1172 return false
1175 return true
1178 func isNameString(s string) bool {
1179 if len(s) == 0 {
1180 return false
1182 c, n := utf8.DecodeRuneInString(s)
1183 if c == utf8.RuneError && n == 1 {
1184 return false
1186 if !unicode.Is(first, c) {
1187 return false
1189 for n < len(s) {
1190 s = s[n:]
1191 c, n = utf8.DecodeRuneInString(s)
1192 if c == utf8.RuneError && n == 1 {
1193 return false
1195 if !unicode.Is(first, c) && !unicode.Is(second, c) {
1196 return false
1199 return true
1202 // These tables were generated by cut and paste from Appendix B of
1203 // the XML spec at http://www.xml.com/axml/testaxml.htm
1204 // and then reformatting. First corresponds to (Letter | '_' | ':')
1205 // and second corresponds to NameChar.
1207 var first = &unicode.RangeTable{
1208 R16: []unicode.Range16{
1209 {0x003A, 0x003A, 1},
1210 {0x0041, 0x005A, 1},
1211 {0x005F, 0x005F, 1},
1212 {0x0061, 0x007A, 1},
1213 {0x00C0, 0x00D6, 1},
1214 {0x00D8, 0x00F6, 1},
1215 {0x00F8, 0x00FF, 1},
1216 {0x0100, 0x0131, 1},
1217 {0x0134, 0x013E, 1},
1218 {0x0141, 0x0148, 1},
1219 {0x014A, 0x017E, 1},
1220 {0x0180, 0x01C3, 1},
1221 {0x01CD, 0x01F0, 1},
1222 {0x01F4, 0x01F5, 1},
1223 {0x01FA, 0x0217, 1},
1224 {0x0250, 0x02A8, 1},
1225 {0x02BB, 0x02C1, 1},
1226 {0x0386, 0x0386, 1},
1227 {0x0388, 0x038A, 1},
1228 {0x038C, 0x038C, 1},
1229 {0x038E, 0x03A1, 1},
1230 {0x03A3, 0x03CE, 1},
1231 {0x03D0, 0x03D6, 1},
1232 {0x03DA, 0x03E0, 2},
1233 {0x03E2, 0x03F3, 1},
1234 {0x0401, 0x040C, 1},
1235 {0x040E, 0x044F, 1},
1236 {0x0451, 0x045C, 1},
1237 {0x045E, 0x0481, 1},
1238 {0x0490, 0x04C4, 1},
1239 {0x04C7, 0x04C8, 1},
1240 {0x04CB, 0x04CC, 1},
1241 {0x04D0, 0x04EB, 1},
1242 {0x04EE, 0x04F5, 1},
1243 {0x04F8, 0x04F9, 1},
1244 {0x0531, 0x0556, 1},
1245 {0x0559, 0x0559, 1},
1246 {0x0561, 0x0586, 1},
1247 {0x05D0, 0x05EA, 1},
1248 {0x05F0, 0x05F2, 1},
1249 {0x0621, 0x063A, 1},
1250 {0x0641, 0x064A, 1},
1251 {0x0671, 0x06B7, 1},
1252 {0x06BA, 0x06BE, 1},
1253 {0x06C0, 0x06CE, 1},
1254 {0x06D0, 0x06D3, 1},
1255 {0x06D5, 0x06D5, 1},
1256 {0x06E5, 0x06E6, 1},
1257 {0x0905, 0x0939, 1},
1258 {0x093D, 0x093D, 1},
1259 {0x0958, 0x0961, 1},
1260 {0x0985, 0x098C, 1},
1261 {0x098F, 0x0990, 1},
1262 {0x0993, 0x09A8, 1},
1263 {0x09AA, 0x09B0, 1},
1264 {0x09B2, 0x09B2, 1},
1265 {0x09B6, 0x09B9, 1},
1266 {0x09DC, 0x09DD, 1},
1267 {0x09DF, 0x09E1, 1},
1268 {0x09F0, 0x09F1, 1},
1269 {0x0A05, 0x0A0A, 1},
1270 {0x0A0F, 0x0A10, 1},
1271 {0x0A13, 0x0A28, 1},
1272 {0x0A2A, 0x0A30, 1},
1273 {0x0A32, 0x0A33, 1},
1274 {0x0A35, 0x0A36, 1},
1275 {0x0A38, 0x0A39, 1},
1276 {0x0A59, 0x0A5C, 1},
1277 {0x0A5E, 0x0A5E, 1},
1278 {0x0A72, 0x0A74, 1},
1279 {0x0A85, 0x0A8B, 1},
1280 {0x0A8D, 0x0A8D, 1},
1281 {0x0A8F, 0x0A91, 1},
1282 {0x0A93, 0x0AA8, 1},
1283 {0x0AAA, 0x0AB0, 1},
1284 {0x0AB2, 0x0AB3, 1},
1285 {0x0AB5, 0x0AB9, 1},
1286 {0x0ABD, 0x0AE0, 0x23},
1287 {0x0B05, 0x0B0C, 1},
1288 {0x0B0F, 0x0B10, 1},
1289 {0x0B13, 0x0B28, 1},
1290 {0x0B2A, 0x0B30, 1},
1291 {0x0B32, 0x0B33, 1},
1292 {0x0B36, 0x0B39, 1},
1293 {0x0B3D, 0x0B3D, 1},
1294 {0x0B5C, 0x0B5D, 1},
1295 {0x0B5F, 0x0B61, 1},
1296 {0x0B85, 0x0B8A, 1},
1297 {0x0B8E, 0x0B90, 1},
1298 {0x0B92, 0x0B95, 1},
1299 {0x0B99, 0x0B9A, 1},
1300 {0x0B9C, 0x0B9C, 1},
1301 {0x0B9E, 0x0B9F, 1},
1302 {0x0BA3, 0x0BA4, 1},
1303 {0x0BA8, 0x0BAA, 1},
1304 {0x0BAE, 0x0BB5, 1},
1305 {0x0BB7, 0x0BB9, 1},
1306 {0x0C05, 0x0C0C, 1},
1307 {0x0C0E, 0x0C10, 1},
1308 {0x0C12, 0x0C28, 1},
1309 {0x0C2A, 0x0C33, 1},
1310 {0x0C35, 0x0C39, 1},
1311 {0x0C60, 0x0C61, 1},
1312 {0x0C85, 0x0C8C, 1},
1313 {0x0C8E, 0x0C90, 1},
1314 {0x0C92, 0x0CA8, 1},
1315 {0x0CAA, 0x0CB3, 1},
1316 {0x0CB5, 0x0CB9, 1},
1317 {0x0CDE, 0x0CDE, 1},
1318 {0x0CE0, 0x0CE1, 1},
1319 {0x0D05, 0x0D0C, 1},
1320 {0x0D0E, 0x0D10, 1},
1321 {0x0D12, 0x0D28, 1},
1322 {0x0D2A, 0x0D39, 1},
1323 {0x0D60, 0x0D61, 1},
1324 {0x0E01, 0x0E2E, 1},
1325 {0x0E30, 0x0E30, 1},
1326 {0x0E32, 0x0E33, 1},
1327 {0x0E40, 0x0E45, 1},
1328 {0x0E81, 0x0E82, 1},
1329 {0x0E84, 0x0E84, 1},
1330 {0x0E87, 0x0E88, 1},
1331 {0x0E8A, 0x0E8D, 3},
1332 {0x0E94, 0x0E97, 1},
1333 {0x0E99, 0x0E9F, 1},
1334 {0x0EA1, 0x0EA3, 1},
1335 {0x0EA5, 0x0EA7, 2},
1336 {0x0EAA, 0x0EAB, 1},
1337 {0x0EAD, 0x0EAE, 1},
1338 {0x0EB0, 0x0EB0, 1},
1339 {0x0EB2, 0x0EB3, 1},
1340 {0x0EBD, 0x0EBD, 1},
1341 {0x0EC0, 0x0EC4, 1},
1342 {0x0F40, 0x0F47, 1},
1343 {0x0F49, 0x0F69, 1},
1344 {0x10A0, 0x10C5, 1},
1345 {0x10D0, 0x10F6, 1},
1346 {0x1100, 0x1100, 1},
1347 {0x1102, 0x1103, 1},
1348 {0x1105, 0x1107, 1},
1349 {0x1109, 0x1109, 1},
1350 {0x110B, 0x110C, 1},
1351 {0x110E, 0x1112, 1},
1352 {0x113C, 0x1140, 2},
1353 {0x114C, 0x1150, 2},
1354 {0x1154, 0x1155, 1},
1355 {0x1159, 0x1159, 1},
1356 {0x115F, 0x1161, 1},
1357 {0x1163, 0x1169, 2},
1358 {0x116D, 0x116E, 1},
1359 {0x1172, 0x1173, 1},
1360 {0x1175, 0x119E, 0x119E - 0x1175},
1361 {0x11A8, 0x11AB, 0x11AB - 0x11A8},
1362 {0x11AE, 0x11AF, 1},
1363 {0x11B7, 0x11B8, 1},
1364 {0x11BA, 0x11BA, 1},
1365 {0x11BC, 0x11C2, 1},
1366 {0x11EB, 0x11F0, 0x11F0 - 0x11EB},
1367 {0x11F9, 0x11F9, 1},
1368 {0x1E00, 0x1E9B, 1},
1369 {0x1EA0, 0x1EF9, 1},
1370 {0x1F00, 0x1F15, 1},
1371 {0x1F18, 0x1F1D, 1},
1372 {0x1F20, 0x1F45, 1},
1373 {0x1F48, 0x1F4D, 1},
1374 {0x1F50, 0x1F57, 1},
1375 {0x1F59, 0x1F5B, 0x1F5B - 0x1F59},
1376 {0x1F5D, 0x1F5D, 1},
1377 {0x1F5F, 0x1F7D, 1},
1378 {0x1F80, 0x1FB4, 1},
1379 {0x1FB6, 0x1FBC, 1},
1380 {0x1FBE, 0x1FBE, 1},
1381 {0x1FC2, 0x1FC4, 1},
1382 {0x1FC6, 0x1FCC, 1},
1383 {0x1FD0, 0x1FD3, 1},
1384 {0x1FD6, 0x1FDB, 1},
1385 {0x1FE0, 0x1FEC, 1},
1386 {0x1FF2, 0x1FF4, 1},
1387 {0x1FF6, 0x1FFC, 1},
1388 {0x2126, 0x2126, 1},
1389 {0x212A, 0x212B, 1},
1390 {0x212E, 0x212E, 1},
1391 {0x2180, 0x2182, 1},
1392 {0x3007, 0x3007, 1},
1393 {0x3021, 0x3029, 1},
1394 {0x3041, 0x3094, 1},
1395 {0x30A1, 0x30FA, 1},
1396 {0x3105, 0x312C, 1},
1397 {0x4E00, 0x9FA5, 1},
1398 {0xAC00, 0xD7A3, 1},
1402 var second = &unicode.RangeTable{
1403 R16: []unicode.Range16{
1404 {0x002D, 0x002E, 1},
1405 {0x0030, 0x0039, 1},
1406 {0x00B7, 0x00B7, 1},
1407 {0x02D0, 0x02D1, 1},
1408 {0x0300, 0x0345, 1},
1409 {0x0360, 0x0361, 1},
1410 {0x0387, 0x0387, 1},
1411 {0x0483, 0x0486, 1},
1412 {0x0591, 0x05A1, 1},
1413 {0x05A3, 0x05B9, 1},
1414 {0x05BB, 0x05BD, 1},
1415 {0x05BF, 0x05BF, 1},
1416 {0x05C1, 0x05C2, 1},
1417 {0x05C4, 0x0640, 0x0640 - 0x05C4},
1418 {0x064B, 0x0652, 1},
1419 {0x0660, 0x0669, 1},
1420 {0x0670, 0x0670, 1},
1421 {0x06D6, 0x06DC, 1},
1422 {0x06DD, 0x06DF, 1},
1423 {0x06E0, 0x06E4, 1},
1424 {0x06E7, 0x06E8, 1},
1425 {0x06EA, 0x06ED, 1},
1426 {0x06F0, 0x06F9, 1},
1427 {0x0901, 0x0903, 1},
1428 {0x093C, 0x093C, 1},
1429 {0x093E, 0x094C, 1},
1430 {0x094D, 0x094D, 1},
1431 {0x0951, 0x0954, 1},
1432 {0x0962, 0x0963, 1},
1433 {0x0966, 0x096F, 1},
1434 {0x0981, 0x0983, 1},
1435 {0x09BC, 0x09BC, 1},
1436 {0x09BE, 0x09BF, 1},
1437 {0x09C0, 0x09C4, 1},
1438 {0x09C7, 0x09C8, 1},
1439 {0x09CB, 0x09CD, 1},
1440 {0x09D7, 0x09D7, 1},
1441 {0x09E2, 0x09E3, 1},
1442 {0x09E6, 0x09EF, 1},
1443 {0x0A02, 0x0A3C, 0x3A},
1444 {0x0A3E, 0x0A3F, 1},
1445 {0x0A40, 0x0A42, 1},
1446 {0x0A47, 0x0A48, 1},
1447 {0x0A4B, 0x0A4D, 1},
1448 {0x0A66, 0x0A6F, 1},
1449 {0x0A70, 0x0A71, 1},
1450 {0x0A81, 0x0A83, 1},
1451 {0x0ABC, 0x0ABC, 1},
1452 {0x0ABE, 0x0AC5, 1},
1453 {0x0AC7, 0x0AC9, 1},
1454 {0x0ACB, 0x0ACD, 1},
1455 {0x0AE6, 0x0AEF, 1},
1456 {0x0B01, 0x0B03, 1},
1457 {0x0B3C, 0x0B3C, 1},
1458 {0x0B3E, 0x0B43, 1},
1459 {0x0B47, 0x0B48, 1},
1460 {0x0B4B, 0x0B4D, 1},
1461 {0x0B56, 0x0B57, 1},
1462 {0x0B66, 0x0B6F, 1},
1463 {0x0B82, 0x0B83, 1},
1464 {0x0BBE, 0x0BC2, 1},
1465 {0x0BC6, 0x0BC8, 1},
1466 {0x0BCA, 0x0BCD, 1},
1467 {0x0BD7, 0x0BD7, 1},
1468 {0x0BE7, 0x0BEF, 1},
1469 {0x0C01, 0x0C03, 1},
1470 {0x0C3E, 0x0C44, 1},
1471 {0x0C46, 0x0C48, 1},
1472 {0x0C4A, 0x0C4D, 1},
1473 {0x0C55, 0x0C56, 1},
1474 {0x0C66, 0x0C6F, 1},
1475 {0x0C82, 0x0C83, 1},
1476 {0x0CBE, 0x0CC4, 1},
1477 {0x0CC6, 0x0CC8, 1},
1478 {0x0CCA, 0x0CCD, 1},
1479 {0x0CD5, 0x0CD6, 1},
1480 {0x0CE6, 0x0CEF, 1},
1481 {0x0D02, 0x0D03, 1},
1482 {0x0D3E, 0x0D43, 1},
1483 {0x0D46, 0x0D48, 1},
1484 {0x0D4A, 0x0D4D, 1},
1485 {0x0D57, 0x0D57, 1},
1486 {0x0D66, 0x0D6F, 1},
1487 {0x0E31, 0x0E31, 1},
1488 {0x0E34, 0x0E3A, 1},
1489 {0x0E46, 0x0E46, 1},
1490 {0x0E47, 0x0E4E, 1},
1491 {0x0E50, 0x0E59, 1},
1492 {0x0EB1, 0x0EB1, 1},
1493 {0x0EB4, 0x0EB9, 1},
1494 {0x0EBB, 0x0EBC, 1},
1495 {0x0EC6, 0x0EC6, 1},
1496 {0x0EC8, 0x0ECD, 1},
1497 {0x0ED0, 0x0ED9, 1},
1498 {0x0F18, 0x0F19, 1},
1499 {0x0F20, 0x0F29, 1},
1500 {0x0F35, 0x0F39, 2},
1501 {0x0F3E, 0x0F3F, 1},
1502 {0x0F71, 0x0F84, 1},
1503 {0x0F86, 0x0F8B, 1},
1504 {0x0F90, 0x0F95, 1},
1505 {0x0F97, 0x0F97, 1},
1506 {0x0F99, 0x0FAD, 1},
1507 {0x0FB1, 0x0FB7, 1},
1508 {0x0FB9, 0x0FB9, 1},
1509 {0x20D0, 0x20DC, 1},
1510 {0x20E1, 0x3005, 0x3005 - 0x20E1},
1511 {0x302A, 0x302F, 1},
1512 {0x3031, 0x3035, 1},
1513 {0x3099, 0x309A, 1},
1514 {0x309D, 0x309E, 1},
1515 {0x30FC, 0x30FE, 1},
1519 // HTMLEntity is an entity map containing translations for the
1520 // standard HTML entity characters.
1521 var HTMLEntity = htmlEntity
1523 var htmlEntity = map[string]string{
1525 hget http://www.w3.org/TR/html4/sgml/entities.html |
1526 ssam '
1527 ,y /\&gt;/ x/\&lt;(.|\n)+/ s/\n/ /g
1528 ,x v/^\&lt;!ENTITY/d
1529 ,s/\&lt;!ENTITY ([^ ]+) .*U\+([0-9A-F][0-9A-F][0-9A-F][0-9A-F]) .+/ "\1": "\\u\2",/g
1532 "nbsp": "\u00A0",
1533 "iexcl": "\u00A1",
1534 "cent": "\u00A2",
1535 "pound": "\u00A3",
1536 "curren": "\u00A4",
1537 "yen": "\u00A5",
1538 "brvbar": "\u00A6",
1539 "sect": "\u00A7",
1540 "uml": "\u00A8",
1541 "copy": "\u00A9",
1542 "ordf": "\u00AA",
1543 "laquo": "\u00AB",
1544 "not": "\u00AC",
1545 "shy": "\u00AD",
1546 "reg": "\u00AE",
1547 "macr": "\u00AF",
1548 "deg": "\u00B0",
1549 "plusmn": "\u00B1",
1550 "sup2": "\u00B2",
1551 "sup3": "\u00B3",
1552 "acute": "\u00B4",
1553 "micro": "\u00B5",
1554 "para": "\u00B6",
1555 "middot": "\u00B7",
1556 "cedil": "\u00B8",
1557 "sup1": "\u00B9",
1558 "ordm": "\u00BA",
1559 "raquo": "\u00BB",
1560 "frac14": "\u00BC",
1561 "frac12": "\u00BD",
1562 "frac34": "\u00BE",
1563 "iquest": "\u00BF",
1564 "Agrave": "\u00C0",
1565 "Aacute": "\u00C1",
1566 "Acirc": "\u00C2",
1567 "Atilde": "\u00C3",
1568 "Auml": "\u00C4",
1569 "Aring": "\u00C5",
1570 "AElig": "\u00C6",
1571 "Ccedil": "\u00C7",
1572 "Egrave": "\u00C8",
1573 "Eacute": "\u00C9",
1574 "Ecirc": "\u00CA",
1575 "Euml": "\u00CB",
1576 "Igrave": "\u00CC",
1577 "Iacute": "\u00CD",
1578 "Icirc": "\u00CE",
1579 "Iuml": "\u00CF",
1580 "ETH": "\u00D0",
1581 "Ntilde": "\u00D1",
1582 "Ograve": "\u00D2",
1583 "Oacute": "\u00D3",
1584 "Ocirc": "\u00D4",
1585 "Otilde": "\u00D5",
1586 "Ouml": "\u00D6",
1587 "times": "\u00D7",
1588 "Oslash": "\u00D8",
1589 "Ugrave": "\u00D9",
1590 "Uacute": "\u00DA",
1591 "Ucirc": "\u00DB",
1592 "Uuml": "\u00DC",
1593 "Yacute": "\u00DD",
1594 "THORN": "\u00DE",
1595 "szlig": "\u00DF",
1596 "agrave": "\u00E0",
1597 "aacute": "\u00E1",
1598 "acirc": "\u00E2",
1599 "atilde": "\u00E3",
1600 "auml": "\u00E4",
1601 "aring": "\u00E5",
1602 "aelig": "\u00E6",
1603 "ccedil": "\u00E7",
1604 "egrave": "\u00E8",
1605 "eacute": "\u00E9",
1606 "ecirc": "\u00EA",
1607 "euml": "\u00EB",
1608 "igrave": "\u00EC",
1609 "iacute": "\u00ED",
1610 "icirc": "\u00EE",
1611 "iuml": "\u00EF",
1612 "eth": "\u00F0",
1613 "ntilde": "\u00F1",
1614 "ograve": "\u00F2",
1615 "oacute": "\u00F3",
1616 "ocirc": "\u00F4",
1617 "otilde": "\u00F5",
1618 "ouml": "\u00F6",
1619 "divide": "\u00F7",
1620 "oslash": "\u00F8",
1621 "ugrave": "\u00F9",
1622 "uacute": "\u00FA",
1623 "ucirc": "\u00FB",
1624 "uuml": "\u00FC",
1625 "yacute": "\u00FD",
1626 "thorn": "\u00FE",
1627 "yuml": "\u00FF",
1628 "fnof": "\u0192",
1629 "Alpha": "\u0391",
1630 "Beta": "\u0392",
1631 "Gamma": "\u0393",
1632 "Delta": "\u0394",
1633 "Epsilon": "\u0395",
1634 "Zeta": "\u0396",
1635 "Eta": "\u0397",
1636 "Theta": "\u0398",
1637 "Iota": "\u0399",
1638 "Kappa": "\u039A",
1639 "Lambda": "\u039B",
1640 "Mu": "\u039C",
1641 "Nu": "\u039D",
1642 "Xi": "\u039E",
1643 "Omicron": "\u039F",
1644 "Pi": "\u03A0",
1645 "Rho": "\u03A1",
1646 "Sigma": "\u03A3",
1647 "Tau": "\u03A4",
1648 "Upsilon": "\u03A5",
1649 "Phi": "\u03A6",
1650 "Chi": "\u03A7",
1651 "Psi": "\u03A8",
1652 "Omega": "\u03A9",
1653 "alpha": "\u03B1",
1654 "beta": "\u03B2",
1655 "gamma": "\u03B3",
1656 "delta": "\u03B4",
1657 "epsilon": "\u03B5",
1658 "zeta": "\u03B6",
1659 "eta": "\u03B7",
1660 "theta": "\u03B8",
1661 "iota": "\u03B9",
1662 "kappa": "\u03BA",
1663 "lambda": "\u03BB",
1664 "mu": "\u03BC",
1665 "nu": "\u03BD",
1666 "xi": "\u03BE",
1667 "omicron": "\u03BF",
1668 "pi": "\u03C0",
1669 "rho": "\u03C1",
1670 "sigmaf": "\u03C2",
1671 "sigma": "\u03C3",
1672 "tau": "\u03C4",
1673 "upsilon": "\u03C5",
1674 "phi": "\u03C6",
1675 "chi": "\u03C7",
1676 "psi": "\u03C8",
1677 "omega": "\u03C9",
1678 "thetasym": "\u03D1",
1679 "upsih": "\u03D2",
1680 "piv": "\u03D6",
1681 "bull": "\u2022",
1682 "hellip": "\u2026",
1683 "prime": "\u2032",
1684 "Prime": "\u2033",
1685 "oline": "\u203E",
1686 "frasl": "\u2044",
1687 "weierp": "\u2118",
1688 "image": "\u2111",
1689 "real": "\u211C",
1690 "trade": "\u2122",
1691 "alefsym": "\u2135",
1692 "larr": "\u2190",
1693 "uarr": "\u2191",
1694 "rarr": "\u2192",
1695 "darr": "\u2193",
1696 "harr": "\u2194",
1697 "crarr": "\u21B5",
1698 "lArr": "\u21D0",
1699 "uArr": "\u21D1",
1700 "rArr": "\u21D2",
1701 "dArr": "\u21D3",
1702 "hArr": "\u21D4",
1703 "forall": "\u2200",
1704 "part": "\u2202",
1705 "exist": "\u2203",
1706 "empty": "\u2205",
1707 "nabla": "\u2207",
1708 "isin": "\u2208",
1709 "notin": "\u2209",
1710 "ni": "\u220B",
1711 "prod": "\u220F",
1712 "sum": "\u2211",
1713 "minus": "\u2212",
1714 "lowast": "\u2217",
1715 "radic": "\u221A",
1716 "prop": "\u221D",
1717 "infin": "\u221E",
1718 "ang": "\u2220",
1719 "and": "\u2227",
1720 "or": "\u2228",
1721 "cap": "\u2229",
1722 "cup": "\u222A",
1723 "int": "\u222B",
1724 "there4": "\u2234",
1725 "sim": "\u223C",
1726 "cong": "\u2245",
1727 "asymp": "\u2248",
1728 "ne": "\u2260",
1729 "equiv": "\u2261",
1730 "le": "\u2264",
1731 "ge": "\u2265",
1732 "sub": "\u2282",
1733 "sup": "\u2283",
1734 "nsub": "\u2284",
1735 "sube": "\u2286",
1736 "supe": "\u2287",
1737 "oplus": "\u2295",
1738 "otimes": "\u2297",
1739 "perp": "\u22A5",
1740 "sdot": "\u22C5",
1741 "lceil": "\u2308",
1742 "rceil": "\u2309",
1743 "lfloor": "\u230A",
1744 "rfloor": "\u230B",
1745 "lang": "\u2329",
1746 "rang": "\u232A",
1747 "loz": "\u25CA",
1748 "spades": "\u2660",
1749 "clubs": "\u2663",
1750 "hearts": "\u2665",
1751 "diams": "\u2666",
1752 "quot": "\u0022",
1753 "amp": "\u0026",
1754 "lt": "\u003C",
1755 "gt": "\u003E",
1756 "OElig": "\u0152",
1757 "oelig": "\u0153",
1758 "Scaron": "\u0160",
1759 "scaron": "\u0161",
1760 "Yuml": "\u0178",
1761 "circ": "\u02C6",
1762 "tilde": "\u02DC",
1763 "ensp": "\u2002",
1764 "emsp": "\u2003",
1765 "thinsp": "\u2009",
1766 "zwnj": "\u200C",
1767 "zwj": "\u200D",
1768 "lrm": "\u200E",
1769 "rlm": "\u200F",
1770 "ndash": "\u2013",
1771 "mdash": "\u2014",
1772 "lsquo": "\u2018",
1773 "rsquo": "\u2019",
1774 "sbquo": "\u201A",
1775 "ldquo": "\u201C",
1776 "rdquo": "\u201D",
1777 "bdquo": "\u201E",
1778 "dagger": "\u2020",
1779 "Dagger": "\u2021",
1780 "permil": "\u2030",
1781 "lsaquo": "\u2039",
1782 "rsaquo": "\u203A",
1783 "euro": "\u20AC",
1786 // HTMLAutoClose is the set of HTML elements that
1787 // should be considered to close automatically.
1788 var HTMLAutoClose = htmlAutoClose
1790 var htmlAutoClose = []string{
1792 hget http://www.w3.org/TR/html4/loose.dtd |
1793 9 sed -n 's/<!ELEMENT ([^ ]*) +- O EMPTY.+/ "\1",/p' | tr A-Z a-z
1795 "basefont",
1796 "br",
1797 "area",
1798 "link",
1799 "img",
1800 "param",
1801 "hr",
1802 "input",
1803 "col",
1804 "frame",
1805 "isindex",
1806 "base",
1807 "meta",
1810 var (
1811 esc_quot = []byte("&#34;") // shorter than "&quot;"
1812 esc_apos = []byte("&#39;") // shorter than "&apos;"
1813 esc_amp = []byte("&amp;")
1814 esc_lt = []byte("&lt;")
1815 esc_gt = []byte("&gt;")
1816 esc_tab = []byte("&#x9;")
1817 esc_nl = []byte("&#xA;")
1818 esc_cr = []byte("&#xD;")
1819 esc_fffd = []byte("\uFFFD") // Unicode replacement character
1822 // EscapeText writes to w the properly escaped XML equivalent
1823 // of the plain text data s.
1824 func EscapeText(w io.Writer, s []byte) error {
1825 var esc []byte
1826 last := 0
1827 for i := 0; i < len(s); {
1828 r, width := utf8.DecodeRune(s[i:])
1829 i += width
1830 switch r {
1831 case '"':
1832 esc = esc_quot
1833 case '\'':
1834 esc = esc_apos
1835 case '&':
1836 esc = esc_amp
1837 case '<':
1838 esc = esc_lt
1839 case '>':
1840 esc = esc_gt
1841 case '\t':
1842 esc = esc_tab
1843 case '\n':
1844 esc = esc_nl
1845 case '\r':
1846 esc = esc_cr
1847 default:
1848 if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
1849 esc = esc_fffd
1850 break
1852 continue
1854 if _, err := w.Write(s[last : i-width]); err != nil {
1855 return err
1857 if _, err := w.Write(esc); err != nil {
1858 return err
1860 last = i
1862 if _, err := w.Write(s[last:]); err != nil {
1863 return err
1865 return nil
1868 // EscapeString writes to p the properly escaped XML equivalent
1869 // of the plain text data s.
1870 func (p *printer) EscapeString(s string) {
1871 var esc []byte
1872 last := 0
1873 for i := 0; i < len(s); {
1874 r, width := utf8.DecodeRuneInString(s[i:])
1875 i += width
1876 switch r {
1877 case '"':
1878 esc = esc_quot
1879 case '\'':
1880 esc = esc_apos
1881 case '&':
1882 esc = esc_amp
1883 case '<':
1884 esc = esc_lt
1885 case '>':
1886 esc = esc_gt
1887 case '\t':
1888 esc = esc_tab
1889 case '\n':
1890 esc = esc_nl
1891 case '\r':
1892 esc = esc_cr
1893 default:
1894 if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
1895 esc = esc_fffd
1896 break
1898 continue
1900 p.WriteString(s[last : i-width])
1901 p.Write(esc)
1902 last = i
1904 p.WriteString(s[last:])
1907 // Escape is like EscapeText but omits the error return value.
1908 // It is provided for backwards compatibility with Go 1.0.
1909 // Code targeting Go 1.1 or later should use EscapeText.
1910 func Escape(w io.Writer, s []byte) {
1911 EscapeText(w, s)
1914 // procInstEncoding parses the `encoding="..."` or `encoding='...'`
1915 // value out of the provided string, returning "" if not found.
1916 func procInstEncoding(s string) string {
1917 // TODO: this parsing is somewhat lame and not exact.
1918 // It works for all actual cases, though.
1919 idx := strings.Index(s, "encoding=")
1920 if idx == -1 {
1921 return ""
1923 v := s[idx+len("encoding="):]
1924 if v == "" {
1925 return ""
1927 if v[0] != '\'' && v[0] != '"' {
1928 return ""
1930 idx = strings.IndexRune(v[1:], rune(v[0]))
1931 if idx == -1 {
1932 return ""
1934 return v[1 : idx+1]