Make maxlength not affect initial values or scripted sets. Bug 345267, r+sr=mats, a=dbaron
authorbzbarsky@mit.edu
Tue, 21 Aug 2007 20:22:21 -0700
changeset 4889 c094f5843336de9a3475602cc29db4b4ef100398
parent 4888 7210da91ec4351e3173c52de6dbfd708add9e708
child 4890 b108ae5f1aacadf4c7e597a16093c2243a6ceccf
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs345267
milestone1.9a8pre
Make maxlength not affect initial values or scripted sets. Bug 345267, r+sr=mats, a=dbaron
layout/forms/Makefile.in
layout/forms/nsTextControlFrame.cpp
layout/forms/test/Makefile.in
layout/forms/test/test_bug345267.html
layout/reftests/bugs/345267-1-ref.html
layout/reftests/bugs/345267-1a.html
layout/reftests/bugs/345267-1b.html
layout/reftests/bugs/345267-1c.html
layout/reftests/bugs/345267-1d.html
layout/reftests/bugs/reftest.list
--- a/layout/forms/Makefile.in
+++ b/layout/forms/Makefile.in
@@ -37,16 +37,22 @@
 
 DEPTH		= ../..
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
+DIRS = $(NULL)
+
+ifdef MOZ_MOCHITEST
+DIRS		+= test
+endif
+
 MODULE		= layout
 LIBRARY_NAME	= gkforms_s
 LIBXUL_LIBRARY	= 1
 
 REQUIRES	= xpcom \
 		  string \
 		  locale \
 		  content \
