📄 dom_position.cpp
字号:
/*
* Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "dom_position.h"
#include <qstring.h>
#include "css_computedstyle.h"
#include "css_valueimpl.h"
#include "dom_elementimpl.h"
#include "dom_nodeimpl.h"
#include "dom2_range.h"
#include "dom2_rangeimpl.h"
#include "dom2_viewsimpl.h"
#include "helper.h"
#include "htmltags.h"
#include "text_affinity.h"
#include "visible_position.h"
#include "rendering/render_block.h"
#include "rendering/render_flow.h"
#include "rendering/render_line.h"
#include "rendering/render_style.h"
#include "rendering/render_text.h"
#include "editing/visible_text.h"
#if APPLE_CHANGES
#include "KWQAssertions.h"
#include "KWQLogging.h"
#else
#define ASSERT(assertion) assert(assertion)
#define LOG(channel, formatAndArgs...) ((void)0)
#endif
using khtml::EAffinity;
using khtml::InlineBox;
using khtml::InlineTextBox;
using khtml::isCollapsibleWhitespace;
using khtml::RenderBlock;
using khtml::RenderFlow;
using khtml::RenderObject;
using khtml::RenderText;
using khtml::RootInlineBox;
using khtml::VISIBLE;
using khtml::VisiblePosition;
namespace DOM {
static NodeImpl *nextRenderedEditable(NodeImpl *node)
{
while (1) {
node = node->nextEditable();
if (!node)
return 0;
if (!node->renderer())
continue;
if (node->renderer()->inlineBox(0))
return node;
}
return 0;
}
static NodeImpl *previousRenderedEditable(NodeImpl *node)
{
while (1) {
node = node->previousEditable();
if (!node)
return 0;
if (!node->renderer())
continue;
if (node->renderer()->inlineBox(0))
return node;
}
return 0;
}
Position::Position(NodeImpl *node, long offset)
: m_node(node), m_offset(offset)
{
if (node) {
node->ref();
}
}
Position::Position(const Position &o)
: m_node(o.m_node), m_offset(o.m_offset)
{
if (m_node) {
m_node->ref();
}
}
Position::~Position()
{
if (m_node) {
m_node->deref();
}
}
Position &Position::operator=(const Position &o)
{
if (m_node) {
m_node->deref();
}
m_node = o.m_node;
if (m_node) {
m_node->ref();
}
m_offset = o.m_offset;
return *this;
}
void Position::clear()
{
if (m_node) {
m_node->deref();
m_node = 0;
}
m_offset = 0;
}
ElementImpl *Position::element() const
{
NodeImpl *n;
for (n = node(); n && !n->isElementNode(); n = n->parentNode())
; // empty loop body
return static_cast<ElementImpl *>(n);
}
CSSComputedStyleDeclarationImpl *Position::computedStyle() const
{
ElementImpl *elem = element();
if (!elem)
return 0;
return new CSSComputedStyleDeclarationImpl(elem);
}
Position Position::previous(EUsingComposedCharacters usingComposedCharacters) const
{
NodeImpl *n = node();
if (!n)
return *this;
long o = offset();
assert(o >= 0);
if (o > 0) {
NodeImpl *child = n->childNode(o - 1);
if (child) {
return Position(child, child->maxDeepOffset());
}
// There are two reasons child might be 0:
// 1) The node is node like a text node that is not an element, and therefore has no children.
// Going backward one character at a time is correct.
// 2) The old offset was a bogus offset like (<br>, 1), and there is no child.
// Going from 1 to 0 is correct.
return Position(n, usingComposedCharacters ? n->previousOffset(o) : o - 1);
}
NodeImpl *parent = n->parentNode();
if (!parent)
return *this;
return Position(parent, n->nodeIndex());
}
Position Position::next(EUsingComposedCharacters usingComposedCharacters) const
{
NodeImpl *n = node();
if (!n)
return *this;
long o = offset();
assert(o >= 0);
if (o < n->maxDeepOffset()) {
NodeImpl *child = n->childNode(o);
if (child) {
return Position(child, 0);
}
// There are two reasons child might be 0:
// 1) The node is node like a text node that is not an element, and therefore has no children.
// Going forward one character at a time is correct.
// 2) The new offset is a bogus offset like (<br>, 1), and there is no child.
// Going from 0 to 1 is correct.
return Position(n, usingComposedCharacters ? n->nextOffset(o) : o + 1);
}
NodeImpl *parent = n->parentNode();
if (!parent)
return *this;
return Position(parent, n->nodeIndex() + 1);
}
bool Position::atStart() const
{
NodeImpl *n = node();
if (!n)
return true;
return offset() <= 0 && n->parent() == 0;
}
bool Position::atEnd() const
{
NodeImpl *n = node();
if (!n)
return true;
return offset() >= n->maxDeepOffset() && n->parent() == 0;
}
long Position::renderedOffset() const
{
if (!node()->isTextNode())
return offset();
if (!node()->renderer())
return offset();
long result = 0;
RenderText *textRenderer = static_cast<RenderText *>(node()->renderer());
for (InlineTextBox *box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
long start = box->m_start;
long end = box->m_start + box->m_len;
if (offset() < start)
return result;
if (offset() <= end) {
result += offset() - start;
return result;
}
result += box->m_len;
}
return result;
}
// return first preceding DOM position rendered at a different location, or "this"
Position Position::previousCharacterPosition(EAffinity affinity) const
{
if (isNull())
return Position();
NodeImpl *fromRootEditableElement = node()->rootEditableElement();
bool atStartOfLine = isFirstVisiblePositionOnLine(VisiblePosition(*this, affinity));
bool rendered = inRenderedContent();
Position currentPos = *this;
while (!currentPos.atStart()) {
currentPos = currentPos.previous();
if (currentPos.node()->rootEditableElement() != fromRootEditableElement)
return *this;
if (atStartOfLine || !rendered) {
if (currentPos.inRenderedContent())
return currentPos;
} else if (rendersInDifferentPosition(currentPos))
return currentPos;
}
return *this;
}
// return first following position rendered at a different location, or "this"
Position Position::nextCharacterPosition(EAffinity affinity) const
{
if (isNull())
return Position();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -