Bug 366944 - Range.surroundContents should throw InvalidNodeTypeError early; r=smaug

This commit is contained in:
Aryeh Gregor 2012-03-21 14:06:50 -04:00
Родитель 3bd6379e4d
Коммит 98c50b2bfb
3 изменённых файлов: 62 добавлений и 1 удалений

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

@ -2171,11 +2171,22 @@ nsRange::SurroundContents(nsIDOMNode* aNewParent)
NS_ERROR_DOM_INVALID_STATE_ERR);
}
// INVALID_NODE_TYPE_ERROR if aNewParent is something that can't be inserted
// (Document, DocumentType, DocumentFragment)
PRUint16 nodeType;
nsresult res = aNewParent->GetNodeType(&nodeType);
if (NS_FAILED(res)) return res;
if (nodeType == nsIDOMNode::DOCUMENT_NODE ||
nodeType == nsIDOMNode::DOCUMENT_TYPE_NODE ||
nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
}
// Extract the contents within the range.
nsCOMPtr<nsIDOMDocumentFragment> docFrag;
nsresult res = ExtractContents(getter_AddRefs(docFrag));
res = ExtractContents(getter_AddRefs(docFrag));
if (NS_FAILED(res)) return res;
if (!docFrag) return NS_ERROR_FAILURE;

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

@ -571,6 +571,7 @@ _TEST_FILES2 = \
test_bug737565.html \
test_bug737612.html \
test_bug738108.html \
test_bug366944.html \
$(NULL)
_CHROME_FILES = \

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

@ -0,0 +1,49 @@
<!doctype html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=366944
-->
<title>Test for Bug 366944</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=366944">Mozilla Bug 366944</a>
<script>
/** Test for Bug 366944 **/
var testNodes = [document, document.doctype, document.createDocumentFragment()];
for (var i = 0; i < testNodes.length; i++) {
var range = document.createRange();
// If a non-Text node is partially contained, we expect to throw for that
// first
range.setStart(document.head, 0);
range.setEnd(document.body, 0);
var threw = false;
var desc = " (surrounding a range with partially-contained Element "
+ "with " + (i == 0 ? "document" : i == 1 ? "doctype" : "docfrag") + ")";
try {
range.surroundContents(testNodes[i]);
} catch(e) {
threw = true;
is(Object.getPrototypeOf(e), DOMException.prototype,
"Must throw DOMException" + desc);
is(e.name, "InvalidStateError", "Must throw InvalidStateError" + desc);
}
ok(threw, "Must throw" + desc);
range.setStart(document.body, 0);
range.setEnd(document.body, 1);
threw = false;
desc = " (surrounding a regular range "
+ "with " + (i == 0 ? "document" : i == 1 ? "doctype" : "docfrag") + ")";
try {
range.surroundContents(testNodes[i]);
} catch(e) {
threw = true;
is(Object.getPrototypeOf(e), DOMException.prototype,
"Must throw DOMException" + desc);
is(e.name, "InvalidNodeTypeError",
"Must throw InvalidNodeTypeError" + desc);
}
ok(threw, "Must throw" + desc);
}
</script>