Logo Search packages:      
Sourcecode: xulrunner-1.9 version File versions  Download package

PRInt32 nsHyperTextAccessible::GetCaretLineNumber (  )  [protected, inherited]

Provide the line number for the caret, relative to the current DOM node.

1-based index for the line number with the caret

Definition at line 1523 of file nsHyperTextAccessible.cpp.

References nsIFrame::GetContent(), nsIFrame::GetFirstChild(), nsIFrame::GetNextSibling(), nsIFrame::GetParent(), and nsHyperTextAccessible::GetSelections().

  // Provide the line number for the caret, relative to the
  // currently focused node. Use a 1-based index
  nsCOMPtr<nsISelection> domSel;
  GetSelections(nsnull, getter_AddRefs(domSel));
  nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(domSel));
  NS_ENSURE_TRUE(privateSelection, -1);
  nsCOMPtr<nsFrameSelection> frameSelection;
  NS_ENSURE_TRUE(frameSelection, -1);

  nsCOMPtr<nsIDOMNode> caretNode;
  nsCOMPtr<nsIContent> caretContent = do_QueryInterface(caretNode);
  if (!caretContent || !nsAccUtils::IsAncestorOf(mDOMNode, caretNode)) {
    return -1;

  PRInt32 caretOffset, returnOffsetUnused;
  nsFrameSelection::HINT hint = frameSelection->GetHint();
  nsIFrame *caretFrame = frameSelection->GetFrameForNodeOffset(caretContent, caretOffset,
                                                               hint, &returnOffsetUnused);
  NS_ENSURE_TRUE(caretFrame, -1);

  PRInt32 lineNumber = 1;
  nsCOMPtr<nsILineIterator> lineIterForCaret;
  nsCOMPtr<nsIContent> hyperTextContent = do_QueryInterface(mDOMNode);
  while (caretFrame) {
    if (hyperTextContent == caretFrame->GetContent()) {
      return lineNumber; // Must be in a single line hyper text, there is no line iterator
    nsIFrame *parentFrame = caretFrame->GetParent();
    if (!parentFrame)

    // Add lines for the sibling frames before the caret
    nsIFrame *sibling = parentFrame->GetFirstChild(nsnull);
    while (sibling && sibling != caretFrame) {
      nsCOMPtr<nsILineIterator> lineIterForSibling = do_QueryInterface(sibling);
      if (lineIterForSibling) {
        PRInt32 addLines;
        // For the frames before that grab all the lines
        lineNumber += addLines;
      sibling = sibling->GetNextSibling();

    // Get the line number relative to the container with lines
    if (!lineIterForCaret) {   // Add the caret line just once
      lineIterForCaret = do_QueryInterface(parentFrame);
      if (lineIterForCaret) {
        // Ancestor of caret
        PRInt32 addLines;
        lineIterForCaret->FindLineContaining(caretFrame, &addLines);
        lineNumber += addLines;

    caretFrame = parentFrame;

  NS_NOTREACHED("DOM ancestry had this hypertext but frame ancestry didn't");
  return lineNumber;

Generated by  Doxygen 1.6.0   Back to index