NS_IMETHODIMP
nsAccessiblePivot::MovePreviousByText(TextBoundaryType aBoundary, bool* aResult)
{
  NS_ENSURE_ARG(aResult);

  *aResult = false;

  int32_t oldStart = mStartOffset, oldEnd = mEndOffset;
  HyperTextAccessible* text = mPosition->AsHyperText();
  Accessible* oldPosition = mPosition;
  while (!text) {
    oldPosition = mPosition;
    mPosition = mPosition->Parent();
    text = mPosition->AsHyperText();
  }

  if (mStartOffset == -1)
    mStartOffset = text != oldPosition ? text->GetChildOffset(oldPosition) : 0;

  if (mStartOffset == 0)
    return NS_OK;

  AccessibleTextBoundary startBoundary, endBoundary;
  switch (aBoundary) {
    case CHAR_BOUNDARY:
      startBoundary = nsIAccessibleText::BOUNDARY_CHAR;
      endBoundary = nsIAccessibleText::BOUNDARY_CHAR;
      break;
    case WORD_BOUNDARY:
      startBoundary = nsIAccessibleText::BOUNDARY_WORD_START;
      endBoundary = nsIAccessibleText::BOUNDARY_WORD_END;
      break;
    default:
      return NS_ERROR_INVALID_ARG;
  }

  nsAutoString unusedText;
  int32_t newStart = 0, newEnd = 0;
  text->GetTextBeforeOffset(mStartOffset, startBoundary, &newStart, &newEnd,
                            unusedText);
  if (newStart < mStartOffset)
    mStartOffset = newEnd == mStartOffset ? newStart : newEnd;
  else // XXX: In certain odd cases newStart is equal to mStartOffset
    text->GetTextBeforeOffset(mStartOffset - 1, startBoundary, &newStart,
                              &mStartOffset, unusedText);
  text->GetTextAtOffset(mStartOffset, endBoundary, &newStart, &mEndOffset,
                        unusedText);

  *aResult = true;

  NotifyOfPivotChange(mPosition, oldStart, oldEnd,
                      nsIAccessiblePivot::REASON_TEXT);
  return NS_OK;
}
static gchar*
getTextBeforeOffsetCB(AtkText *aText, gint aOffset,
                      AtkTextBoundary aBoundaryType,
                      gint *aStartOffset, gint *aEndOffset)
{
  AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
  if (!accWrap)
    return nullptr;

  HyperTextAccessible* text = accWrap->AsHyperText();
  if (!text || !text->IsTextRole())
    return nullptr;

    nsAutoString autoStr;
    int32_t startOffset = 0, endOffset = 0;
    nsresult rv =
        text->GetTextBeforeOffset(aOffset, aBoundaryType,
                                  &startOffset, &endOffset, autoStr);
    *aStartOffset = startOffset;
    *aEndOffset = endOffset;

    NS_ENSURE_SUCCESS(rv, nullptr);

    ConvertTexttoAsterisks(accWrap, autoStr);
    NS_ConvertUTF16toUTF8 cautoStr(autoStr);
    return (cautoStr.get()) ? g_strdup(cautoStr.get()) : nullptr;
}