--- a/layout/forms/nsTextControlFrame.cpp
+++ b/layout/forms/nsTextControlFrame.cpp
@@ -2691,24 +2691,29 @@ nsTextControlFrame::SetValue(const nsASt
       // restore flags
       PRUint32 flags, savedFlags;
       editor->GetFlags(&savedFlags);
       flags = savedFlags;
       flags &= ~(nsIPlaintextEditor::eEditorDisabledMask);
       flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
       editor->SetFlags(flags);
 
+      // Also don't enforce max-length here
+      PRInt32 savedMaxLength;
+      plaintextEditor->GetMaxTextLength(&savedMaxLength);
+      plaintextEditor->SetMaxTextLength(-1);
+
       if (currentValue.Length() < 1)
         editor->DeleteSelection(nsIEditor::eNone);
       else {
-        nsCOMPtr<nsIPlaintextEditor> textEditor = do_QueryInterface(editor);
-        if (textEditor)
-          textEditor->InsertText(currentValue);
+        if (plaintextEditor)
+          plaintextEditor->InsertText(currentValue);
       }
 
+      plaintextEditor->SetMaxTextLength(savedMaxLength);
       editor->SetFlags(savedFlags);
       if (selPriv)
         selPriv->EndBatchChanges();
 
       if (pushed) {
         JSContext* cx;
         stack->Pop(&cx);
         NS_ASSERTION(!cx, "Unexpected JSContext popped!");
new file mode 100644
--- /dev/null
+++ b/layout/forms/test/Makefile.in
@@ -0,0 +1,51 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Mozilla Foundation.
+# Portions created by the Initial Developer are Copyright (C) 2007
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either of the GNU General Public License Version 2 or later (the "GPL"),
+# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH		= ../../..
+topsrcdir	= @top_srcdir@
+srcdir		= @srcdir@
+VPATH		= @srcdir@
+relativesrcdir  = layout/forms/test
+
+include $(DEPTH)/config/autoconf.mk
+include $(topsrcdir)/config/rules.mk
+
+_TEST_FILES =	test_bug345267.html \
+		$(NULL)
+
+libs:: $(_TEST_FILES)
+	$(INSTALL) $^ $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/layout/forms/test/test_bug345267.html
@@ -0,0 +1,93 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=345267
+-->
+<head>
+  <title>Test for Bug 345267</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.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=345267">Mozilla Bug 345267</a>
+<p id="display">
+ <input id="d1" maxlength="3" value="abcde">
+ <input id="d2" maxlength="3">
+ <input id="d3" maxlength="3">
+ <input id="d4" value="abcdefghijk">
+ <input id="target" value="abcdefghijklm" maxlength="3">
+</p>
+<div id="content" style="display: none">
+ <input id="u1" maxlength="3" value="abcdef">
+ <input id="u2" maxlength="3">
+ <input id="u3" maxlength="3">
+ <input id="u4" value="abcdefghijkl">
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/** Test for Bug 345267 **/
+is($("d1").value, "abcde",
+   "Displayed initial value should not be truncated by maxlength");
+is($("u1").value, "abcdef",
+   "Undisplayed initial value should not be truncated by maxlength");
+
+$("d2").value = "abcdefg";
+is($("d2").value, "abcdefg",
+   "Displayed set value should not be truncated by maxlength");
+
+$("u2").value = "abcdefgh";
+is($("u2").value, "abcdefgh",
+   "Undisplayed set value should not be truncated by maxlength");
+
+$("d3").defaultValue = "abcdefghi";
+is($("d3").value, "abcdefghi",
+   "Displayed set defaultValue should not be truncated by maxlength");
+
+$("u3").defaultValue = "abcdefghij";
+is($("u3").value, "abcdefghij",
+   "Undisplayed set defaultValue should not be truncated by maxlength");
+
+$("d4").maxLength = "3";
+is($("d4").value, "abcdefghijk",
+   "Displayed: setting maxLength should not truncate existing value");
+
+$("u4").maxLength = "3";
+is($("u4").value, "abcdefghijkl",
+   "Undisplayed: setting maxLength should not truncate existing value");
+
+// Now start the editing tests
+is($("target").value, "abcdefghijklm", "Test starting state incorrect");
+$("target").focus();
+sendKey("back_space");
+is($("target").value, "abcdefghijkl", "Should only delete one char");
+sendKey("back_space");
+is($("target").value, "abcdefghijk", "Should only delete one char again");
+(function () {
+  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+  $("target").controllers.getControllerForCommand('cmd_undo')
+                         .doCommand('cmd_undo');
+})();
+is($("target").value, "abcdefghijklm",
+   "Should be able to undo deletion in the face of maxlength");
+sendString("nopq");
+is($("target").value, "abcdefghijklm",
+   "Typing should have no effect when already past maxlength");
+
+$("target").value = "";
+sendString("abcde");
+is($("target").value, "abc", "Typing should be limited by maxlength");
+
+$("target").value = "";
+sendString("ad");
+sendKey("left");
+sendString("bc")
+is($("target").value, "abd", "Typing should be limited by maxlength again");
+
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/layout/reftests/bugs/345267-1-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+<body>
+  <input size="20" value="abcde">
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/bugs/345267-1a.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+<body>
+  <input size="20" maxlength="2" value="abcde">
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/bugs/345267-1b.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<body>
+  <input size="20" maxlength="2">
+  <script>
+    document.body.offsetWidth;
+    document.getElementsByTagName("input")[0].value = "abcde";
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/bugs/345267-1c.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<body>
+  <input size="20" value="abcde">
+  <script>
+    document.body.offsetWidth;
+    document.getElementsByTagName("input")[0].maxLength = "2";
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/reftests/bugs/345267-1d.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<body>
+  <input size="20" maxlength="2">
+  <script>
+    document.body.offsetWidth;
+    document.getElementsByTagName("input")[0].setAttribute("value", "abcde");
+  </script>
+</body>
+</html>
--- a/layout/reftests/bugs/reftest.list
+++ b/layout/reftests/bugs/reftest.list
@@ -141,16 +141,20 @@ random == 328829-1.xhtml 328829-1-ref.xh
 == 332975-1.html 332975-1-ref.html
 == 335628-1.html 335628-1-ref.html
 != 335628-2.xul 335628-2-ref.xul
 == 336147-1.html 336147-1-ref.html
 fails-if(MOZ_WIDGET_TOOLKIT=="cocoa") == 336736-1.html 336736-1-ref.html # something wrong with subpixel horizontal alignment (tracked in bug 336736)
 == 339289-1.html 339289-1-ref.html
 == 341043-1a.html 341043-1-ref.html
 != 341043-1b.html 341043-1-ref.html
+== 345267-1a.html 345267-1-ref.html
+== 345267-1b.html 345267-1-ref.html
+== 345267-1c.html 345267-1-ref.html
+== 345267-1d.html 345267-1-ref.html
 == 346774-1a.html 346774-1-ref.html
 == 346774-1b.html 346774-1-ref.html
 == 346774-1c.html 346774-1-ref.html
 == 347912-1.html 347912-1-ref.html
 == 348516-1.html 348516-1-ref.html
 == 348516-2.html 348516-2-ref.html
 != 348516-2.html 348516-2-notref.html
 != 348516-3.html 348516-3-notref.html