xml.go 43 KB

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