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