📄 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 "dom_positioniterator.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);
}
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;
}
Position Position::previousCharacterPosition(EAffinity affinity) const
{
if (isNull())
return Position();
NodeImpl *fromRootEditableElement = node()->rootEditableElement();
PositionIterator it(*this);
bool atStartOfLine = isFirstVisiblePositionOnLine(VisiblePosition(*this, affinity));
bool rendered = inRenderedContent();
while (!it.atStart()) {
Position pos = it.previous();
if (pos.node()->rootEditableElement() != fromRootEditableElement)
return *this;
if (atStartOfLine || !rendered) {
if (pos.inRenderedContent())
return pos;
}
else if (rendersInDifferentPosition(pos))
return pos;
}
return *this;
}
Position Position::nextCharacterPosition(EAffinity affinity) const
{
if (isNull())
return Position();
NodeImpl *fromRootEditableElement = node()->rootEditableElement();
PositionIterator it(*this);
bool atEndOfLine = isLastVisiblePositionOnLine(VisiblePosition(*this, affinity));
bool rendered = inRenderedContent();
while (!it.atEnd()) {
Position pos = it.next();
if (pos.node()->rootEditableElement() != fromRootEditableElement)
return *this;
if (atEndOfLine || !rendered) {
if (pos.inRenderedContent())
return pos;
}
else if (rendersInDifferentPosition(pos))
return pos;
}
return *this;
}
Position Position::upstream(EStayInBlock stayInBlock) const
{
Position start = equivalentDeepPosition();
NodeImpl *startNode = start.node();
if (!startNode)
return Position();
NodeImpl *block = startNode->enclosingBlockFlowOrTableElement();
Position lastVisible;
PositionIterator it(start);
for (; !it.atStart(); it.previous()) {
NodeImpl *currentNode = it.current().node();
if (stayInBlock) {
NodeImpl *currentBlock = currentNode->enclosingBlockFlowOrTableElement();
if (block != currentBlock)
return it.next();
}
RenderObject *renderer = currentNode->renderer();
if (!renderer)
continue;
if (renderer->style()->visibility() != VISIBLE)
continue;
lastVisible = it.current();
if (renderer->isReplaced() || renderer->isBR()) {
if (it.current().offset() >= renderer->caretMaxOffset())
return Position(currentNode, renderer->caretMaxOffset());
else
continue;
}
if (renderer->isText() && static_cast<RenderText *>(renderer)->firstTextBox()) {
if (currentNode != startNode)
return Position(currentNode, renderer->caretMaxOffset());
if (it.current().offset() < 0)
continue;
uint textOffset = it.current().offset();
RenderText *textRenderer = static_cast<RenderText *>(renderer);
for (InlineTextBox *box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
if (textOffset > box->start() && textOffset <= box->start() + box->len())
return it.current();
else if (box != textRenderer->lastTextBox() &&
!box->nextOnLine() &&
textOffset == box->start() + box->len() + 1)
return it.current();
}
}
}
return lastVisible.isNotNull() ? lastVisible : *this;
}
Position Position::downstream(EStayInBlock stayInBlock) const
{
Position start = equivalentDeepPosition();
NodeImpl *startNode = start.node();
if (!startNode)
return Position();
NodeImpl *block = startNode->enclosingBlockFlowOrTableElement();
Position lastVisible;
PositionIterator it(start);
for (; !it.atEnd(); it.next()) {
NodeImpl *currentNode = it.current().node();
if (stayInBlock) {
NodeImpl *currentBlock = currentNode->enclosingBlockFlowOrTableElement();
if (block != currentBlock)
return it.previous();
}
RenderObject *renderer = currentNode->renderer();
if (!renderer)
continue;
if (renderer->style()->visibility() != VISIBLE)
continue;
lastVisible = it.current();
if (currentNode != startNode && renderer->isBlockFlow()) {
if (it.current().offset() == 0) {
// If no first child, or first visible child is a not a block, return; otherwise continue.
if (!currentNode->firstChild())
return Position(currentNode, 0);
for (NodeImpl *child = currentNode->firstChild(); child; child = child->nextSibling()) {
RenderObject *r = child->renderer();
if (r && r->style()->visibility() == VISIBLE) {
if (r->isBlockFlow())
break; // break causes continue code below to run.
else
return Position(child, 0);
}
}
continue;
}
}
if (renderer->isReplaced() || renderer->isBR()) {
if (it.current().offset() <= renderer->caretMinOffset())
return Position(currentNode, renderer->caretMinOffset());
else
continue;
}
if (renderer->isText() && static_cast<RenderText *>(renderer)->firstTextBox()) {
if (currentNode != start.node())
return Position(currentNode, renderer->caretMinOffset());
if (it.current().offset() < 0)
continue;
uint textOffset = it.current().offset();
RenderText *textRenderer = static_cast<RenderText *>(renderer);
for (InlineTextBox *box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -