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