[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdelibs/kdeui
From:       Thomas McGuire <mcguire () kde ! org>
Date:       2011-01-26 12:20:27
Message-ID: 20110126122027.31ED23E1F1 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1217226 by tmcguire:

Make toCleanHtml() more robust and add a unit test.

Patch by Frank Vanderham, thank you!

CCMAIL: twelve.eighty@gmail.com


 M  +153 -0    tests/krichtextedittest.cpp  
 M  +3 -0      tests/krichtextedittest.h  
 M  +21 -11    widgets/krichtextedit.cpp  


--- trunk/KDE/kdelibs/kdeui/tests/krichtextedittest.cpp #1217225:1217226
@@ -25,8 +25,11 @@
 #include <qtestevent.h>
 #include <qtest_kde.h>
 #include <qtextcursor.h>
+#include <QTextList>
 #include <qfont.h>
 
+#include <kdebug.h>
+
 QTEST_KDEMAIN(KRichTextEditTest, GUI)
 
 void KRichTextEditTest::testLinebreaks()
@@ -116,3 +119,153 @@
     QCOMPARE(charFormat.underlineColor().name(), QColor(Qt::black).name());
     QCOMPARE(charFormat.underlineStyle(), QTextCharFormat::NoUnderline);
 }
+
+
+void KRichTextEditTest::testHTMLLineBreaks()
+{
+  KRichTextEdit edit;
+  edit.enableRichTextMode();
+
+  // Create the following text:
+  //A
+  //
+  //B
+  QTest::keyClick( &edit, Qt::Key_A );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+
+  edit.setTextUnderline( true );
+  
+  QTest::keyClick( &edit, Qt::Key_Enter );
+
+  QTest::keyClick( &edit, Qt::Key_B );
+  
+  QString html = edit.toCleanHtml();
+  
+  // The problem we have is that we need to "fake" being a viewer such
+  // as Thunderbird or MS-Outlook to unit test our html line breaks.
+  // For now, we'll parse the 6th line (the empty one) and make sure it has the \
proper format +  // The first four (4) HTML code lines are DOCTYPE through <body> \
declaration +  
+  const QStringList lines = html.split('\n');
+  
+//  for (int idx=0; idx<lines.size(); idx++) {
+//    kDebug() << ( idx + 1 ) << QString( " : " ) << lines.at( idx );
+//  }
+  
+  QVERIFY2( lines.size() == 7,  "we can't perform this unit test: "
+			       "the html rendering has changed beyond recognition");
+  
+  const QString& line6 = lines.at(5);
+  
+  // make sure that this is an empty <p> line
+  QVERIFY( line6.startsWith( QString( "<p style=\"-qt-paragraph-type:empty;" ) ) );
+  
+  // make sure that empty lines have the &nbsp; inserted
+  QVERIFY2( line6.endsWith( QString( ">&nbsp;</p>" ) ), "Empty lines must have \
&nbsp; or otherwise 3rd party " +							"viewers render those as non-existing lines" \
); +  
+}
+
+void KRichTextEditTest::testHTMLOrderedLists() 
+{
+
+  // The problem we have is that we need to "fake" being a viewer such
+  // as Thunderbird or MS-Outlook to unit test our html lists.
+  // For now, we'll parse the 6th line (the <ol> element) and make sure it has the \
proper format +
+  KRichTextEdit edit;
+  edit.enableRichTextMode();
+  
+  edit.setTextUnderline( true );
+
+  // create a numbered (ordered) list
+  QTextCursor cursor = edit.textCursor();
+  cursor.insertList( QTextListFormat::ListDecimal );
+  
+  QTest::keyClick( &edit, Qt::Key_A );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+  QTest::keyClick( &edit, Qt::Key_B );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+  QTest::keyClick( &edit, Qt::Key_C );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+  
+  QString html = edit.toCleanHtml();
+  
+  const QStringList lines = html.split('\n');
+  
+//  Uncomment this section in case the first test fails to see if the HTML
+//  rendering has actually introduced a bug, or merely a problem with the unit test \
itself +//  
+//  for (int idx=0; idx<lines.size(); idx++) {
+//    kDebug() << ( idx + 1 ) << QString( " : " ) << lines.at( idx );
+//  }
+  
+  QVERIFY2( lines.size() == 9,  "we can't perform this unit test: "
+			       "the html rendering has changed beyond recognition");
+  
+  // this is the <ol> declaration line
+  const QString& line6 = lines.at(5);
+  
+//  kDebug() << line6;
+  
+  // there should not be a margin-left: 0 defined for the <ol> element
+  QRegExp regex( QString ( "<ol.*margin-left: 0px.*><li" ) );
+  regex.setMinimal( true );
+  
+  QVERIFY2 ( regex.indexIn( line6, 0 ) == -1, "margin-left: 0px specified for \
ordered lists " +					      "removes numbers in 3rd party viewers " );
+  
+}
+
+void KRichTextEditTest::testHTMLUnorderedLists()
+{
+  // The problem we have is that we need to "fake" being a viewer such
+  // as Thunderbird or MS-Outlook to unit test our html lists.
+  // For now, we'll parse the 6th line (the <ul> element) and make sure it has the \
proper format +  // The first four (4) HTML code lines are DOCTYPE through <body> \
declaration +
+  KRichTextEdit edit;
+  edit.enableRichTextMode();
+  
+  edit.setTextUnderline( true );
+
+  // create a numbered (ordered) list
+  QTextCursor cursor = edit.textCursor();
+  cursor.insertList( QTextListFormat::ListDisc );
+  
+  QTest::keyClick( &edit, Qt::Key_A );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+  QTest::keyClick( &edit, Qt::Key_B );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+  QTest::keyClick( &edit, Qt::Key_C );
+  QTest::keyClick( &edit, Qt::Key_Enter );
+  
+  QString html = edit.toCleanHtml();
+  
+  const QStringList lines = html.split('\n');
+  
+//  Uncomment this section in case the first test fails to see if the HTML
+//  rendering has actually introduced a bug, or merely a problem with the unit test \
itself +//  
+//  for (int idx=0; idx<lines.size(); idx++) {
+//    kDebug() << ( idx + 1 ) << QString( " : " ) << lines.at( idx );
+//  }
+  
+  QVERIFY2( lines.size() == 9,  "we can't perform this unit test: "
+			       "the html rendering has changed beyond recognition");
+  
+  // this is the <ol> declaration line
+  const QString& line6 = lines.at(5);
+  
+//  kDebug() << line6;
+  
+  // there should not be a margin-left: 0 defined for the <ol> element
+  QRegExp regex( QString ( "<ul.*margin-left: 0px.*><li" ) );
+  regex.setMinimal( true );
+  
+  QVERIFY2 ( regex.indexIn( line6, 0 ) == -1, "margin-left: 0px specified for \
unordered lists " +					      "removes numbers in 3rd party viewers " );
+  
+}
+
+
--- trunk/KDE/kdelibs/kdeui/tests/krichtextedittest.h #1217225:1217226
@@ -30,6 +30,9 @@
         void testLinebreaks();
         void testUpdateLinkAdd();
         void testUpdateLinkRemove();
