Bug 630001, part3 - rewrite GetText, r=davidb, a=betaN
authorAlexander Surkov <surkov.alexander@gmail.com>
Wed, 02 Feb 2011 00:08:36 +0800
changeset 61730 6a2099306114ea14cac313611b15fdbc82bb610a
parent 61729 83e9acc504c2eb417a3cf137a0032bf7a6be7919
child 61731 ae9bc934ead9b02f0840644e1a29bda833dfbb46
push id18458
push usersurkov.alexander@gmail.com
push dateTue, 01 Feb 2011 16:09:29 +0000
treeherdermozilla-central@19bdf0a54cce [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdavidb, betaN
bugs630001
milestone2.0b11pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 630001, part3 - rewrite GetText, r=davidb, a=betaN
accessible/src/html/nsHyperTextAccessible.cpp
accessible/tests/mochitest/text.js
accessible/tests/mochitest/text/Makefile.in
accessible/tests/mochitest/text/test_hypertext.html
--- a/accessible/src/html/nsHyperTextAccessible.cpp
+++ b/accessible/src/html/nsHyperTextAccessible.cpp
@@ -469,22 +469,72 @@ nsHyperTextAccessible::GetPosAndText(PRI
     *aEndFrame = startFrame;
     if (aStartAcc && aEndAcc)
       NS_IF_ADDREF(*aEndAcc = *aStartAcc);
   }
 
   return startFrame;
 }
 
