Skip to content

Commit

Permalink
Make sure Id for NodeDocComponent is unique
Browse files Browse the repository at this point in the history
Before, this generated the same Id for any component that had the same content no matter the tag or attributes, which leads to problems with empty components: <ColorCard color="#FF00000"></ColorCard> and <TableOfContents></TableOfContents> got the same Id. Adding the position to the string to be hashed ensures uniqueness.
  • Loading branch information
ChristophLabacher committed Oct 6, 2021
1 parent e66c0e0 commit 6583933
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/ddt/node_doc_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"regexp"
"strings"
"strconv"
)

const (
Expand All @@ -32,6 +33,7 @@ func (c *NodeDocComponent) Id() string {
cleaner := regexp.MustCompile(`\s`)

content := strings.ToLower(cleaner.ReplaceAllString(c.RawInner, ""))
content += strconv.Itoa(c.Position)
return fmt.Sprintf("%x", sha1.Sum([]byte(content)))
}

Expand Down

1 comment on commit 6583933

@mariuswilms
Copy link
Member

@mariuswilms mariuswilms commented on 6583933 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to fix this! Instead of making the position part of the hash by adding it to the content which is than hashed, it would be more correct to initialize the hash once, and than update with the contents and than with the position, as in https://gobyexample.com/sha1-hashes

Please sign in to comment.