2011-03-16  Naoki Takano  <takano.naoki@gmail.com>

        Reviewed by Ryosuke Niwa.

        Textarea maxlength doesn't account for newlines
        https://bugs.webkit.org/show_bug.cgi?id=54443

        * fast/forms/script-tests/textarea-maxlength.js:
        (createFocusedTextAreaWithMaxLength3): Added two tests to make sure consecutive insertbreaks
        work correctly for textarea maxlength.
        * fast/forms/textarea-maxlength-expected.txt: Added two test results.
2011-03-16  Naoki Takano  <takano.naoki@gmail.com>

        Reviewed by Ryosuke Niwa.

        Textarea maxlength doesn't account for newlines
        https://bugs.webkit.org/show_bug.cgi?id=54443

        When a user presses a return key, TypingCommand::insertLineBreak() is called.
        So before append a new line, check if we can add the new line.

        * editing/TypingCommand.cpp:
        (WebCore::canAppendNewLineFeed): Implement new helper function to check if we can add new line.
        (WebCore::TypingCommand::insertLineBreak): Added check logic before adding the new line.
        (WebCore::TypingCommand::insertParagraphSeparator): Added check logic before adding the new line.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@81328 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/editing/TypingCommand.cpp b/Source/WebCore/editing/TypingCommand.cpp
index 8437658..35604f6 100644
--- a/Source/WebCore/editing/TypingCommand.cpp
+++ b/Source/WebCore/editing/TypingCommand.cpp
@@ -47,6 +47,14 @@
 
 using namespace HTMLNames;
 
+static bool canAppendNewLineFeed(const VisibleSelection& selection)
+{
+    ExceptionCode ec = 0;
+    RefPtr<BeforeTextInsertedEvent> event = BeforeTextInsertedEvent::create(String("\n"));
+    selection.rootEditableElement()->dispatchEvent(event, ec);
+    return event->text().length();
+}
+
 TypingCommand::TypingCommand(Document *document, ETypingCommand commandType, const String &textToInsert, TypingCommandOptions options, TextGranularity granularity, TextCompositionType compositionType)
     : CompositeEditCommand(document)
     , m_commandType(commandType)
@@ -403,12 +411,18 @@
 
 void TypingCommand::insertLineBreak()
 {
+    if (!canAppendNewLineFeed(endingSelection()))
+        return;
+
     applyCommandToComposite(InsertLineBreakCommand::create(document()));
     typingAddedToOpenCommand(InsertLineBreak);
 }
 
 void TypingCommand::insertParagraphSeparator()
 {
+    if (!canAppendNewLineFeed(endingSelection()))
+        return;
+
     applyCommandToComposite(InsertParagraphSeparatorCommand::create(document()));
     typingAddedToOpenCommand(InsertParagraphSeparator);
 }