Bug 433662 part 2 - Make Range.insertNode insert the node into the range even when the range is collapsed

This commit is contained in:
Aryeh Gregor 2012-04-01 08:02:21 -04:00
Родитель b08a88fdd7
Коммит 8b74f084eb
4 изменённых файлов: 67 добавлений и 7 удалений

Просмотреть файл

@ -2116,6 +2116,7 @@ nsRange::InsertNode(nsIDOMNode* aNode)
nsCOMPtr<nsIDOMNode> referenceParentNode = tStartContainer;
nsCOMPtr<nsIDOMText> startTextNode(do_QueryInterface(tStartContainer));
nsCOMPtr<nsIDOMNodeList> tChildList;
if (startTextNode) {
res = tStartContainer->GetParentNode(getter_AddRefs(referenceParentNode));
NS_ENSURE_SUCCESS(res, res);
@ -2127,7 +2128,6 @@ nsRange::InsertNode(nsIDOMNode* aNode)
referenceNode = secondPart;
} else {
nsCOMPtr<nsIDOMNodeList>tChildList;
res = tStartContainer->GetChildNodes(getter_AddRefs(tChildList));
NS_ENSURE_SUCCESS(res, res);
@ -2135,9 +2135,37 @@ nsRange::InsertNode(nsIDOMNode* aNode)
res = tChildList->Item(tStartOffset, getter_AddRefs(referenceNode));
NS_ENSURE_SUCCESS(res, res);
}
// We might need to update the end to include the new node (bug 433662)
PRInt32 newOffset;
if (Collapsed()) {
if (referenceNode) {
newOffset = IndexOf(referenceNode);
} else {
PRUint32 length;
res = tChildList->GetLength(&length);
NS_ENSURE_SUCCESS(res, res);
newOffset = length;
}
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
NS_ENSURE_STATE(node);
if (node->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
newOffset += node->GetChildCount();
} else {
newOffset++;
}
}
nsCOMPtr<nsIDOMNode> tResultNode;
return referenceParentNode->InsertBefore(aNode, referenceNode, getter_AddRefs(tResultNode));
res = referenceParentNode->InsertBefore(aNode, referenceNode, getter_AddRefs(tResultNode));
NS_ENSURE_SUCCESS(res, res);
if (Collapsed()) {
return SetEnd(referenceParentNode, newOffset);
}
return NS_OK;
}
NS_IMETHODIMP

Просмотреть файл

@ -577,6 +577,7 @@ _TEST_FILES2 = \
file_bug650386_report.sjs \
test_bug719533.html \
test_bug737087.html \
test_bug433662.html \
$(NULL)
_CHROME_FILES = \

Просмотреть файл

@ -0,0 +1,31 @@
<!doctype html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=433662
-->
<title>Test for Bug 433662</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=433662">Mozilla Bug 433662</a>
<script>
/** Test for Bug 433662 **/
var range = document.createRange();
range.setStart(document.body, 0);
range.insertNode(document.createComment("abc"));
is(document.body.firstChild.nodeType, Node.COMMENT_NODE,
"Comment must be inserted (start of node)");
is(document.body.firstChild.nodeValue, "abc",
"Comment must have right contents (start of node)");
is(range.endOffset, 1,
"insertNode() needs to include the newly-added node (start of node)");
range.setStart(document.body, document.body.childNodes.length);
range.insertNode(document.createComment("def"));
is(document.body.lastChild.nodeType, Node.COMMENT_NODE,
"Comment must be inserted (end of node)");
is(document.body.lastChild.nodeValue, "def",
"Comment must have right contents (end of node)");
is(range.endOffset, document.body.childNodes.length,
"insertNode() needs to include the newly-added node (end of node)");
</script>

Просмотреть файл

@ -77,12 +77,12 @@ function testInsertNode(node) {
p = create('g<b>h</b>');
select(p.childNodes[0],0,p.childNodes[0],0);
insertClone(node);
check(p.childNodes[0],0,p.childNodes[0],0);
check(p.childNodes[0],0,p,2);
p = create('i<b>j</b>');
select(p.childNodes[0],1,p.childNodes[0],1);
insertClone(node);
check(p.childNodes[0],1,p.childNodes[0],1);
check(p.childNodes[0],1,p,2);
p = create('k<b>l</b>');
select(p.childNodes[0],0,p.childNodes[1].childNodes[0],0);
@ -137,12 +137,12 @@ function testInsertNode(node) {
p = create('<i>O</i><b>P</b>');
select(p,1,p,1);
insertClone(node);
check(p,1,p,1);
check(p,1,p,2);
p = create('<i>Q</i><b>R</b>');
select(p,2,p,2);
insertClone(node);
check(p,2,p,2);
check(p,2,p,3);
p = create('<i>S</i><b>T</b>');
select(p,1,p,1);