📄 dom2_rangeimpl.cpp
字号:
/** * This file is part of the DOM implementation for KDE. * * (C) 1999 Lars Knoll (knoll@kde.org) * (C) 2000 Gunnstein Lye (gunnstein@netcom.no) * (C) 2000 Frederik Holljen (frederik.holljen@hig.no) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * $Id: dom2_rangeimpl.cpp,v 1.1.1.1 2002/01/16 10:39:55 ymwei Exp $ */#include <stdio.h> // for printf#include <qstring.h>#include "dom2_traversal.h"#include "dom_node.h"#include "dom_doc.h"#include "dom_string.h"#include "dom_text.h"#include "dom_exception.h"#include "dom_docimpl.h"#include "dom2_rangeimpl.h"#include "dom2_traversalimpl.h"using namespace DOM;RangeImpl::RangeImpl(){ startContainer = 0; endContainer = 0; startOffset = 0; endOffset = 0; commonAncestorContainer = 0; collapsed = true; detached = false;}RangeImpl::RangeImpl(DocumentImpl */*rootContainer*/){ // ###}RangeImpl::RangeImpl(const Document rootContainer){ ownerDocument = rootContainer; startContainer = (Node)rootContainer; endContainer = (Node)rootContainer; startOffset = 0; endOffset = 0; commonAncestorContainer = (Node)rootContainer; collapsed = true; detached = false;}RangeImpl::RangeImpl(const RangeImpl &other) : DomShared(other){ ownerDocument = other.ownerDocument; startContainer = other.startContainer; startOffset = other.startOffset; endContainer = other.endContainer; endOffset = other.endOffset; commonAncestorContainer = other.commonAncestorContainer; collapsed = other.collapsed; detached = false;}RangeImpl::RangeImpl(const Node sc, const long so, const Node ec, const long eo){ startContainer = sc; startOffset = so; endContainer = ec; endOffset = eo; commonAncestorContainer = getCommonAncestorContainer(); if( startContainer == endContainer && startOffset == endOffset ) collapsed = true; else collapsed = false; detached = false;}RangeImpl &RangeImpl::operator = (const RangeImpl &other){ ownerDocument = other.ownerDocument; startContainer = other.startContainer; startOffset = other.startOffset; endContainer = other.endContainer; endOffset = other.endOffset; commonAncestorContainer = other.commonAncestorContainer; collapsed = other.collapsed; detached = false; return *this;}RangeImpl::~RangeImpl(){}Node RangeImpl::getStartContainer() const{ if( isDetached() ) throw DOMException(DOMException::INVALID_STATE_ERR); else return startContainer;}long RangeImpl::getStartOffset() const{ if( isDetached() ) throw DOMException(DOMException::INVALID_STATE_ERR); else return startOffset;}Node RangeImpl::getEndContainer() const{ if( isDetached() ) throw DOMException(DOMException::INVALID_STATE_ERR); else return endContainer;}long RangeImpl::getEndOffset() const{ if( isDetached() ) throw DOMException(DOMException::INVALID_STATE_ERR); else return endOffset;}Node RangeImpl::getCommonAncestorContainer() /*const*/{ if( isDetached() ) throw DOMException(DOMException::INVALID_STATE_ERR); Node parentStart = startContainer; Node parentEnd = endContainer; while( !parentStart.isNull() && (parentStart != parentEnd) ) { while( !parentEnd.isNull() && (parentStart != parentEnd) ) parentEnd = parentEnd.parentNode(); if(parentStart == parentEnd) break; parentStart = parentStart.parentNode(); parentEnd = endContainer; } if(parentStart == parentEnd) commonAncestorContainer = parentStart; else { return Node(); } return commonAncestorContainer;}bool RangeImpl::getCollapsed() const{ if( isDetached() ) throw DOMException(DOMException::INVALID_STATE_ERR); else return collapsed;}void RangeImpl::setStart( const Node &refNode, long offset ){ Node _tempNode = refNode; if( _tempNode.isNull() ) throw DOMException( DOMException::NOT_FOUND_ERR ); while( !_tempNode.isNull() ) { if( _tempNode.nodeType() == Node::ATTRIBUTE_NODE || _tempNode.nodeType() == Node::ENTITY_NODE || _tempNode.nodeType() == Node::NOTATION_NODE || _tempNode.nodeType() == Node::DOCUMENT_TYPE_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); _tempNode = _tempNode.parentNode(); } if( offset < 0 ) throw DOMException( DOMException::INDEX_SIZE_ERR ); if( refNode.nodeType() != Node::TEXT_NODE ) { if( (unsigned)offset > refNode.childNodes().length() ) throw DOMException( DOMException::INDEX_SIZE_ERR ); } else { Text t; t = refNode; if( t.isNull() || (unsigned)offset > t.length() ) throw DOMException( DOMException::INDEX_SIZE_ERR ); } if( isDetached() ) throw DOMException( DOMException::INVALID_STATE_ERR ); startContainer = refNode; startOffset = offset; if( endContainer != 0 ) { if( commonAncestorContainer != 0 ) { Node oldCommonAncestorContainer = commonAncestorContainer; if( oldCommonAncestorContainer != getCommonAncestorContainer() ) collapse( true ); if( !boundaryPointsValid() ) collapse( true ); } else getCommonAncestorContainer(); }}void RangeImpl::setEnd( const Node &refNode, long offset ){ Node _tempNode = refNode; if( _tempNode.isNull() ) throw DOMException( DOMException::NOT_FOUND_ERR ); while( !_tempNode.isNull() ) { if( _tempNode.nodeType() == Node::ATTRIBUTE_NODE || _tempNode.nodeType() == Node::ENTITY_NODE || _tempNode.nodeType() == Node::NOTATION_NODE || _tempNode.nodeType() == Node::DOCUMENT_TYPE_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); _tempNode = _tempNode.parentNode(); } if( offset < 0 ) throw DOMException( DOMException::INDEX_SIZE_ERR ); if( refNode.nodeType() != Node::TEXT_NODE ) { if( (unsigned)offset > refNode.childNodes().length() ) throw DOMException( DOMException::INDEX_SIZE_ERR ); } else { Text t; t = refNode; if( t.isNull() || (unsigned)offset > t.length() ) throw DOMException( DOMException::INDEX_SIZE_ERR ); } if( isDetached() ) throw DOMException( DOMException::INVALID_STATE_ERR ); endContainer = refNode; endOffset = offset; if( startContainer != 0 ) { if( commonAncestorContainer != 0 ) { Node oldCommonAncestorContainer = commonAncestorContainer; if( oldCommonAncestorContainer != getCommonAncestorContainer() ) collapse( true ); if( !boundaryPointsValid() ) collapse( true ); } else getCommonAncestorContainer(); }}void RangeImpl::setStartBefore( const Node &refNode ){ Node _tempNode = refNode; if( _tempNode.isNull() ) throw DOMException( DOMException::NOT_FOUND_ERR ); _tempNode = refNode.parentNode(); while( !_tempNode.isNull() ) { if( _tempNode.nodeType() == Node::ATTRIBUTE_NODE || _tempNode.nodeType() == Node::ENTITY_NODE || _tempNode.nodeType() == Node::NOTATION_NODE || _tempNode.nodeType() == Node::DOCUMENT_TYPE_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); _tempNode = _tempNode.parentNode(); } if( refNode.nodeType() == Node::DOCUMENT_NODE || refNode.nodeType() == Node::DOCUMENT_FRAGMENT_NODE || refNode.nodeType() == Node::ATTRIBUTE_NODE || refNode.nodeType() == Node::ENTITY_NODE || refNode.nodeType() == Node::NOTATION_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); if( isDetached() ) throw DOMException( DOMException::INVALID_STATE_ERR ); try { setStart( refNode.parentNode(), refNode.index() ); } catch( RangeException r ) { if( r.code == RangeException::INVALID_NODE_TYPE_ERR ) fprintf( stderr, "Exception: Invalid Node type\n" ); return; } catch( DOMException d ) { if( d.code == DOMException::NOT_FOUND_ERR ) fprintf( stderr, "Exception: Null Nodes not accepted\n" ); if( d.code == DOMException::INDEX_SIZE_ERR ) fprintf( stderr, "Exception: offset has wrong size\n" ); if( d.code == DOMException::INVALID_STATE_ERR ) fprintf( stderr, "Exception: detach() has been invoked\n" ); return; }}void RangeImpl::setStartAfter( const Node &refNode ){ Node _tempNode = refNode; if( _tempNode.isNull() ) throw DOMException( DOMException::NOT_FOUND_ERR ); _tempNode = refNode.parentNode(); while( !_tempNode.isNull() ) { if( _tempNode.nodeType() == Node::ATTRIBUTE_NODE || _tempNode.nodeType() == Node::ENTITY_NODE || _tempNode.nodeType() == Node::NOTATION_NODE || _tempNode.nodeType() == Node::DOCUMENT_TYPE_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); _tempNode = _tempNode.parentNode(); } if( refNode.nodeType() == Node::DOCUMENT_NODE || refNode.nodeType() == Node::DOCUMENT_FRAGMENT_NODE || refNode.nodeType() == Node::ATTRIBUTE_NODE || refNode.nodeType() == Node::ENTITY_NODE || refNode.nodeType() == Node::NOTATION_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); if( isDetached() ) throw DOMException( DOMException::INVALID_STATE_ERR ); try { setStart( refNode.parentNode(), refNode.index()+1 ); } catch( RangeException r ) { if( r.code == RangeException::INVALID_NODE_TYPE_ERR ) fprintf( stderr, "Exception: Invalid Node type\n" ); return; } catch( DOMException d ) { if( d.code == DOMException::NOT_FOUND_ERR ) fprintf( stderr, "Exception: Null Nodes not accepted\n" ); if( d.code == DOMException::INDEX_SIZE_ERR ) fprintf( stderr, "Exception: offset has wrong size\n" ); if( d.code == DOMException::INVALID_STATE_ERR ) fprintf( stderr, "Exception: detach() has been invoked\n" ); return; }}void RangeImpl::setEndBefore( const Node &refNode ){ Node _tempNode = refNode; if( _tempNode.isNull() ) throw DOMException( DOMException::NOT_FOUND_ERR ); _tempNode = refNode.parentNode(); while( !_tempNode.isNull() ) { if( _tempNode.nodeType() == Node::ATTRIBUTE_NODE || _tempNode.nodeType() == Node::ENTITY_NODE || _tempNode.nodeType() == Node::NOTATION_NODE || _tempNode.nodeType() == Node::DOCUMENT_TYPE_NODE ) throw RangeException( RangeException::INVALID_NODE_TYPE_ERR ); _tempNode = _tempNode.parentNode(); } if( refNode.nodeType() == Node::DOCUMENT_NODE || refNode.nodeType() == Node::DOCUMENT_FRAGMENT_NODE || refNode.nodeType() == Node::ATTRIBUTE_NODE ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -