Merge pull request #540 from twpayne/export-is-container

Export Node.IsContainer and add Node.IsLeaf
pull/581/head
Russ Ross 2019-06-29 09:15:18 -06:00 committed by GitHub
commit 3e56bb68c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions

12
node.go
View File

@ -199,7 +199,8 @@ func (n *Node) InsertBefore(sibling *Node) {
}
}
func (n *Node) isContainer() bool {
// IsContainer returns true if 'n' can contain children.
func (n *Node) IsContainer() bool {
switch n.Type {
case Document:
fallthrough
@ -238,6 +239,11 @@ func (n *Node) isContainer() bool {
}
}
// IsLeaf returns true if 'n' is a leaf node.
func (n *Node) IsLeaf() bool {
return !n.IsContainer()
}
func (n *Node) canContain(t NodeType) bool {
if n.Type == List {
return t == Item
@ -309,11 +315,11 @@ func newNodeWalker(root *Node) *nodeWalker {
}
func (nw *nodeWalker) next() {
if (!nw.current.isContainer() || !nw.entering) && nw.current == nw.root {
if (!nw.current.IsContainer() || !nw.entering) && nw.current == nw.root {
nw.current = nil
return
}
if nw.entering && nw.current.isContainer() {
if nw.entering && nw.current.IsContainer() {
if nw.current.FirstChild != nil {
nw.current = nw.current.FirstChild
nw.entering = true