Commit bf826990 authored by ch.dumez@samsung.com's avatar ch.dumez@samsung.com

Have NodeWithIndex deal with references instead of pointers

Have NodeWithIndex deal with references instead of pointers. This makes the
code look safer.

R=adamk@chromium.org, adamk, rwlbuis

Review URL: https://codereview.chromium.org/176863009

git-svn-id: svn://svn.chromium.org/blink/trunk@168418 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 792377c0
...@@ -3667,7 +3667,7 @@ void Document::didRemoveText(Node* text, unsigned offset, unsigned length) ...@@ -3667,7 +3667,7 @@ void Document::didRemoveText(Node* text, unsigned offset, unsigned length)
m_markers->shiftMarkers(text, offset + length, 0 - length); m_markers->shiftMarkers(text, offset + length, 0 - length);
} }
void Document::didMergeTextNodes(Text* oldNode, unsigned offset) void Document::didMergeTextNodes(Text& oldNode, unsigned offset)
{ {
if (!m_ranges.isEmpty()) { if (!m_ranges.isEmpty()) {
NodeWithIndex oldNodeWithIndex(oldNode); NodeWithIndex oldNodeWithIndex(oldNode);
...@@ -3677,12 +3677,12 @@ void Document::didMergeTextNodes(Text* oldNode, unsigned offset) ...@@ -3677,12 +3677,12 @@ void Document::didMergeTextNodes(Text* oldNode, unsigned offset)
} }
if (m_frame) if (m_frame)
m_frame->selection().didMergeTextNodes(*oldNode, offset); m_frame->selection().didMergeTextNodes(oldNode, offset);
// FIXME: This should update markers for spelling and grammar checking. // FIXME: This should update markers for spelling and grammar checking.
} }
void Document::didSplitTextNode(Text* oldNode) void Document::didSplitTextNode(Text& oldNode)
{ {
if (!m_ranges.isEmpty()) { if (!m_ranges.isEmpty()) {
HashSet<Range*>::const_iterator end = m_ranges.end(); HashSet<Range*>::const_iterator end = m_ranges.end();
...@@ -3691,7 +3691,7 @@ void Document::didSplitTextNode(Text* oldNode) ...@@ -3691,7 +3691,7 @@ void Document::didSplitTextNode(Text* oldNode)
} }
if (m_frame) if (m_frame)
m_frame->selection().didSplitTextNode(*oldNode); m_frame->selection().didSplitTextNode(oldNode);
// FIXME: This should update markers for spelling and grammar checking. // FIXME: This should update markers for spelling and grammar checking.
} }
......
...@@ -666,8 +666,8 @@ public: ...@@ -666,8 +666,8 @@ public:
void didInsertText(Node*, unsigned offset, unsigned length); void didInsertText(Node*, unsigned offset, unsigned length);
void didRemoveText(Node*, unsigned offset, unsigned length); void didRemoveText(Node*, unsigned offset, unsigned length);
void didMergeTextNodes(Text* oldNode, unsigned offset); void didMergeTextNodes(Text& oldNode, unsigned offset);
void didSplitTextNode(Text* oldNode); void didSplitTextNode(Text& oldNode);
void clearDOMWindow() { m_domWindow = 0; } void clearDOMWindow() { m_domWindow = 0; }
DOMWindow* domWindow() const { return m_domWindow; } DOMWindow* domWindow() const { return m_domWindow; }
......
...@@ -34,27 +34,26 @@ namespace WebCore { ...@@ -34,27 +34,26 @@ namespace WebCore {
// only want to walk the child list to figure out the index once. // only want to walk the child list to figure out the index once.
class NodeWithIndex { class NodeWithIndex {
public: public:
explicit NodeWithIndex(Node* node) explicit NodeWithIndex(Node& node)
: m_node(node) : m_node(node)
, m_haveIndex(false) , m_haveIndex(false)
{ {
ASSERT(node);
} }
Node* node() const { return m_node; } Node& node() const { return m_node; }
int index() const int index() const
{ {
if (!m_haveIndex) { if (!m_haveIndex) {
m_index = m_node->nodeIndex(); m_index = m_node.nodeIndex();
m_haveIndex = true; m_haveIndex = true;
} }
ASSERT(m_index == static_cast<int>(m_node->nodeIndex())); ASSERT(m_index == static_cast<int>(m_node.nodeIndex()));
return m_index; return m_index;
} }
private: private:
Node* m_node; Node& m_node;
mutable bool m_haveIndex; mutable bool m_haveIndex;
mutable int m_index; mutable int m_index;
}; };
......
...@@ -1743,44 +1743,42 @@ void Range::didRemoveText(Node* text, unsigned offset, unsigned length) ...@@ -1743,44 +1743,42 @@ void Range::didRemoveText(Node* text, unsigned offset, unsigned length)
boundaryTextRemoved(m_end, text, offset, length); boundaryTextRemoved(m_end, text, offset, length);
} }
static inline void boundaryTextNodesMerged(RangeBoundaryPoint& boundary, NodeWithIndex& oldNode, unsigned offset) static inline void boundaryTextNodesMerged(RangeBoundaryPoint& boundary, const NodeWithIndex& oldNode, unsigned offset)
{ {
if (boundary.container() == oldNode.node()) if (boundary.container() == oldNode.node())
boundary.set(oldNode.node()->previousSibling(), boundary.offset() + offset, 0); boundary.set(oldNode.node().previousSibling(), boundary.offset() + offset, 0);
else if (boundary.container() == oldNode.node()->parentNode() && boundary.offset() == oldNode.index()) else if (boundary.container() == oldNode.node().parentNode() && boundary.offset() == oldNode.index())
boundary.set(oldNode.node()->previousSibling(), offset, 0); boundary.set(oldNode.node().previousSibling(), offset, 0);
} }
void Range::didMergeTextNodes(NodeWithIndex& oldNode, unsigned offset) void Range::didMergeTextNodes(const NodeWithIndex& oldNode, unsigned offset)
{ {
ASSERT(oldNode.node()); ASSERT(oldNode.node().document() == m_ownerDocument);
ASSERT(oldNode.node()->document() == m_ownerDocument); ASSERT(oldNode.node().parentNode());
ASSERT(oldNode.node()->parentNode()); ASSERT(oldNode.node().isTextNode());
ASSERT(oldNode.node()->isTextNode()); ASSERT(oldNode.node().previousSibling());
ASSERT(oldNode.node()->previousSibling()); ASSERT(oldNode.node().previousSibling()->isTextNode());
ASSERT(oldNode.node()->previousSibling()->isTextNode());
boundaryTextNodesMerged(m_start, oldNode, offset); boundaryTextNodesMerged(m_start, oldNode, offset);
boundaryTextNodesMerged(m_end, oldNode, offset); boundaryTextNodesMerged(m_end, oldNode, offset);
} }
static inline void boundaryTextNodeSplit(RangeBoundaryPoint& boundary, Text* oldNode) static inline void boundaryTextNodeSplit(RangeBoundaryPoint& boundary, Text& oldNode)
{ {
if (boundary.container() != oldNode) if (boundary.container() != oldNode)
return; return;
unsigned boundaryOffset = boundary.offset(); unsigned boundaryOffset = boundary.offset();
if (boundaryOffset <= oldNode->length()) if (boundaryOffset <= oldNode.length())
return; return;
boundary.set(oldNode->nextSibling(), boundaryOffset - oldNode->length(), 0); boundary.set(oldNode.nextSibling(), boundaryOffset - oldNode.length(), 0);
} }
void Range::didSplitTextNode(Text* oldNode) void Range::didSplitTextNode(Text& oldNode)
{ {
ASSERT(oldNode); ASSERT(oldNode.document() == m_ownerDocument);
ASSERT(oldNode->document() == m_ownerDocument); ASSERT(oldNode.parentNode());
ASSERT(oldNode->parentNode()); ASSERT(oldNode.isTextNode());
ASSERT(oldNode->isTextNode()); ASSERT(oldNode.nextSibling());
ASSERT(oldNode->nextSibling()); ASSERT(oldNode.nextSibling()->isTextNode());
ASSERT(oldNode->nextSibling()->isTextNode());
boundaryTextNodeSplit(m_start, oldNode); boundaryTextNodeSplit(m_start, oldNode);
boundaryTextNodeSplit(m_end, oldNode); boundaryTextNodeSplit(m_end, oldNode);
} }
......
...@@ -134,8 +134,8 @@ public: ...@@ -134,8 +134,8 @@ public:
void didInsertText(Node*, unsigned offset, unsigned length); void didInsertText(Node*, unsigned offset, unsigned length);
void didRemoveText(Node*, unsigned offset, unsigned length); void didRemoveText(Node*, unsigned offset, unsigned length);
void didMergeTextNodes(NodeWithIndex& oldNode, unsigned offset); void didMergeTextNodes(const NodeWithIndex& oldNode, unsigned offset);
void didSplitTextNode(Text* oldNode); void didSplitTextNode(Text& oldNode);
// Expand range to a unit (word or sentence or block or document) boundary. // Expand range to a unit (word or sentence or block or document) boundary.
// Please refer to https://bugs.webkit.org/show_bug.cgi?id=27632 comment #5 // Please refer to https://bugs.webkit.org/show_bug.cgi?id=27632 comment #5
......
...@@ -89,7 +89,7 @@ PassRefPtr<Node> Text::mergeNextSiblingNodesIfPossible() ...@@ -89,7 +89,7 @@ PassRefPtr<Node> Text::mergeNextSiblingNodesIfPossible()
nextText->setDataWithoutUpdate(emptyString()); nextText->setDataWithoutUpdate(emptyString());
nextText->updateTextRenderer(0, nextTextData.length()); nextText->updateTextRenderer(0, nextTextData.length());
document().didMergeTextNodes(nextText.get(), offset); document().didMergeTextNodes(*nextText, offset);
// Restore nextText for mutation event. // Restore nextText for mutation event.
nextText->setDataWithoutUpdate(nextTextData); nextText->setDataWithoutUpdate(nextTextData);
...@@ -128,7 +128,7 @@ PassRefPtr<Text> Text::splitText(unsigned offset, ExceptionState& exceptionState ...@@ -128,7 +128,7 @@ PassRefPtr<Text> Text::splitText(unsigned offset, ExceptionState& exceptionState
toRenderText(renderer())->setTextWithOffset(dataImpl(), 0, oldStr.length()); toRenderText(renderer())->setTextWithOffset(dataImpl(), 0, oldStr.length());
if (parentNode()) if (parentNode())
document().didSplitTextNode(this); document().didSplitTextNode(*this);
return newText.release(); return newText.release();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment