1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-23 22:11:10 +02:00
Files
imgproxy/xmlparser/document.go

51 lines
1.0 KiB
Go
Raw Permalink Normal View History

2025-10-24 23:34:05 +03:00
package xmlparser
2025-10-14 19:40:21 +03:00
import (
"bufio"
"io"
)
type Document struct {
Node
}
2025-10-21 20:52:16 +03:00
func NewDocument(r io.Reader) (*Document, error) {
2025-11-21 21:31:12 +03:00
doc := &Document{
Node: Node{
// Attributes for document are always empty, but they are exposed,
// so we need to initialize them to avoid nil pointer dereference.
Attrs: NewAttributes(),
},
}
2025-10-14 19:40:21 +03:00
if err := doc.readFrom(r); err != nil {
return nil, err
}
return doc, nil
}
func (doc *Document) WriteTo(w io.Writer) (int64, error) {
wc := writeCounter{Writer: w}
bw := bufio.NewWriter(&wc)
if err := doc.writeChildrenTo(bw); err != nil {
return 0, err
}
if err := bw.Flush(); err != nil {
return 0, err
}
return wc.Count, nil
}
// ReplaceEntities replaces XML entities in the document
// according to the entity declarations in the DOCTYPE.
// It modifies the document in place.
//
// Entities are replaced only once to avoid attacks like the
// "Billion Laughs" XML entity expansion attack.
func (doc *Document) ReplaceEntities() {
if em := parseEntityMap(doc.Children); em != nil {
doc.replaceEntities(em)
}
}