|
@@ -9,12 +9,12 @@ import (
|
|
|
|
|
|
/* End Node Functions */
|
|
|
func LoadHTML(htmlStr string) *Node {
|
|
|
- tokList := getTokenList(htmlStr)
|
|
|
- bt := buildTree(tokList)
|
|
|
+ tokList := GetTokenList(htmlStr)
|
|
|
+ bt := BuildTree(tokList)
|
|
|
return bt
|
|
|
}
|
|
|
|
|
|
-func getTokenList(htmlStr string) []string {
|
|
|
+func GetTokenList(htmlStr string) []string {
|
|
|
var tokList []string
|
|
|
var tok string
|
|
|
|
|
@@ -96,39 +96,48 @@ func getTokenList(htmlStr string) []string {
|
|
|
return tokList
|
|
|
}
|
|
|
|
|
|
-func buildTree(tokList []string) *Node {
|
|
|
+func BuildTree(tokList []string, params ...*Node) *Node {
|
|
|
root := &Node{}
|
|
|
+ root.token = ""
|
|
|
|
|
|
- node := &Node{}
|
|
|
- node.Parent = root
|
|
|
+ if len(params) > 0 {
|
|
|
+ root = params[0]
|
|
|
+ }
|
|
|
|
|
|
- for _, tok := range tokList {
|
|
|
- prev := node
|
|
|
- prev.token = tok
|
|
|
- node = &Node{}
|
|
|
+ prev := root
|
|
|
|
|
|
+ for _, tok := range tokList {
|
|
|
+ node := &Node{}
|
|
|
+ node.token = tok
|
|
|
if getTokType(tok) == "open" {
|
|
|
node.Parent = prev
|
|
|
+ prev.Children = append(prev.Children, node)
|
|
|
+ prev = node
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if getTokType(tok) == "close" {
|
|
|
+ prev = prev.Parent
|
|
|
+ node.Parent = prev
|
|
|
+ prev.Children = append(prev.Children, node)
|
|
|
+ continue
|
|
|
}
|
|
|
-
|
|
|
if getTokType(tok) == "text" {
|
|
|
- node.Parent = prev.Parent
|
|
|
+ node.Parent = prev
|
|
|
+ prev.Children = append(prev.Children, node)
|
|
|
+ continue
|
|
|
}
|
|
|
|
|
|
if getTokType(tok) == "selfclosing" {
|
|
|
- node.Parent = prev.Parent
|
|
|
+ node.Parent = prev
|
|
|
+ prev.Children = append(prev.Children, node)
|
|
|
+ continue
|
|
|
}
|
|
|
|
|
|
if getTokType(tok) == "comment" {
|
|
|
- node.Parent = prev.Parent
|
|
|
- }
|
|
|
-
|
|
|
- if getTokType(tok) == "close" {
|
|
|
- prev.Parent = prev.Parent.Parent
|
|
|
- node.Parent = prev.Parent
|
|
|
+ node.Parent = prev
|
|
|
+ prev.Children = append(prev.Children, node)
|
|
|
+ continue
|
|
|
}
|
|
|
-
|
|
|
- prev.Parent.Children = append(prev.Parent.Children, prev)
|
|
|
}
|
|
|
|
|
|
return root
|