B2G/gecko/content/test/unit/test_range.js
/**
 * Translate an XPath to a DOM node. This method uses a document
 * fragment as context node.
 *
 * @param aContextNode The context node to apply the XPath to.
 * @param aPath        The XPath to use.
 *
 * @return nsIDOMNode  The target node retrieved from the XPath.
 */
Translate an XPath to a DOM node. This method uses a document
fragment as context node.

@param aContextNode The context node to apply the XPath to.
@param aPath        The XPath to use.

@return nsIDOMNode  The target node retrieved from the XPath.
Translate an XPath to a DOM node . This method uses a document fragment as context node . @param aContextNode The context node to apply the XPath to . @param aPath The XPath to use . @return nsIDOMNode The target node retrieved from the XPath .
function evalXPathInDocumentFragment(aContextNode, aPath) {
  do_check_true(aContextNode instanceof C_i.nsIDOMDocumentFragment);
  do_check_true(aContextNode.childNodes.length > 0);
  if (aPath == ".") {
    return aContextNode;
  }
  var firstSlash = aPath.indexOf("/");
  if (firstSlash == -1) {
    firstSlash = aPath.length;
  }
  var prefix = aPath.substr(0, firstSlash);
  var realPath = aPath.substr(firstSlash + 1);
  if (!realPath) {
    realPath = ".";
  }
  var childIndex = 1;
  var bracketIndex = prefix.indexOf("[");
  if (bracketIndex != -1) {
    childIndex = Number(prefix.substring(bracketIndex + 1, prefix.indexOf("]")));
    do_check_true(childIndex > 0);
    prefix = prefix.substr(0, bracketIndex);
  }
  var targetType = C_i.nsIDOMNodeFilter.SHOW_ELEMENT;
  var targetNodeName = prefix;
  if (prefix.indexOf("processing-instruction(") == 0) {
    targetType = C_i.nsIDOMNodeFilter.SHOW_PROCESSING_INSTRUCTION;
    targetNodeName = prefix.substring(prefix.indexOf("(") + 2, prefix.indexOf(")") - 1);
  }
  switch (prefix) {
    case "text()":
      targetType = C_i.nsIDOMNodeFilter.SHOW_TEXT |
                   C_i.nsIDOMNodeFilter.SHOW_CDATA_SECTION;
      targetNodeName = null;
      break;
    case "comment()":
      targetType = C_i.nsIDOMNodeFilter.SHOW_COMMENT;
      targetNodeName = null;
      break;
    case "node()":
      targetType = C_i.nsIDOMNodeFilter.SHOW_ALL;
      targetNodeName = null;
  }
  var filter = {
    count: 0,
    acceptNode: function acceptNode(aNode) {
      if (aNode.parentNode != aContextNode) {
        return C_i.nsIDOMNodeFilter.FILTER_REJECT;
      }
      if (targetNodeName && targetNodeName != aNode.nodeName) {
        return C_i.nsIDOMNodeFilter.FILTER_SKIP;
      }
      this.count++;
      if (this.count != childIndex) {
        return C_i.nsIDOMNodeFilter.FILTER_SKIP;
      }
      return C_i.nsIDOMNodeFilter.FILTER_ACCEPT;
    }
  };
  var walker = aContextNode.ownerDocument.createTreeWalker(
                 aContextNode,
                 targetType,
                 filter,
                 true);
  var targetNode = walker.nextNode();
  do_check_neq(targetNode, null);
  var expr = aContextNode.ownerDocument.createExpression(realPath, null);
  var result = expr.evaluate(targetNode, UNORDERED_TYPE, null);
  do_check_true(result instanceof C_i.nsIDOMXPathResult);
  return result.singleNodeValue;
}
function evalXPathInDocumentFragment ( aContextNode , aPath ) { do_check_true ( aContextNode instanceof C_i . nsIDOMDocumentFragment ) ; do_check_true ( aContextNode . childNodes . length > 0 ) ; if ( aPath == "." ) { return aContextNode ; } var firstSlash = aPath . indexOf ( "/" ) ; if ( firstSlash == - 1 ) { firstSlash = aPath . length ; } var prefix = aPath . substr ( 0 , firstSlash ) ; var realPath = aPath . substr ( firstSlash + 1 ) ; if ( ! realPath ) { realPath = "." ; } var childIndex = 1 ; var bracketIndex = prefix . indexOf ( "[" ) ; if ( bracketIndex != - 1 ) { childIndex = Number ( prefix . substring ( bracketIndex + 1 , prefix . indexOf ( "]" ) ) ) ; do_check_true ( childIndex > 0 ) ; prefix = prefix . substr ( 0 , bracketIndex ) ; } var targetType = C_i . nsIDOMNodeFilter . SHOW_ELEMENT ; var targetNodeName = prefix ; if ( prefix . indexOf ( "processing-instruction(" ) == 0 ) { targetType = C_i . nsIDOMNodeFilter . SHOW_PROCESSING_INSTRUCTION ; targetNodeName = prefix . substring ( prefix . indexOf ( "(" ) + 2 , prefix . indexOf ( ")" ) - 1 ) ; } switch ( prefix ) { case "text()" : targetType = C_i . nsIDOMNodeFilter . SHOW_TEXT | C_i . nsIDOMNodeFilter . SHOW_CDATA_SECTION ; targetNodeName = null ; break ; case "comment()" : targetType = C_i . nsIDOMNodeFilter . SHOW_COMMENT ; targetNodeName = null ; break ; case "node()" : targetType = C_i . nsIDOMNodeFilter . SHOW_ALL ; targetNodeName = null ; } var filter = { count : 0 , acceptNode : function acceptNode ( aNode ) { if ( aNode . parentNode != aContextNode ) { return C_i . nsIDOMNodeFilter . FILTER_REJECT ; } if ( targetNodeName && targetNodeName != aNode . nodeName ) { return C_i . nsIDOMNodeFilter . FILTER_SKIP ; } this . count ++ ; if ( this . count != childIndex ) { return C_i . nsIDOMNodeFilter . FILTER_SKIP ; } return C_i . nsIDOMNodeFilter . FILTER_ACCEPT ; } } ; var walker = aContextNode . ownerDocument . createTreeWalker ( aContextNode , targetType , filter , true ) ; var targetNode = walker . nextNode ( ) ; do_check_neq ( targetNode , null ) ; var expr = aContextNode . ownerDocument . createExpression ( realPath , null ) ; var result = expr . evaluate ( targetNode , UNORDERED_TYPE , null ) ; do_check_true ( result instanceof C_i . nsIDOMXPathResult ) ; return result . singleNodeValue ; }
Translate an XPath to a DOM node.
Translate an XPath to a DOM node .
// Separate the fragment's xpath lookup from the rest. // Set up a special node filter to look among the fragment's child nodes. // nsIDOMNodeFilter // Don't bother looking at kids either. // Look for the node matching the step from the document fragment. // Apply our remaining xpath to the found node.