public static function pq($arg1, $context = null)
{
if ($arg1 instanceof DOMNODE && !isset($context)) {
foreach (self::$documents as $documentWrapper) {
$compare = $arg1 instanceof DOMDocument ? $arg1 : $arg1->ownerDocument;
if ($documentWrapper->document->isSameNode($compare)) {
$context = $documentWrapper->id;
}
}
}
if (!$context) {
$domId = self::$defaultDocumentID;
if (!$domId) {
throw new Exception("Can't use last created DOM, because there isn't any. Use phpQuery::newDocument() first.");
}
// } else if (is_object($context) && ($context instanceof PHPQUERY || is_subclass_of($context, 'phpQueryObject')))
} elseif (is_object($context) && $context instanceof phpQueryObject) {
$domId = $context->getDocumentID();
} elseif ($context instanceof DOMDOCUMENT) {
$domId = self::getDocumentID($context);
if (!$domId) {
//throw new Exception('Orphaned DOMDocument');
$domId = self::newDocument($context)->getDocumentID();
}
} elseif ($context instanceof DOMNODE) {
$domId = self::getDocumentID($context);
if (!$domId) {
throw new Exception('Orphaned DOMNode');
// $domId = self::newDocument($context->ownerDocument);
}
} else {
$domId = $context;
}
if ($arg1 instanceof phpQueryObject) {
// if (is_object($arg1) && (get_class($arg1) == 'phpQueryObject' || $arg1 instanceof PHPQUERY || is_subclass_of($arg1, 'phpQueryObject'))) {
/**
* Return $arg1 or import $arg1 stack if document differs:
* pq(pq('<div/>')).
*/
if ($arg1->getDocumentID() == $domId) {
return $arg1;
}
$class = get_class($arg1);
// support inheritance by passing old object to overloaded constructor
$phpQuery = $class != 'phpQuery' ? new $class($arg1, $domId) : new phpQueryObject($domId);
$phpQuery->elements = array();
foreach ($arg1->elements as $node) {
$phpQuery->elements[] = $phpQuery->document->importNode($node, true);
}
return $phpQuery;
} elseif ($arg1 instanceof DOMNODE || is_array($arg1) && isset($arg1[0]) && $arg1[0] instanceof DOMNODE) {
/*
* Wrap DOM nodes with phpQuery object, import into document when needed:
* pq(array($domNode1, $domNode2))
*/
$phpQuery = new phpQueryObject($domId);
if (!$arg1 instanceof DOMNODELIST && !is_array($arg1)) {
$arg1 = array($arg1);
}
$phpQuery->elements = array();
foreach ($arg1 as $node) {
$sameDocument = $node->ownerDocument instanceof DOMDOCUMENT && !$node->ownerDocument->isSameNode($phpQuery->document);
$phpQuery->elements[] = $sameDocument ? $phpQuery->document->importNode($node, true) : $node;
}
return $phpQuery;
} elseif (self::isMarkup($arg1)) {
/*
* Import HTML:
* pq('<div/>')
*/
$phpQuery = new phpQueryObject($domId);
return $phpQuery->newInstance($phpQuery->documentWrapper->import($arg1));
} else {
/*
* Run CSS query:
* pq('div.myClass')
*/
$phpQuery = new phpQueryObject($domId);
// if ($context && ($context instanceof PHPQUERY || is_subclass_of($context, 'phpQueryObject')))
if ($context && $context instanceof phpQueryObject) {
$phpQuery->elements = $context->elements;
} elseif ($context && $context instanceof DOMNODELIST) {
$phpQuery->elements = array();
foreach ($context as $node) {
$phpQuery->elements[] = $node;
}
} elseif ($context && $context instanceof DOMNODE) {
$phpQuery->elements = array($context);
}
return $phpQuery->find($arg1);
}
}