From 3ceab085167a6be9cf7dd124972b613d16d05b75 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 16 Oct 2013 16:04:26 +0200 Subject: [PATCH] Bug 926890 - Throw JavaScript exceptions for URL, r=ehsan --- dom/base/URL.cpp | 24 ++++++++-- dom/base/test/mochitest.ini | 1 + dom/base/test/test_urlExceptions.html | 57 +++++++++++++++++++++++ dom/workers/test/mochitest.ini | 2 + dom/workers/test/test_url_exceptions.html | 44 +++++++++++++++++ dom/workers/test/url_exceptions_worker.js | 38 +++++++++++++++ 6 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 dom/base/test/test_urlExceptions.html create mode 100644 dom/workers/test/test_url_exceptions.html create mode 100644 dom/workers/test/url_exceptions_worker.js diff --git a/dom/base/URL.cpp b/dom/base/URL.cpp index 998629999d0..e7f03565e06 100644 --- a/dom/base/URL.cpp +++ b/dom/base/URL.cpp @@ -46,7 +46,7 @@ URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl, rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aUrl), nullptr, aBase.GetURI(), getter_AddRefs(uri)); if (NS_FAILED(rv)) { - aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); + aRv.Throw(NS_ERROR_DOM_TYPE_ERR); return nullptr; } @@ -69,7 +69,7 @@ URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl, rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aBase), nullptr, nullptr, getter_AddRefs(baseUri)); if (NS_FAILED(rv)) { - aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); + aRv.Throw(NS_ERROR_DOM_TYPE_ERR); return nullptr; } @@ -77,7 +77,7 @@ URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl, rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aUrl), nullptr, baseUri, getter_AddRefs(uri)); if (NS_FAILED(rv)) { - aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); + aRv.Throw(NS_ERROR_DOM_TYPE_ERR); return nullptr; } @@ -191,7 +191,23 @@ URL::GetHref(nsString& aHref) const void URL::SetHref(const nsAString& aHref, ErrorResult& aRv) { - aRv = mURI->SetSpec(NS_ConvertUTF16toUTF8(aHref)); + nsCString href = NS_ConvertUTF16toUTF8(aHref); + + nsresult rv; + nsCOMPtr ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv)); + if (NS_FAILED(rv)) { + aRv.Throw(rv); + return; + } + + nsCOMPtr uri; + rv = ioService->NewURI(href, nullptr, nullptr, getter_AddRefs(uri)); + if (NS_FAILED(rv)) { + aRv.Throw(NS_ERROR_DOM_TYPE_ERR); + return; + } + + aRv = mURI->SetSpec(href); } void diff --git a/dom/base/test/mochitest.ini b/dom/base/test/mochitest.ini index 38fb86b53e6..a768585c90f 100644 --- a/dom/base/test/mochitest.ini +++ b/dom/base/test/mochitest.ini @@ -35,3 +35,4 @@ support-files = [test_window_extensible.html] [test_window_indexing.html] [test_writable-replaceable.html] +[test_urlExceptions.html] diff --git a/dom/base/test/test_urlExceptions.html b/dom/base/test/test_urlExceptions.html new file mode 100644 index 00000000000..7df3e9ddd28 --- /dev/null +++ b/dom/base/test/test_urlExceptions.html @@ -0,0 +1,57 @@ + + + + + + Test for Bug 926890 + + + + +Mozilla Bug 926890 +

+ +
+
+ + + + diff --git a/dom/workers/test/mochitest.ini b/dom/workers/test/mochitest.ini index 817557c47a9..ce42a9cb05d 100644 --- a/dom/workers/test/mochitest.ini +++ b/dom/workers/test/mochitest.ini @@ -53,6 +53,7 @@ support-files = xhrAbort_worker.js xhr_implicit_cancel_worker.js xhr_worker.js + url_exceptions_worker.js [test_404.html] [test_atob.html] @@ -107,3 +108,4 @@ support-files = [test_xhr_parameters.js] [test_xhr_system.html] [test_xhr_system.js] +[test_url_exceptions.html] diff --git a/dom/workers/test/test_url_exceptions.html b/dom/workers/test/test_url_exceptions.html new file mode 100644 index 00000000000..93ef0f2311b --- /dev/null +++ b/dom/workers/test/test_url_exceptions.html @@ -0,0 +1,44 @@ + + + + + Test for URL exceptions in workers + + + + +

+ +

+
+
+
+
+
+
diff --git a/dom/workers/test/url_exceptions_worker.js b/dom/workers/test/url_exceptions_worker.js
new file mode 100644
index 00000000000..caefc22021e
--- /dev/null
+++ b/dom/workers/test/url_exceptions_worker.js
@@ -0,0 +1,38 @@
+function ok(a, msg) {
+  postMessage({type: 'status', status: !!a, msg: msg });
+}
+
+onmessage = function(event) {
+  // URL.href throws
+  var url = new URL('http://www.example.com');
+  ok(url, "URL created");
+
+  var status = false;
+  try {
+    url.href = '42';
+  } catch(e) {
+    status = true;
+  }
+  ok(status, "url.href = 42 should throw");
+
+  url.href = 'http://www.example.org';
+  ok(true, "url.href should not throw");
+
+  status = false
+  try {
+    new URL('42');
+  } catch(e) {
+    status = true;
+  }
+  ok(status, "new URL(42) should throw");
+
+  status = false
+  try {
+    new URL('http://www.example.com', '42');
+  } catch(e) {
+    status = true;
+  }
+  ok(status, "new URL(something, 42) should throw");
+
+  postMessage({type: 'finish' });
+}