Skip to content

Commit

Permalink
Add optional support for HTML entities
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzuf authored and beevik committed Nov 20, 2018
1 parent f2d9344 commit 6d7c4e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Expand Up @@ -6,4 +6,5 @@ Matt Smith (ma314smith)
Michal Jemala (michaljemala)
Nicolas Piganeau (npiganeau)
Chris Brown (ccbrown)
Earncef Sequeira (earncef)
Earncef Sequeira (earncef)
Gabriel de Labachelerie (wuzuf)
4 changes: 4 additions & 0 deletions etree.go
Expand Up @@ -34,6 +34,9 @@ type ReadSettings struct {
// Permissive allows input containing common mistakes such as missing tags
// or attribute values. Default: false.
Permissive bool

// Entity to be passed to standard xml.Decoder. Default: nil.
Entity map[string]string
}

// newReadSettings creates a default ReadSettings record.
Expand Down Expand Up @@ -402,6 +405,7 @@ func (e *Element) readFrom(ri io.Reader, settings ReadSettings) (n int64, err er
dec := xml.NewDecoder(r)
dec.CharsetReader = settings.CharsetReader
dec.Strict = !settings.Permissive
dec.Entity = settings.Entity
var stack stack
stack.push(e)
for {
Expand Down
22 changes: 22 additions & 0 deletions etree_test.go
Expand Up @@ -5,6 +5,7 @@
package etree

import (
"encoding/xml"
"io"
"testing"
)
Expand Down Expand Up @@ -170,6 +171,27 @@ func TestDocumentRead_Permissive(t *testing.T) {
}
}

func TestDocumentRead_HTMLEntities(t *testing.T) {
s := `<store>
<book lang="en">
<title>&rarr;&nbsp;Great Expectations</title>
<author>Charles Dickens</author>
</book>
</store>`

doc := NewDocument()
err := doc.ReadFromString(s)
if err == nil {
t.Fatal("etree: incorrect ReadFromString result")
}

doc.ReadSettings.Entity = xml.HTMLEntity
err = doc.ReadFromString(s)
if err != nil {
t.Fatal("etree: incorrect ReadFromString result")
}
}

func TestEscapeCodes(t *testing.T) {
cases := []struct {
input string
Expand Down

0 comments on commit 6d7c4e9

Please sign in to comment.