Bug 708620 - Whine to console when a form is submitted in an encoding that cannot represent all of Unicode. r=smaug.
authorHenri Sivonen <hsivonen@iki.fi>
Mon, 02 Jan 2012 16:18:30 +0200
changeset 84883 f4f47800d2ff0b6736979f117d78bf13276f161d
parent 84882 f50dc1ff3794dbc313133276dbed60482db9742e
child 84884 06df9b7d4a693292b28e0957dc3416f48e450663
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs708620
milestone12.0a1
Bug 708620 - Whine to console when a form is submitted in an encoding that cannot represent all of Unicode. r=smaug.
content/base/test/Makefile.in
content/base/test/file_bug708620-2.html
content/base/test/file_bug708620.html
content/base/test/test_bug708620.html
content/html/content/src/nsFormSubmission.cpp
dom/locales/en-US/chrome/layout/HtmlForm.properties
--- a/content/base/test/Makefile.in
+++ b/content/base/test/Makefile.in
@@ -535,16 +535,19 @@ include $(topsrcdir)/config/rules.mk
 		test_bug702439.html \
 		test_bug702439.html^headers^ \
 		file_bug702439.html \
 		test_bug707142.html \
 		file_bug707142_baseline.json \
 		file_bug707142_bom.json \
 		file_bug707142_utf-16.json \
 		test_reentrant_flush.html \
+		test_bug708620.html \
+		file_bug708620.html \
+		file_bug708620-2.html \
 		$(NULL)
 
 _CHROME_FILES =	\
 		test_bug357450.js \
 		$(NULL)
 
 # This test fails on the Mac for some reason
 ifneq (,$(filter gtk2 windows,$(MOZ_WIDGET_TOOLKIT)))
new file mode 100644
--- /dev/null
+++ b/content/base/test/file_bug708620-2.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<title>Non-UTF form target</title>
+<body onload="parent.finish();">
+
new file mode 100644
--- /dev/null
+++ b/content/base/test/file_bug708620.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<meta charset=windows-1252>
+<title>Non-UTF form</title>
+<body onload="document.forms[0].submit();">
+<form action="file_bug708620-2.html">
+<input name=foo value=bar>
+</form>
+
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug708620.html
@@ -0,0 +1,54 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=708620
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 708620</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body onload="start();">
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=708620">Mozilla Bug 708620</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+  <iframe></iframe>
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 708620 **/
+
+SimpleTest.waitForExplicitFinish();
+
+var tests = [
+  "file_bug708620.html"
+];
+
+var expectedErrors = [
+  '[JavaScript Warning: "A form was submitted in the windows-1252 encoding which cannot encode all Unicode characters, so user input may get corrupted. To avoid this problem, the page should be changed so that the form is submitted in the UTF-8 encoding either by changing the encoding of the page itself to UTF-8 or by specifying accept-charset=utf-8 on the form element."]'
+];
+
+function consoleError(msg) {
+  var expected = expectedErrors.shift();
+  is(msg, expected, "Not the right error message");
+}
+
+SpecialPowers.addErrorConsoleListener(consoleError);
+
+function start() {
+  var url = tests.shift();
+  document.getElementsByTagName("iframe")[0].src = url;
+}
+
+function finish() {
+    is(expectedErrors.length, 0, "The error supply was not exhausted");
+    SpecialPowers.removeErrorConsoleListener(consoleError);
+    SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>
--- a/content/html/content/src/nsFormSubmission.cpp
+++ b/content/html/content/src/nsFormSubmission.cpp
@@ -719,16 +719,27 @@ nsEncodingFormSubmission::nsEncodingForm
   }
 
   // use UTF-8 for UTF-16* (per WHATWG and existing practice of
   // MS IE/Opera). 
   if (StringBeginsWith(charset, NS_LITERAL_CSTRING("UTF-16"))) {
     charset.AssignLiteral("UTF-8");
   }
 
+  if (!(charset.EqualsLiteral("UTF-8") || charset.EqualsLiteral("gb18030"))) {
+    nsAutoString charsetUtf16;
+    CopyUTF8toUTF16(charset, charsetUtf16);
+    const PRUnichar* charsetPtr = charsetUtf16.get();
+    SendJSWarning(aOriginatingElement ? aOriginatingElement->GetOwnerDocument()
+                                      : nsnull,
+                  "CannotEncodeAllUnicode",
+                  &charsetPtr,
+                  1);
+  }
+
   mEncoder = do_CreateInstance(NS_SAVEASCHARSET_CONTRACTID);
   if (mEncoder) {
     nsresult rv =
       mEncoder->Init(charset.get(),
                      (nsISaveAsCharset::attr_EntityAfterCharsetConv + 
                       nsISaveAsCharset::attr_FallbackDecimalNCR),
                      0);
     if (NS_FAILED(rv)) {
--- a/dom/locales/en-US/chrome/layout/HtmlForm.properties
+++ b/dom/locales/en-US/chrome/layout/HtmlForm.properties
@@ -43,8 +43,9 @@ MediaUpload=Media Upload
 # should be a space (U+0020) in most locales. The prompt is followed by an 
 # input field. The space needs be escaped in the property file to avoid 
 # trimming.
 IsIndexPromptWithSpace=This is a searchable index. Enter search keywords:\u0020
 ForgotPostWarning=Form contains enctype=%S, but does not contain method=post.  Submitting normally with method=GET and no enctype instead.
 ForgotFileEnctypeWarning=Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form.  The file will not be sent.
 # LOCALIZATION NOTE (DefaultFormSubject): %S will be replaced with brandShortName
 DefaultFormSubject=Form Post from %S
+CannotEncodeAllUnicode=A form was submitted in the %S encoding which cannot encode all Unicode characters, so user input may get corrupted. To avoid this problem, the page should be changed so that the form is submitted in the UTF-8 encoding either by changing the encoding of the page itself to UTF-8 or by specifying accept-charset=utf-8 on the form element.