Diff
Modified: trunk/Source/WebCore/ChangeLog (89504 => 89505)
--- trunk/Source/WebCore/ChangeLog 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/ChangeLog 2011-06-23 00:45:26 UTC (rev 89505)
@@ -1,3 +1,33 @@
+2011-06-22 Ryosuke Niwa <rn...@webkit.org>
+
+ Reviewed by Darin Adler.
+
+ Add a Position constructor that takes (Text*, unsigned offset)
+ https://bugs.webkit.org/show_bug.cgi?id=63181
+
+ Added Position::Position(PassRefPtr<Text*>, unsigned offset) and deployed in a couple of places
+ by replacing the calls to the old constructor.
+
+ * dom/Position.cpp:
+ (WebCore::Position::Position): Added.
+ * dom/Position.h:
+ * editing/CompositeEditCommand.cpp:
+ (WebCore::CompositeEditCommand::replaceSelectedTextInNode): Calls new constructor; extracted
+ from InsertTextCommand::performTrivialReplace and ReplaceSelectionCommand::performTrivialReplace.
+ (WebCore::CompositeEditCommand::rebalanceWhitespaceOnTextSubstring): Calls new constructor
+ * editing/CompositeEditCommand.h:
+ * editing/InsertTextCommand.cpp:
+ (WebCore::InsertTextCommand::performTrivialReplace): Calls replaceSelectedTextInNode.
+ (WebCore::InsertTextCommand::input): Calls new constructor.
+ (WebCore::InsertTextCommand::insertTab): Use RefPtr instead of a raw pointer.
+ * editing/ReplaceSelectionCommand.cpp:
+ (WebCore::ReplaceSelectionCommand::performTrivialReplace): Calls replaceSelectedTextInNode.
+ * editing/visible_units.cpp:
+ (WebCore::startPositionForLine): Calls new constructor.
+ * rendering/RenderTextControl.cpp:
+ (WebCore::RenderTextControl::visiblePositionForIndex): Calls new constructor; calls endPosition
+ on Range instead of avoid manually constructing a VisiblePosition out of endContainer and endOffset.
+
2011-06-22 Adam Barth <aba...@webkit.org>
Reviewed by Darin Fisher.
Modified: trunk/Source/WebCore/dom/Position.cpp (89504 => 89505)
--- trunk/Source/WebCore/dom/Position.cpp 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/dom/Position.cpp 2011-06-23 00:45:26 UTC (rev 89505)
@@ -103,6 +103,15 @@
ASSERT(anchorType == PositionIsOffsetInAnchor);
}
+Position::Position(PassRefPtr<Text> textNode, unsigned offset)
+ : m_anchorNode(textNode)
+ , m_offset(static_cast<int>(offset))
+ , m_anchorType(PositionIsOffsetInAnchor)
+ , m_isLegacyEditingPosition(false)
+{
+ ASSERT(m_anchorNode);
+}
+
void Position::moveToPosition(PassRefPtr<Node> node, int offset)
{
ASSERT(!editingIgnoresContent(node.get()));
Modified: trunk/Source/WebCore/dom/Position.h (89504 => 89505)
--- trunk/Source/WebCore/dom/Position.h 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/dom/Position.h 2011-06-23 00:45:26 UTC (rev 89505)
@@ -42,6 +42,7 @@
class Node;
class Range;
class RenderObject;
+class Text;
enum PositionMoveType {
CodePoint, // Move by a single code point.
@@ -80,7 +81,10 @@
// For creating before/after positions:
Position(PassRefPtr<Node> anchorNode, AnchorType);
+ Position(PassRefPtr<Text> textNode, unsigned offset);
+
// For creating offset positions:
+ // FIXME: This constructor should eventually go away. See bug 63040.
Position(PassRefPtr<Node> anchorNode, int offset, AnchorType);
AnchorType anchorType() const { return static_cast<AnchorType>(m_anchorType); }
Modified: trunk/Source/WebCore/editing/CompositeEditCommand.cpp (89504 => 89505)
--- trunk/Source/WebCore/editing/CompositeEditCommand.cpp 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/editing/CompositeEditCommand.cpp 2011-06-23 00:45:26 UTC (rev 89505)
@@ -340,6 +340,19 @@
applyCommandToComposite(InsertIntoTextNodeCommand::create(node, offset, replacementText));
}
+Position CompositeEditCommand::replaceSelectedTextInNode(const String& text)
+{
+ Position start = endingSelection().start();
+ Position end = endingSelection().end();
+ if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode() || isTabSpanTextNode(start.containerNode()))
+ return Position();
+
+ RefPtr<Text> textNode = static_cast<Text*>(start.containerNode());
+ replaceTextInNode(textNode, start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
+
+ return Position(textNode.release(), start.offsetInContainerNode() + text.length());
+}
+
void CompositeEditCommand::replaceTextInNodePreservingMarkers(PassRefPtr<Text> prpNode, unsigned offset, unsigned count, const String& replacementText)
{
RefPtr<Text> node(prpNode);
@@ -479,8 +492,8 @@
if (!length)
return;
- VisiblePosition visibleUpstreamPos(Position(textNode, upstream, Position::PositionIsOffsetInAnchor));
- VisiblePosition visibleDownstreamPos(Position(textNode, downstream, Position::PositionIsOffsetInAnchor));
+ VisiblePosition visibleUpstreamPos(Position(textNode, upstream));
+ VisiblePosition visibleDownstreamPos(Position(textNode, downstream));
String string = text.substring(upstream, length);
String rebalancedString = stringWithRebalancedWhitespace(string,
Modified: trunk/Source/WebCore/editing/CompositeEditCommand.h (89504 => 89505)
--- trunk/Source/WebCore/editing/CompositeEditCommand.h 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/editing/CompositeEditCommand.h 2011-06-23 00:45:26 UTC (rev 89505)
@@ -82,6 +82,7 @@
void removeNodeAndPruneAncestors(PassRefPtr<Node>);
void prune(PassRefPtr<Node>);
void replaceTextInNode(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
+ Position replaceSelectedTextInNode(const String&);
void replaceTextInNodePreservingMarkers(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
Position positionOutsideTabSpan(const Position&);
void setNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute, const AtomicString& value);
Modified: trunk/Source/WebCore/editing/InsertTextCommand.cpp (89504 => 89505)
--- trunk/Source/WebCore/editing/InsertTextCommand.cpp 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/editing/InsertTextCommand.cpp 2011-06-23 00:45:26 UTC (rev 89505)
@@ -76,19 +76,12 @@
if (text.contains('\t') || text.contains(' ') || text.contains('\n'))
return false;
-
- Position start = endingSelection().start().parentAnchoredEquivalent();
- Position end = endingSelection().end().parentAnchoredEquivalent();
- ASSERT(start.anchorType() == Position::PositionIsOffsetInAnchor);
- ASSERT(end.anchorType() == Position::PositionIsOffsetInAnchor);
- if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode() || isTabSpanTextNode(start.containerNode()))
+ Position start = endingSelection().start();
+ Position endPosition = replaceSelectedTextInNode(text);
+ if (endPosition.isNull())
return false;
- replaceTextInNode(static_cast<Text*>(start.containerNode()), start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
-
- Position endPosition(start.containerNode(), start.offsetInContainerNode() + text.length(), Position::PositionIsOffsetInAnchor);
-
// We could have inserted a part of composed character sequence,
// so we are basically treating ending selection as a range to avoid validation.
// <http://bugs.webkit.org/show_bug.cgi?id=15781>
@@ -165,11 +158,11 @@
ASSERT(startPosition.containerNode()->isTextNode());
if (placeholder.isNotNull())
removePlaceholderAt(placeholder);
- Text* textNode = static_cast<Text*>(startPosition.containerNode());
+ RefPtr<Text> textNode = static_cast<Text*>(startPosition.containerNode());
const unsigned offset = startPosition.offsetInContainerNode();
insertTextIntoNode(textNode, offset, text);
- endPosition = Position(textNode, offset + text.length(), Position::PositionIsOffsetInAnchor);
+ endPosition = Position(textNode, offset + text.length());
if (whitespaceRebalance == RebalanceLeadingAndTrailingWhitespaces) {
// The insertion may require adjusting adjacent whitespace, if it is present.
@@ -211,8 +204,9 @@
// keep tabs coalesced in tab span
if (isTabSpanTextNode(node)) {
- insertTextIntoNode(static_cast<Text *>(node), offset, "\t");
- return Position(node, offset + 1, Position::PositionIsOffsetInAnchor);
+ RefPtr<Text> textNode = static_cast<Text*>(node);
+ insertTextIntoNode(textNode, offset, "\t");
+ return Position(textNode.release(), offset + 1);
}
// create new tab span
@@ -222,17 +216,17 @@
if (!node->isTextNode()) {
insertNodeAt(spanNode.get(), insertPos);
} else {
- Text *textNode = static_cast<Text *>(node);
- if (offset >= textNode->length()) {
- insertNodeAfter(spanNode.get(), textNode);
- } else {
+ RefPtr<Text> textNode = static_cast<Text*>(node);
+ if (offset >= textNode->length())
+ insertNodeAfter(spanNode, textNode.release());
+ else {
// split node to make room for the span
// NOTE: splitTextNode uses textNode for the
// second node in the split, so we need to
// insert the span before it.
if (offset > 0)
splitTextNode(textNode, offset);
- insertNodeBefore(spanNode, textNode);
+ insertNodeBefore(spanNode, textNode.release());
}
}
Modified: trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp (89504 => 89505)
--- trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp 2011-06-23 00:45:26 UTC (rev 89505)
@@ -1305,20 +1305,12 @@
Text* textNode = static_cast<Text*>(fragment.firstChild());
// Our fragment creation code handles tabs, spaces, and newlines, so we don't have to worry about those here.
- String text(textNode->data());
-
- Position start = endingSelection().start().parentAnchoredEquivalent();
- Position end = endingSelection().end().parentAnchoredEquivalent();
- ASSERT(start.anchorType() == Position::PositionIsOffsetInAnchor);
- ASSERT(end.anchorType() == Position::PositionIsOffsetInAnchor);
- if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode())
+ Position start = endingSelection().start();
+ Position end = replaceSelectedTextInNode(textNode->data());
+ if (end.isNull())
return false;
- replaceTextInNode(static_cast<Text*>(start.containerNode()), start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
-
- end = Position(start.containerNode(), start.offsetInContainerNode() + text.length(), Position::PositionIsOffsetInAnchor);
-
VisibleSelection selectionAfterReplace(m_selectReplacement ? start : end, end);
setEndingSelection(selectionAfterReplace);
Modified: trunk/Source/WebCore/editing/visible_units.cpp (89504 => 89505)
--- trunk/Source/WebCore/editing/visible_units.cpp 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/editing/visible_units.cpp 2011-06-23 00:45:26 UTC (rev 89505)
@@ -34,6 +34,7 @@
#include "RenderBlock.h"
#include "RenderLayer.h"
#include "RenderObject.h"
+#include "Text.h"
#include "TextBoundaries.h"
#include "TextBreakIterator.h"
#include "TextIterator.h"
@@ -386,7 +387,7 @@
startBox = startBox->nextLeafChild();
}
- VisiblePosition visPos = startNode->isTextNode() ? VisiblePosition(Position(startNode, static_cast<InlineTextBox *>(startBox)->start(), Position::PositionIsOffsetInAnchor), DOWNSTREAM)
+ VisiblePosition visPos = startNode->isTextNode() ? VisiblePosition(Position(static_cast<Text*>(startNode), static_cast<InlineTextBox*>(startBox)->start()), DOWNSTREAM)
: VisiblePosition(positionBeforeNode(startNode), DOWNSTREAM);
return positionAvoidingFirstPositionInTable(visPos);
}
Modified: trunk/Source/WebCore/rendering/RenderTextControl.cpp (89504 => 89505)
--- trunk/Source/WebCore/rendering/RenderTextControl.cpp 2011-06-23 00:39:10 UTC (rev 89504)
+++ trunk/Source/WebCore/rendering/RenderTextControl.cpp 2011-06-23 00:45:26 UTC (rev 89505)
@@ -304,18 +304,14 @@
VisiblePosition RenderTextControl::visiblePositionForIndex(int index) const
{
if (index <= 0)
- return VisiblePosition(Position(innerTextElement(), 0, Position::PositionIsOffsetInAnchor), DOWNSTREAM);
+ return VisiblePosition(firstPositionInNode(innerTextElement()), DOWNSTREAM);
ExceptionCode ec = 0;
RefPtr<Range> range = Range::create(document());
range->selectNodeContents(innerTextElement(), ec);
ASSERT(!ec);
CharacterIterator it(range.get());
it.advance(index - 1);
- Node* endContainer = it.range()->endContainer(ec);
- ASSERT(!ec);
- int endOffset = it.range()->endOffset(ec);
- ASSERT(!ec);
- return VisiblePosition(Position(endContainer, endOffset, Position::PositionIsOffsetInAnchor), UPSTREAM);
+ return VisiblePosition(it.range()->endPosition(), UPSTREAM);
}
int RenderTextControl::indexForVisiblePosition(HTMLElement* innerTextElement, const VisiblePosition& pos)