Bug 788369. Allow passing strings to the XHR constructor, since CoffeeScript seems to want to do it. r=peterv

This commit is contained in:
Boris Zbarsky 2012-09-07 11:07:12 -04:00
parent 32f516b2f5
commit 41c2bbabd7
5 changed files with 69 additions and 2 deletions

View File

@ -146,7 +146,7 @@ public:
return GetOwner();
}
// The WebIDL constructor.
// The WebIDL constructors.
static already_AddRefed<nsXMLHttpRequest>
Constructor(JSContext* aCx,
nsISupports* aGlobal,
@ -166,6 +166,22 @@ public:
return req.forget();
}
static already_AddRefed<nsXMLHttpRequest>
Constructor(JSContext* aCx,
nsISupports* aGlobal,
const nsAString& ignored,
ErrorResult& aRv)
{
// Pretend like someone passed null, so we can pick up the default values
mozilla::dom::MozXMLHttpRequestParameters params;
if (!params.Init(aCx, JS::NullValue())) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
return Constructor(aCx, aGlobal, params, aRv);
}
void Construct(nsIPrincipal* aPrincipal,
nsPIDOMWindow* aOwnerWindow,
nsIURI* aBaseURI = NULL)

View File

@ -61,6 +61,7 @@ MOCHITEST_FILES := \
forOf_iframe.html \
test_sequence_wrapping.html \
file_bug775543.html \
test_bug788369.html \
$(NULL)
MOCHITEST_CHROME_FILES = \

View File

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=788369
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 788369</title>
<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=788369">Mozilla Bug 788369</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 788369 **/
try {
var xhr = new(window.ActiveXObject || XMLHttpRequest)("Microsoft.XMLHTTP");
ok(xhr instanceof XMLHttpRequest, "Should have an XHR object");
} catch (e) {
ok(false, "Should not throw exception when constructing: " + e);
}
</script>
</pre>
</body>
</html>

View File

@ -51,7 +51,12 @@ dictionary MozXMLHttpRequestParameters
boolean mozSystem = false;
};
[Constructor(optional MozXMLHttpRequestParameters params)]
[Constructor(optional MozXMLHttpRequestParameters params),
// There are apparently callers, specifically CoffeeScript, who do
// things like this:
// c = new(window.ActiveXObject || XMLHttpRequest)("Microsoft.XMLHTTP")
// To handle that, we need a constructor that takes a string.
Constructor(DOMString ignored)]
interface XMLHttpRequest : XMLHttpRequestEventTarget {
// event handler
[TreatNonCallableAsNull, SetterThrows, GetterThrows=Workers]

View File

@ -74,6 +74,21 @@ public:
Constructor(JSContext* aCx, JSObject* aGlobal,
const MozXMLHttpRequestParametersWorkers& aParams,
ErrorResult& aRv);
static XMLHttpRequest*
Constructor(JSContext* aCx, JSObject* aGlobal,
const nsAString& ignored, ErrorResult& aRv)
{
// Pretend like someone passed null, so we can pick up the default values
MozXMLHttpRequestParametersWorkers params;
if (!params.Init(aCx, JS::NullValue())) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
return Constructor(aCx, aGlobal, params, aRv);
}
void
Unpin();