📄 dom_nodeimpl.cpp
字号:
if(newParent) newParent->removeChild( child ); // seems ok, lets's insert it. if (prev) prev->setNextSibling(child); else _first = child; refChild->setPreviousSibling(child); child->setParent(this); child->setPreviousSibling(prev); child->setNextSibling(refChild); if (attached()) child->attach(document->view()); prev = child; child = nextChild; } // ### set style in case it's attached setChanged(true); return newChild;}NodeImpl *NodeBaseImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild ){ checkReadOnly(); if (!newChild || (newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE && !newChild->firstChild())) throw DOMException(DOMException::NOT_FOUND_ERR); checkSameDocument(newChild); checkIsChild(oldChild); checkNoOwner(newChild); bool isFragment = newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE; NodeImpl *nextChild; NodeImpl *child = isFragment ? newChild->firstChild() : newChild; // make sure we will be able to insert the first node before we go removing the old one checkNoOwner(isFragment ? newChild->firstChild() : newChild); if(!childAllowed(isFragment ? newChild->firstChild() : newChild)) throw DOMException(DOMException::HIERARCHY_REQUEST_ERR); NodeImpl *prev = oldChild->previousSibling(); NodeImpl *next = oldChild->nextSibling(); oldChild->setPreviousSibling(0); oldChild->setNextSibling(0); oldChild->setParent(0); if (m_render && oldChild->renderer()) m_render->removeChild(oldChild->renderer()); while (child) { nextChild = isFragment ? child->nextSibling() : 0; checkNoOwner(child); if(!childAllowed(child)) throw DOMException(DOMException::HIERARCHY_REQUEST_ERR); // if already in the tree, remove it first! NodeImpl *newParent = child->parentNode(); if(newParent) newParent->removeChild( child ); // seems ok, lets's insert it. if (prev) prev->setNextSibling(child); if (next) next->setPreviousSibling(child); if(!prev) _first = child; if(!next) _last = child; child->setParent(this); child->setPreviousSibling(prev); child->setNextSibling(next); if (attached()) child->attach(document->view()); prev = child; child = nextChild; } // ### set style in case it's attached setChanged(true); return oldChild;}NodeImpl *NodeBaseImpl::removeChild ( NodeImpl *oldChild ){ checkReadOnly(); checkIsChild(oldChild); NodeImpl *prev, *next; prev = oldChild->previousSibling(); next = oldChild->nextSibling(); if(next) next->setPreviousSibling(prev); if(prev) prev->setNextSibling(next); if(_first == oldChild) _first = next; if(_last == oldChild) _last = prev; oldChild->setPreviousSibling(0); oldChild->setNextSibling(0); oldChild->setParent(0); if (m_render && oldChild->renderer()) m_render->removeChild(oldChild->renderer()); setChanged(true); return oldChild;}NodeImpl *NodeBaseImpl::appendChild ( NodeImpl *newChild ){// kdDebug(6010) << "NodeBaseImpl::appendChild( " << newChild << " );" <<endl; checkReadOnly(); if (!newChild || (newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE && !newChild->firstChild())) throw DOMException(DOMException::NOT_FOUND_ERR); checkSameDocument(newChild); checkNoOwner(newChild); if(newChild->parentNode() == this) removeChild(newChild); bool isFragment = newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE; NodeImpl *nextChild; NodeImpl *child = isFragment ? newChild->firstChild() : newChild; while (child) { nextChild = isFragment ? child->nextSibling() : 0; checkNoOwner(child); if(!childAllowed(child)) throw DOMException(DOMException::HIERARCHY_REQUEST_ERR); // if already in the tree, remove it first! NodeImpl *newParent = child->parentNode(); if(newParent) newParent->removeChild( child ); // lets append it child->setParent(this); if(_last) { child->setPreviousSibling(_last); _last->setNextSibling(child); _last = child; } else { _first = _last = child; } if (attached()) child->attach(document->view()); child = nextChild; } setChanged(true); // ### set style in case it's attached return newChild;}bool NodeBaseImpl::hasChildNodes ( ){ return _first != 0;}NodeImpl *NodeBaseImpl::cloneNode ( bool deep ){ NodeImpl *newImpl = new NodeBaseImpl(document); newImpl->setParent(0); newImpl->setFirstChild(0); newImpl->setLastChild(0); if(deep) { NodeImpl *n; for(n = firstChild(); n != lastChild(); n = n->nextSibling()) { newImpl->appendChild(n->cloneNode(deep)); } } return newImpl;}// not part of the DOMvoid NodeBaseImpl::setFirstChild(NodeImpl *child){ _first = child;}void NodeBaseImpl::setLastChild(NodeImpl *child){ _last = child;}// check for same source document:void NodeBaseImpl::checkSameDocument( NodeImpl *newChild ){ DocumentImpl *ownerDocThis = static_cast<DocumentImpl*>(nodeType() == Node::DOCUMENT_NODE ? this : ownerDocument()); DocumentImpl *ownerDocNew = static_cast<DocumentImpl*>(newChild->nodeType() == Node::DOCUMENT_NODE ? newChild : newChild->ownerDocument()); if(ownerDocThis != ownerDocNew) {// kdDebug(6010)<< "not same document, newChild = " << newChild << "document = " << document << endl; throw DOMException(DOMException::WRONG_DOCUMENT_ERR); }}// check for being (grand-..)father:void NodeBaseImpl::checkNoOwner( NodeImpl *newChild ){ //check if newChild is parent of this... NodeImpl *n; for( n = this; n != (NodeImpl *)document && n!= 0; n = n->parentNode() ) if(n == newChild) throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);}// check for being child:void NodeBaseImpl::checkIsChild( NodeImpl *oldChild ){ if(!oldChild || oldChild->parentNode() != this) throw DOMException(DOMException::NOT_FOUND_ERR);}bool NodeBaseImpl::childAllowed( NodeImpl *newChild ){ return checkChild(id(), newChild->id());}NodeImpl *NodeBaseImpl::addChild(NodeImpl *newChild){ // do not add applyChanges here! This function is only used during parsing // short check for consistency with DTD if(!childAllowed(newChild)) { //kdDebug( 6020 ) << "AddChild failed! id=" << id() << ", child->id=" << newChild->id() << endl; throw DOMException(DOMException::HIERARCHY_REQUEST_ERR); } // just add it... newChild->setParent(this); if(_last) { newChild->setPreviousSibling(_last); _last->setNextSibling(newChild); _last = newChild; } else { _first = _last = newChild; } if(newChild->nodeType() == Node::ELEMENT_NODE) return newChild; return this;}void NodeBaseImpl::attach(MGHTMLView *w){ NodeImpl *child = _first; while(child != 0) { child->attach(w); child = child->nextSibling(); } NodeWParentImpl::attach(w);}void NodeBaseImpl::detach(){ NodeImpl *child = _first; while(child != 0) { child->detach(); child = child->nextSibling(); } NodeWParentImpl::detach(); delete m_render; m_render = 0;}void NodeBaseImpl::setOwnerDocument(DocumentImpl *_document){ NodeImpl *n; for(n = _first; n != 0; n = n->nextSibling()) n->setOwnerDocument(_document); NodeWParentImpl::setOwnerDocument(_document);}// ---------------------------------------------------------------------------NodeImpl *NodeListImpl::item( unsigned long /*index*/ ) const{ return 0;}unsigned long NodeListImpl::length() const{ return 0;}unsigned long NodeListImpl::recursiveLength(NodeImpl *start) const{ unsigned long len = 0; for(NodeImpl *n = start->firstChild(); n != 0; n = n->nextSibling()) { if ( n->nodeType() == Node::ELEMENT_NODE ) { if (nodeMatches(n)) len++; len+= recursiveLength(n); } } return len;}NodeImpl *NodeListImpl::recursiveItem ( NodeImpl *start, unsigned long &offset ) const{ for(NodeImpl *n = start->firstChild(); n != 0; n = n->nextSibling()) { if ( n->nodeType() == Node::ELEMENT_NODE ) { if (nodeMatches(n)) if (!offset--) return n; NodeImpl *depthSearch= recursiveItem(n, offset); if (depthSearch) return depthSearch; } } return 0; // no matching node in this subtree}bool NodeListImpl::nodeMatches( NodeImpl */*testNode*/ ) const{ // ### return false;}ChildNodeListImpl::ChildNodeListImpl( NodeImpl *n ){ refNode = n; refNode->ref();}ChildNodeListImpl::~ChildNodeListImpl(){ refNode->deref();}unsigned long ChildNodeListImpl::length() const{ unsigned long len = 0; NodeImpl *n; for(n = refNode->firstChild(); n != 0; n = n->nextSibling()) len++; return len;}NodeImpl *ChildNodeListImpl::item ( unsigned long index ) const{ unsigned int pos = 0; NodeImpl *n = refNode->firstChild(); while( n != 0 && pos < index ) { n = n->nextSibling(); pos++; } return n;}TagNodeListImpl::TagNodeListImpl(NodeImpl *n, const DOMString &t ) : tagName(t){ refNode = n; refNode->ref(); allElements = (t == "*");}TagNodeListImpl::~TagNodeListImpl(){ refNode->deref();}unsigned long TagNodeListImpl::length() const{ return recursiveLength( refNode );}NodeImpl *TagNodeListImpl::item ( unsigned long index ) const{ return recursiveItem( refNode, index );}bool TagNodeListImpl::nodeMatches( NodeImpl *testNode ) const{ return ((allElements && testNode->nodeType() == Node::ELEMENT_NODE) || !strcasecmp(testNode->nodeName(),tagName));}NameNodeListImpl::NameNodeListImpl(NodeImpl *n, const DOMString &t ) : nodeName(t){ refNode= n; refNode->ref();}NameNodeListImpl::~NameNodeListImpl(){ refNode->deref();}unsigned long NameNodeListImpl::length() const{ return recursiveLength( refNode );}NodeImpl *NameNodeListImpl::item ( unsigned long index ) const{ return recursiveItem( refNode, index );}bool NameNodeListImpl::nodeMatches( NodeImpl *testNode ) const{ return static_cast<ElementImpl *>(testNode)->getAttribute(ATTR_NAME) == nodeName;}// ---------------------------------------------------------------------------NamedNodeMapImpl::NamedNodeMapImpl(){}NamedNodeMapImpl::~NamedNodeMapImpl(){}unsigned long NamedNodeMapImpl::length() const{ // ### return 0;}NodeImpl *NamedNodeMapImpl::getNamedItem ( const DOMString &/*name*/ ) const{ // ### return 0;}NodeImpl *NamedNodeMapImpl::setNamedItem ( const Node &/*arg*/ ){ // ### return 0;}NodeImpl *NamedNodeMapImpl::removeNamedItem ( const DOMString &/*name*/ ){ // ### return 0;}NodeImpl *NamedNodeMapImpl::item ( unsigned long /*index*/ ) const{ // ### return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -