-
Notifications
You must be signed in to change notification settings - Fork 17
/
pos.go
47 lines (42 loc) · 1017 Bytes
/
pos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package htmldiff
import "golang.org/x/net/html"
// posTT gives the relative position within one level of a nested container.
type posTT struct {
nodesBefore int
node *html.Node
}
// posT gives the relative position within a nested set of containers.
type posT []posTT
// getPos returns the relative posion of this node within the enclosing containers, if there are any.
func getPos(n *html.Node) posT {
if n == nil {
return nil
}
depth := 0
for root := n; inContainer(root); root = root.Parent {
depth++
}
ret := make([]posTT, 0, depth) // for speed
for root := n; depth > 0; root = root.Parent {
var before int
for sib := root.Parent.FirstChild; sib != root; sib = sib.NextSibling {
if sib.Type == html.ElementNode {
before++
}
}
ret = append(ret, posTT{before, root})
depth--
}
return ret
}
func posEqual(a, b posT) bool {
if len(a) != len(b) {
return false
}
for i, aa := range a {
if aa.nodesBefore != b[i].nodesBefore {
return false
}
}
return true
}