xml.go 45 KB

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