From 070bddc9a07e63550be35a06d6efb1ece245dc52 Mon Sep 17 00:00:00 2001
From: Andrew den Exter <andrew.den-exter@nokia.com>
Date: Tue, 13 Dec 2011 13:56:42 +1000
Subject: [PATCH] Make PlainText the default textFormat of a TextEdit.

With AutoText as the default it's easy to assume that a TextEdit accepts
plain text or rich text only which can lead to unexpected behaviour
when user supplied text is does not have the expected formatting and
implicitly changes the text format.

Task-number: QTBUG-23109
Change-Id: I08900fb23545672fd0e181058633bafeb826a746
Reviewed-by: Martin Jones <martin.jones@nokia.com>
---
 doc/src/declarative/whatsnew.qdoc             |  3 +
 .../textalignment/textalignment.qml           |  1 +
 .../text/textselection/textselection.qml      |  1 +
 src/quick/items/qquicktextedit.cpp            |  3 +-
 src/quick/items/qquicktextedit_p_p.h          |  2 +-
 .../data/horizontalAlignment_RightToLeft.qml  |  1 +
 .../qquicktextedit/tst_qquicktextedit.cpp     | 69 ++++++++++++++++++-
 7 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/doc/src/declarative/whatsnew.qdoc b/doc/src/declarative/whatsnew.qdoc
index 622566ca3c..2ae46714b6 100644
--- a/doc/src/declarative/whatsnew.qdoc
+++ b/doc/src/declarative/whatsnew.qdoc
@@ -124,6 +124,9 @@ Text improvements:
  - a \c doLayout method was added to trigger the layout from Javascript.
  - now automatically switch to StyledText instead of RichText if textFormat is set to AutoText.
 
+TextEdit:
+ - the default value of the textFormat property is now PlainText instead of AutoText.
+
 PathView now has a \c currentItem property
 
 ListView and GridView:
diff --git a/examples/declarative/righttoleft/textalignment/textalignment.qml b/examples/declarative/righttoleft/textalignment/textalignment.qml
index 13abca1fb1..a7af6f1b55 100644
--- a/examples/declarative/righttoleft/textalignment/textalignment.qml
+++ b/examples/declarative/righttoleft/textalignment/textalignment.qml
@@ -361,6 +361,7 @@ Rectangle {
             font.pixelSize: 24
             cursorVisible: true
             wrapMode: TextEdit.WordWrap
+            textFormat: TextEdit.RichText
             horizontalAlignment: root.horizontalAlignment
             LayoutMirroring.enabled: root.mirror
             Rectangle {
diff --git a/examples/declarative/text/textselection/textselection.qml b/examples/declarative/text/textselection/textselection.qml
index a25d0df621..2e8268d8ee 100644
--- a/examples/declarative/text/textselection/textselection.qml
+++ b/examples/declarative/text/textselection/textselection.qml
@@ -95,6 +95,7 @@ Rectangle {
                 height: flick.height
                 focus: true
                 wrapMode: TextEdit.Wrap
+                textFormat: TextEdit.RichText
 
                 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
 
diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp
index 9e76c21fb8..107da166ea 100644
--- a/src/quick/items/qquicktextedit.cpp
+++ b/src/quick/items/qquicktextedit.cpp
@@ -280,7 +280,7 @@ void QQuickTextEdit::setText(const QString &text)
     \o TextEdit.RichText
     \endlist
 
-    The default is TextEdit.AutoText.  If the text format is TextEdit.AutoText the text edit
+    The default is TextEdit.PlainText.  If the text format is TextEdit.AutoText the text edit
     will automatically determine whether the text should be treated as
     rich text.  This determination is made using Qt::mightBeRichText().
 
@@ -1633,6 +1633,7 @@ void QQuickTextEditPrivate::init()
     control = new QQuickTextControl(document, q);
     control->setView(q);
     control->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable);
+    control->setAcceptRichText(false);
 
     // QQuickTextControl follows the default text color
     // defined by the platform, declarative text
diff --git a/src/quick/items/qquicktextedit_p_p.h b/src/quick/items/qquicktextedit_p_p.h
index 909fd95449..9886abaab1 100644
--- a/src/quick/items/qquicktextedit_p_p.h
+++ b/src/quick/items/qquicktextedit_p_p.h
@@ -75,7 +75,7 @@ public:
       persistentSelection(true), requireImplicitWidth(false), selectByMouse(false), canPaste(false),
       hAlignImplicit(true), rightToLeftText(false), useImageFallback(false),
       textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0),
-      format(QQuickTextEdit::AutoText), document(0), wrapMode(QQuickTextEdit::NoWrap),
+      format(QQuickTextEdit::PlainText), document(0), wrapMode(QQuickTextEdit::NoWrap),
       mouseSelectionMode(QQuickTextEdit::SelectCharacters),
       lineCount(0), yoff(0), nodeType(NodeIsNull), texture(0)
     {
diff --git a/tests/auto/qtquick2/qquicktextedit/data/horizontalAlignment_RightToLeft.qml b/tests/auto/qtquick2/qquicktextedit/data/horizontalAlignment_RightToLeft.qml
index 4cd92367ec..2163838488 100644
--- a/tests/auto/qtquick2/qquicktextedit/data/horizontalAlignment_RightToLeft.qml
+++ b/tests/auto/qtquick2/qquicktextedit/data/horizontalAlignment_RightToLeft.qml
@@ -19,6 +19,7 @@ Rectangle {
             anchors.fill: parent
             text: top.text
             focus: true
+            textFormat: TextEdit.AutoText
         }
     }
 }
