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