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