diff --git a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp
index 9cc904eef3..fc7b0329cb 100644
--- a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp
+++ b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp
@@ -336,6 +336,71 @@ void tst_qquicktextedit::text()
         QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + richText.at(i) + "\" }";
         QDeclarativeComponent texteditComponent(&engine);
         texteditComponent.setData(componentStr.toLatin1(), QUrl());
+
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+
+        QString expected = richText.at(i);
+        expected.replace(QRegExp("\\\\(.)"),"\\1");
+        QCOMPARE(textEditObject->text(), expected);
+        QCOMPARE(textEditObject->length(), expected.length());
+    }
+
+    for (int i = 0; i < standard.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + standard.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+
+        QString actual = textEditObject->text();
+        QString expected = standard.at(i);
+        actual.remove(QRegExp(".*<body[^>]*>"));
+        actual.remove(QRegExp("(<[^>]*>)+"));
+        expected.remove("\n");
+        QCOMPARE(actual.simplified(), expected);
+        QCOMPARE(textEditObject->length(), expected.length());
+    }
+
+    for (int i = 0; i < richText.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + richText.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+        QString actual = textEditObject->text();
+        QString expected = richText.at(i);
+        actual.replace(QRegExp(".*<body[^>]*>"),"");
+        actual.replace(QRegExp("(<[^>]*>)+"),"<>");
+        expected.replace(QRegExp("(<[^>]*>)+"),"<>");
+        QCOMPARE(actual.simplified(),expected.simplified());
+
+        expected.replace("<>", " ");
+        QCOMPARE(textEditObject->length(), expected.simplified().length());
+    }
+
+    for (int i = 0; i < standard.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + standard.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+        QCOMPARE(textEditObject->text(), standard.at(i));
+        QCOMPARE(textEditObject->length(), standard.at(i).length());
+    }
+
+    for (int i = 0; i < richText.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + richText.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
         QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
 
         QVERIFY(textEditObject != 0);
@@ -409,7 +474,7 @@ void tst_qquicktextedit::width()
 
         int documentWidth = ceil(document.idealWidth());
 
-        QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + richText.at(i) + "\" }";
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + richText.at(i) + "\" }";
         QDeclarativeComponent texteditComponent(&engine);
         texteditComponent.setData(componentStr.toLatin1(), QUrl());
         QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
@@ -2467,7 +2532,7 @@ void tst_qquicktextedit::getText()
     QFETCH(int, end);
     QFETCH(QString, expectedText);
 
-    QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + text + "\" }";
+    QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + text + "\" }";
     QDeclarativeComponent textEditComponent(&engine);
     textEditComponent.setData(componentStr.toLatin1(), QUrl());
     QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());
-- 
GitLab