| 1 |
// Copyright 2015-2019 Brett Vickers. |
| 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 etree |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/xml" |
| 10 |
"errors" |
| 11 |
"io" |
| 12 |
"io/fs" |
| 13 |
"math/rand" |
| 14 |
"os" |
| 15 |
"path" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
var bookstoreXML = ` |
| 21 |
<?xml version="1.0" encoding="UTF-8"?> |
| 22 |
<bookstore xmlns:p="urn:books-com:prices"> |
| 23 |
|
| 24 |
<!Directive> |
| 25 |
|
| 26 |
<book category="COOKING"> |
| 27 |
<title lang="en">Everyday Italian</title> |
| 28 |
<author>Giada De Laurentiis</author> |
| 29 |
<year>2005</year> |
| 30 |
<p:price>30.00</p:price> |
| 31 |
<editor>Clarkson Potter</editor> |
| 32 |
</book> |
| 33 |
|
| 34 |
<book category="CHILDREN"> |
| 35 |
<title lang="en" sku="150">Harry Potter</title> |
| 36 |
<author>J K. Rowling</author> |
| 37 |
<year>2005</year> |
| 38 |
<p:price p:tax="1.99">29.99</p:price> |
| 39 |
<editor></editor> |
| 40 |
<editor/> |
| 41 |
</book> |
| 42 |
|
| 43 |
<book category="WEB"> |
| 44 |
<title lang="en">XQuery Kick Start</title> |
| 45 |
<author>James McGovern</author> |
| 46 |
<author>Per Bothner</author> |
| 47 |
<author>Kurt Cagle</author> |
| 48 |
<author>James Linn</author> |
| 49 |
<author>Vaidyanathan Nagarajan</author> |
| 50 |
<year>2003</year> |
| 51 |
<price>49.99</price> |
| 52 |
<editor> |
| 53 |
</editor> |
| 54 |
</book> |
| 55 |
|
| 56 |
<!-- Final book --> |
| 57 |
<book category="WEB" path="/books/xml"> |
| 58 |
<title lang="en">Learning XML</title> |
| 59 |
<author>Erik T. Ray</author> |
| 60 |
<year>2003</year> |
| 61 |
<p:price>39.95</p:price> |
| 62 |
</book> |
| 63 |
|
| 64 |
</bookstore> |
| 65 |
` |
| 66 |
|
| 67 |
func newDocumentFromString(t *testing.T, s string) *Document { |
| 68 |
return newDocumentFromString2(t, s, ReadSettings{}) |
| 69 |
} |
| 70 |
|
| 71 |
func newDocumentFromString2(t *testing.T, s string, settings ReadSettings) *Document { |
| 72 |
t.Helper() |
| 73 |
doc := NewDocument() |
| 74 |
doc.ReadSettings = settings |
| 75 |
err := doc.ReadFromString(s) |
| 76 |
if err != nil { |
| 77 |
t.Fatal("etree: failed to parse document") |
| 78 |
} |
| 79 |
return doc |
| 80 |
} |
| 81 |
|
| 82 |
func checkStrEq(t *testing.T, got, want string) { |
| 83 |
t.Helper() |
| 84 |
if got != want { |
| 85 |
t.Errorf("etree: unexpected result.\nGot:\n%s\nWanted:\n%s\n", got, want) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func checkStrBinaryEq(t *testing.T, got, want string) { |
| 90 |
t.Helper() |
| 91 |
if got != want { |
| 92 |
t.Errorf("etree: unexpected result.\nGot:\n%v\nWanted:\n%v\n", []byte(got), []byte(want)) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func checkIntEq(t *testing.T, got, want int) { |
| 97 |
t.Helper() |
| 98 |
if got != want { |
| 99 |
t.Errorf("etree: unexpected integer. Got: %d. Wanted: %d\n", got, want) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func checkBoolEq(t *testing.T, got, want bool) { |
| 104 |
t.Helper() |
| 105 |
if got != want { |
| 106 |
t.Errorf("etree: unexpected boolean. Got: %v. Wanted: %v\n", got, want) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func checkElementEq(t *testing.T, got, want *Element) { |
| 111 |
t.Helper() |
| 112 |
if got != want { |
| 113 |
t.Errorf("etree: unexpected element. Got: %v. Wanted: %v.\n", got, want) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func checkDocEq(t *testing.T, doc *Document, expected string) { |
| 118 |
t.Helper() |
| 119 |
doc.Indent(NoIndent) |
| 120 |
s, err := doc.WriteToString() |
| 121 |
if err != nil { |
| 122 |
t.Error("etree: failed to serialize document") |
| 123 |
} |
| 124 |
if s != expected { |
| 125 |
t.Errorf("etree: unexpected document.\nGot:\n%s\nWanted:\n%s\n", s, expected) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func checkIndexes(t *testing.T, e *Element) { |
| 130 |
t.Helper() |
| 131 |
for i := 0; i < len(e.Child); i++ { |
| 132 |
c := e.Child[i] |
| 133 |
if c.Index() != i { |
| 134 |
t.Errorf("Child index mismatch. Got %d, expected %d.", c.Index(), i) |
| 135 |
} |
| 136 |
if ce, ok := c.(*Element); ok { |
| 137 |
checkIndexes(t, ce) |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestDocument(t *testing.T) { |
| 143 |
// Create a document |
| 144 |
doc := NewDocument() |
| 145 |
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) |
| 146 |
doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`) |
| 147 |
store := doc.CreateElement("store") |
| 148 |
store.CreateAttr("xmlns:t", "urn:books-com:titles") |
| 149 |
store.CreateDirective("Directive") |
| 150 |
store.CreateComment("This is a comment") |
| 151 |
book := store.CreateElement("book") |
| 152 |
book.CreateAttr("lang", "fr") |
| 153 |
book.CreateAttr("lang", "en") |
| 154 |
title := book.CreateElement("t:title") |
| 155 |
title.SetText("Nicholas Nickleby") |
| 156 |
title.SetText("Great Expectations") |
| 157 |
author := book.CreateElement("author") |
| 158 |
author.CreateCharData("Charles Dickens") |
| 159 |
review := book.CreateElement("review") |
| 160 |
review.CreateCData("<<< Will be replaced") |
| 161 |
review.SetCData(">>> Excellent book") |
| 162 |
doc.IndentTabs() |
| 163 |
|
| 164 |
checkIndexes(t, &doc.Element) |
| 165 |
|
| 166 |
// Serialize the document to a string |
| 167 |
s, err := doc.WriteToString() |
| 168 |
if err != nil { |
| 169 |
t.Error("etree: failed to serialize document") |
| 170 |
} |
| 171 |
|
| 172 |
// Make sure the serialized XML matches expectation. |
| 173 |
expected := `<?xml version="1.0" encoding="UTF-8"?> |
| 174 |
<?xml-stylesheet type="text/xsl" href="style.xsl"?> |
| 175 |
<store xmlns:t="urn:books-com:titles"> |
| 176 |
<!Directive> |
| 177 |
<!--This is a comment--> |
| 178 |
<book lang="en"> |
| 179 |
<t:title>Great Expectations</t:title> |
| 180 |
<author>Charles Dickens</author> |
| 181 |
<review><![CDATA[>>> Excellent book]]></review> |
| 182 |
</book> |
| 183 |
</store> |
| 184 |
` |
| 185 |
checkStrEq(t, s, expected) |
| 186 |
|
| 187 |
// Test the structure of the XML |
| 188 |
if doc.Root() != store { |
| 189 |
t.Error("etree: root mismatch") |
| 190 |
} |
| 191 |
if len(store.ChildElements()) != 1 || len(store.Child) != 7 { |
| 192 |
t.Error("etree: incorrect tree structure") |
| 193 |
} |
| 194 |
if len(book.ChildElements()) != 3 || len(book.Attr) != 1 || len(book.Child) != 7 { |
| 195 |
t.Error("etree: incorrect tree structure") |
| 196 |
} |
| 197 |
if len(title.ChildElements()) != 0 || len(title.Child) != 1 || len(title.Attr) != 0 { |
| 198 |
t.Error("etree: incorrect tree structure") |
| 199 |
} |
| 200 |
if len(author.ChildElements()) != 0 || len(author.Child) != 1 || len(author.Attr) != 0 { |
| 201 |
t.Error("etree: incorrect tree structure") |
| 202 |
} |
| 203 |
if len(review.ChildElements()) != 0 || len(review.Child) != 1 || len(review.Attr) != 0 { |
| 204 |
t.Error("etree: incorrect tree structure") |
| 205 |
} |
| 206 |
if book.parent != store || store.parent != &doc.Element || doc.parent != nil { |
| 207 |
t.Error("etree: incorrect tree structure") |
| 208 |
} |
| 209 |
if title.parent != book || author.parent != book { |
| 210 |
t.Error("etree: incorrect tree structure") |
| 211 |
} |
| 212 |
|
| 213 |
// Perform some basic queries on the document |
| 214 |
elements := doc.SelectElements("store") |
| 215 |
if len(elements) != 1 || elements[0] != store { |
| 216 |
t.Error("etree: incorrect SelectElements result") |
| 217 |
} |
| 218 |
element := doc.SelectElement("store") |
| 219 |
if element != store { |
| 220 |
t.Error("etree: incorrect SelectElement result") |
| 221 |
} |
| 222 |
elements = store.SelectElements("book") |
| 223 |
if len(elements) != 1 || elements[0] != book { |
| 224 |
t.Error("etree: incorrect SelectElements result") |
| 225 |
} |
| 226 |
element = store.SelectElement("book") |
| 227 |
if element != book { |
| 228 |
t.Error("etree: incorrect SelectElement result") |
| 229 |
} |
| 230 |
attr := book.SelectAttr("lang") |
| 231 |
if attr == nil || attr.Key != "lang" || attr.Value != "en" { |
| 232 |
t.Error("etree: incorrect SelectAttr result") |
| 233 |
} |
| 234 |
if book.SelectAttrValue("lang", "unknown") != "en" { |
| 235 |
t.Error("etree: incorrect SelectAttrValue result") |
| 236 |
} |
| 237 |
if book.SelectAttrValue("t:missing", "unknown") != "unknown" { |
| 238 |
t.Error("etree: incorrect SelectAttrValue result") |
| 239 |
} |
| 240 |
attr = book.RemoveAttr("lang") |
| 241 |
if attr.Value != "en" { |
| 242 |
t.Error("etree: incorrect RemoveAttr result") |
| 243 |
} |
| 244 |
book.CreateAttr("lang", "de") |
| 245 |
attr = book.RemoveAttr("lang") |
| 246 |
if attr.Value != "de" { |
| 247 |
t.Error("etree: incorrect RemoveAttr result") |
| 248 |
} |
| 249 |
element = book.SelectElement("t:title") |
| 250 |
if element != title || element.Text() != "Great Expectations" || len(element.Attr) != 0 { |
| 251 |
t.Error("etree: incorrect SelectElement result") |
| 252 |
} |
| 253 |
element = book.SelectElement("title") |
| 254 |
if element != title { |
| 255 |
t.Error("etree: incorrect SelectElement result") |
| 256 |
} |
| 257 |
element = book.SelectElement("p:title") |
| 258 |
if element != nil { |
| 259 |
t.Error("etree: incorrect SelectElement result") |
| 260 |
} |
| 261 |
element = book.RemoveChildAt(title.Index()).(*Element) |
| 262 |
if element != title { |
| 263 |
t.Error("etree: incorrect RemoveElement result") |
| 264 |
} |
| 265 |
element = book.SelectElement("title") |
| 266 |
if element != nil { |
| 267 |
t.Error("etree: incorrect SelectElement result") |
| 268 |
} |
| 269 |
element = book.SelectElement("review") |
| 270 |
if element != review || element.Text() != ">>> Excellent book" || len(element.Attr) != 0 { |
| 271 |
t.Error("etree: incorrect SelectElement result") |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
func TestSelectElementsSeq(t *testing.T) { |
| 276 |
doc := NewDocument() |
| 277 |
store := doc.CreateElement("store") |
| 278 |
book1 := store.CreateElement("book") |
| 279 |
book1.CreateAttr("id", "1") |
| 280 |
book2 := store.CreateElement("book") |
| 281 |
book2.CreateAttr("id", "2") |
| 282 |
other := store.CreateElement("other") |
| 283 |
other.CreateAttr("id", "3") |
| 284 |
book3 := store.CreateElement("book") |
| 285 |
book3.CreateAttr("id", "4") |
| 286 |
|
| 287 |
expectedBooks := store.SelectElements("book") |
| 288 |
if len(expectedBooks) != 3 { |
| 289 |
t.Error("etree: setup error - expected 3 books") |
| 290 |
} |
| 291 |
|
| 292 |
var iterBooks []*Element |
| 293 |
for book := range store.SelectElementsSeq("book") { |
| 294 |
iterBooks = append(iterBooks, book) |
| 295 |
} |
| 296 |
|
| 297 |
if len(iterBooks) != len(expectedBooks) { |
| 298 |
t.Errorf("etree: SelectElementsSeq returned %d elements, expected %d", len(iterBooks), len(expectedBooks)) |
| 299 |
} |
| 300 |
|
| 301 |
for i, book := range iterBooks { |
| 302 |
if book != expectedBooks[i] { |
| 303 |
t.Errorf("etree: SelectElementsSeq element %d mismatch", i) |
| 304 |
} |
| 305 |
if book.SelectAttrValue("id", "") == "" { |
| 306 |
t.Errorf("etree: SelectElementsSeq element %d missing id attribute", i) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
count := 0 |
| 311 |
for book := range store.SelectElementsSeq("book") { |
| 312 |
count++ |
| 313 |
if count == 2 { |
| 314 |
break |
| 315 |
} |
| 316 |
_ = book |
| 317 |
} |
| 318 |
if count != 2 { |
| 319 |
t.Errorf("etree: early termination failed, got %d iterations", count) |
| 320 |
} |
| 321 |
|
| 322 |
nonExistentCount := 0 |
| 323 |
for range store.SelectElementsSeq("nonexistent") { |
| 324 |
nonExistentCount++ |
| 325 |
} |
| 326 |
if nonExistentCount != 0 { |
| 327 |
t.Errorf("etree: SelectElementsSeq found %d non-existent elements", nonExistentCount) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func TestFindElementsSeq(t *testing.T) { |
| 332 |
doc := NewDocument() |
| 333 |
store := doc.CreateElement("store") |
| 334 |
|
| 335 |
book1 := store.CreateElement("book") |
| 336 |
book1.CreateAttr("category", "fiction") |
| 337 |
title1 := book1.CreateElement("title") |
| 338 |
title1.SetText("Book One") |
| 339 |
author1 := book1.CreateElement("author") |
| 340 |
author1.SetText("Author A") |
| 341 |
|
| 342 |
book2 := store.CreateElement("book") |
| 343 |
book2.CreateAttr("category", "nonfiction") |
| 344 |
title2 := book2.CreateElement("title") |
| 345 |
title2.SetText("Book Two") |
| 346 |
author2 := book2.CreateElement("author") |
| 347 |
author2.SetText("Author B") |
| 348 |
|
| 349 |
magazine := store.CreateElement("magazine") |
| 350 |
magazine.CreateAttr("category", "tech") |
| 351 |
magTitle := magazine.CreateElement("title") |
| 352 |
magTitle.SetText("Tech Magazine") |
| 353 |
|
| 354 |
section := store.CreateElement("section") |
| 355 |
section.CreateAttr("name", "classics") |
| 356 |
book3 := section.CreateElement("book") |
| 357 |
book3.CreateAttr("category", "classic") |
| 358 |
title3 := book3.CreateElement("title") |
| 359 |
title3.SetText("Classic Book") |
| 360 |
|
| 361 |
testCases := []struct { |
| 362 |
name string |
| 363 |
path string |
| 364 |
}{ |
| 365 |
{"direct children", "book"}, |
| 366 |
{"all descendants", "//book"}, |
| 367 |
{"all titles", "//title"}, |
| 368 |
{"books with specific category", "book[@category='fiction']"}, |
| 369 |
{"nested path", "section/book"}, |
| 370 |
{"all elements", "//*"}, |
| 371 |
{"specific attribute", "//book[@category='classic']"}, |
| 372 |
} |
| 373 |
|
| 374 |
for _, tc := range testCases { |
| 375 |
t.Run(tc.name, func(t *testing.T) { |
| 376 |
expected := store.FindElements(tc.path) |
| 377 |
|
| 378 |
var iterResults []*Element |
| 379 |
for elem := range store.FindElementsSeq(tc.path) { |
| 380 |
iterResults = append(iterResults, elem) |
| 381 |
} |
| 382 |
|
| 383 |
if len(iterResults) != len(expected) { |
| 384 |
t.Errorf("FindElementsSeq returned %d elements, expected %d for path %s", |
| 385 |
len(iterResults), len(expected), tc.path) |
| 386 |
} |
| 387 |
|
| 388 |
expectedMap := make(map[*Element]bool) |
| 389 |
for _, e := range expected { |
| 390 |
expectedMap[e] = true |
| 391 |
} |
| 392 |
|
| 393 |
for _, e := range iterResults { |
| 394 |
if !expectedMap[e] { |
| 395 |
t.Errorf("FindElementsSeq returned unexpected element for path %s", tc.path) |
| 396 |
} |
| 397 |
} |
| 398 |
}) |
| 399 |
} |
| 400 |
|
| 401 |
t.Run("early termination", func(t *testing.T) { |
| 402 |
count := 0 |
| 403 |
for elem := range store.FindElementsSeq("//title") { |
| 404 |
count++ |
| 405 |
if count == 2 { |
| 406 |
break |
| 407 |
} |
| 408 |
_ = elem |
| 409 |
} |
| 410 |
if count != 2 { |
| 411 |
t.Errorf("Early termination failed, got %d iterations", count) |
| 412 |
} |
| 413 |
}) |
| 414 |
|
| 415 |
t.Run("compiled path", func(t *testing.T) { |
| 416 |
path := MustCompilePath("//book") |
| 417 |
expected := store.FindElementsPath(path) |
| 418 |
|
| 419 |
var iterResults []*Element |
| 420 |
for elem := range store.FindElementsPathSeq(path) { |
| 421 |
iterResults = append(iterResults, elem) |
| 422 |
} |
| 423 |
|
| 424 |
if len(iterResults) != len(expected) { |
| 425 |
t.Errorf("FindElementsPathSeq returned %d elements, expected %d", |
| 426 |
len(iterResults), len(expected)) |
| 427 |
} |
| 428 |
}) |
| 429 |
|
| 430 |
t.Run("empty results", func(t *testing.T) { |
| 431 |
count := 0 |
| 432 |
for range store.FindElementsSeq("//nonexistent") { |
| 433 |
count++ |
| 434 |
} |
| 435 |
if count != 0 { |
| 436 |
t.Errorf("FindElementsSeq found %d non-existent elements", count) |
| 437 |
} |
| 438 |
}) |
| 439 |
} |
| 440 |
|
| 441 |
func TestImbalancedXML(t *testing.T) { |
| 442 |
cases := []string{ |
| 443 |
`<test>`, |
| 444 |
`</test>`, |
| 445 |
`<test></test2>`, |
| 446 |
`<doc xmlns:p="xyz"><p:test></test></doc>`, |
| 447 |
`<doc xmlns:p="xyz"><test></p:test></doc>`, |
| 448 |
`<test>malformed`, |
| 449 |
`malformed</test>`, |
| 450 |
`<test><test></test>`, |
| 451 |
`<test></test></test>`, |
| 452 |
`<test><test></test></test2>`, |
| 453 |
`<test><test2></test></test2>`, |
| 454 |
} |
| 455 |
for _, c := range cases { |
| 456 |
doc := NewDocument() |
| 457 |
err := doc.ReadFromString(c) |
| 458 |
if err == nil { |
| 459 |
t.Errorf("etree: imbalanced XML should have failed:\n%s", c) |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
func TestDocumentCharsetReader(t *testing.T) { |
| 465 |
s := `<?xml version="1.0" encoding="lowercase"?> |
| 466 |
<Store> |
| 467 |
<Book Lang="en"> |
| 468 |
<Title>Great Expectations</Title> |
| 469 |
<Author>Charles Dickens</Author> |
| 470 |
</Book> |
| 471 |
</Store>` |
| 472 |
|
| 473 |
doc := newDocumentFromString2(t, s, ReadSettings{ |
| 474 |
CharsetReader: func(label string, input io.Reader) (io.Reader, error) { |
| 475 |
if label == "lowercase" { |
| 476 |
return &lowercaseCharsetReader{input}, nil |
| 477 |
} |
| 478 |
return nil, errors.New("unknown charset") |
| 479 |
}, |
| 480 |
}) |
| 481 |
|
| 482 |
cases := []struct { |
| 483 |
path string |
| 484 |
text string |
| 485 |
}{ |
| 486 |
{"/store/book/title", "great expectations"}, |
| 487 |
{"/store/book/author", "charles dickens"}, |
| 488 |
} |
| 489 |
for _, c := range cases { |
| 490 |
e := doc.FindElement(c.path) |
| 491 |
if e == nil { |
| 492 |
t.Errorf("etree: failed to find element '%s'", c.path) |
| 493 |
} else if e.Text() != c.text { |
| 494 |
t.Errorf("etree: expected path '%s' to contain '%s', got '%s'", c.path, c.text, e.Text()) |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
type lowercaseCharsetReader struct { |
| 500 |
r io.Reader |
| 501 |
} |
| 502 |
|
| 503 |
func (c *lowercaseCharsetReader) Read(p []byte) (n int, err error) { |
| 504 |
n, err = c.r.Read(p) |
| 505 |
if err != nil { |
| 506 |
return n, err |
| 507 |
} |
| 508 |
for i := 0; i < n; i++ { |
| 509 |
if p[i] >= 'A' && p[i] <= 'Z' { |
| 510 |
p[i] = p[i] - 'A' + 'a' |
| 511 |
} |
| 512 |
} |
| 513 |
return n, nil |
| 514 |
} |
| 515 |
|
| 516 |
func TestDocumentReadPermissive(t *testing.T) { |
| 517 |
s := "<select disabled></select>" |
| 518 |
|
| 519 |
doc := NewDocument() |
| 520 |
err := doc.ReadFromString(s) |
| 521 |
if err == nil { |
| 522 |
t.Fatal("etree: incorrect ReadFromString result") |
| 523 |
} |
| 524 |
|
| 525 |
doc.ReadSettings.Permissive = true |
| 526 |
err = doc.ReadFromString(s) |
| 527 |
if err != nil { |
| 528 |
t.Fatal("etree: incorrect ReadFromString result") |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
func TestEmbeddedComment(t *testing.T) { |
| 533 |
s := `<a>123<!-- test -->456</a>` |
| 534 |
|
| 535 |
doc := NewDocument() |
| 536 |
err := doc.ReadFromString(s) |
| 537 |
if err != nil { |
| 538 |
t.Fatal("etree: incorrect ReadFromString result") |
| 539 |
} |
| 540 |
|
| 541 |
a := doc.SelectElement("a") |
| 542 |
checkStrEq(t, a.Text(), "123456") |
| 543 |
} |
| 544 |
|
| 545 |
func TestDocumentReadHTMLEntities(t *testing.T) { |
| 546 |
s := `<store> |
| 547 |
<book lang="en"> |
| 548 |
<title>→ Great Expectations</title> |
| 549 |
<author>Charles Dickens</author> |
| 550 |
</book> |
| 551 |
</store>` |
| 552 |
|
| 553 |
doc := NewDocument() |
| 554 |
err := doc.ReadFromString(s) |
| 555 |
if err == nil { |
| 556 |
t.Fatal("etree: incorrect ReadFromString result") |
| 557 |
} |
| 558 |
|
| 559 |
doc.ReadSettings.Entity = xml.HTMLEntity |
| 560 |
err = doc.ReadFromString(s) |
| 561 |
if err != nil { |
| 562 |
t.Fatal("etree: incorrect ReadFromString result") |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
func TestDocumentReadHTMLAutoClose(t *testing.T) { |
| 567 |
cases := []struct { |
| 568 |
name string |
| 569 |
input string |
| 570 |
want string |
| 571 |
}{ |
| 572 |
{"empty", ``, ``}, |
| 573 |
{"oneSelfClosing", `<br>`, `<br/>`}, |
| 574 |
{"twoSelfClosingAndText", `<br>some text<br>`, `<br/>some text<br/>`}, |
| 575 |
{ |
| 576 |
name: "largerExample", |
| 577 |
input: `<img src="cover.jpg"> |
| 578 |
<hr> |
| 579 |
Author: Charles Dickens<br> |
| 580 |
Book: Great Expectations<br>`, |
| 581 |
want: `<img src="cover.jpg"/> |
| 582 |
<hr/> |
| 583 |
Author: Charles Dickens<br/> |
| 584 |
Book: Great Expectations<br/>`}, |
| 585 |
} |
| 586 |
|
| 587 |
for _, c := range cases { |
| 588 |
t.Run(c.name, func(t *testing.T) { |
| 589 |
doc := NewDocument() |
| 590 |
doc.ReadSettings.Permissive = true |
| 591 |
doc.ReadSettings.AutoClose = xml.HTMLAutoClose |
| 592 |
err := doc.ReadFromString(c.input) |
| 593 |
if err != nil { |
| 594 |
t.Fatal("etree: ReadFromString() error = ", err) |
| 595 |
} |
| 596 |
s, err := doc.WriteToString() |
| 597 |
if err != nil { |
| 598 |
t.Fatal("etree: WriteToString() error = ", err) |
| 599 |
} |
| 600 |
checkStrEq(t, s, c.want) |
| 601 |
}) |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
func TestEscapeCodes(t *testing.T) { |
| 606 |
cases := []struct { |
| 607 |
input string |
| 608 |
normal string |
| 609 |
attrCanonical string |
| 610 |
textCanonical string |
| 611 |
}{ |
| 612 |
{ |
| 613 |
"&<>'\"\t\n\r", |
| 614 |
"<e a=\"&<>'"\t\n\r\">&<>'"\t\n\r</e>", |
| 615 |
"<e a=\"&<>'"	

\">&<>'"\t\n\r</e>", |
| 616 |
"<e a=\"&<>'"\t\n\r\">&<>'\"\t\n
</e>", |
| 617 |
}, |
| 618 |
{ |
| 619 |
"\x00\x1f\x08\x09\x0a\x0d", |
| 620 |
"<e a=\"���\t\n\r\">���\t\n\r</e>", |
| 621 |
"<e a=\"���	

\">���\t\n\r</e>", |
| 622 |
"<e a=\"���\t\n\r\">���\t\n
</e>", |
| 623 |
}, |
| 624 |
} |
| 625 |
for _, c := range cases { |
| 626 |
doc := NewDocument() |
| 627 |
|
| 628 |
e := doc.CreateElement("e") |
| 629 |
e.SetText(c.input) |
| 630 |
e.CreateAttr("a", c.input) |
| 631 |
|
| 632 |
doc.WriteSettings.CanonicalText = false |
| 633 |
doc.WriteSettings.CanonicalAttrVal = false |
| 634 |
s, err := doc.WriteToString() |
| 635 |
if err != nil { |
| 636 |
t.Error("etree: Escape test produced inocrrect result.") |
| 637 |
} |
| 638 |
checkStrEq(t, s, c.normal) |
| 639 |
|
| 640 |
doc.WriteSettings.CanonicalText = false |
| 641 |
doc.WriteSettings.CanonicalAttrVal = true |
| 642 |
s, err = doc.WriteToString() |
| 643 |
if err != nil { |
| 644 |
t.Error("etree: Escape test produced inocrrect result.") |
| 645 |
} |
| 646 |
checkStrEq(t, s, c.attrCanonical) |
| 647 |
|
| 648 |
doc.WriteSettings.CanonicalText = true |
| 649 |
doc.WriteSettings.CanonicalAttrVal = false |
| 650 |
s, err = doc.WriteToString() |
| 651 |
if err != nil { |
| 652 |
t.Error("etree: Escape test produced inocrrect result.") |
| 653 |
} |
| 654 |
checkStrEq(t, s, c.textCanonical) |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
func TestSanitizeCData(t *testing.T) { |
| 659 |
cases := []struct { |
| 660 |
data string |
| 661 |
expected string |
| 662 |
}{ |
| 663 |
{"", "<t><![CDATA[]]></t>"}, |
| 664 |
{"text", "<t><![CDATA[text]]></t>"}, |
| 665 |
{"a<b&c>d", "<t><![CDATA[a<b&c>d]]></t>"}, |
| 666 |
{"]]>", "<t><![CDATA[]]]]><![CDATA[>]]></t>"}, |
| 667 |
{"a]]>b", "<t><![CDATA[a]]]]><![CDATA[>b]]></t>"}, |
| 668 |
{"a]]>b]]>c", "<t><![CDATA[a]]]]><![CDATA[>b]]]]><![CDATA[>c]]></t>"}, |
| 669 |
{"]]]>", "<t><![CDATA[]]]]]><![CDATA[>]]></t>"}, |
| 670 |
{"]]></t><injected/><t>", "<t><![CDATA[]]]]><![CDATA[></t><injected/><t>]]></t>"}, |
| 671 |
} |
| 672 |
for _, c := range cases { |
| 673 |
doc := NewDocument() |
| 674 |
e := doc.CreateElement("t") |
| 675 |
e.CreateCData(c.data) |
| 676 |
|
| 677 |
s, err := doc.WriteToString() |
| 678 |
if err != nil { |
| 679 |
t.Fatal("etree: failed to serialize document") |
| 680 |
} |
| 681 |
checkStrEq(t, s, c.expected) |
| 682 |
checkTokenIsolation(t, s) |
| 683 |
|
| 684 |
doc2 := newDocumentFromString2(t, s, ReadSettings{PreserveCData: true}) |
| 685 |
checkStrEq(t, doc2.Root().Text(), c.data) |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
func TestSanitizeComment(t *testing.T) { |
| 690 |
cases := []struct { |
| 691 |
data string |
| 692 |
expected string |
| 693 |
}{ |
| 694 |
{"", "<t><!----></t>"}, |
| 695 |
{" comment ", "<t><!-- comment --></t>"}, |
| 696 |
{"-", "<t><!--- --></t>"}, |
| 697 |
{"--", "<t><!--- - --></t>"}, |
| 698 |
{"---", "<t><!--- - - --></t>"}, |
| 699 |
{"a--b", "<t><!--a- -b--></t>"}, |
| 700 |
{"a-b-c", "<t><!--a-b-c--></t>"}, |
| 701 |
{"-->", "<t><!--- ->--></t>"}, |
| 702 |
{"--></t><injected/><t><!--", "<t><!--- -></t><injected/><t><!- - --></t>"}, |
| 703 |
} |
| 704 |
for _, c := range cases { |
| 705 |
doc := NewDocument() |
| 706 |
e := doc.CreateElement("t") |
| 707 |
e.CreateComment(c.data) |
| 708 |
|
| 709 |
s, err := doc.WriteToString() |
| 710 |
if err != nil { |
| 711 |
t.Fatal("etree: failed to serialize document") |
| 712 |
} |
| 713 |
checkStrEq(t, s, c.expected) |
| 714 |
checkTokenIsolation(t, s) |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
func TestSanitizeDirective(t *testing.T) { |
| 719 |
cases := []struct { |
| 720 |
data string |
| 721 |
expected string |
| 722 |
}{ |
| 723 |
{"DOCTYPE root SYSTEM \"root.dtd\"", "<t><!DOCTYPE root SYSTEM \"root.dtd\"></t>"}, |
| 724 |
{"DOCTYPE root [<!ELEMENT root (#PCDATA)>]", "<t><!DOCTYPE root [<!ELEMENT root (#PCDATA)>]></t>"}, |
| 725 |
{"DOCTYPE root [<!ENTITY e \"a>b\">]", "<t><!DOCTYPE root [<!ENTITY e \"a>b\">]></t>"}, |
| 726 |
{"DOCTYPE root [<!-- a > b -->]", "<t><!DOCTYPE root [<!-- a > b -->]></t>"}, |
| 727 |
|
| 728 |
// Unbalanced contents are character-escaped. |
| 729 |
{"DOCTYPE root><injected/", "<t><!DOCTYPE root><injected/></t>"}, |
| 730 |
{"DOCTYPE root \"unterminated", "<t><!DOCTYPE root "unterminated></t>"}, |
| 731 |
{"DOCTYPE root [<!ENTITY e \"a\"", "<t><!DOCTYPE root [<!ENTITY e "a"></t>"}, |
| 732 |
{"DOCTYPE root <!-- unterminated", "<t><!DOCTYPE root <!-- unterminated></t>"}, |
| 733 |
|
| 734 |
// The decoder consumes a directive's first character literally, so |
| 735 |
// contents whose balance depends on that character are escaped. |
| 736 |
{"\"><injected/>\"", "<t><!"><injected/>"></t>"}, |
| 737 |
{"<!-- --><injected/><!--", "<t><!<!-- --><injected/><!--></t>"}, |
| 738 |
|
| 739 |
// Contents that are empty or that begin with '-' or '[' are preceded |
| 740 |
// by a space so the directive isn't decoded as a directive, comment |
| 741 |
// or CDATA section. |
| 742 |
{"", "<t><! ></t>"}, |
| 743 |
{"-- comment --", "<t><! -- comment --></t>"}, |
| 744 |
{"--> <injected/> <!--", "<t><! --> <injected/> <!--></t>"}, |
| 745 |
{"[CDATA[]]> <injected/>", "<t><! [CDATA[]]> <injected/>></t>"}, |
| 746 |
} |
| 747 |
for _, c := range cases { |
| 748 |
doc := NewDocument() |
| 749 |
e := doc.CreateElement("t") |
| 750 |
e.CreateDirective(c.data) |
| 751 |
|
| 752 |
s, err := doc.WriteToString() |
| 753 |
if err != nil { |
| 754 |
t.Fatal("etree: failed to serialize document") |
| 755 |
} |
| 756 |
checkStrEq(t, s, c.expected) |
| 757 |
checkTokenIsolation(t, s) |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
func TestSanitizeProcInst(t *testing.T) { |
| 762 |
cases := []struct { |
| 763 |
target string |
| 764 |
inst string |
| 765 |
expected string |
| 766 |
}{ |
| 767 |
{"xml", `version="1.0" encoding="UTF-8"`, `<t><?xml version="1.0" encoding="UTF-8"?></t>`}, |
| 768 |
{"target", "", "<t><?target?></t>"}, |
| 769 |
{"target", "a?>b", "<t><?target a? >b?></t>"}, |
| 770 |
{"target", "?>", "<t><?target ? >?></t>"}, |
| 771 |
{"target", "a??>b", "<t><?target a?? >b?></t>"}, |
| 772 |
{"target", "?></t><injected/><t><?target", "<t><?target ? ></t><injected/><t><?target?></t>"}, |
| 773 |
{"target?>", "inst", "<t><?target? > inst?></t>"}, |
| 774 |
} |
| 775 |
for _, c := range cases { |
| 776 |
doc := NewDocument() |
| 777 |
e := doc.CreateElement("t") |
| 778 |
e.CreateProcInst(c.target, c.inst) |
| 779 |
|
| 780 |
s, err := doc.WriteToString() |
| 781 |
if err != nil { |
| 782 |
t.Fatal("etree: failed to serialize document") |
| 783 |
} |
| 784 |
checkStrEq(t, s, c.expected) |
| 785 |
checkTokenIsolation(t, s) |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
func TestFuzzInjection(t *testing.T) { |
| 790 |
fragments := []string{ |
| 791 |
"<", |
| 792 |
">", |
| 793 |
"-", |
| 794 |
"--", |
| 795 |
"-->", |
| 796 |
"<!--", |
| 797 |
"]]>", |
| 798 |
"[CDATA[", |
| 799 |
"?>", |
| 800 |
"<?", |
| 801 |
"!", |
| 802 |
"\"", |
| 803 |
"'", |
| 804 |
"[", |
| 805 |
"]", |
| 806 |
"<injected/>", |
| 807 |
"</t>", |
| 808 |
"a", |
| 809 |
" ", |
| 810 |
"\t", |
| 811 |
"DOCTYPE", |
| 812 |
"&", |
| 813 |
} |
| 814 |
|
| 815 |
rng := rand.New(rand.NewSource(1)) |
| 816 |
randData := func() string { |
| 817 |
var sb strings.Builder |
| 818 |
for i := rng.Intn(15) + 1; i >= 0; i-- { |
| 819 |
fragment := fragments[rng.Intn(len(fragments))] |
| 820 |
sb.WriteString(fragment) |
| 821 |
} |
| 822 |
return sb.String() |
| 823 |
} |
| 824 |
|
| 825 |
for i := range 100000 { |
| 826 |
data := randData() |
| 827 |
doc := NewDocument() |
| 828 |
e := doc.CreateElement("t") |
| 829 |
|
| 830 |
var kind string |
| 831 |
switch i % 4 { |
| 832 |
case 0: |
| 833 |
e.CreateCData(data) |
| 834 |
kind = "cdata" |
| 835 |
case 1: |
| 836 |
e.CreateComment(data) |
| 837 |
kind = "comment" |
| 838 |
case 2: |
| 839 |
e.CreateDirective(data) |
| 840 |
kind = "directive" |
| 841 |
case 3: |
| 842 |
e.CreateProcInst("t"+data, randData()) |
| 843 |
kind = "procinst" |
| 844 |
} |
| 845 |
|
| 846 |
s, err := doc.WriteToString() |
| 847 |
if err != nil { |
| 848 |
t.Fatalf("%s write err: %v", kind, err) |
| 849 |
} |
| 850 |
|
| 851 |
doc2 := NewDocument() |
| 852 |
doc2.ReadSettings.PreserveCData = true |
| 853 |
|
| 854 |
if err := doc2.ReadFromString(s); err != nil { |
| 855 |
t.Fatalf("%s: data %q produced unparseable xml %q: %v", kind, data, s, err) |
| 856 |
} |
| 857 |
|
| 858 |
if len(doc2.Child) != 1 || doc2.Root() == nil || doc2.Root().Tag != "t" { |
| 859 |
t.Fatalf("%s: data %q escaped the root: %q", kind, data, s) |
| 860 |
} |
| 861 |
|
| 862 |
if n := len(doc2.Root().ChildElements()); n != 0 { |
| 863 |
t.Fatalf("%s: data %q injected %d element(s): %q", kind, data, n, s) |
| 864 |
} |
| 865 |
|
| 866 |
if kind == "cdata" && doc2.Root().Text() != data { |
| 867 |
t.Fatalf("cdata: data %q did not round trip: %q", data, doc2.Root().Text()) |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
// checkTokenIsolation re-reads the XML string 's', which is expected to |
| 873 |
// contain a lone root element 't' holding a single token. It confirms that |
| 874 |
// the token's data remained trapped within the token instead of injecting new |
| 875 |
// XML into the document. |
| 876 |
func checkTokenIsolation(t *testing.T, s string) { |
| 877 |
t.Helper() |
| 878 |
|
| 879 |
doc := newDocumentFromString2(t, s, ReadSettings{PreserveCData: true}) |
| 880 |
if len(doc.Child) != 1 { |
| 881 |
t.Errorf("etree: token data escaped its document: %s\n", s) |
| 882 |
return |
| 883 |
} |
| 884 |
|
| 885 |
root := doc.Root() |
| 886 |
if root == nil || root.Tag != "t" { |
| 887 |
t.Errorf("etree: token data replaced the root element: %s\n", s) |
| 888 |
return |
| 889 |
} |
| 890 |
if len(root.ChildElements()) != 0 { |
| 891 |
t.Errorf("etree: token data injected an element: %s\n", s) |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
func TestCanonical(t *testing.T) { |
| 896 |
BOM := "\xef\xbb\xbf" |
| 897 |
|
| 898 |
doc := NewDocument() |
| 899 |
doc.WriteSettings.CanonicalEndTags = true |
| 900 |
doc.WriteSettings.CanonicalText = true |
| 901 |
doc.WriteSettings.CanonicalAttrVal = true |
| 902 |
doc.CreateCharData(BOM) |
| 903 |
doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`) |
| 904 |
|
| 905 |
people := doc.CreateElement("People") |
| 906 |
people.CreateComment("These are all known people") |
| 907 |
|
| 908 |
jon := people.CreateElement("Person") |
| 909 |
jon.CreateAttr("name", "Jon O'Reilly") |
| 910 |
jon.SetText("\r<'\">&\u0004\u0005\u001f�") |
| 911 |
|
| 912 |
sally := people.CreateElement("Person") |
| 913 |
sally.CreateAttr("name", "Sally") |
| 914 |
sally.CreateAttr("escape", "\r\n\t<'\">&") |
| 915 |
|
| 916 |
doc.Indent(2) |
| 917 |
s, err := doc.WriteToString() |
| 918 |
if err != nil { |
| 919 |
t.Error("etree: WriteSettings WriteTo produced incorrect result.") |
| 920 |
} |
| 921 |
|
| 922 |
expected := BOM + `<?xml-stylesheet type="text/xsl" href="style.xsl"?> |
| 923 |
<People> |
| 924 |
<!--These are all known people--> |
| 925 |
<Person name="Jon O'Reilly">
<'">&����</Person> |
| 926 |
<Person name="Sally" escape="
	<'">&"></Person> |
| 927 |
</People> |
| 928 |
` |
| 929 |
checkStrEq(t, s, expected) |
| 930 |
} |
| 931 |
|
| 932 |
func TestCopy(t *testing.T) { |
| 933 |
s := `<store> |
| 934 |
<book lang="en"> |
| 935 |
<title>Great Expectations</title> |
| 936 |
<author>Charles Dickens</author> |
| 937 |
</book> |
| 938 |
</store>` |
| 939 |
|
| 940 |
doc := newDocumentFromString(t, s) |
| 941 |
|
| 942 |
s1, err := doc.WriteToString() |
| 943 |
if err != nil { |
| 944 |
t.Error("etree: incorrect WriteToString result") |
| 945 |
} |
| 946 |
|
| 947 |
doc2 := doc.Copy() |
| 948 |
checkIndexes(t, &doc2.Element) |
| 949 |
s2, err := doc2.WriteToString() |
| 950 |
if err != nil { |
| 951 |
t.Error("etree: incorrect Copy result") |
| 952 |
} |
| 953 |
|
| 954 |
if s1 != s2 { |
| 955 |
t.Error("etree: mismatched Copy result") |
| 956 |
t.Error("wanted:\n" + s1) |
| 957 |
t.Error("got:\n" + s2) |
| 958 |
} |
| 959 |
|
| 960 |
e1 := doc.FindElement("./store/book/title") |
| 961 |
e2 := doc2.FindElement("./store/book/title") |
| 962 |
if e1 == nil || e2 == nil || e1.parent == nil || e1 == e2 { |
| 963 |
t.Error("etree: incorrect FindElement result") |
| 964 |
} |
| 965 |
|
| 966 |
e1.parent.RemoveChildAt(e1.Index()) |
| 967 |
s1, _ = doc.WriteToString() |
| 968 |
s2, _ = doc2.WriteToString() |
| 969 |
if s1 == s2 { |
| 970 |
t.Error("etree: incorrect result after RemoveElement") |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
func TestGetPath(t *testing.T) { |
| 975 |
s := `<a> |
| 976 |
<b1> |
| 977 |
<c1> |
| 978 |
<d1/> |
| 979 |
<d1a/> |
| 980 |
</c1> |
| 981 |
</b1> |
| 982 |
<b2> |
| 983 |
<c2> |
| 984 |
<d2/> |
| 985 |
</c2> |
| 986 |
</b2> |
| 987 |
</a>` |
| 988 |
|
| 989 |
doc := newDocumentFromString(t, s) |
| 990 |
|
| 991 |
cases := []struct { |
| 992 |
from string |
| 993 |
to string |
| 994 |
relpath string |
| 995 |
topath string |
| 996 |
}{ |
| 997 |
{"a", ".", "..", "/"}, |
| 998 |
{".", "a", "./a", "/a"}, |
| 999 |
{"a/b1/c1/d1", ".", "../../../..", "/"}, |
| 1000 |
{".", "a/b1/c1/d1", "./a/b1/c1/d1", "/a/b1/c1/d1"}, |
| 1001 |
{"a", "a", ".", "/a"}, |
| 1002 |
{"a/b1", "a/b1/c1", "./c1", "/a/b1/c1"}, |
| 1003 |
{"a/b1/c1", "a/b1", "..", "/a/b1"}, |
| 1004 |
{"a/b1/c1", "a/b1/c1", ".", "/a/b1/c1"}, |
| 1005 |
{"a", "a/b1", "./b1", "/a/b1"}, |
| 1006 |
{"a/b1", "a", "..", "/a"}, |
| 1007 |
{"a", "a/b1/c1", "./b1/c1", "/a/b1/c1"}, |
| 1008 |
{"a/b1/c1", "a", "../..", "/a"}, |
| 1009 |
{"a/b1/c1/d1", "a", "../../..", "/a"}, |
| 1010 |
{"a", "a/b1/c1/d1", "./b1/c1/d1", "/a/b1/c1/d1"}, |
| 1011 |
{"a/b1", "a/b2", "../b2", "/a/b2"}, |
| 1012 |
{"a/b2", "a/b1", "../b1", "/a/b1"}, |
| 1013 |
{"a/b1/c1/d1", "a/b2/c2/d2", "../../../b2/c2/d2", "/a/b2/c2/d2"}, |
| 1014 |
{"a/b2/c2/d2", "a/b1/c1/d1", "../../../b1/c1/d1", "/a/b1/c1/d1"}, |
| 1015 |
{"a/b1/c1/d1", "a/b1/c1/d1a", "../d1a", "/a/b1/c1/d1a"}, |
| 1016 |
} |
| 1017 |
|
| 1018 |
for _, c := range cases { |
| 1019 |
fe := doc.FindElement(c.from) |
| 1020 |
te := doc.FindElement(c.to) |
| 1021 |
|
| 1022 |
rp := te.GetRelativePath(fe) |
| 1023 |
if rp != c.relpath { |
| 1024 |
t.Errorf("GetRelativePath from '%s' to '%s'. Expected '%s', got '%s'.\n", c.from, c.to, c.relpath, rp) |
| 1025 |
} |
| 1026 |
|
| 1027 |
p := te.GetPath() |
| 1028 |
if p != c.topath { |
| 1029 |
t.Errorf("GetPath for '%s'. Expected '%s', got '%s'.\n", c.to, c.topath, p) |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
func TestInsertChild(t *testing.T) { |
| 1035 |
s := `<book lang="en"> |
| 1036 |
<t:title>Great Expectations</t:title> |
| 1037 |
<author>Charles Dickens</author> |
| 1038 |
</book> |
| 1039 |
` |
| 1040 |
|
| 1041 |
doc := newDocumentFromString(t, s) |
| 1042 |
|
| 1043 |
year := NewElement("year") |
| 1044 |
year.SetText("1861") |
| 1045 |
|
| 1046 |
book := doc.FindElement("//book") |
| 1047 |
book.InsertChildAt(book.SelectElement("t:title").Index(), year) |
| 1048 |
|
| 1049 |
expected1 := `<book lang="en"> |
| 1050 |
<year>1861</year> |
| 1051 |
<t:title>Great Expectations</t:title> |
| 1052 |
<author>Charles Dickens</author> |
| 1053 |
</book> |
| 1054 |
` |
| 1055 |
doc.Indent(2) |
| 1056 |
s1, _ := doc.WriteToString() |
| 1057 |
checkStrEq(t, s1, expected1) |
| 1058 |
|
| 1059 |
book.RemoveChildAt(year.Index()) |
| 1060 |
book.InsertChildAt(book.SelectElement("author").Index(), year) |
| 1061 |
|
| 1062 |
expected2 := `<book lang="en"> |
| 1063 |
<t:title>Great Expectations</t:title> |
| 1064 |
<year>1861</year> |
| 1065 |
<author>Charles Dickens</author> |
| 1066 |
</book> |
| 1067 |
` |
| 1068 |
doc.Indent(2) |
| 1069 |
s2, _ := doc.WriteToString() |
| 1070 |
checkStrEq(t, s2, expected2) |
| 1071 |
|
| 1072 |
book.RemoveChildAt(year.Index()) |
| 1073 |
book.InsertChildAt(len(book.Child), year) |
| 1074 |
|
| 1075 |
expected3 := `<book lang="en"> |
| 1076 |
<t:title>Great Expectations</t:title> |
| 1077 |
<author>Charles Dickens</author> |
| 1078 |
<year>1861</year> |
| 1079 |
</book> |
| 1080 |
` |
| 1081 |
doc.Indent(2) |
| 1082 |
s3, _ := doc.WriteToString() |
| 1083 |
checkStrEq(t, s3, expected3) |
| 1084 |
|
| 1085 |
book.RemoveChildAt(year.Index()) |
| 1086 |
book.InsertChildAt(999, year) |
| 1087 |
|
| 1088 |
expected4 := `<book lang="en"> |
| 1089 |
<t:title>Great Expectations</t:title> |
| 1090 |
<author>Charles Dickens</author> |
| 1091 |
<year>1861</year> |
| 1092 |
</book> |
| 1093 |
` |
| 1094 |
doc.Indent(2) |
| 1095 |
s4, _ := doc.WriteToString() |
| 1096 |
checkStrEq(t, s4, expected4) |
| 1097 |
|
| 1098 |
year = doc.FindElement("//book/year") |
| 1099 |
book.InsertChildAt(0, year) |
| 1100 |
|
| 1101 |
expected5 := `<book lang="en"> |
| 1102 |
<year>1861</year> |
| 1103 |
<t:title>Great Expectations</t:title> |
| 1104 |
<author>Charles Dickens</author> |
| 1105 |
</book> |
| 1106 |
` |
| 1107 |
|
| 1108 |
doc.Indent(2) |
| 1109 |
s5, _ := doc.WriteToString() |
| 1110 |
checkStrEq(t, s5, expected5) |
| 1111 |
|
| 1112 |
author := doc.FindElement("//book/author") |
| 1113 |
year = doc.FindElement("//book/year") |
| 1114 |
book.InsertChildAt(author.Index(), year) |
| 1115 |
|
| 1116 |
expected6 := `<book lang="en"> |
| 1117 |
<t:title>Great Expectations</t:title> |
| 1118 |
<year>1861</year> |
| 1119 |
<author>Charles Dickens</author> |
| 1120 |
</book> |
| 1121 |
` |
| 1122 |
doc.Indent(2) |
| 1123 |
s6, _ := doc.WriteToString() |
| 1124 |
checkStrEq(t, s6, expected6) |
| 1125 |
} |
| 1126 |
|
| 1127 |
func TestCdata(t *testing.T) { |
| 1128 |
var tests = []struct { |
| 1129 |
in, out string |
| 1130 |
}{ |
| 1131 |
{`<tag>1234567</tag>`, "1234567"}, |
| 1132 |
{`<tag><![CDATA[1234567]]></tag>`, "1234567"}, |
| 1133 |
{`<tag>1<![CDATA[2]]>3<![CDATA[4]]>5<![CDATA[6]]>7</tag>`, "1234567"}, |
| 1134 |
{`<tag>1<![CDATA[2]]>3<inner>4</inner>5<![CDATA[6]]>7</tag>`, "123"}, |
| 1135 |
{`<tag>1<inner>4</inner>5<![CDATA[6]]>7</tag>`, "1"}, |
| 1136 |
{`<tag><![CDATA[1]]><inner>4</inner>5<![CDATA[6]]>7</tag>`, "1"}, |
| 1137 |
} |
| 1138 |
|
| 1139 |
for _, test := range tests { |
| 1140 |
doc := NewDocument() |
| 1141 |
err := doc.ReadFromString(test.in) |
| 1142 |
if err != nil { |
| 1143 |
t.Fatal("etree ReadFromString: " + err.Error()) |
| 1144 |
} |
| 1145 |
|
| 1146 |
tag := doc.FindElement("tag") |
| 1147 |
if tag.Text() != test.out { |
| 1148 |
t.Fatalf("etree invalid cdata. Expected: %v. Got: %v\n", test.out, tag.Text()) |
| 1149 |
} |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
func TestAddChild(t *testing.T) { |
| 1154 |
s := `<book lang="en"> |
| 1155 |
<t:title>Great Expectations</t:title> |
| 1156 |
<author>Charles Dickens</author> |
| 1157 |
</book> |
| 1158 |
` |
| 1159 |
doc1 := newDocumentFromString(t, s) |
| 1160 |
|
| 1161 |
doc2 := NewDocument() |
| 1162 |
root := doc2.CreateElement("root") |
| 1163 |
|
| 1164 |
for _, e := range doc1.FindElements("//book/*") { |
| 1165 |
root.AddChild(e) |
| 1166 |
} |
| 1167 |
|
| 1168 |
expected1 := `<book lang="en"/> |
| 1169 |
` |
| 1170 |
doc1.Indent(2) |
| 1171 |
s1, _ := doc1.WriteToString() |
| 1172 |
checkStrEq(t, s1, expected1) |
| 1173 |
|
| 1174 |
expected2 := `<root> |
| 1175 |
<t:title>Great Expectations</t:title> |
| 1176 |
<author>Charles Dickens</author> |
| 1177 |
</root> |
| 1178 |
` |
| 1179 |
doc2.Indent(2) |
| 1180 |
s2, _ := doc2.WriteToString() |
| 1181 |
checkStrEq(t, s2, expected2) |
| 1182 |
} |
| 1183 |
|
| 1184 |
func TestSetRoot(t *testing.T) { |
| 1185 |
s := `<?test a="wow"?> |
| 1186 |
<book> |
| 1187 |
<title>Great Expectations</title> |
| 1188 |
<author>Charles Dickens</author> |
| 1189 |
</book> |
| 1190 |
` |
| 1191 |
doc := newDocumentFromString(t, s) |
| 1192 |
|
| 1193 |
origroot := doc.Root() |
| 1194 |
if origroot.Parent() != &doc.Element { |
| 1195 |
t.Error("Root incorrect") |
| 1196 |
} |
| 1197 |
|
| 1198 |
newroot := NewElement("root") |
| 1199 |
doc.SetRoot(newroot) |
| 1200 |
|
| 1201 |
if doc.Root() != newroot { |
| 1202 |
t.Error("doc.Root() != newroot") |
| 1203 |
} |
| 1204 |
if origroot.Parent() != nil { |
| 1205 |
t.Error("origroot.Parent() != nil") |
| 1206 |
} |
| 1207 |
|
| 1208 |
expected1 := `<?test a="wow"?> |
| 1209 |
<root/> |
| 1210 |
` |
| 1211 |
doc.Indent(2) |
| 1212 |
s1, _ := doc.WriteToString() |
| 1213 |
checkStrEq(t, s1, expected1) |
| 1214 |
|
| 1215 |
doc.SetRoot(origroot) |
| 1216 |
doc.Indent(2) |
| 1217 |
expected2 := s |
| 1218 |
s2, _ := doc.WriteToString() |
| 1219 |
checkStrEq(t, s2, expected2) |
| 1220 |
|
| 1221 |
doc2 := NewDocument() |
| 1222 |
doc2.CreateProcInst("test", `a="wow"`) |
| 1223 |
doc2.SetRoot(NewElement("root")) |
| 1224 |
doc2.Indent(2) |
| 1225 |
expected3 := expected1 |
| 1226 |
s3, _ := doc2.WriteToString() |
| 1227 |
checkStrEq(t, s3, expected3) |
| 1228 |
|
| 1229 |
doc2.SetRoot(doc.Root()) |
| 1230 |
doc2.Indent(2) |
| 1231 |
expected4 := s |
| 1232 |
s4, _ := doc2.WriteToString() |
| 1233 |
checkStrEq(t, s4, expected4) |
| 1234 |
|
| 1235 |
expected5 := `<?test a="wow"?> |
| 1236 |
` |
| 1237 |
doc.Indent(2) |
| 1238 |
s5, _ := doc.WriteToString() |
| 1239 |
checkStrEq(t, s5, expected5) |
| 1240 |
} |
| 1241 |
|
| 1242 |
func TestSortAttrs(t *testing.T) { |
| 1243 |
s := `<el foo='5' Foo='2' aaa='4' สวัสดี='7' AAA='1' a01='3' z='6' a:ZZZ='9' a:AAA='8'/>` |
| 1244 |
doc := newDocumentFromString(t, s) |
| 1245 |
doc.Root().SortAttrs() |
| 1246 |
doc.Indent(2) |
| 1247 |
out, _ := doc.WriteToString() |
| 1248 |
checkStrEq(t, out, `<el AAA="1" Foo="2" a01="3" aaa="4" foo="5" z="6" สวัสดี="7" a:AAA="8" a:ZZZ="9"/>`+"\n") |
| 1249 |
} |
| 1250 |
|
| 1251 |
func TestCharsetReaderDefaultSetting(t *testing.T) { |
| 1252 |
// Test encodings where the default pass-through charset conversion |
| 1253 |
// should work for common single-byte character encodings. |
| 1254 |
cases := []string{ |
| 1255 |
`<?xml version="1.0"?><foo></foo>`, |
| 1256 |
`<?xml version="1.0" encoding="ISO-8859-1"?><foo></foo>`, |
| 1257 |
`<?xml version="1.0" encoding="Windows-1252"?><foo></foo>`, |
| 1258 |
`<?xml version="1.0" encoding="UTF-8"?><foo></foo>`, |
| 1259 |
`<?xml version="1.0" encoding="US-ASCII"?><foo></foo>`, |
| 1260 |
} |
| 1261 |
|
| 1262 |
for _, c := range cases { |
| 1263 |
doc := NewDocument() |
| 1264 |
if err := doc.ReadFromBytes([]byte(c)); err != nil { |
| 1265 |
t.Error(err) |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
func TestCharData(t *testing.T) { |
| 1271 |
doc := NewDocument() |
| 1272 |
root := doc.CreateElement("root") |
| 1273 |
root.CreateCharData("This ") |
| 1274 |
root.CreateCData("is ") |
| 1275 |
e1 := NewText("a ") |
| 1276 |
e2 := NewCData("text ") |
| 1277 |
root.AddChild(e1) |
| 1278 |
root.AddChild(e2) |
| 1279 |
root.CreateCharData("Element!!") |
| 1280 |
|
| 1281 |
s, err := doc.WriteToString() |
| 1282 |
if err != nil { |
| 1283 |
t.Error("etree: failed to serialize document") |
| 1284 |
} |
| 1285 |
|
| 1286 |
checkStrEq(t, s, `<root>This <![CDATA[is ]]>a <![CDATA[text ]]>Element!!</root>`) |
| 1287 |
|
| 1288 |
// Check we can parse the output |
| 1289 |
err = doc.ReadFromString(s) |
| 1290 |
if err != nil { |
| 1291 |
t.Fatal("etree: incorrect ReadFromString result") |
| 1292 |
} |
| 1293 |
if doc.Root().Text() != "This is a text Element!!" { |
| 1294 |
t.Error("etree: invalid text") |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
func TestIndentSimple(t *testing.T) { |
| 1299 |
doc := NewDocument() |
| 1300 |
root := doc.CreateElement("root") |
| 1301 |
ch1 := root.CreateElement("child1") |
| 1302 |
ch1.CreateElement("child2") |
| 1303 |
|
| 1304 |
// First test Unindent. |
| 1305 |
doc.Unindent() |
| 1306 |
s, err := doc.WriteToString() |
| 1307 |
if err != nil { |
| 1308 |
t.Error("etree: failed to serialize document") |
| 1309 |
} |
| 1310 |
expected := "<root><child1><child2/></child1></root>" |
| 1311 |
checkStrEq(t, s, expected) |
| 1312 |
|
| 1313 |
// Now test Indent with NoIndent (which should produce the same result |
| 1314 |
// as Unindent). |
| 1315 |
doc.Indent(NoIndent) |
| 1316 |
s, err = doc.WriteToString() |
| 1317 |
if err != nil { |
| 1318 |
t.Error("etree: failed to serialize document") |
| 1319 |
} |
| 1320 |
checkStrEq(t, s, expected) |
| 1321 |
|
| 1322 |
// Run all indent test cases. |
| 1323 |
tests := []struct { |
| 1324 |
useTabs, useCRLF bool |
| 1325 |
ws, nl string |
| 1326 |
}{ |
| 1327 |
{false, false, " ", "\n"}, |
| 1328 |
{false, true, " ", "\r\n"}, |
| 1329 |
{true, false, "\t", "\n"}, |
| 1330 |
{true, true, "\t", "\r\n"}, |
| 1331 |
} |
| 1332 |
|
| 1333 |
for _, test := range tests { |
| 1334 |
doc.WriteSettings.UseCRLF = test.useCRLF |
| 1335 |
if test.useTabs { |
| 1336 |
doc.IndentTabs() |
| 1337 |
s, err := doc.WriteToString() |
| 1338 |
if err != nil { |
| 1339 |
t.Error("etree: failed to serialize document") |
| 1340 |
} |
| 1341 |
tab := test.ws |
| 1342 |
expected := "<root>" + test.nl + tab + "<child1>" + test.nl + |
| 1343 |
tab + tab + "<child2/>" + test.nl + tab + |
| 1344 |
"</child1>" + test.nl + "</root>" + test.nl |
| 1345 |
checkStrEq(t, s, expected) |
| 1346 |
} else { |
| 1347 |
for i := 0; i < 256; i++ { |
| 1348 |
doc.Indent(i) |
| 1349 |
s, err := doc.WriteToString() |
| 1350 |
if err != nil { |
| 1351 |
t.Error("etree: failed to serialize document") |
| 1352 |
} |
| 1353 |
tab := strings.Repeat(test.ws, i) |
| 1354 |
expected := "<root>" + test.nl + tab + "<child1>" + test.nl + |
| 1355 |
tab + tab + "<child2/>" + test.nl + tab + |
| 1356 |
"</child1>" + test.nl + "</root>" + test.nl |
| 1357 |
checkStrEq(t, s, expected) |
| 1358 |
} |
| 1359 |
} |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
func TestIndentWithDefaultSettings(t *testing.T) { |
| 1364 |
input := `<root> |
| 1365 |
<child1> |
| 1366 |
<child2> </child2> |
| 1367 |
</child1> |
| 1368 |
</root>` |
| 1369 |
|
| 1370 |
doc := NewDocument() |
| 1371 |
err := doc.ReadFromString(input) |
| 1372 |
if err != nil { |
| 1373 |
t.Error("etree: failed to read string") |
| 1374 |
} |
| 1375 |
|
| 1376 |
settings := NewIndentSettings() |
| 1377 |
doc.IndentWithSettings(settings) |
| 1378 |
s, err := doc.WriteToString() |
| 1379 |
if err != nil { |
| 1380 |
t.Error("etree: failed to serialize document") |
| 1381 |
} |
| 1382 |
expected := "<root>\n <child1>\n <child2/>\n </child1>\n</root>\n" |
| 1383 |
checkStrEq(t, s, expected) |
| 1384 |
} |
| 1385 |
|
| 1386 |
func TestIndentWithSettings(t *testing.T) { |
| 1387 |
doc := NewDocument() |
| 1388 |
root := doc.CreateElement("root") |
| 1389 |
ch1 := root.CreateElement("child1") |
| 1390 |
ch1.CreateElement("child2") |
| 1391 |
|
| 1392 |
// First test with NoIndent. |
| 1393 |
settings := NewIndentSettings() |
| 1394 |
settings.UseCRLF = false |
| 1395 |
settings.UseTabs = false |
| 1396 |
settings.Spaces = NoIndent |
| 1397 |
doc.IndentWithSettings(settings) |
| 1398 |
s, err := doc.WriteToString() |
| 1399 |
if err != nil { |
| 1400 |
t.Error("etree: failed to serialize document") |
| 1401 |
} |
| 1402 |
expected := "<root><child1><child2/></child1></root>" |
| 1403 |
checkStrEq(t, s, expected) |
| 1404 |
|
| 1405 |
// Run all indent test cases. |
| 1406 |
tests := []struct { |
| 1407 |
useTabs, useCRLF bool |
| 1408 |
ws, nl string |
| 1409 |
}{ |
| 1410 |
{false, false, " ", "\n"}, |
| 1411 |
{false, true, " ", "\r\n"}, |
| 1412 |
{true, false, "\t", "\n"}, |
| 1413 |
{true, true, "\t", "\r\n"}, |
| 1414 |
} |
| 1415 |
|
| 1416 |
for _, test := range tests { |
| 1417 |
if test.useTabs { |
| 1418 |
settings := NewIndentSettings() |
| 1419 |
settings.UseTabs = true |
| 1420 |
settings.UseCRLF = test.useCRLF |
| 1421 |
doc.IndentWithSettings(settings) |
| 1422 |
s, err := doc.WriteToString() |
| 1423 |
if err != nil { |
| 1424 |
t.Error("etree: failed to serialize document") |
| 1425 |
} |
| 1426 |
tab := test.ws |
| 1427 |
expected := "<root>" + test.nl + tab + "<child1>" + test.nl + |
| 1428 |
tab + tab + "<child2/>" + test.nl + tab + |
| 1429 |
"</child1>" + test.nl + "</root>" + test.nl |
| 1430 |
checkStrEq(t, s, expected) |
| 1431 |
} else { |
| 1432 |
for i := 0; i < 256; i++ { |
| 1433 |
settings := NewIndentSettings() |
| 1434 |
settings.Spaces = i |
| 1435 |
settings.UseTabs = false |
| 1436 |
settings.UseCRLF = test.useCRLF |
| 1437 |
doc.IndentWithSettings(settings) |
| 1438 |
s, err := doc.WriteToString() |
| 1439 |
if err != nil { |
| 1440 |
t.Error("etree: failed to serialize document") |
| 1441 |
} |
| 1442 |
tab := strings.Repeat(test.ws, i) |
| 1443 |
expected := "<root>" + test.nl + tab + "<child1>" + test.nl + |
| 1444 |
tab + tab + "<child2/>" + test.nl + tab + |
| 1445 |
"</child1>" + test.nl + "</root>" + test.nl |
| 1446 |
checkStrEq(t, s, expected) |
| 1447 |
} |
| 1448 |
} |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
func TestIndentPreserveWhitespace(t *testing.T) { |
| 1453 |
tests := []struct { |
| 1454 |
input string |
| 1455 |
expected string |
| 1456 |
}{ |
| 1457 |
{"<test></test>", "<test/>"}, |
| 1458 |
{"<test> </test>", "<test> </test>"}, |
| 1459 |
{"<test>\t</test>", "<test>\t</test>"}, |
| 1460 |
{"<test>\t\n \t</test>", "<test>\t\n \t</test>"}, |
| 1461 |
{"<test><![CDATA[ ]]></test>", "<test> </test>"}, |
| 1462 |
{"<test> <![CDATA[ ]]> </test>", "<test/>"}, |
| 1463 |
{"<outer> <inner> </inner> </outer>", "<outer>\n <inner> </inner>\n</outer>"}, |
| 1464 |
} |
| 1465 |
|
| 1466 |
for _, test := range tests { |
| 1467 |
doc := NewDocument() |
| 1468 |
err := doc.ReadFromString(test.input) |
| 1469 |
if err != nil { |
| 1470 |
t.Error("etree: failed to read string") |
| 1471 |
} |
| 1472 |
|
| 1473 |
s := NewIndentSettings() |
| 1474 |
s.Spaces = 2 |
| 1475 |
s.PreserveLeafWhitespace = true |
| 1476 |
s.SuppressTrailingWhitespace = true |
| 1477 |
doc.IndentWithSettings(s) |
| 1478 |
|
| 1479 |
output, err := doc.WriteToString() |
| 1480 |
if err != nil { |
| 1481 |
t.Error("etree: failed to read string") |
| 1482 |
} |
| 1483 |
checkStrEq(t, output, test.expected) |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
func TestPreserveCData(t *testing.T) { |
| 1488 |
tests := []struct { |
| 1489 |
input string |
| 1490 |
expectedWithPreserve string |
| 1491 |
expectedWithoutPreserve string |
| 1492 |
}{ |
| 1493 |
{ |
| 1494 |
"<test><![CDATA[x]]></test>", |
| 1495 |
"<test><![CDATA[x]]></test>", |
| 1496 |
"<test>x</test>", |
| 1497 |
}, |
| 1498 |
{ |
| 1499 |
"<tag><![CDATA[x <b>foo</b>]]></tag>", |
| 1500 |
"<tag><![CDATA[x <b>foo</b>]]></tag>", |
| 1501 |
"<tag>x <b>foo</b></tag>", |
| 1502 |
}, |
| 1503 |
{ |
| 1504 |
"<name><![CDATA[My]]> <b>name</b> <![CDATA[is]]></name>", |
| 1505 |
"<name><![CDATA[My]]> <b>name</b> <![CDATA[is]]></name>", |
| 1506 |
"<name>My <b>name</b> is</name>", |
| 1507 |
}, |
| 1508 |
} |
| 1509 |
|
| 1510 |
for _, test := range tests { |
| 1511 |
doc := newDocumentFromString2(t, test.input, ReadSettings{PreserveCData: true}) |
| 1512 |
output, _ := doc.WriteToString() |
| 1513 |
checkStrEq(t, output, test.expectedWithPreserve) |
| 1514 |
} |
| 1515 |
|
| 1516 |
for _, test := range tests { |
| 1517 |
doc := newDocumentFromString2(t, test.input, ReadSettings{PreserveCData: false}) |
| 1518 |
output, _ := doc.WriteToString() |
| 1519 |
checkStrEq(t, output, test.expectedWithoutPreserve) |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
func TestTokenIndexing(t *testing.T) { |
| 1524 |
s := `<?xml version="1.0" encoding="UTF-8"?> |
| 1525 |
<?xml-stylesheet type="text/xsl" href="style.xsl"?> |
| 1526 |
<store xmlns:t="urn:books-com:titles"> |
| 1527 |
<!Directive> |
| 1528 |
<!--This is a comment--> |
| 1529 |
<book lang="en"> |
| 1530 |
<t:title>Great Expectations</t:title> |
| 1531 |
<author>Charles Dickens</author> |
| 1532 |
<review/> |
| 1533 |
</book> |
| 1534 |
</store>` |
| 1535 |
|
| 1536 |
doc := newDocumentFromString(t, s) |
| 1537 |
review := doc.FindElement("/store/book/review") |
| 1538 |
review.SetText("Excellent") |
| 1539 |
|
| 1540 |
checkIndexes(t, &doc.Element) |
| 1541 |
|
| 1542 |
doc.Indent(4) |
| 1543 |
checkIndexes(t, &doc.Element) |
| 1544 |
|
| 1545 |
doc.Indent(NoIndent) |
| 1546 |
checkIndexes(t, &doc.Element) |
| 1547 |
|
| 1548 |
e := NewElement("foo") |
| 1549 |
store := doc.SelectElement("store") |
| 1550 |
store.InsertChildAt(0, e) |
| 1551 |
checkIndexes(t, &doc.Element) |
| 1552 |
|
| 1553 |
store.RemoveChildAt(0) |
| 1554 |
checkIndexes(t, &doc.Element) |
| 1555 |
} |
| 1556 |
|
| 1557 |
func TestSetText(t *testing.T) { |
| 1558 |
doc := NewDocument() |
| 1559 |
root := doc.CreateElement("root") |
| 1560 |
|
| 1561 |
checkDocEq(t, doc, `<root/>`) |
| 1562 |
checkStrEq(t, root.Text(), "") |
| 1563 |
checkIntEq(t, len(root.Child), 0) |
| 1564 |
|
| 1565 |
root.SetText("foo") |
| 1566 |
checkDocEq(t, doc, `<root>foo</root>`) |
| 1567 |
checkStrEq(t, root.Text(), "foo") |
| 1568 |
checkIntEq(t, len(root.Child), 1) |
| 1569 |
|
| 1570 |
root.SetText("bar") |
| 1571 |
checkDocEq(t, doc, `<root>bar</root>`) |
| 1572 |
checkStrEq(t, root.Text(), "bar") |
| 1573 |
checkIntEq(t, len(root.Child), 1) |
| 1574 |
|
| 1575 |
root.CreateCData("cdata") |
| 1576 |
checkDocEq(t, doc, `<root>bar<![CDATA[cdata]]></root>`) |
| 1577 |
checkStrEq(t, root.Text(), "barcdata") |
| 1578 |
checkIntEq(t, len(root.Child), 2) |
| 1579 |
|
| 1580 |
root.SetText("qux") |
| 1581 |
checkDocEq(t, doc, `<root>qux</root>`) |
| 1582 |
checkStrEq(t, root.Text(), "qux") |
| 1583 |
checkIntEq(t, len(root.Child), 1) |
| 1584 |
|
| 1585 |
root.CreateCData("cdata") |
| 1586 |
checkDocEq(t, doc, `<root>qux<![CDATA[cdata]]></root>`) |
| 1587 |
checkStrEq(t, root.Text(), "quxcdata") |
| 1588 |
checkIntEq(t, len(root.Child), 2) |
| 1589 |
|
| 1590 |
root.SetCData("baz") |
| 1591 |
checkDocEq(t, doc, `<root><![CDATA[baz]]></root>`) |
| 1592 |
checkStrEq(t, root.Text(), "baz") |
| 1593 |
checkIntEq(t, len(root.Child), 1) |
| 1594 |
|
| 1595 |
root.CreateText("corge") |
| 1596 |
root.CreateCData("grault") |
| 1597 |
root.CreateText("waldo") |
| 1598 |
root.CreateCData("fred") |
| 1599 |
root.CreateElement("child") |
| 1600 |
checkDocEq(t, doc, `<root><![CDATA[baz]]>corge<![CDATA[grault]]>waldo<![CDATA[fred]]><child/></root>`) |
| 1601 |
checkStrEq(t, root.Text(), "bazcorgegraultwaldofred") |
| 1602 |
checkIntEq(t, len(root.Child), 6) |
| 1603 |
|
| 1604 |
root.SetText("plugh") |
| 1605 |
checkDocEq(t, doc, `<root>plugh<child/></root>`) |
| 1606 |
checkStrEq(t, root.Text(), "plugh") |
| 1607 |
checkIntEq(t, len(root.Child), 2) |
| 1608 |
|
| 1609 |
root.SetText("") |
| 1610 |
checkDocEq(t, doc, `<root><child/></root>`) |
| 1611 |
checkStrEq(t, root.Text(), "") |
| 1612 |
checkIntEq(t, len(root.Child), 1) |
| 1613 |
|
| 1614 |
root.SetText("") |
| 1615 |
checkDocEq(t, doc, `<root><child/></root>`) |
| 1616 |
checkStrEq(t, root.Text(), "") |
| 1617 |
checkIntEq(t, len(root.Child), 1) |
| 1618 |
|
| 1619 |
root.RemoveChildAt(0) |
| 1620 |
root.CreateText("corge") |
| 1621 |
root.CreateCData("grault") |
| 1622 |
root.CreateText("waldo") |
| 1623 |
root.CreateCData("fred") |
| 1624 |
root.CreateElement("child") |
| 1625 |
checkDocEq(t, doc, `<root>corge<![CDATA[grault]]>waldo<![CDATA[fred]]><child/></root>`) |
| 1626 |
checkStrEq(t, root.Text(), "corgegraultwaldofred") |
| 1627 |
checkIntEq(t, len(root.Child), 5) |
| 1628 |
|
| 1629 |
root.SetText("") |
| 1630 |
checkDocEq(t, doc, `<root><child/></root>`) |
| 1631 |
checkStrEq(t, root.Text(), "") |
| 1632 |
checkIntEq(t, len(root.Child), 1) |
| 1633 |
} |
| 1634 |
|
| 1635 |
func TestSetTail(t *testing.T) { |
| 1636 |
doc := NewDocument() |
| 1637 |
root := doc.CreateElement("root") |
| 1638 |
child := root.CreateElement("child") |
| 1639 |
root.CreateText("\n\t") |
| 1640 |
child.SetText("foo") |
| 1641 |
checkDocEq(t, doc, "<root><child>foo</child>\n\t</root>") |
| 1642 |
checkStrEq(t, child.Tail(), "\n\t") |
| 1643 |
checkIntEq(t, len(root.Child), 2) |
| 1644 |
checkIntEq(t, len(child.Child), 1) |
| 1645 |
|
| 1646 |
root.CreateCData(" ") |
| 1647 |
checkDocEq(t, doc, "<root><child>foo</child>\n\t<![CDATA[ ]]></root>") |
| 1648 |
checkStrEq(t, child.Tail(), "\n\t ") |
| 1649 |
checkIntEq(t, len(root.Child), 3) |
| 1650 |
checkIntEq(t, len(child.Child), 1) |
| 1651 |
|
| 1652 |
child.SetTail("") |
| 1653 |
checkDocEq(t, doc, "<root><child>foo</child></root>") |
| 1654 |
checkStrEq(t, child.Tail(), "") |
| 1655 |
checkIntEq(t, len(root.Child), 1) |
| 1656 |
checkIntEq(t, len(child.Child), 1) |
| 1657 |
|
| 1658 |
child.SetTail("\t\t\t") |
| 1659 |
checkDocEq(t, doc, "<root><child>foo</child>\t\t\t</root>") |
| 1660 |
checkStrEq(t, child.Tail(), "\t\t\t") |
| 1661 |
checkIntEq(t, len(root.Child), 2) |
| 1662 |
checkIntEq(t, len(child.Child), 1) |
| 1663 |
|
| 1664 |
child.SetTail("\t\n\n\t") |
| 1665 |
checkDocEq(t, doc, "<root><child>foo</child>\t\n\n\t</root>") |
| 1666 |
checkStrEq(t, child.Tail(), "\t\n\n\t") |
| 1667 |
checkIntEq(t, len(root.Child), 2) |
| 1668 |
checkIntEq(t, len(child.Child), 1) |
| 1669 |
|
| 1670 |
child.SetTail("") |
| 1671 |
checkDocEq(t, doc, "<root><child>foo</child></root>") |
| 1672 |
checkStrEq(t, child.Tail(), "") |
| 1673 |
checkIntEq(t, len(root.Child), 1) |
| 1674 |
checkIntEq(t, len(child.Child), 1) |
| 1675 |
} |
| 1676 |
|
| 1677 |
func TestAttrParent(t *testing.T) { |
| 1678 |
doc := NewDocument() |
| 1679 |
root := doc.CreateElement("root") |
| 1680 |
attr1 := root.CreateAttr("bar", "1") |
| 1681 |
attr2 := root.CreateAttr("qux", "2") |
| 1682 |
|
| 1683 |
checkIntEq(t, len(root.Attr), 2) |
| 1684 |
checkElementEq(t, attr1.Element(), root) |
| 1685 |
checkElementEq(t, attr2.Element(), root) |
| 1686 |
|
| 1687 |
attr1 = root.RemoveAttr("bar") |
| 1688 |
attr2 = root.RemoveAttr("qux") |
| 1689 |
checkElementEq(t, attr1.Element(), nil) |
| 1690 |
checkElementEq(t, attr2.Element(), nil) |
| 1691 |
|
| 1692 |
s := `<root a="1" b="2" c="3" d="4"/>` |
| 1693 |
err := doc.ReadFromString(s) |
| 1694 |
if err != nil { |
| 1695 |
t.Error("etree: failed to parse document") |
| 1696 |
} |
| 1697 |
|
| 1698 |
root = doc.SelectElement("root") |
| 1699 |
for i := range root.Attr { |
| 1700 |
checkElementEq(t, root.Attr[i].Element(), root) |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
func TestDefaultNamespaceURI(t *testing.T) { |
| 1705 |
s := ` |
| 1706 |
<root xmlns="https://root.example.com" xmlns:attrib="https://attrib.example.com" attrib:a="foo" b="bar"> |
| 1707 |
<child1 xmlns="https://child.example.com" attrib:a="foo"> |
| 1708 |
<grandchild1 xmlns="https://grandchild.example.com" a="foo"> |
| 1709 |
</grandchild1> |
| 1710 |
<grandchild2 a="foo"> |
| 1711 |
<greatgrandchild1 attrib:a="foo"/> |
| 1712 |
</grandchild2> |
| 1713 |
</child1> |
| 1714 |
<child2 a="foo"/> |
| 1715 |
</root>` |
| 1716 |
|
| 1717 |
doc := newDocumentFromString(t, s) |
| 1718 |
root := doc.SelectElement("root") |
| 1719 |
child1 := root.SelectElement("child1") |
| 1720 |
child2 := root.SelectElement("child2") |
| 1721 |
grandchild1 := child1.SelectElement("grandchild1") |
| 1722 |
grandchild2 := child1.SelectElement("grandchild2") |
| 1723 |
greatgrandchild1 := grandchild2.SelectElement("greatgrandchild1") |
| 1724 |
|
| 1725 |
checkStrEq(t, doc.NamespaceURI(), "") |
| 1726 |
checkStrEq(t, root.NamespaceURI(), "https://root.example.com") |
| 1727 |
checkStrEq(t, child1.NamespaceURI(), "https://child.example.com") |
| 1728 |
checkStrEq(t, child2.NamespaceURI(), "https://root.example.com") |
| 1729 |
checkStrEq(t, grandchild1.NamespaceURI(), "https://grandchild.example.com") |
| 1730 |
checkStrEq(t, grandchild2.NamespaceURI(), "https://child.example.com") |
| 1731 |
checkStrEq(t, greatgrandchild1.NamespaceURI(), "https://child.example.com") |
| 1732 |
|
| 1733 |
checkStrEq(t, root.Attr[0].NamespaceURI(), "") |
| 1734 |
checkStrEq(t, root.Attr[1].NamespaceURI(), "") |
| 1735 |
checkStrEq(t, root.Attr[2].NamespaceURI(), "https://attrib.example.com") |
| 1736 |
checkStrEq(t, root.Attr[3].NamespaceURI(), "") |
| 1737 |
checkStrEq(t, child1.Attr[0].NamespaceURI(), "") |
| 1738 |
checkStrEq(t, child1.Attr[1].NamespaceURI(), "https://attrib.example.com") |
| 1739 |
checkStrEq(t, child2.Attr[0].NamespaceURI(), "") |
| 1740 |
checkStrEq(t, grandchild1.Attr[0].NamespaceURI(), "") |
| 1741 |
checkStrEq(t, grandchild1.Attr[1].NamespaceURI(), "") |
| 1742 |
checkStrEq(t, grandchild2.Attr[0].NamespaceURI(), "") |
| 1743 |
checkStrEq(t, greatgrandchild1.Attr[0].NamespaceURI(), "https://attrib.example.com") |
| 1744 |
|
| 1745 |
f := doc.FindElements("//*[namespace-uri()='https://root.example.com']") |
| 1746 |
if len(f) != 2 || f[0] != root || f[1] != child2 { |
| 1747 |
t.Error("etree: failed namespace-uri test") |
| 1748 |
} |
| 1749 |
|
| 1750 |
f = doc.FindElements("//*[namespace-uri()='https://child.example.com']") |
| 1751 |
if len(f) != 3 || f[0] != child1 || f[1] != grandchild2 || f[2] != greatgrandchild1 { |
| 1752 |
t.Error("etree: failed namespace-uri test") |
| 1753 |
} |
| 1754 |
|
| 1755 |
f = doc.FindElements("//*[namespace-uri()='https://grandchild.example.com']") |
| 1756 |
if len(f) != 1 || f[0] != grandchild1 { |
| 1757 |
t.Error("etree: failed namespace-uri test") |
| 1758 |
} |
| 1759 |
|
| 1760 |
f = doc.FindElements("//*[namespace-uri()='']") |
| 1761 |
if len(f) != 0 { |
| 1762 |
t.Error("etree: failed namespace-uri test") |
| 1763 |
} |
| 1764 |
|
| 1765 |
f = doc.FindElements("//*[namespace-uri()='foo']") |
| 1766 |
if len(f) != 0 { |
| 1767 |
t.Error("etree: failed namespace-uri test") |
| 1768 |
} |
| 1769 |
} |
| 1770 |
|
| 1771 |
func TestLocalNamespaceURI(t *testing.T) { |
| 1772 |
s := ` |
| 1773 |
<a:root xmlns:a="https://root.example.com"> |
| 1774 |
<b:child1 xmlns:b="https://child.example.com"> |
| 1775 |
<c:grandchild1 xmlns:c="https://grandchild.example.com"/> |
| 1776 |
<b:grandchild2> |
| 1777 |
<a:greatgrandchild1/> |
| 1778 |
</b:grandchild2> |
| 1779 |
<a:grandchild3/> |
| 1780 |
<grandchild4/> |
| 1781 |
</b:child1> |
| 1782 |
<a:child2> |
| 1783 |
</a:child2> |
| 1784 |
<child3> |
| 1785 |
</child3> |
| 1786 |
</a:root>` |
| 1787 |
|
| 1788 |
doc := newDocumentFromString(t, s) |
| 1789 |
root := doc.SelectElement("root") |
| 1790 |
child1 := root.SelectElement("child1") |
| 1791 |
child2 := root.SelectElement("child2") |
| 1792 |
child3 := root.SelectElement("child3") |
| 1793 |
grandchild1 := child1.SelectElement("grandchild1") |
| 1794 |
grandchild2 := child1.SelectElement("grandchild2") |
| 1795 |
grandchild3 := child1.SelectElement("grandchild3") |
| 1796 |
grandchild4 := child1.SelectElement("grandchild4") |
| 1797 |
greatgrandchild1 := grandchild2.SelectElement("greatgrandchild1") |
| 1798 |
|
| 1799 |
checkStrEq(t, doc.NamespaceURI(), "") |
| 1800 |
checkStrEq(t, root.NamespaceURI(), "https://root.example.com") |
| 1801 |
checkStrEq(t, child1.NamespaceURI(), "https://child.example.com") |
| 1802 |
checkStrEq(t, child2.NamespaceURI(), "https://root.example.com") |
| 1803 |
checkStrEq(t, child3.NamespaceURI(), "") |
| 1804 |
checkStrEq(t, grandchild1.NamespaceURI(), "https://grandchild.example.com") |
| 1805 |
checkStrEq(t, grandchild2.NamespaceURI(), "https://child.example.com") |
| 1806 |
checkStrEq(t, grandchild3.NamespaceURI(), "https://root.example.com") |
| 1807 |
checkStrEq(t, grandchild4.NamespaceURI(), "") |
| 1808 |
checkStrEq(t, greatgrandchild1.NamespaceURI(), "https://root.example.com") |
| 1809 |
|
| 1810 |
f := doc.FindElements("//*[namespace-uri()='https://root.example.com']") |
| 1811 |
if len(f) != 4 || f[0] != root || f[1] != child2 || f[2] != grandchild3 || f[3] != greatgrandchild1 { |
| 1812 |
t.Error("etree: failed namespace-uri test") |
| 1813 |
} |
| 1814 |
|
| 1815 |
f = doc.FindElements("//*[namespace-uri()='https://child.example.com']") |
| 1816 |
if len(f) != 2 || f[0] != child1 || f[1] != grandchild2 { |
| 1817 |
t.Error("etree: failed namespace-uri test") |
| 1818 |
} |
| 1819 |
|
| 1820 |
f = doc.FindElements("//*[namespace-uri()='https://grandchild.example.com']") |
| 1821 |
if len(f) != 1 || f[0] != grandchild1 { |
| 1822 |
t.Error("etree: failed namespace-uri test") |
| 1823 |
} |
| 1824 |
|
| 1825 |
f = doc.FindElements("//*[namespace-uri()='']") |
| 1826 |
if len(f) != 2 || f[0] != child3 || f[1] != grandchild4 { |
| 1827 |
t.Error("etree: failed namespace-uri test") |
| 1828 |
} |
| 1829 |
|
| 1830 |
f = doc.FindElements("//*[namespace-uri()='foo']") |
| 1831 |
if len(f) != 0 { |
| 1832 |
t.Error("etree: failed namespace-uri test") |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
func TestWhitespace(t *testing.T) { |
| 1837 |
s := "<root>\n\t<child>\n\t\t<grandchild> x</grandchild>\n </child>\n</root>" |
| 1838 |
|
| 1839 |
doc := newDocumentFromString(t, s) |
| 1840 |
root := doc.Root() |
| 1841 |
checkIntEq(t, len(root.Child), 3) |
| 1842 |
|
| 1843 |
cd := root.Child[0].(*CharData) |
| 1844 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1845 |
checkStrBinaryEq(t, cd.Data, "\n\t") |
| 1846 |
|
| 1847 |
cd = root.Child[2].(*CharData) |
| 1848 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1849 |
checkStrBinaryEq(t, cd.Data, "\n") |
| 1850 |
|
| 1851 |
child := root.SelectElement("child") |
| 1852 |
checkIntEq(t, len(child.Child), 3) |
| 1853 |
|
| 1854 |
cd = child.Child[0].(*CharData) |
| 1855 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1856 |
checkStrBinaryEq(t, cd.Data, "\n\t\t") |
| 1857 |
|
| 1858 |
cd = child.Child[2].(*CharData) |
| 1859 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1860 |
checkStrBinaryEq(t, cd.Data, "\n ") |
| 1861 |
|
| 1862 |
grandchild := child.SelectElement("grandchild") |
| 1863 |
checkIntEq(t, len(grandchild.Child), 1) |
| 1864 |
|
| 1865 |
cd = grandchild.Child[0].(*CharData) |
| 1866 |
checkBoolEq(t, cd.IsWhitespace(), false) |
| 1867 |
|
| 1868 |
cd.SetData(" ") |
| 1869 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1870 |
|
| 1871 |
cd.SetData(" x") |
| 1872 |
checkBoolEq(t, cd.IsWhitespace(), false) |
| 1873 |
|
| 1874 |
cd.SetData("\t\n\r ") |
| 1875 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1876 |
|
| 1877 |
cd.SetData("\uFFFD") |
| 1878 |
checkBoolEq(t, cd.IsWhitespace(), false) |
| 1879 |
|
| 1880 |
cd.SetData("") |
| 1881 |
checkBoolEq(t, cd.IsWhitespace(), true) |
| 1882 |
} |
| 1883 |
|
| 1884 |
func TestTokenWriteTo(t *testing.T) { |
| 1885 |
s := `<store> |
| 1886 |
<!-- comment --> |
| 1887 |
<book> |
| 1888 |
<title>Great Expectations</title> |
| 1889 |
</book> |
| 1890 |
</store>` |
| 1891 |
doc := newDocumentFromString(t, s) |
| 1892 |
|
| 1893 |
writeSettings := WriteSettings{} |
| 1894 |
indentSettings := IndentSettings{UseTabs: true} |
| 1895 |
|
| 1896 |
tests := []struct { |
| 1897 |
path string |
| 1898 |
expected string |
| 1899 |
}{ |
| 1900 |
{"//store", "<store>\n\t<!-- comment -->\n\t<book>\n\t\t<title>Great Expectations</title>\n\t</book>\n</store>"}, |
| 1901 |
{"//store/book", "<book>\n\t<title>Great Expectations</title>\n</book>"}, |
| 1902 |
{"//store/book/title", "<title>Great Expectations</title>"}, |
| 1903 |
} |
| 1904 |
for _, test := range tests { |
| 1905 |
var buffer bytes.Buffer |
| 1906 |
|
| 1907 |
c := doc.FindElement(test.path) |
| 1908 |
c.IndentWithSettings(&indentSettings) |
| 1909 |
c.WriteTo(&buffer, &writeSettings) |
| 1910 |
checkStrEq(t, buffer.String(), test.expected) |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
func TestReindexChildren(t *testing.T) { |
| 1915 |
s := `<root> |
| 1916 |
<child1/> |
| 1917 |
<child2/> |
| 1918 |
<child3/> |
| 1919 |
<child4/> |
| 1920 |
<child5/> |
| 1921 |
</root>` |
| 1922 |
doc := newDocumentFromString(t, s) |
| 1923 |
doc.Unindent() |
| 1924 |
|
| 1925 |
root := doc.Root() |
| 1926 |
if root == nil || root.Tag != "root" || len(root.Child) != 5 { |
| 1927 |
t.Error("etree: expected root element not found") |
| 1928 |
} |
| 1929 |
|
| 1930 |
for i := 0; i < len(root.Child); i++ { |
| 1931 |
if root.Child[i].Index() != i { |
| 1932 |
t.Error("etree: incorrect child index found in root element child") |
| 1933 |
} |
| 1934 |
} |
| 1935 |
|
| 1936 |
rand.Shuffle(len(root.Child), func(i, j int) { |
| 1937 |
root.Child[i], root.Child[j] = root.Child[j], root.Child[i] |
| 1938 |
}) |
| 1939 |
|
| 1940 |
root.ReindexChildren() |
| 1941 |
|
| 1942 |
for i := 0; i < len(root.Child); i++ { |
| 1943 |
if root.Child[i].Index() != i { |
| 1944 |
t.Error("etree: incorrect child index found in root element child") |
| 1945 |
} |
| 1946 |
} |
| 1947 |
} |
| 1948 |
|
| 1949 |
func TestPreserveDuplicateAttrs(t *testing.T) { |
| 1950 |
s := `<element x="value1" y="value2" x="value3" x="value4" y="value5"/>` |
| 1951 |
|
| 1952 |
checkAttrCount := func(e *Element, n int) { |
| 1953 |
if len(e.Attr) != n { |
| 1954 |
t.Errorf("etree: expected %d attributes, got %d", n, len(e.Attr)) |
| 1955 |
} |
| 1956 |
} |
| 1957 |
checkAttr := func(e *Element, i int, key, value string) { |
| 1958 |
if i >= len(e.Attr) { |
| 1959 |
t.Errorf("etree: attr[%d] out of bounds", i) |
| 1960 |
return |
| 1961 |
} |
| 1962 |
if e.Attr[i].Key != key { |
| 1963 |
t.Errorf("etree: attr[%d] expected key %s, got %s", i, key, e.Attr[i].Key) |
| 1964 |
} |
| 1965 |
if e.Attr[i].Value != value { |
| 1966 |
t.Errorf("etree: attr[%d] expected value %s, got %s", i, value, e.Attr[i].Value) |
| 1967 |
} |
| 1968 |
} |
| 1969 |
|
| 1970 |
t.Run("enabled", func(t *testing.T) { |
| 1971 |
doc := newDocumentFromString2(t, s, ReadSettings{PreserveDuplicateAttrs: true}) |
| 1972 |
e := doc.FindElement("element") |
| 1973 |
checkAttrCount(e, 5) |
| 1974 |
checkAttr(e, 0, "x", "value1") |
| 1975 |
checkAttr(e, 1, "y", "value2") |
| 1976 |
checkAttr(e, 2, "x", "value3") |
| 1977 |
checkAttr(e, 3, "x", "value4") |
| 1978 |
checkAttr(e, 4, "y", "value5") |
| 1979 |
}) |
| 1980 |
|
| 1981 |
t.Run("disabled", func(t *testing.T) { |
| 1982 |
doc := newDocumentFromString2(t, s, ReadSettings{}) |
| 1983 |
e := doc.FindElement("element") |
| 1984 |
checkAttrCount(e, 2) |
| 1985 |
checkAttr(e, 0, "x", "value4") |
| 1986 |
checkAttr(e, 1, "y", "value5") |
| 1987 |
}) |
| 1988 |
} |
| 1989 |
|
| 1990 |
func TestNotNil(t *testing.T) { |
| 1991 |
s := `<enabled>true</enabled>` |
| 1992 |
|
| 1993 |
doc := newDocumentFromString(t, s) |
| 1994 |
doc.SelectElement("enabled").NotNil().SetText("false") |
| 1995 |
doc.SelectElement("visible").NotNil().SetText("true") |
| 1996 |
|
| 1997 |
want := `<enabled>false</enabled>` |
| 1998 |
got, err := doc.WriteToString() |
| 1999 |
if err != nil { |
| 2000 |
t.Fatal("etree: failed to write document to string") |
| 2001 |
} |
| 2002 |
if got != want { |
| 2003 |
t.Error("etree: unexpected NotNil result") |
| 2004 |
t.Error("wanted:\n" + want) |
| 2005 |
t.Error("got:\n" + got) |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
func TestValidateInput(t *testing.T) { |
| 2010 |
tests := []struct { |
| 2011 |
s string |
| 2012 |
err string |
| 2013 |
}{ |
| 2014 |
{`<root>x</root>`, ""}, |
| 2015 |
{`<root/>`, ""}, |
| 2016 |
{`<root>x`, `XML syntax error on line 1: unexpected EOF`}, |
| 2017 |
{`</root><root>`, `XML syntax error on line 1: unexpected end element </root>`}, |
| 2018 |
{`<>`, `XML syntax error on line 1: expected element name after <`}, |
| 2019 |
{`<root>x</root>trailing`, "etree: invalid XML format"}, |
| 2020 |
{`<root>x</root><`, "etree: invalid XML format"}, |
| 2021 |
{`<root><child>x</child></root1>`, `XML syntax error on line 1: element <root> closed by </root1>`}, |
| 2022 |
} |
| 2023 |
|
| 2024 |
type readFunc func(doc *Document, s string) error |
| 2025 |
runTests := func(t *testing.T, read readFunc) { |
| 2026 |
for i, test := range tests { |
| 2027 |
doc := NewDocument() |
| 2028 |
doc.ReadSettings.ValidateInput = true |
| 2029 |
err := read(doc, test.s) |
| 2030 |
if err == nil { |
| 2031 |
if test.err != "" { |
| 2032 |
t.Errorf("etree: test #%d:\nExpected error:\n %s\nReceived error:\n nil", i, test.err) |
| 2033 |
} |
| 2034 |
root := doc.Root() |
| 2035 |
if root == nil || root.Tag != "root" { |
| 2036 |
t.Errorf("etree: test #%d: failed to read document after input validation", i) |
| 2037 |
} |
| 2038 |
} else { |
| 2039 |
te := err.Error() |
| 2040 |
if te != test.err { |
| 2041 |
t.Errorf("etree: test #%d:\nExpected error;\n %s\nReceived error:\n %s", i, test.err, te) |
| 2042 |
} |
| 2043 |
} |
| 2044 |
} |
| 2045 |
} |
| 2046 |
|
| 2047 |
readFromString := func(doc *Document, s string) error { |
| 2048 |
return doc.ReadFromString(s) |
| 2049 |
} |
| 2050 |
t.Run("ReadFromString", func(t *testing.T) { runTests(t, readFromString) }) |
| 2051 |
|
| 2052 |
readFromBytes := func(doc *Document, s string) error { |
| 2053 |
return doc.ReadFromBytes([]byte(s)) |
| 2054 |
} |
| 2055 |
t.Run("ReadFromBytes", func(t *testing.T) { runTests(t, readFromBytes) }) |
| 2056 |
|
| 2057 |
readFromFile := func(doc *Document, s string) error { |
| 2058 |
pathtmp := path.Join(t.TempDir(), "etree-test") |
| 2059 |
err := os.WriteFile(pathtmp, []byte(s), fs.ModePerm) |
| 2060 |
if err != nil { |
| 2061 |
return errors.New("unable to write tmp file for input validation") |
| 2062 |
} |
| 2063 |
return doc.ReadFromFile(pathtmp) |
| 2064 |
} |
| 2065 |
t.Run("ReadFromFile", func(t *testing.T) { runTests(t, readFromFile) }) |
| 2066 |
} |
| 2067 |
|
| 2068 |
func TestSiblingElement(t *testing.T) { |
| 2069 |
doc := newDocumentFromString(t, `<root><a/><b> <b1/> </b> <!--test--> <c/></root>`) |
| 2070 |
|
| 2071 |
root := doc.SelectElement("root") |
| 2072 |
a := root.SelectElement("a") |
| 2073 |
b := root.SelectElement("b") |
| 2074 |
c := root.SelectElement("c") |
| 2075 |
b1 := b.SelectElement("b1") |
| 2076 |
|
| 2077 |
tests := []struct { |
| 2078 |
e *Element |
| 2079 |
next *Element |
| 2080 |
prev *Element |
| 2081 |
}{ |
| 2082 |
{root, nil, nil}, |
| 2083 |
{a, b, nil}, |
| 2084 |
{b, c, a}, |
| 2085 |
{c, nil, b}, |
| 2086 |
{b1, nil, nil}, |
| 2087 |
} |
| 2088 |
|
| 2089 |
toString := func(e *Element) string { |
| 2090 |
if e == nil { |
| 2091 |
return "nil" |
| 2092 |
} |
| 2093 |
return e.Tag |
| 2094 |
} |
| 2095 |
|
| 2096 |
for i, test := range tests { |
| 2097 |
next := test.e.NextSibling() |
| 2098 |
if next != test.next { |
| 2099 |
t.Errorf("etree: test #%d unexpected NextSibling result.\n Expected: %s\n Received: %s\n", |
| 2100 |
i, toString(next), toString(test.next)) |
| 2101 |
} |
| 2102 |
|
| 2103 |
prev := test.e.PrevSibling() |
| 2104 |
if prev != test.prev { |
| 2105 |
t.Errorf("etree: test #%d unexpected PrevSibling result.\n Expected: %s\n Received: %s\n", |
| 2106 |
i, toString(prev), toString(test.prev)) |
| 2107 |
} |
| 2108 |
} |
| 2109 |
} |
| 2110 |
|
| 2111 |
func TestContinuations(t *testing.T) { |
| 2112 |
doc := NewDocument() |
| 2113 |
root := doc.CreateChild("root", func(e *Element) { |
| 2114 |
e.CreateChild("child1", func(e *Element) { |
| 2115 |
e.CreateComment("Grandchildren of child #1") |
| 2116 |
e.CreateChild("grandchild1", func(e *Element) { |
| 2117 |
e.CreateAttr("attr1", "1") |
| 2118 |
e.CreateAttr("attr2", "2") |
| 2119 |
}) |
| 2120 |
e.CreateChild("grandchild2", func(e *Element) { |
| 2121 |
e.CreateAttr("attr1", "3") |
| 2122 |
e.CreateAttr("attr2", "4") |
| 2123 |
}) |
| 2124 |
}) |
| 2125 |
e.CreateChild("child2", func(e *Element) { |
| 2126 |
e.CreateComment("Grandchildren of child #2") |
| 2127 |
e.CreateChild("grandchild1", func(e *Element) { |
| 2128 |
e.CreateAttr("attr1", "5") |
| 2129 |
e.CreateAttr("attr2", "6") |
| 2130 |
}) |
| 2131 |
e.CreateChild("grandchild2", func(e *Element) { |
| 2132 |
e.CreateAttr("attr1", "7") |
| 2133 |
e.CreateAttr("attr2", "8") |
| 2134 |
}) |
| 2135 |
}) |
| 2136 |
}) |
| 2137 |
checkStrEq(t, root.Tag, "root") |
| 2138 |
|
| 2139 |
// Serialize the document to a string |
| 2140 |
doc.IndentTabs() |
| 2141 |
s, err := doc.WriteToString() |
| 2142 |
if err != nil { |
| 2143 |
t.Error("etree: failed to serialize document") |
| 2144 |
} |
| 2145 |
|
| 2146 |
// Make sure the serialized XML matches expectation. |
| 2147 |
expected := `<root> |
| 2148 |
<child1> |
| 2149 |
<!--Grandchildren of child #1--> |
| 2150 |
<grandchild1 attr1="1" attr2="2"/> |
| 2151 |
<grandchild2 attr1="3" attr2="4"/> |
| 2152 |
</child1> |
| 2153 |
<child2> |
| 2154 |
<!--Grandchildren of child #2--> |
| 2155 |
<grandchild1 attr1="5" attr2="6"/> |
| 2156 |
<grandchild2 attr1="7" attr2="8"/> |
| 2157 |
</child2> |
| 2158 |
</root> |
| 2159 |
` |
| 2160 |
|
| 2161 |
checkStrEq(t, s, expected) |
| 2162 |
} |
| 2163 |
|
| 2164 |
func TestMaxDepth(t *testing.T) { |
| 2165 |
tests := []struct { |
| 2166 |
maxDepth int |
| 2167 |
depth int |
| 2168 |
err error |
| 2169 |
}{ |
| 2170 |
{0, defaultMaxDepth, nil}, |
| 2171 |
{0, defaultMaxDepth + 1, ErrMaxDepth}, |
| 2172 |
{-1, defaultMaxDepth, nil}, |
| 2173 |
{-1, defaultMaxDepth + 1, ErrMaxDepth}, |
| 2174 |
{1, 1, nil}, |
| 2175 |
{1, 2, ErrMaxDepth}, |
| 2176 |
{5, 4, nil}, |
| 2177 |
{5, 5, nil}, |
| 2178 |
{5, 6, ErrMaxDepth}, |
| 2179 |
} |
| 2180 |
|
| 2181 |
for _, test := range tests { |
| 2182 |
for _, validate := range []bool{false, true} { |
| 2183 |
s := nestedXML(test.depth) |
| 2184 |
settings := ReadSettings{MaxDepth: test.maxDepth, ValidateInput: validate} |
| 2185 |
|
| 2186 |
doc := NewDocument() |
| 2187 |
doc.ReadSettings = settings |
| 2188 |
err := doc.ReadFromString(s) |
| 2189 |
if !errors.Is(err, test.err) { |
| 2190 |
t.Errorf("etree: ReadFromString(MaxDepth=%d, depth=%d, ValidateInput=%v) returned error %v, expected %v", |
| 2191 |
test.maxDepth, test.depth, validate, err, test.err) |
| 2192 |
} |
| 2193 |
|
| 2194 |
doc = NewDocument() |
| 2195 |
doc.ReadSettings = settings |
| 2196 |
err = doc.ReadFromBytes([]byte(s)) |
| 2197 |
if !errors.Is(err, test.err) { |
| 2198 |
t.Errorf("etree: ReadFromBytes(MaxDepth=%d, depth=%d, ValidateInput=%v) returned error %v, expected %v", |
| 2199 |
test.maxDepth, test.depth, validate, err, test.err) |
| 2200 |
} |
| 2201 |
|
| 2202 |
doc = NewDocument() |
| 2203 |
doc.ReadSettings = settings |
| 2204 |
_, err = doc.ReadFrom(strings.NewReader(s)) |
| 2205 |
if !errors.Is(err, test.err) { |
| 2206 |
t.Errorf("etree: ReadFrom(MaxDepth=%d, depth=%d, ValidateInput=%v) returned error %v, expected %v", |
| 2207 |
test.maxDepth, test.depth, validate, err, test.err) |
| 2208 |
} |
| 2209 |
} |
| 2210 |
} |
| 2211 |
} |
| 2212 |
|
| 2213 |
func nestedXML(depth int) string { |
| 2214 |
var sb strings.Builder |
| 2215 |
for i := 0; i < depth; i++ { |
| 2216 |
sb.WriteString("<a>") |
| 2217 |
} |
| 2218 |
for i := 0; i < depth; i++ { |
| 2219 |
sb.WriteString("</a>") |
| 2220 |
} |
| 2221 |
return sb.String() |
| 2222 |
} |
| 2223 |
|
| 2224 |
func TestTokenRemove(t *testing.T) { |
| 2225 |
doc := NewDocument() |
| 2226 |
err := doc.ReadFromString(bookstoreXML) |
| 2227 |
if err != nil { |
| 2228 |
t.Error(err) |
| 2229 |
} |
| 2230 |
|
| 2231 |
bookstore := doc.SelectElement("bookstore") |
| 2232 |
if bookstore == nil { |
| 2233 |
t.Error("bookstore element not found") |
| 2234 |
return |
| 2235 |
} |
| 2236 |
|
| 2237 |
// Remove all child tokens from the bookstore element. |
| 2238 |
for len(bookstore.Child) > 0 { |
| 2239 |
bookstore.Child[0].Remove() |
| 2240 |
} |
| 2241 |
|
| 2242 |
result, err := doc.WriteToString() |
| 2243 |
if err != nil { |
| 2244 |
t.Error(err) |
| 2245 |
} |
| 2246 |
|
| 2247 |
expected := ` |
| 2248 |
<?xml version="1.0" encoding="UTF-8"?> |
| 2249 |
<bookstore xmlns:p="urn:books-com:prices"/> |
| 2250 |
` |
| 2251 |
checkStrEq(t, result, expected) |
| 2252 |
|
| 2253 |
// Remove all top-level child tokens from the document. |
| 2254 |
for len(doc.Child) > 0 { |
| 2255 |
doc.Child[0].Remove() |
| 2256 |
} |
| 2257 |
|
| 2258 |
result, err = doc.WriteToString() |
| 2259 |
if err != nil { |
| 2260 |
t.Error(err) |
| 2261 |
} |
| 2262 |
|
| 2263 |
checkStrEq(t, result, "") |
| 2264 |
} |