-NS_IMETHODIMP nsHyperTextAccessible::GetText(PRInt32 aStartOffset, PRInt32 aEndOffset, nsAString &aText)
+NS_IMETHODIMP
+nsHyperTextAccessible::GetText(PRInt32 aStartOffset, PRInt32 aEndOffset,
+                               nsAString &aText)
 {
+  aText.Truncate();
+
   if (IsDefunct())
     return NS_ERROR_FAILURE;
 
-  return GetPosAndText(aStartOffset, aEndOffset, &aText) ? NS_OK : NS_ERROR_FAILURE;
+  if (aStartOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
+    aStartOffset = CharacterCount();
+  else if (aStartOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
+    GetCaretOffset(&aStartOffset);
+
+  if (aEndOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
+    aEndOffset = CharacterCount();
+  else if (aEndOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
+    GetCaretOffset(&aEndOffset);
+
+  PRInt32 startChildIdx = GetChildIndexAtOffset(aStartOffset);
+  if (startChildIdx == -1)
+    return NS_ERROR_INVALID_ARG;
+
+  PRInt32 endChildIdx = GetChildIndexAtOffset(aEndOffset);
+  if (endChildIdx == -1)
+    return NS_ERROR_INVALID_ARG;
+
+  if (startChildIdx == endChildIdx) {
+    PRInt32 childOffset =  GetChildOffset(startChildIdx);
+    NS_ENSURE_STATE(childOffset != -1);
+
+    nsAccessible* child = GetChildAt(startChildIdx);
+    child->AppendTextTo(aText, aStartOffset - childOffset,
+                        aEndOffset - aStartOffset);
+
+    return NS_OK;
+  }
+
+  PRInt32 startChildOffset =  GetChildOffset(startChildIdx);
+  NS_ENSURE_STATE(startChildOffset != -1);
+
+  nsAccessible* startChild = GetChildAt(startChildIdx);
+  startChild->AppendTextTo(aText, aStartOffset - startChildOffset);
+
+  for (PRInt32 childIdx = startChildIdx + 1; childIdx < endChildIdx; childIdx++) {
+    nsAccessible* child = GetChildAt(childIdx);
+    child->AppendTextTo(aText);
+  }
+
+  PRInt32 endChildOffset =  GetChildOffset(endChildIdx);
+  NS_ENSURE_STATE(endChildOffset != -1);
+
+  nsAccessible* endChild = GetChildAt(endChildIdx);
+  endChild->AppendTextTo(aText, 0, aEndOffset - endChildOffset);
+
+  return NS_OK;
 }
 
 /*
  * Gets the character count.
  */
 NS_IMETHODIMP nsHyperTextAccessible::GetCharacterCount(PRInt32 *aCharacterCount)
 {
   NS_ENSURE_ARG_POINTER(aCharacterCount);
--- a/accessible/tests/mochitest/text.js
+++ b/accessible/tests/mochitest/text.js
@@ -14,17 +14,17 @@ const kOk = 2;
 /**
  * Test characterCount for the given array of accessibles.
  *
  * @param aCount  [in] the expected character count
  * @param aIDs    [in] array of accessible identifiers to test
  */
 function testCharacterCount(aIDs, aCount)
 {
-  for (var i = 1; i < aIDs.length; i++) {
+  for (var i = 0; i < aIDs.length; i++) {
     var textacc = getAccessible(aIDs[i], [nsIAccessibleText]);
     is(textacc.characterCount, aCount,
        "Wrong character count for " + prettyName(aIDs[i]));
   }
 }
 
 function testText(aIDs, aStartOffset, aEndOffset, aText)
 {
--- a/accessible/tests/mochitest/text/Makefile.in
+++ b/accessible/tests/mochitest/text/Makefile.in
@@ -43,15 +43,16 @@ VPATH		= @srcdir@
 relativesrcdir  = accessible/text
 
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES = \
 		doc.html \
 		test_doc.html \
+		test_hypertext.html \
 		test_singleline.html \
 		test_whitespaces.html \
 		test_words.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/a11y/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/accessible/tests/mochitest/text/test_hypertext.html
@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>nsIAccessibleText getText related function tests for rich text</title>
+  <link rel="stylesheet" type="text/css"
+        href="chrome://mochikit/content/tests/SimpleTest/test.css" />
+
+  <script type="application/javascript"
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript"
+          src="../common.js"></script>
+  <script type="application/javascript"
+          src="../text.js"></script>
+
+  <script type="application/javascript">
+    function doTest()
+    {
+      //////////////////////////////////////////////////////////////////////////
+      // hypertext
+      //////////////////////////////////////////////////////////////////////////
+
+      // ! - embedded object char
+      // @ - imaginary object char, space is used
+      // __h__e__l__l__o__ __!__ __s__e__e__ __@__
+      //  0  1  2  3  4  5  6  7  8  9 10 11 12 13
+
+      var IDs = [ "hypertext" ];
+
+      ////////////////////////////////////////////////////////////////////////
+      // characterCount
+
+      testCharacterCount(IDs, 13);
+
+      ////////////////////////////////////////////////////////////////////////
+      // getText
+
+      testText(IDs, 0, 1, "h");
+      testText(IDs, 5, 7, " " + kEmbedChar);
+      testText(IDs, 10, 13, "e  ");
+      testText(IDs, 0, 13, "hello " + kEmbedChar + " see  ");
+
+      //////////////////////////////////////////////////////////////////////////
+      // list
+      //////////////////////////////////////////////////////////////////////////
+
+      IDs = [ "list" ];
+      testCharacterCount(IDs, 1);
+      testText(IDs, 0, 1, kEmbedChar);
+
+      IDs = [ "listitem" ];
+      testCharacterCount(IDs, 5);
+      testText(IDs, 0, 5, "1.foo");
+
+      SimpleTest.finish();
+    }
+
+    SimpleTest.waitForExplicitFinish();
+    addA11yLoadEvent(doTest);
+  </script>
+</head>
+<body>
+
+  <a target="_blank"
+     title="Fix getText"
+     href="https://bugzilla.mozilla.org/show_bug.cgi?id=630001">Mozilla Bug 630001, part3</a>
+  <p id="display"></p>
+  <div id="content" style="display: none"></div>
+  <pre id="test">
+  </pre>
+
+  <div id="hypertext">hello <a>friend</a> see <img></div>
+  <ol id="list"><li id="listitem">foo</li></ol>
+
+</body>
+</html>