Bug 469020 - range.surroundContents throws a NS_ERROR_INVALID_POINTER, r+sr=sicking

This commit is contained in:
Olli Pettay 2008-12-13 22:09:29 +02:00
Родитель f4283494b8
Коммит e91dbb4af9
3 изменённых файлов: 132 добавлений и 2 удалений

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

@ -1305,7 +1305,7 @@ nsresult nsRange::CutContents(nsIDOMDocumentFragment** aFragment)
rv = charData->GetLength(&dataLength);
NS_ENSURE_SUCCESS(rv, rv);
if (dataLength > (PRUint32)startOffset)
if (dataLength >= (PRUint32)startOffset)
{
nsCOMPtr<nsIDOMCharacterData> cutNode;
rv = SplitDataNode(charData, startOffset, dataLength,
@ -1321,7 +1321,7 @@ nsresult nsRange::CutContents(nsIDOMDocumentFragment** aFragment)
{
// Delete or extract everything before endOffset.
if (endOffset > 0)
if (endOffset >= 0)
{
nsCOMPtr<nsIDOMCharacterData> cutNode;
/* The Range spec clearly states clones get cut and original nodes

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

@ -263,6 +263,7 @@ _TEST_FILES = test_bug5141.html \
test_elementTraversal.html \
test_w3element_traversal.html \
test_w3element_traversal.xhtml \
test_bug469020.html \
test_w3element_traversal_svg.html \
w3element_traversal.svg \
test_bug444322.html \

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

@ -0,0 +1,129 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=469020
-->
<head>
<title>Test for Bug 469020</title>
<script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=469020">Mozilla Bug 469020</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 469020 **/
var range = null;
var anchor = null;
function doRangeAnchor(elem, start, end) {
range = document.createRange();
range.setStart(elem.firstChild, start);
end = end < elem.lastChild.length ? end : elem.lastChild.length
range.setEnd(elem.lastChild, end);
anchor = document.createElement('a');
anchor.href = "javascript: void(0);";
range.surroundContents(anchor);
}
function undoRangeAnchor() {
var pnode = anchor.parentNode;
var range2 = document.createRange();
range2.selectNodeContents(anchor);
var contents = range2.extractContents();
pnode.replaceChild(contents,anchor);
}
function serializeNode(node) {
var s;
var isElem = false;
if (node.nodeName == "#text") {
if (node.nodeValue) {
s = node.nodeValue
} else {
s = "<#empty>"
}
} else {
isElem = true;
s = "<" + node.nodeName + ">";
}
for (var j = 0; j < node.childNodes.length; ++j) {
s += serializeNode(node.childNodes[j]);
}
if (isElem) {
s += "</" + node.nodeName + ">";
}
return s;
}
function runTest(elementID, start, end, expected1, expected2, expected3) {
var e = document.getElementById(elementID);
doRangeAnchor(e, start, end);
is(serializeNode(e), expected1, "Wrong range behavior!");
document.getElementById('log').textContent += serializeNode(e) + "\n";
undoRangeAnchor();
is(serializeNode(e), expected2, "Wrong range behavior!");
document.getElementById('log').textContent += serializeNode(e) + "\n";
doRangeAnchor(e, start, end);
is(serializeNode(e), expected3, "Wrong range behavior!");
document.getElementById('log').textContent += serializeNode(e) + "\n";
}
function runTests() {
runTest("test1", 0, 3,
"<P><#empty><A>http://www.<SPAN>mozilla.</SPAN>org</A><#empty></P>",
"<P><#empty>http://www.<SPAN>mozilla.</SPAN>org<#empty></P>",
"<P><#empty><A><#empty>http://www.<SPAN>mozilla.</SPAN>org<#empty></A><#empty></P>");
runTest("test2", 1, 3,
"<P>h<A>ttp://www.<SPAN>mozilla.</SPAN>org</A><#empty></P>",
"<P>http://www.<SPAN>mozilla.</SPAN>org<#empty></P>",
"<P>h<A><#empty>ttp://www.<SPAN>mozilla.</SPAN>org<#empty></A><#empty></P>");
runTest("test3", 0, 2,
"<P><#empty><A>http://www.<SPAN>mozilla.</SPAN>or</A>g</P>",
"<P><#empty>http://www.<SPAN>mozilla.</SPAN>org</P>",
"<P><#empty><A><#empty>http://www.<SPAN>mozilla.</SPAN>org</A><#empty></P>");
runTest("test4", 1, 2,
"<P>h<A>ttp://www.<SPAN>mozilla.</SPAN>or</A>g</P>",
"<P>http://www.<SPAN>mozilla.</SPAN>org</P>",
"<P>h<A><#empty>ttp://www.<SPAN>mozilla.</SPAN>org</A><#empty></P>");
runTest("test5", 11, 0,
"<P>http://www.<A><#empty><SPAN>mozilla.</SPAN><#empty></A>org</P>",
"<P>http://www.<#empty><SPAN>mozilla.</SPAN><#empty>org</P>",
"<P>http://www.<A><#empty><#empty><SPAN>mozilla.</SPAN><#empty><#empty></A>org</P>");
runTest("test6", 10, 1,
"<P>http://www<A>.<SPAN>mozilla.</SPAN>o</A>rg</P>",
"<P>http://www.<SPAN>mozilla.</SPAN>org</P>",
"<P>http://www<A><#empty>.<SPAN>mozilla.</SPAN>or</A>g</P>");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(runTests);
</script>
</pre>
<p id="test1">http://www.<span>mozilla.</span>org</p>
<p id="test2">http://www.<span>mozilla.</span>org</p>
<p id="test3">http://www.<span>mozilla.</span>org</p>
<p id="test4">http://www.<span>mozilla.</span>org</p>
<p id="test5">http://www.<span>mozilla.</span>org</p>
<p id="test6">http://www.<span>mozilla.</span>org</p>
<pre id="log">
</pre>
</body>
</html>