+	void testHTMLLineBreaks();
+	void testHTMLOrderedLists();
+	void testHTMLUnorderedLists();
 };
 
 #endif
--- trunk/KDE/kdelibs/kdeui/widgets/krichtextedit.cpp #1217225:1217226
@@ -524,16 +524,15 @@
 {
   QString result = toHtml();
 
-  static const QString EMPTYLINEFROMQT = QLatin1String(
-  "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
-  "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
-  "-qt-user-state:0;\"></p>" );
-
   static const QString EMPTYLINEHTML = QLatin1String(
   "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
-  "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
-  "-qt-user-state:0;\"><br /></p>" );
+  "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; \
\">&nbsp;</p>" );  
+  // Qt inserts various style properties based on the current mode of the editor \
(underline,  +  // bold, etc), but only empty paragraphs *also* have \
qt-paragraph-type set to 'empty'. +  static const QString EMPTYLINEREGEX = \
QLatin1String( +    "<p style=\"-qt-paragraph-type:empty;(.*)</p>" );
+  
   static const QString OLLISTPATTERNQT = QLatin1String(
   "<ol style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px;" );
 
@@ -549,11 +548,22 @@
   // fix 1 - empty lines should show as empty lines - MS Outlook treats \
margin-top:0px; as  // a non-existing line.
   // Although we can simply remove the margin-top style property, we still get \
                unwanted results
-  // if you have three or more empty lines. It's best to replace empty <p> elements \
                with <p><br /></p>.
-  // As per http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_XHTML-1.0-Strict, <br> \
                elements are still proper
-  // HTML.
-  result.replace(EMPTYLINEFROMQT, EMPTYLINEHTML);
+  // if you have three or more empty lines. It's best to replace empty <p> elements \
with <p>&nbsp;</p>.  
+  QRegExp emptyLineFinder( EMPTYLINEREGEX );
+  emptyLineFinder.setMinimal( true );
+  
+  // find the first occurance
+  int offset = emptyLineFinder.indexIn( result, 0 );
+  while (offset != -1) {
+    // replace all the matching text with the new line text
+    result.replace( offset, emptyLineFinder.matchedLength(), EMPTYLINEHTML );
+    // advance the search offset to just beyond the last replace
+    offset += EMPTYLINEHTML.length();
+    // find the next occurance
+    offset = emptyLineFinder.indexIn( result, offset );
+  }
+  
   // fix 2a - ordered lists - MS Outlook treats margin-left:0px; as
   // a non-existing number; e.g: "1. First item" turns into "First Item"
   result.replace(OLLISTPATTERNQT, ORDEREDLISTHTML);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic