[Gtk] atk_text_get_text_at_offset() sometimes fails to provide the correct line
authormario@webkit.org <mario@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 2 Feb 2012 13:18:09 +0000 (13:18 +0000)
committermario@webkit.org <mario@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 2 Feb 2012 13:18:09 +0000 (13:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=72382

Reviewed by Martin Robinson.

Source/WebCore:

Do not add unnecesary blanks at the end of a line of text.

* accessibility/gtk/WebKitAccessibleInterfaceText.cpp:
(textForRenderer): Do not just append a '\n' at the end of a line
if the linebreak for that line was already considered.

Source/WebKit/gtk:

New unit test to ensure the right line is retrieved for the first
position of each line in a multiline preformatted portion of text.

* tests/testatk.c:
(testWebkitAtkGetTextAtOffsetWithPreformattedText): New unit test.
(main): Add the new unit test.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@106545 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/accessibility/gtk/WebKitAccessibleInterfaceText.cpp
Source/WebKit/gtk/ChangeLog
Source/WebKit/gtk/tests/testatk.c

index 22d79f0..fef6599 100644 (file)
@@ -1,3 +1,16 @@
+2012-02-02  Mario Sanchez Prada  <msanchez@igalia.com>
+
+        [Gtk] atk_text_get_text_at_offset() sometimes fails to provide the correct line
+        https://bugs.webkit.org/show_bug.cgi?id=72382
+
+        Reviewed by Martin Robinson.
+
+        Do not add unnecesary blanks at the end of a line of text.
+
+        * accessibility/gtk/WebKitAccessibleInterfaceText.cpp:
+        (textForRenderer): Do not just append a '\n' at the end of a line
+        if the linebreak for that line was already considered.
+
 2012-02-02  Raul Hudea  <rhudea@adobe.com>
 
         Regions should ignore the saved currentRenderFlowThread during repainting
index da6489e..207be0c 100644 (file)
@@ -100,8 +100,15 @@ static gchar* textForRenderer(RenderObject* renderer)
             // Newline chars in the source result in separate text boxes, so check
             // before adding a newline in the layout. See bug 25415 comment #78.
             // If the next sibling is a BR, we'll add the newline when we examine that child.
-            if (!box->nextOnLineExists() && (!object->nextSibling() || !object->nextSibling()->isBR()))
+            if (!box->nextOnLineExists() && !(object->nextSibling() && object->nextSibling()->isBR())) {
+                // If there was a '\n' in the last position of the
+                // current text box, it would have been converted to a
+                // space in String::replace(), so remove it first.
+                if (renderText->characters()[box->end()] == '\n')
+                    g_string_erase(resultText, resultText->len - 1, -1);
+
                 g_string_append(resultText, "\n");
+            }
             box = box->nextTextBox();
         }
     }
index fc88e9a..24076a1 100644 (file)
@@ -1,3 +1,17 @@
+2012-02-02  Mario Sanchez Prada  <msanchez@igalia.com>
+
+        [Gtk] atk_text_get_text_at_offset() sometimes fails to provide the correct line
+        https://bugs.webkit.org/show_bug.cgi?id=72382
+
+        Reviewed by Martin Robinson.
+
+        New unit test to ensure the right line is retrieved for the first
+        position of each line in a multiline preformatted portion of text.
+
+        * tests/testatk.c:
+        (testWebkitAtkGetTextAtOffsetWithPreformattedText): New unit test.
+        (main): Add the new unit test.
+
 2012-02-01  Philippe Normand  <pnormand@igalia.com>
 
         [GStreamer] FFTFrame implementation
index b48b557..a790101 100644 (file)
@@ -33,6 +33,8 @@ static const char* contents = "<html><body><p>This is a test. This is the second
 
 static const char* contentsWithNewlines = "<html><body><p>This is a test. \n\nThis\n is the second sentence. And this the third.</p></body></html>";
 
+static const char* contentsWithPreformattedText = "<html><body><pre>\n\t\n\tfirst line\n\tsecond line\n</pre></body></html>";
+
 static const char* contentsWithSpecialChars = "<html><body><p>&laquo;&nbsp;This is a paragraph with &ldquo;special&rdquo; characters inside.&nbsp;&raquo;</p></body></html>";
 
 static const char* contentsInTextarea = "<html><body><textarea cols='80'>This is a test. This is the second sentence. And this the third.</textarea></body></html>";
@@ -769,6 +771,34 @@ static void testWebkitAtkGetTextAtOffsetTextInput()
     g_object_unref(webView);
 }
 
+static void testWebkitAtkGetTextAtOffsetWithPreformattedText()
+{
+    WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
+    g_object_ref_sink(webView);
+    GtkAllocation allocation = { 0, 0, 800, 600 };
+    gtk_widget_size_allocate(GTK_WIDGET(webView), &allocation);
+    webkit_web_view_load_string(webView, contentsWithPreformattedText, 0, 0, 0);
+
+    AtkObject* object = getWebAreaObject(webView);
+    g_assert(object);
+
+    AtkObject* preformattedText = atk_object_ref_accessible_child(object, 0);
+    g_assert(ATK_IS_OBJECT(preformattedText));
+    g_assert(atk_object_get_role(preformattedText) == ATK_ROLE_PANEL);
+    g_assert(ATK_IS_TEXT(preformattedText));
+    char* text = atk_text_get_text(ATK_TEXT(preformattedText), 0, -1);
+    g_assert_cmpstr(text, ==, "\t\n\tfirst line\n\tsecond line\n");
+    g_free(text);
+
+    /* Try retrieving all the lines indicating the position of the offsets at the beginning of each of them. */
+    testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 0, "\t\n", 0, 2);
+    testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 2, "\tfirst line\n", 2, 14);
+    testGetTextFunction(ATK_TEXT(preformattedText), atk_text_get_text_at_offset, ATK_TEXT_BOUNDARY_LINE_START, 14, "\tsecond line\n", 14, 27);
+
+    g_object_unref(preformattedText);
+    g_object_unref(webView);
+}
+
 static void testWebkitAtkGetTextAtOffsetWithSpecialCharacters()
 {
     WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
@@ -1831,6 +1861,7 @@ int main(int argc, char** argv)
     g_test_add_func("/webkit/atk/getTextAtOffsetNewlines", testWebkitAtkGetTextAtOffsetNewlines);
     g_test_add_func("/webkit/atk/getTextAtOffsetTextarea", testWebkitAtkGetTextAtOffsetTextarea);
     g_test_add_func("/webkit/atk/getTextAtOffsetTextInput", testWebkitAtkGetTextAtOffsetTextInput);
+    g_test_add_func("/webkit/atk/getTextAtOffsetWithPreformattedText", testWebkitAtkGetTextAtOffsetWithPreformattedText);
     g_test_add_func("/webkit/atk/getTextAtOffsetWithSpecialCharacters", testWebkitAtkGetTextAtOffsetWithSpecialCharacters);
     g_test_add_func("/webkit/atk/getTextInParagraphAndBodySimple", testWebkitAtkGetTextInParagraphAndBodySimple);
     g_test_add_func("/webkit/atk/getTextInParagraphAndBodyModerate", testWebkitAtkGetTextInParagraphAndBodyModerate);