Back to index

lightning-sunbird  0.9+nobinonly
Functions
nsFIXptr.cpp File Reference
#include "nsIDOMDocument.h"
#include "nsIDOMRange.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsIDOMNodeList.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsFIXptr.h"
#include "nsCRT.h"
#include "nsString.h"
#include "nsContentCID.h"

Go to the source code of this file.

Functions

static NS_DEFINE_IID (kRangeCID, NS_RANGE_CID)
 Implementation for FIXptr, a W3C proposal.
static nsresult GetChild (nsIDOMNode *aParent, PRInt32 aChildNum, nsIDOMNode **aChild)
static nsresult GetCharRange (nsIDOMNode *aParent, PRInt32 aCharNum, nsIDOMRange **aRange)
static nsresult GetTumblerNode (nsIDOMNode *aParent, const nsString &aTumbler, nsIDOMNode **aNode)
static nsresult GetRange (nsIDOMDocument *aDocument, const nsAString &aExpression, nsIDOMRange **aRange)

Function Documentation

static nsresult GetCharRange ( nsIDOMNode aParent,
PRInt32  aCharNum,
nsIDOMRange **  aRange 
) [static]

Definition at line 108 of file nsFIXptr.cpp.

{
  NS_ENSURE_ARG_POINTER(aParent);
  NS_ENSURE_ARG_POINTER(aRange);

  *aRange = nsnull;
  nsCOMPtr<nsIDOMNodeList> list;
  aParent->GetChildNodes(getter_AddRefs(list));
  if (!list)
    return NS_OK;

  PRUint32 count;
  list->GetLength(&count);
  PRUint32 i;
  PRInt32 maxCharNum = 0;
  PRInt32 prevCharNum = 0;
  for (i = 0; i < count; i++) {
    nsCOMPtr<nsIDOMNode> node;
    list->Item(i, getter_AddRefs(node));
    if (!node)
      break;
    PRUint16 nodeType;
    node->GetNodeType(&nodeType);
    if (nodeType & (nsIDOMNode::TEXT_NODE | nsIDOMNode::CDATA_SECTION_NODE)) {
      nsAutoString value;
      node->GetNodeValue(value);
      maxCharNum += value.Length();
    }
    if (maxCharNum >= aCharNum) {
      nsCOMPtr<nsIDOMRange> range(do_CreateInstance(kRangeCID));
      if (!range)
        return NS_ERROR_OUT_OF_MEMORY;
      range->SetStart(node, aCharNum - prevCharNum);
      range->SetEnd(node, aCharNum - prevCharNum + 1);
      *aRange = range;
      NS_ADDREF(*aRange);
      break;
    }
    prevCharNum = maxCharNum;
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult GetChild ( nsIDOMNode aParent,
PRInt32  aChildNum,
nsIDOMNode **  aChild 
) [static]

Definition at line 72 of file nsFIXptr.cpp.

{
  NS_ENSURE_ARG_POINTER(aParent);
  NS_ENSURE_ARG_POINTER(aChild);

  *aChild = nsnull;
  nsCOMPtr<nsIDOMNodeList> list;
  aParent->GetChildNodes(getter_AddRefs(list));
  if (!list)
    return NS_OK;
  
  PRUint32 count;
  list->GetLength(&count);
  PRUint32 i;
  PRInt32 curChildNum = 0;
  for (i = 0; i < count; i++) {
    nsCOMPtr<nsIDOMNode> node;
    list->Item(i, getter_AddRefs(node));
    if (!node)
      break;
    PRUint16 nodeType;
    node->GetNodeType(&nodeType);
    if (nodeType == nsIDOMNode::ELEMENT_NODE) {
      curChildNum++;
    }
    if (curChildNum == aChildNum) {
      *aChild = node;
      NS_ADDREF(*aChild);
      break;
    }
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult GetRange ( nsIDOMDocument aDocument,
const nsAString &  aExpression,
nsIDOMRange **  aRange 
) [static]

Definition at line 200 of file nsFIXptr.cpp.

{
  nsresult rv = NS_OK;
  nsCOMPtr<nsIDOMNode> node;
  if (nsCRT::IsAsciiAlpha(aExpression.First())) {
    // name
    nsAutoString id;
    const nsAutoString expression(aExpression);
    PRInt32 sep = expression.FindCharInSet("/(");
    if (sep > 0) {
      expression.Left(id,sep);
      nsCOMPtr<nsIDOMElement> element;
      aDocument->GetElementById(id, getter_AddRefs(element));
      node = do_QueryInterface(element);
      if (node) {
        if (expression[sep] == '/') {
          // tumbler
          nsAutoString tumbler;
          expression.Mid(tumbler, sep, expression.Length());
          sep = tumbler.FindChar('(');
          if (sep > 0)
            tumbler.Truncate(sep);
          nsCOMPtr<nsIDOMNode> child;
          GetTumblerNode(node, tumbler, getter_AddRefs(child));
          node = child;
        }
        // char
        sep = expression.FindChar('(');
        if (sep > 0) {
          nsAutoString charNum(aExpression);
          if (charNum[charNum.Length() -1] == ')') {
            charNum.Truncate(charNum.Length() - 1);
            charNum.Cut(0, sep + 1);
            PRInt32 error;
            PRInt32 n = charNum.ToInteger(&error);
            if (n < 1)
              return NS_OK;
            rv = GetCharRange(node, n - 1, aRange);
            if (!*aRange) {
              node = nsnull;
            }
          }
        }
      }
    } else {
      // just name
      nsCOMPtr<nsIDOMElement> element;
      aDocument->GetElementById(expression, getter_AddRefs(element));
      node = do_QueryInterface(element);
    }
  } else if (aExpression.First() == '/') {
    // Starts with tumbler
    node = do_QueryInterface(aDocument);
    nsCOMPtr<nsIDOMNode> child;
    nsAutoString tumbler(aExpression);
    PRInt32 sep = tumbler.FindChar('(');
    if (sep > 0)
      tumbler.Truncate(sep);
    GetTumblerNode(node, tumbler, getter_AddRefs(child));
    node = child;
    // char
    sep = aExpression.FindChar('(');
    if (sep > 0) {
      nsAutoString charNum(aExpression);
      if (charNum[charNum.Length() -1] == ')') {
        charNum.Truncate(charNum.Length() - 1);
        charNum.Cut(0, sep + 1);
        PRInt32 error;
        PRInt32 n = charNum.ToInteger(&error);
        if (n < 1)
          return NS_OK;
        rv = GetCharRange(node, n - 1, aRange);
        if (!*aRange) {
          node = nsnull;
        }
      }
    }
  } else {
    // Not FIXptr
  }

  if (NS_SUCCEEDED(rv) && !*aRange && node) {
    nsCOMPtr<nsIDOMRange> range(do_CreateInstance(kRangeCID));  
    if (!range)
      return NS_ERROR_OUT_OF_MEMORY;
    range->SelectNode(node);
    *aRange = range;
    NS_ADDREF(*aRange);
  }

  return rv;
}

Here is the call graph for this function:

static nsresult GetTumblerNode ( nsIDOMNode aParent,
const nsString aTumbler,
nsIDOMNode **  aNode 
) [static]

Definition at line 153 of file nsFIXptr.cpp.

{
  NS_ENSURE_ARG_POINTER(aParent);
  NS_ENSURE_ARG_POINTER(aNode);

  *aNode = nsnull;
  nsAutoString tumbler(aTumbler);
  if (!tumbler.IsEmpty() && tumbler[0] == '/')
    tumbler.Cut(0, 1);

  nsCOMPtr<nsIDOMNode> node(aParent);
  while (!tumbler.IsEmpty() && node) {
    PRInt32 sep = tumbler.FindChar('/');
    if (sep > 0) {
      nsAutoString num;
      tumbler.Left(num, sep);
      PRInt32 error;
      PRInt32 n = num.ToInteger(&error);
      if (n <= 0) {
        node = nsnull;
        break;
      }
      nsCOMPtr<nsIDOMNode> child;
      GetChild(node, n, getter_AddRefs(child));
      node = child;
    } else {
      // Last
      PRInt32 error;
      PRInt32 n = tumbler.ToInteger(&error);
      if (n <= 0) {
        node = nsnull;
        break;
      }
      nsCOMPtr<nsIDOMNode> child;
      GetChild(node, n, getter_AddRefs(child));
      node = child;
      break;
    }
    tumbler.Cut(0, sep + 1);
  }
  *aNode = node;
  NS_IF_ADDREF(*aNode);
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static NS_DEFINE_IID ( kRangeCID  ,
NS_RANGE_CID   
) [static]