Skip to content

Commit

Permalink
encoder+decoder: escape/unescape HTML characters in bookmark descript…
Browse files Browse the repository at this point in the history
…ions

Signed-off-by: VirtualTam <[email protected]>
  • Loading branch information
virtualtam committed Oct 26, 2023
1 parent 3623544 commit 5fe95e9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion decoder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package netscape

import (
"html"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -105,7 +106,7 @@ func (d *Decoder) decodeFolder(f FolderNode) (Folder, error) {

func (d Decoder) decodeBookmark(b BookmarkNode) (Bookmark, error) {
bookmark := Bookmark{
Description: b.Description,
Description: html.UnescapeString(b.Description),
URL: b.Href,
Title: b.Title,
Attributes: map[string]string{},
Expand Down
44 changes: 44 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,50 @@ func TestDecodeBookmark(t *testing.T) {
Description: "Nested lists:\n- list1\n - item1.1\n - item1.2\n - item1.3\n- list2\n - item2.1",
},
},
{
tname: "bookmark with description containing escaped HTML characters",
input: BookmarkNode{
Description: "&#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;[email protected]&gt;",
Href: "https://domain.tld",
Title: "Test Domain",
},
want: Bookmark{
Title: "Test Domain",
URL: "https://domain.tld",
Description: `"Fran & Freddie's Diner" <[email protected]>`,
},
},
{
tname: "bookmark with multi-line description containing escaped HTML characters",
input: BookmarkNode{
Description: `
&gt; The format of here-documents is:
` + "```shell" + `
[n]&lt;&lt;[-]word
here-document
delimiter
` + "```" + `
&gt; If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.`,
Href: "https://domain.tld",
Title: "Test Domain",
},
want: Bookmark{
Title: "Test Domain",
URL: "https://domain.tld",
Description: `
> The format of here-documents is:
` + "```shell" + `
[n]<<[-]word
here-document
delimiter
` + "```" + `
> If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.`,
},
},
{
tname: "bookmark with creation date",
input: BookmarkNode{
Expand Down
3 changes: 2 additions & 1 deletion encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/xml"
"fmt"
"html"
"io"
"sort"
"strings"
Expand Down Expand Up @@ -230,7 +231,7 @@ func (p *printer) marshalBookmark(b *Bookmark) error {
}

if b.Description != "" {
_, err = p.writeString(fmt.Sprintf("<DD>%s\n", b.Description))
_, err = p.writeString(fmt.Sprintf("<DD>%s\n", html.EscapeString(b.Description)))
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ func TestMarshal(t *testing.T) {
URL: "https://test.domain.tld",
Title: "Test Domain II",
},
{
Description: `
> The format of here-documents is:
` + "```shell" + `
[n]<<[-]word
here-document
delimiter
` + "```" + `
> If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.`,
URL: "https://markdown.xml",
Title: "Markdown Description",
},
},
},
},
Expand All @@ -58,6 +72,17 @@ func TestMarshal(t *testing.T) {
<DT><A HREF="https://domain.tld" PRIVATE="0">Test Domain</A>
<DT><A HREF="https://test.domain.tld" PRIVATE="0">Test Domain II</A>
<DD>Second test
<DT><A HREF="https://markdown.xml" PRIVATE="0">Markdown Description</A>
<DD>
&gt; The format of here-documents is:
` + "```shell" + `
[n]&lt;&lt;[-]word
here-document
delimiter
` + "```" + `
&gt; If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.
</DL><p>
`,
},
Expand Down

0 comments on commit 5fe95e9

Please sign in to comment.