File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed
Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,13 @@ func WithoutComments() OutputOption {
8080 }
8181}
8282
83+ // WithPreserveSpace will preserve spaces in output
84+ func WithPreserveSpace () OutputOption {
85+ return func (oc * outputConfiguration ) {
86+ oc .preserveSpaces = true
87+ }
88+ }
89+
8390func newXMLName (name string ) xml.Name {
8491 if i := strings .IndexByte (name , ':' ); i > 0 {
8592 return xml.Name {
@@ -216,8 +223,8 @@ func (n *Node) OutputXMLWithOptions(opts ...OutputOption) string {
216223 for _ , opt := range opts {
217224 opt (config )
218225 }
219-
220- preserveSpaces := calculatePreserveSpaces (n , false )
226+ pastPreserveSpaces := config . preserveSpaces
227+ preserveSpaces := calculatePreserveSpaces (n , pastPreserveSpaces )
221228 var b strings.Builder
222229 if config .printSelf && n .Type != DocumentNode {
223230 outputXML (& b , n , preserveSpaces , config )
Original file line number Diff line number Diff line change @@ -592,3 +592,22 @@ func TestOutputXMLWithOptions(t *testing.T) {
592592 t .Errorf ("output was not expected. expected %v but got %v" , expected , result )
593593 }
594594}
595+
596+ func TestOutputXMLWithPreserveSpaceOption (t * testing.T ) {
597+ s := `<?xml version="1.0" encoding="utf-8"?>
598+ <class_list>
599+ <student>
600+ <name> Robert </name>
601+ <grade>A+</grade>
602+ </student>
603+ </class_list>`
604+ doc , _ := Parse (strings .NewReader (s ))
605+ resultWithSpace := doc .OutputXMLWithOptions (WithPreserveSpace ())
606+ resultWithoutSpace := doc .OutputXMLWithOptions ()
607+ if ! strings .Contains (resultWithSpace , "> Robert <" ) {
608+ t .Errorf ("output was not expected. expected %v but got %v" , " Robert " , resultWithSpace )
609+ }
610+ if ! strings .Contains (resultWithoutSpace , ">Robert<" ) {
611+ t .Errorf ("output was not expected. expected %v but got %v" , " Robert " , resultWithoutSpace )
612+ }
613+ }
You can’t perform that action at this time.
0 commit comments