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