?? visible_units.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 "visible_units.h"
#include <qstring.h>
#include "htmltags.h"
#include "misc/helper.h"
#include "rendering/render_text.h"
#include "rendering/render_block.h"
#include "visible_position.h"
#include "visible_text.h"
#include "xml/dom_docimpl.h"
#include "xml/dom_elementimpl.h"
using DOM::DocumentImpl;
using DOM::ElementImpl;
using DOM::NodeImpl;
using DOM::Position;
using DOM::Range;
namespace khtml {
static VisiblePosition previousBoundary(const VisiblePosition &c, unsigned (*searchFunction)(const QChar *, unsigned))
{
Position pos = c.deepEquivalent();
NodeImpl *n = pos.node();
if (!n)
return VisiblePosition();
DocumentImpl *d = n->getDocument();
if (!d)
return VisiblePosition();
NodeImpl *de = d->documentElement();
if (!de)
return VisiblePosition();
NodeImpl *boundary = n->enclosingBlockFlowElement();
if (!boundary)
return VisiblePosition();
bool isContentEditable = boundary->isContentEditable();
while (boundary && boundary != de && boundary->parentNode() && isContentEditable == boundary->parentNode()->isContentEditable()) {
boundary = boundary->parentNode();
}
Range searchRange(d);
searchRange.setStartBefore(boundary);
Position end(pos.equivalentRangeCompliantPosition());
searchRange.setEnd(end.node(), end.offset());
SimplifiedBackwardsTextIterator it(searchRange);
QString string;
unsigned next = 0;
while (!it.atEnd() && it.length() > 0) {
// iterate to get chunks until the searchFunction returns a non-zero value.
string.prepend(it.characters(), it.length());
next = searchFunction(string.unicode(), string.length());
if (next != 0)
break;
it.advance();
}
if (it.atEnd() && next == 0) {
Range range(it.range());
pos = Position(range.startContainer().handle(), range.startOffset());
}
else if (!it.atEnd() && it.length() == 0) {
// Got a zero-length chunk.
// This means we have hit a replaced element.
// Make a check to see if the position should be before or after the replaced element
// by performing an additional check with a modified string which uses an "X"
// character to stand in for the replaced element.
QChar chars[2];
chars[0] = 'X';
chars[1] = ' ';
string.prepend(chars, 2);
unsigned pastImage = searchFunction(string.unicode(), string.length());
Range range(it.range());
if (pastImage == 0)
pos = Position(range.startContainer().handle(), range.startOffset());
else
pos = Position(range.endContainer().handle(), range.endOffset());
}
else if (next != 0) {
// The simpler iterator used in this function, as compared to the one used in
// nextWordPosition(), gives us results we can use directly without having to
// iterate again to translate the next value into a DOM position.
NodeImpl *node = it.range().startContainer().handle();
if (node->isTextNode() || (node->renderer() && node->renderer()->isBR())) {
// The next variable contains a usable index into a text node
pos = Position(node, next);
}
else {
// If we are not in a text node, we ended on a node boundary, so the
// range start offset should be used.
pos = Position(node, it.range().startOffset());
}
}
return VisiblePosition(pos, DOWNSTREAM, VisiblePosition::INIT_DOWN);
}
static VisiblePosition nextBoundary(const VisiblePosition &c, unsigned (*searchFunction)(const QChar *, unsigned))
{
Position pos = c.deepEquivalent();
NodeImpl *n = pos.node();
if (!n)
return VisiblePosition();
DocumentImpl *d = n->getDocument();
if (!d)
return VisiblePosition();
NodeImpl *de = d->documentElement();
if (!de)
return VisiblePosition();
NodeImpl *boundary = n->enclosingBlockFlowElement();
if (!boundary)
return VisiblePosition();
bool isContentEditable = boundary->isContentEditable();
while (boundary && boundary != de && boundary->parentNode() && isContentEditable == boundary->parentNode()->isContentEditable()) {
boundary = boundary->parentNode();
}
Range searchRange(d);
Position start(pos.equivalentRangeCompliantPosition());
searchRange.setStart(start.node(), start.offset());
searchRange.setEndAfter(boundary);
TextIterator it(searchRange, RUNFINDER);
QString string;
unsigned next = 0;
while (!it.atEnd() && it.length() > 0) {
// Keep asking the iterator for chunks until the search function
// returns an end value not equal to the length of the string passed to it.
string.append(it.characters(), it.length());
next = searchFunction(string.unicode(), string.length());
if (next != string.length())
break;
it.advance();
}
if (it.atEnd() && next == string.length()) {
Range range(it.range());
pos = Position(range.startContainer().handle(), range.startOffset());
}
else if (!it.atEnd() && it.length() == 0) {
// Got a zero-length chunk.
// This means we have hit a replaced element.
// Make a check to see if the position should be before or after the replaced element
// by performing an additional check with a modified string which uses an "X"
// character to stand in for the replaced element.
QChar chars[2];
chars[0] = ' ';
chars[1] = 'X';
string.append(chars, 2);
unsigned pastImage = searchFunction(string.unicode(), string.length());
Range range(it.range());
if (next != pastImage)
pos = Position(range.endContainer().handle(), range.endOffset());
else
pos = Position(range.startContainer().handle(), range.startOffset());
}
else if (next != 0) {
// Use the character iterator to translate the next value into a DOM position.
CharacterIterator charIt(searchRange);
charIt.advance(next - 1);
pos = Position(charIt.range().endContainer().handle(), charIt.range().endOffset());
}
return VisiblePosition(pos, UPSTREAM, VisiblePosition::INIT_UP);
}
// ---------
static unsigned startWordBoundary(const QChar *characters, unsigned length)
{
int start, end;
findWordBoundary(characters, length, length, &start, &end);
return start;
}
VisiblePosition startOfWord(const VisiblePosition &c, EWordSide side)
{
VisiblePosition p = c;
if (side == RightWordIfOnBoundary) {
// at paragraph end, the startofWord is the current position
if (isEndOfParagraph(c))
return c;
p = c.next();
if (p.isNull())
return c;
}
return previousBoundary(p, startWordBoundary);
}
static unsigned endWordBoundary(const QChar *characters, unsigned length)
{
int start, end;
findWordBoundary(characters, length, 0, &start, &end);
return end;
}
VisiblePosition endOfWord(const VisiblePosition &c, EWordSide side)
{
VisiblePosition p = c;
if (side == LeftWordIfOnBoundary) {
if (isStartOfParagraph(c))
return c;
p = c.previous();
if (p.isNull())
return c;
} else {
// at paragraph end, the endOfWord is the start of next paragraph
if (isEndOfParagraph(c)) {
p = c.next();
return p.isNotNull() ? p : c;
}
}
return nextBoundary(p, endWordBoundary);
}
static unsigned previousWordPositionBoundary(const QChar *characters, unsigned length)
{
return nextWordFromIndex(characters, length, length, false);
}
VisiblePosition previousWordPosition(const VisiblePosition &c)
{
return previousBoundary(c, previousWordPositionBoundary);
}
static unsigned nextWordPositionBoundary(const QChar *characters, unsigned length)
{
return nextWordFromIndex(characters, length, 0, true);
}
VisiblePosition nextWordPosition(const VisiblePosition &c)
{
return nextBoundary(c, nextWordPositionBoundary);
}
// ---------
static RootInlineBox *rootBoxForLine(const VisiblePosition &c)
{
Position p = c.deepEquivalent();
NodeImpl *node = p.node();
if (!node)
return 0;
RenderObject *renderer = node->renderer();
if (!renderer)
return 0;
InlineBox *box = renderer->inlineBox(p.offset(), c.affinity());
if (!box)
return 0;
return box->root();
}
VisiblePosition startOfLine(const VisiblePosition &c)
{
RootInlineBox *rootBox = rootBoxForLine(c);
if (!rootBox)
return VisiblePosition();
InlineBox *startBox = rootBox->firstLeafChild();
if (!startBox)
return VisiblePosition();
RenderObject *startRenderer = startBox->object();
if (!startRenderer)
return VisiblePosition();
NodeImpl *startNode = startRenderer->element();
if (!startNode)
return VisiblePosition();
long startOffset = 0;
if (startBox->isInlineTextBox()) {
InlineTextBox *startTextBox = static_cast<InlineTextBox *>(startBox);
startOffset = startTextBox->m_start;
}
return VisiblePosition(startNode, startOffset, DOWNSTREAM);
}
VisiblePosition endOfLine(const VisiblePosition &c, EIncludeLineBreak includeLineBreak)
{
// FIXME: Need to implement the "include line break" version.
assert(includeLineBreak == DoNotIncludeLineBreak);
RootInlineBox *rootBox = rootBoxForLine(c);
if (!rootBox)
return VisiblePosition();
InlineBox *endBox = rootBox->lastLeafChild();
if (!endBox)
return VisiblePosition();
RenderObject *endRenderer = endBox->object();
if (!endRenderer)
return VisiblePosition();
NodeImpl *endNode = endRenderer->element();
if (!endNode)
return VisiblePosition();
long endOffset = 1;
if (endNode->id() == ID_BR) {
endOffset = 0;
} else if (endBox->isInlineTextBox()) {
InlineTextBox *endTextBox = static_cast<InlineTextBox *>(endBox);
endOffset = endTextBox->m_start + endTextBox->m_len;
}
// generate VisiblePosition with correct affinity
VisiblePosition result = VisiblePosition(endNode, endOffset, DOWNSTREAM);
VisiblePosition temp = result;
temp.setAffinity(UPSTREAM);
if (visiblePositionsOnDifferentLines(temp, result))
result.setAffinity(UPSTREAM);
return result;
}
bool inSameLine(const VisiblePosition &a, const VisiblePosition &b)
{
return a.isNotNull() && startOfLine(a) == startOfLine(b);
}
bool isStartOfLine(const VisiblePosition &p)
{
return p.isNotNull() && p == startOfLine(p);
}
bool isEndOfLine(const VisiblePosition &p)
{
return p.isNotNull() && p == endOfLine(p, DoNotIncludeLineBreak);
}
VisiblePosition previousLinePosition(const VisiblePosition &c, int x)
{
Position p = c.affinity() == UPSTREAM ? c.deepEquivalent() : c.downstreamDeepEquivalent();
NodeImpl *node = p.node();
if (!node || !node->getDocument())
return VisiblePosition();
node->getDocument()->updateLayout();
RenderObject *renderer = node->renderer();
if (!renderer)
return VisiblePosition();
RenderBlock *containingBlock = 0;
RootInlineBox *root = 0;
InlineBox *box = renderer->inlineBox(p.offset(), c.affinity());
if (box) {
root = box->root()->prevRootBox();
if (root)
containingBlock = renderer->containingBlock();
}
if (!root) {
// This containing editable block does not have a previous line.
// Need to move back to previous containing editable block in this root editable
// block and find the last root line box in that block.
NodeImpl *startBlock = node->enclosingBlockFlowElement();
NodeImpl *n = node->previousEditable();
while (n && startBlock == n->enclosingBlockFlowElement())
n = n->previousEditable();
while (n) {
if (!n->inSameRootEditableElement(node))
break;
Position pos(n, n->caretMinOffset());
if (pos.inRenderedContent()) {
assert(n->renderer());
box = n->renderer()->inlineBox(n->caretMaxOffset());
if (box) {
// previous root line box found
root = box->root();
containingBlock = n->renderer()->containingBlock();
break;
}
// Work around <rdar://problem/4040763>. Need to
// doublecheck that pos is really on a different line,
// because it might not be a viable VisiblePosition
// (whereupon the next VP is returned, which could be
// back on c's line)
VisiblePosition c2 = VisiblePosition(pos, DOWNSTREAM);
if (visiblePositionsOnDifferentLines(c, c2))
return c2;
}
n = n->previousEditable();
}
}
if (root) {
int absx, absy;
containingBlock->absolutePosition(absx, absy);
RenderObject *renderer = root->closestLeafChildForXPos(x, absx)->object();
return renderer->positionForCoordinates(x, absy + root->topOverflow());
}
// Could not find a previous line. This means we must already be on the first line.
// Move to the start of the content in this block, which effectively moves us
// to the start of the line we're on.
return VisiblePosition(node->rootEditableElement(), 0, DOWNSTREAM);
}
VisiblePosition nextLinePosition(const VisiblePosition &c, int x)
{
Position p = c.affinity() == UPSTREAM ? c.deepEquivalent() : c.downstreamDeepEquivalent();
NodeImpl *node = p.node();
if (!node || !node->getDocument())
return VisiblePosition();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -