Ejemplo n.º 1
6
 /**
  * Enter description here...
  *
  * @return false|phpQuery|queryTemplatesFetch|queryTemplatesParse|queryTemplatesPickup
  */
 public static function pq($arg1, $context = null)
 {
     // TODO support DOMNodes as $context, find out ownerDocument, search loaded DOMs
     if (!$context) {
         $domId = self::$lastDomId;
     } else {
         if ($context instanceof self) {
             $domId = $context->domId;
         } else {
             if ($context instanceof DOMDOCUMENT) {
                 foreach (phpQuery::$documents as $id => $document) {
                     if ($context->isSameNode($document['document'])) {
                         $domId = $id;
                     }
                 }
                 if (!$domId) {
                     throw new Exception('Orphaned DOMNode');
                     //				$domId = self::newDocument($context);
                 }
             } else {
                 if ($context instanceof DOMNODE) {
                     foreach (phpQuery::$documents as $id => $document) {
                         if ($context->ownerDocument->isSameNode($document['document'])) {
                             $domId = $id;
                         }
                     }
                     if (!$domId) {
                         throw new Exception('Orphaned DOMNode');
                         //				$domId = self::newDocument($context->ownerDocument);
                     }
                 } else {
                     $domId = $context;
                 }
             }
         }
     }
     if ($arg1 instanceof self) {
         /**
          * Return $arg1 or import $arg1 stack if document differs:
          * pq(pq('<div/>'))
          */
         if ($arg1->domId == $domId) {
             return $arg1;
         }
         $phpQuery = new phpQuery($domId);
         $phpQuery->elements = array();
         foreach ($arg1->elements as $node) {
             $phpQuery->elements[] = $phpQuery->DOM->importNode($node, true);
         }
         return $phpQuery;
     } else {
         if ($arg1 instanceof DOMNODE || is_array($arg1) && isset($arg1[0]) && $arg[0] instanceof DOMNODE) {
             /**
              * Wrap DOM nodes with phpQuery object, import into document when needed:
              * pq(array($domNode1, $domNode2))
              */
             $phpQuery = new phpQuery($domId);
             if (!$arg1 instanceof DOMNODELIST && !is_array($arg1)) {
                 $arg1 = array($arg1);
             }
             $phpQuery->elements = array();
             foreach ($arg1 as $node) {
                 $phpQuery->elements[] = !$node->ownerDocument->isSameNode($phpQuery->DOM) ? $phpQuery->DOM->importNode($node, true) : $node;
             }
             return $phpQuery;
         } else {
             if (self::isMarkup($arg1)) {
                 /**
                  * Import HTML:
                  * pq('<div/>')
                  */
                 $phpQuery = new phpQuery($domId);
                 $phpQuery->importMarkup($arg1);
                 return $phpQuery;
             } else {
                 /**
                  * Run CSS query:
                  * pq('div.myClass')
                  */
                 $phpQuery = new phpQuery($domId);
                 if ($context && $context instanceof self) {
                     $phpQuery->elements = $context->elements;
                 } else {
                     if ($context && $context instanceof DOMNODELIST) {
                         $phpQuery->elements = array();
                         foreach ($context as $node) {
                             $phpQuery->elements[] = $node;
                         }
                     } else {
                         if ($context && $context instanceof DOMNODE) {
                             $phpQuery->elements = array(DOMNODE);
                         }
                     }
                 }
                 return $phpQuery->find($arg1);
             }
         }
     }
 }