@@ -9,14 +9,14 @@ import (
9
9
"encoding/xml"
10
10
"fmt"
11
11
"strconv"
12
- "strings"
13
12
)
14
13
15
14
const (
16
15
NT_ROOT = iota
17
16
NT_DIRECTIVE
18
17
NT_PROCINST
19
18
NT_COMMENT
19
+ NT_TEXT
20
20
NT_ELEMENT
21
21
)
22
22
@@ -51,7 +51,7 @@ func NewNode(tid byte) *Node {
51
51
// This wraps the standard xml.Unmarshal function and supplies this particular
52
52
// node as the content to be unmarshalled.
53
53
func (this * Node ) Unmarshal (obj interface {}) error {
54
- return xml .NewDecoder (bytes .NewBuffer (this .bytes (0 ))).Decode (obj )
54
+ return xml .NewDecoder (bytes .NewBuffer (this .bytes ())).Decode (obj )
55
55
}
56
56
57
57
// Get node value as string
@@ -433,20 +433,22 @@ func (this *Node) SetAttr(name, value string) {
433
433
// Note that NT_ROOT is a special-case empty node used as the root for a
434
434
// Document. This one has no representation by itself. It merely forwards the
435
435
// String() call to it's child nodes.
436
- func (this * Node ) Bytes () []byte { return this .bytes (0 ) }
436
+ func (this * Node ) Bytes () []byte { return this .bytes () }
437
437
438
- func (this * Node ) bytes (indent int ) (b []byte ) {
438
+ func (this * Node ) bytes () (b []byte ) {
439
439
switch this .Type {
440
440
case NT_PROCINST :
441
- b = this .printProcInst (indent )
441
+ b = this .printProcInst ()
442
442
case NT_COMMENT :
443
- b = this .printComment (indent )
443
+ b = this .printComment ()
444
444
case NT_DIRECTIVE :
445
- b = this .printDirective (indent )
445
+ b = this .printDirective ()
446
446
case NT_ELEMENT :
447
- b = this .printElement (indent )
447
+ b = this .printElement ()
448
+ case NT_TEXT :
449
+ b = this .printText ()
448
450
case NT_ROOT :
449
- b = this .printRoot (indent )
451
+ b = this .printRoot ()
450
452
}
451
453
return
452
454
}
@@ -456,38 +458,42 @@ func (this *Node) bytes(indent int) (b []byte) {
456
458
// Document. This one has no representation by itself. It merely forwards the
457
459
// String() call to it's child nodes.
458
460
func (this * Node ) String () (s string ) {
459
- return string (this .bytes (0 ))
461
+ return string (this .bytes ())
460
462
}
461
463
462
- func (this * Node ) printRoot (indent int ) []byte {
464
+ func (this * Node ) printRoot () []byte {
463
465
var b bytes.Buffer
464
466
for _ , v := range this .Children {
465
- b .Write (v .bytes (indent ))
467
+ b .Write (v .bytes ())
466
468
}
467
469
return b .Bytes ()
468
470
}
469
471
470
- func (this * Node ) printProcInst (indent int ) []byte {
472
+ func (this * Node ) printProcInst () []byte {
471
473
return []byte ("<?" + this .Target + " " + this .Value + "?>" )
472
474
}
473
475
474
- func (this * Node ) printComment (indent int ) []byte {
476
+ func (this * Node ) printComment () []byte {
475
477
return []byte ("<!-- " + this .Value + " -->" )
476
478
}
477
479
478
- func (this * Node ) printDirective (indent int ) []byte {
480
+ func (this * Node ) printDirective () []byte {
479
481
return []byte ("<!" + this .Value + "!>" )
480
482
}
481
483
482
- func (this * Node ) printElement (indent int ) []byte {
484
+ func (this * Node ) printText () []byte {
485
+ val := []byte (this .Value )
486
+ if len (this .Parent .Children ) > 1 {
487
+ return val
488
+ }
483
489
var b bytes.Buffer
490
+ xml .EscapeText (& b , val )
491
+ return b .Bytes ()
492
+ }
484
493
485
- lineSuffix , linePrefix := "" , strings .Repeat (IndentPrefix , indent )
486
- if len (IndentPrefix ) > 0 {
487
- lineSuffix = "\n "
488
- }
494
+ func (this * Node ) printElement () []byte {
495
+ var b bytes.Buffer
489
496
490
- b .WriteString (linePrefix )
491
497
if len (this .Name .Space ) > 0 {
492
498
b .WriteRune ('<' )
493
499
b .WriteString (this .Name .Space )
@@ -509,23 +515,16 @@ func (this *Node) printElement(indent int) []byte {
509
515
510
516
if len (this .Children ) == 0 && len (this .Value ) == 0 {
511
517
b .WriteString (" />" )
512
- b .WriteString (lineSuffix )
513
518
return b .Bytes ()
514
519
}
515
520
516
521
b .WriteRune ('>' )
517
- if len (this .Value ) == 0 {
518
- b .WriteString (lineSuffix )
519
- }
520
522
521
523
for _ , v := range this .Children {
522
- b .Write (v .bytes (indent + 1 ))
524
+ b .Write (v .bytes ())
523
525
}
524
526
525
- b .WriteString (this .Value )
526
- if len (this .Value ) == 0 {
527
- b .WriteString (linePrefix )
528
- }
527
+ xml .EscapeText (& b , []byte (this .Value ))
529
528
if len (this .Name .Space ) > 0 {
530
529
b .WriteString ("</" )
531
530
b .WriteString (this .Name .Space )
@@ -537,7 +536,6 @@ func (this *Node) printElement(indent int) []byte {
537
536
b .WriteString (this .Name .Local )
538
537
b .WriteRune ('>' )
539
538
}
540
- b .WriteString (lineSuffix )
541
539
542
540
return b .Bytes ()
543
541
}
0 commit comments