Пример #1
0
func (xmlNode *XmlNode) isAccestor(nodePtr unsafe.Pointer) int {
	parentPtr := xmlNode.Ptr.parent

	if C.xmlNodePtrCheck(unsafe.Pointer(parentPtr)) == C.int(0) {
		return -1
	}
	for ; parentPtr != nil; parentPtr = parentPtr.parent {
		if C.xmlNodePtrCheck(unsafe.Pointer(parentPtr)) == C.int(0) {
			return -1
		}
		p := unsafe.Pointer(parentPtr)
		if p == nodePtr {
			return 1
		}
	}
	return 0
}
Пример #2
0
func (xmlNode *XmlNode) addChild(node Node) (err error) {
	nodeType := node.NodeType()
	if nodeType == XML_DOCUMENT_NODE || nodeType == XML_HTML_DOCUMENT_NODE {
		err = ERR_CANNOT_MAKE_DUCMENT_AS_CHILD
		return
	}
	nodePtr := node.NodePtr()
	parentPtr := xmlNode.Ptr.parent

	if C.xmlNodePtrCheck(unsafe.Pointer(parentPtr)) == C.int(0) {
		return
	}

	isNodeAccestor := false
	for ; parentPtr != nil; parentPtr = parentPtr.parent {
		if C.xmlNodePtrCheck(unsafe.Pointer(parentPtr)) == C.int(0) {
			return
		}
		p := unsafe.Pointer(parentPtr)
		if p == nodePtr {
			isNodeAccestor = true
		}
	}
	if !isNodeAccestor {
		C.xmlUnlinkNode((*C.xmlNode)(nodePtr))
		C.xmlAddChild(xmlNode.Ptr, (*C.xmlNode)(nodePtr))
	} else {
		node.Remove()
	}

	/*
		childPtr := C.xmlAddChild(xmlNode.Ptr, (*C.xmlNode)(nodePtr))
		if nodeType == XML_TEXT_NODE && childPtr != (*C.xmlNode)(nodePtr) {
			//check the retured pointer
			//if it is not the text node just added, it means that the text node is freed because it has merged into other nodes
			//then we should invalid this node, because we do not want to have a dangling pointer
			node.Remove()
		}
	*/
	return
}
Пример #3
0
func (xmlNode *XmlNode) Parent() Node {
	if C.xmlNodePtrCheck(unsafe.Pointer(xmlNode.Ptr.parent)) == C.int(0) {
		return nil
	}
	return NewNode(unsafe.Pointer(xmlNode.Ptr.parent), xmlNode.Document)
}