Skip to content

Commit

Permalink
Add choice of quoting style (#109)
Browse files Browse the repository at this point in the history
This adds a config option to choose between `"` and `'` for quoting
attributes.
  • Loading branch information
mdosch authored May 2, 2023
1 parent 73600d0 commit c04c961
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ type WriteSettings struct {
// return followed by a linefeed ("\r\n") when outputting a newline. If
// false, only a linefeed is used ("\n"). Default: false.
UseCRLF bool

// AttrSingleQuote causes attributes to use single quotes (attr = 'example')
// instead of double quotes (attr = "example") when set to true.
// Default: false.
AttrSingleQuote bool
}

// newWriteSettings creates a default WriteSettings record.
Expand Down Expand Up @@ -1173,15 +1178,23 @@ func (a *Attr) NamespaceURI() string {
// writeTo serializes the attribute to the writer.
func (a *Attr) writeTo(w *bufio.Writer, s *WriteSettings) {
w.WriteString(a.FullKey())
w.WriteString(`="`)
if s.AttrSingleQuote {
w.WriteString(`='`)
} else {
w.WriteString(`="`)
}
var m escapeMode
if s.CanonicalAttrVal {
m = escapeCanonicalAttr
} else {
m = escapeNormal
}
escapeString(w, a.Value, m)
w.WriteByte('"')
if s.AttrSingleQuote {
w.WriteByte('\'')
} else {
w.WriteByte('"')
}
}

// NewText creates an unparented CharData token containing simple text data.
Expand Down

0 comments on commit c04c961

Please sign in to comment.