| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236 |
- <?php
- /**
- * Class representing phpQuery objects.
- *
- * @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
- * @package phpQuery
- * @method phpQueryObject clone() clone()
- * @method phpQueryObject empty() empty()
- * @method phpQueryObject next() next($selector = null)
- * @method phpQueryObject prev() prev($selector = null)
- * @property Int $length
- */
- class phpQueryObject
- implements Iterator, Countable, ArrayAccess {
- public $documentID = null;
- /**
- * DOMDocument class.
- *
- * @var DOMDocument
- */
- public $document = null;
- public $charset = null;
- /**
- *
- * @var DOMDocumentWrapper
- */
- public $documentWrapper = null;
- /**
- * XPath interface.
- *
- * @var DOMXPath
- */
- public $xpath = null;
- /**
- * Stack of selected elements.
- * @TODO refactor to ->nodes
- * @var array
- */
- public $elements = array();
- /**
- * @access private
- */
- protected $elementsBackup = array();
- /**
- * @access private
- */
- protected $previous = null;
- /**
- * @access private
- * @TODO deprecate
- */
- protected $root = array();
- /**
- * Indicated if doument is just a fragment (no <html> tag).
- *
- * Every document is realy a full document, so even documentFragments can
- * be queried against <html>, but getDocument(id)->htmlOuter() will return
- * only contents of <body>.
- *
- * @var bool
- */
- public $documentFragment = true;
- /**
- * Iterator interface helper
- * @access private
- */
- protected $elementsInterator = array();
- /**
- * Iterator interface helper
- * @access private
- */
- protected $valid = false;
- /**
- * Iterator interface helper
- * @access private
- */
- protected $current = null;
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function __construct($documentID) {
- // if ($documentID instanceof self)
- // var_dump($documentID->getDocumentID());
- $id = $documentID instanceof self
- ? $documentID->getDocumentID()
- : $documentID;
- // var_dump($id);
- if (! isset(phpQuery::$documents[$id] )) {
- // var_dump(phpQuery::$documents);
- throw new Exception("Document with ID '{$id}' isn't loaded. Use phpQuery::newDocument(\$html) or phpQuery::newDocumentFile(\$file) first.");
- }
- $this->documentID = $id;
- $this->documentWrapper =& phpQuery::$documents[$id];
- $this->document =& $this->documentWrapper->document;
- $this->xpath =& $this->documentWrapper->xpath;
- $this->charset =& $this->documentWrapper->charset;
- $this->documentFragment =& $this->documentWrapper->isDocumentFragment;
- // TODO check $this->DOM->documentElement;
- // $this->root = $this->document->documentElement;
- $this->root =& $this->documentWrapper->root;
- // $this->toRoot();
- $this->elements = array($this->root);
- }
- /**
- *
- * @access private
- * @param $attr
- * @return unknown_type
- */
- public function __get($attr) {
- switch($attr) {
- // FIXME doesnt work at all ?
- case 'length':
- return $this->size();
- break;
- default:
- return $this->$attr;
- }
- }
- /**
- * Saves actual object to $var by reference.
- * Useful when need to break chain.
- * @param phpQueryObject $var
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function toReference(&$var) {
- return $var = $this;
- }
- public function documentFragment($state = null) {
- if ($state) {
- phpQuery::$documents[$this->getDocumentID()]['documentFragment'] = $state;
- return $this;
- }
- return $this->documentFragment;
- }
- /**
- * @access private
- * @TODO documentWrapper
- */
- protected function isRoot( $node) {
- // return $node instanceof DOMDOCUMENT || $node->tagName == 'html';
- return $node instanceof DOMDOCUMENT
- || ($node instanceof DOMELEMENT && $node->tagName == 'html')
- || $this->root->isSameNode($node);
- }
- /**
- * @access private
- */
- protected function stackIsRoot() {
- return $this->size() == 1 && $this->isRoot($this->elements[0]);
- }
- /**
- * Enter description here...
- * NON JQUERY METHOD
- *
- * Watch out, it doesn't creates new instance, can be reverted with end().
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function toRoot() {
- $this->elements = array($this->root);
- return $this;
- // return $this->newInstance(array($this->root));
- }
- /**
- * Saves object's DocumentID to $var by reference.
- * <code>
- * $myDocumentId;
- * phpQuery::newDocument('<div/>')
- * ->getDocumentIDRef($myDocumentId)
- * ->find('div')->...
- * </code>
- *
- * @param unknown_type $domId
- * @see phpQuery::newDocument
- * @see phpQuery::newDocumentFile
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function getDocumentIDRef(&$documentID) {
- $documentID = $this->getDocumentID();
- return $this;
- }
- /**
- * Returns object with stack set to document root.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function getDocument() {
- return phpQuery::getDocument($this->getDocumentID());
- }
- /**
- *
- * @return DOMDocument
- */
- public function getDOMDocument() {
- return $this->document;
- }
- /**
- * Get object's Document ID.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function getDocumentID() {
- return $this->documentID;
- }
- /**
- * Unloads whole document from memory.
- * CAUTION! None further operations will be possible on this document.
- * All objects refering to it will be useless.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function unloadDocument() {
- phpQuery::unloadDocuments($this->getDocumentID());
- }
- public function isHTML() {
- return $this->documentWrapper->isHTML;
- }
- public function isXHTML() {
- return $this->documentWrapper->isXHTML;
- }
- public function isXML() {
- return $this->documentWrapper->isXML;
- }
- /**
- * Enter description here...
- *
- * @link http://docs.jquery.com/Ajax/serialize
- * @return string
- */
- public function serialize() {
- return phpQuery::param($this->serializeArray());
- }
- /**
- * Enter description here...
- *
- * @link http://docs.jquery.com/Ajax/serializeArray
- * @return array
- */
- public function serializeArray($submit = null) {
- $source = $this->filter('form, input, select, textarea')
- ->find('input, select, textarea')
- ->andSelf()
- ->not('form');
- $return = array();
- // $source->dumpDie();
- foreach($source as $input) {
- $input = phpQuery::pq($input);
- if ($input->is('[disabled]'))
- continue;
- if (!$input->is('[name]'))
- continue;
- if ($input->is('[type=checkbox]') && !$input->is('[checked]'))
- continue;
- // jquery diff
- if ($submit && $input->is('[type=submit]')) {
- if ($submit instanceof DOMELEMENT && ! $input->elements[0]->isSameNode($submit))
- continue;
- else if (is_string($submit) && $input->attr('name') != $submit)
- continue;
- }
- $return[] = array(
- 'name' => $input->attr('name'),
- 'value' => $input->val(),
- );
- }
- return $return;
- }
- /**
- * @access private
- */
- protected function debug($in) {
- if (! phpQuery::$debug )
- return;
- print('<pre>');
- print_r($in);
- // file debug
- // file_put_contents(dirname(__FILE__).'/phpQuery.log', print_r($in, true)."\n", FILE_APPEND);
- // quite handy debug trace
- // if ( is_array($in))
- // print_r(array_slice(debug_backtrace(), 3));
- print("</pre>\n");
- }
- /**
- * @access private
- */
- protected function isRegexp($pattern) {
- return in_array(
- $pattern[ mb_strlen($pattern)-1 ],
- array('^','*','$')
- );
- }
- /**
- * Determines if $char is really a char.
- *
- * @param string $char
- * @return bool
- * @todo rewrite me to charcode range ! ;)
- * @access private
- */
- protected function isChar($char) {
- return extension_loaded('mbstring') && phpQuery::$mbstringSupport
- ? mb_eregi('\w', $char)
- : preg_match('@\w@', $char);
- }
- /**
- * @access private
- */
- protected function parseSelector($query) {
- // clean spaces
- // TODO include this inside parsing ?
- $query = trim(
- preg_replace('@\s+@', ' ',
- preg_replace('@\s*(>|\\+|~)\s*@', '\\1', (string)$query)
- )
- );
- $queries = array(array());
- if (! $query)
- return $queries;
- $return =& $queries[0];
- $specialChars = array('>',' ');
- // $specialCharsMapping = array('/' => '>');
- $specialCharsMapping = array();
- $strlen = mb_strlen($query);
- $classChars = array('.', '-');
- $pseudoChars = array('-');
- $tagChars = array('*', '|', '-');
- // split multibyte string
- // http://code.google.com/p/phpquery/issues/detail?id=76
- $_query = array();
- for ($i=0; $i<$strlen; $i++)
- $_query[] = mb_substr($query, $i, 1);
- $query = $_query;
- // it works, but i dont like it...
- $i = 0;
- while( $i < $strlen) {
- $c = $query[$i];
- $tmp = '';
- // TAG
- if ($this->isChar($c) || in_array($c, $tagChars)) {
- while(isset($query[$i])
- && ($this->isChar($query[$i]) || in_array($query[$i], $tagChars))) {
- $tmp .= $query[$i];
- $i++;
- }
- $return[] = $tmp;
- // IDs
- } else if ( $c == '#') {
- $i++;
- while( isset($query[$i]) && ($this->isChar($query[$i]) || $query[$i] == '-')) {
- $tmp .= $query[$i];
- $i++;
- }
- $return[] = '#'.$tmp;
- // SPECIAL CHARS
- } else if (in_array($c, $specialChars)) {
- $return[] = $c;
- $i++;
- // MAPPED SPECIAL MULTICHARS
- // } else if ( $c.$query[$i+1] == '//') {
- // $return[] = ' ';
- // $i = $i+2;
- // MAPPED SPECIAL CHARS
- } else if ( isset($specialCharsMapping[$c])) {
- $return[] = $specialCharsMapping[$c];
- $i++;
- // COMMA
- } else if ( $c == ',') {
- $queries[] = array();
- $return =& $queries[ count($queries)-1 ];
- $i++;
- while( isset($query[$i]) && $query[$i] == ' ')
- $i++;
- // CLASSES
- } else if ($c == '.') {
- while( isset($query[$i]) && ($this->isChar($query[$i]) || in_array($query[$i], $classChars))) {
- $tmp .= $query[$i];
- $i++;
- }
- $return[] = $tmp;
- // ~ General Sibling Selector
- } else if ($c == '~') {
- $spaceAllowed = true;
- $tmp .= $query[$i++];
- while( isset($query[$i])
- && ($this->isChar($query[$i])
- || in_array($query[$i], $classChars)
- || $query[$i] == '*'
- || ($query[$i] == ' ' && $spaceAllowed)
- )) {
- if ($query[$i] != ' ')
- $spaceAllowed = false;
- $tmp .= $query[$i];
- $i++;
- }
- $return[] = $tmp;
- // + Adjacent sibling selectors
- } else if ($c == '+') {
- $spaceAllowed = true;
- $tmp .= $query[$i++];
- while( isset($query[$i])
- && ($this->isChar($query[$i])
- || in_array($query[$i], $classChars)
- || $query[$i] == '*'
- || ($spaceAllowed && $query[$i] == ' ')
- )) {
- if ($query[$i] != ' ')
- $spaceAllowed = false;
- $tmp .= $query[$i];
- $i++;
- }
- $return[] = $tmp;
- // ATTRS
- } else if ($c == '[') {
- $stack = 1;
- $tmp .= $c;
- while( isset($query[++$i])) {
- $tmp .= $query[$i];
- if ( $query[$i] == '[') {
- $stack++;
- } else if ( $query[$i] == ']') {
- $stack--;
- if (! $stack )
- break;
- }
- }
- $return[] = $tmp;
- $i++;
- // PSEUDO CLASSES
- } else if ($c == ':') {
- $stack = 1;
- $tmp .= $query[$i++];
- while( isset($query[$i]) && ($this->isChar($query[$i]) || in_array($query[$i], $pseudoChars))) {
- $tmp .= $query[$i];
- $i++;
- }
- // with arguments ?
- if ( isset($query[$i]) && $query[$i] == '(') {
- $tmp .= $query[$i];
- $stack = 1;
- while( isset($query[++$i])) {
- $tmp .= $query[$i];
- if ( $query[$i] == '(') {
- $stack++;
- } else if ( $query[$i] == ')') {
- $stack--;
- if (! $stack )
- break;
- }
- }
- $return[] = $tmp;
- $i++;
- } else {
- $return[] = $tmp;
- }
- } else {
- $i++;
- }
- }
- foreach($queries as $k => $q) {
- if (isset($q[0])) {
- if (isset($q[0][0]) && $q[0][0] == ':')
- array_unshift($queries[$k], '*');
- if ($q[0] != '>')
- array_unshift($queries[$k], ' ');
- }
- }
- return $queries;
- }
- /**
- * Return matched DOM nodes.
- *
- * @param int $index
- * @return array|DOMElement Single DOMElement or array of DOMElement.
- */
- public function get($index = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- $return = isset($index)
- ? (isset($this->elements[$index]) ? $this->elements[$index] : null)
- : $this->elements;
- // pass thou callbacks
- $args = func_get_args();
- $args = array_slice($args, 1);
- foreach($args as $callback) {
- if (is_array($return))
- foreach($return as $k => $v)
- $return[$k] = phpQuery::callbackRun($callback, array($v));
- else
- $return = phpQuery::callbackRun($callback, array($return));
- }
- return $return;
- }
- /**
- * Return matched DOM nodes.
- * jQuery difference.
- *
- * @param int $index
- * @return array|string Returns string if $index != null
- * @todo implement callbacks
- * @todo return only arrays ?
- * @todo maybe other name...
- */
- public function getString($index = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- if (!is_null($index) && is_int($index))
- $return = $this->eq($index)->text();
- else {
- $return = array();
- for($i = 0; $i < $this->size(); $i++) {
- $return[] = $this->eq($i)->text();
- }
- }
- // pass thou callbacks
- $args = func_get_args();
- $args = array_slice($args, 1);
- foreach($args as $callback) {
- $return = phpQuery::callbackRun($callback, array($return));
- }
- return $return;
- }
- /**
- * Return matched DOM nodes.
- * jQuery difference.
- *
- * @param int $index
- * @return array|string Returns string if $index != null
- * @todo implement callbacks
- * @todo return only arrays ?
- * @todo maybe other name...
- */
- public function getStrings($index = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- if (!is_null($index) && is_int($index))
- $return = $this->eq($index)->text();
- else {
- $return = array();
- for($i = 0; $i < $this->size(); $i++) {
- $return[] = $this->eq($i)->text();
- }
- // pass thou callbacks
- $args = func_get_args();
- $args = array_slice($args, 1);
- }
- foreach($args as $callback) {
- if (is_array($return))
- foreach($return as $k => $v)
- $return[$k] = phpQuery::callbackRun($callback, array($v));
- else
- $return = phpQuery::callbackRun($callback, array($return));
- }
- return $return;
- }
- /**
- * Returns new instance of actual class.
- *
- * @param array $newStack Optional. Will replace old stack with new and move old one to history.c
- */
- public function newInstance($newStack = null) {
- $class = get_class($this);
- // support inheritance by passing old object to overloaded constructor
- $new = $class != 'phpQuery'
- ? new $class($this, $this->getDocumentID())
- : new phpQueryObject($this->getDocumentID());
- $new->previous = $this;
- if (is_null($newStack)) {
- $new->elements = $this->elements;
- if ($this->elementsBackup)
- $this->elements = $this->elementsBackup;
- } else if (is_string($newStack)) {
- $new->elements = phpQuery::pq($newStack, $this->getDocumentID())->stack();
- } else {
- $new->elements = $newStack;
- }
- return $new;
- }
- /**
- * Enter description here...
- *
- * In the future, when PHP will support XLS 2.0, then we would do that this way:
- * contains(tokenize(@class, '\s'), "something")
- * @param unknown_type $class
- * @param unknown_type $node
- * @return boolean
- * @access private
- */
- protected function matchClasses($class, $node) {
- // multi-class
- if ( mb_strpos($class, '.', 1)) {
- $classes = explode('.', substr($class, 1));
- $classesCount = count( $classes );
- $nodeClasses = preg_split("/[\s\t\r\n]+/", $node->getAttribute('class'),-1, PREG_SPLIT_NO_EMPTY);
- $nodeClassesCount = count( $nodeClasses );
- if ( $classesCount > $nodeClassesCount )
- return false;
- $diff = count(
- array_diff(
- $classes,
- $nodeClasses
- )
- );
- if (! $diff )
- return true;
- // single-class
- } else {
- return in_array(
- // strip leading dot from class name
- substr($class, 1),
- // get classes for element as array
- preg_split("/[\s\t\r\n]+/", $node->getAttribute('class'),-1, PREG_SPLIT_NO_EMPTY)
- );
- }
- }
- /**
- * @access private
- */
- protected function runQuery($XQuery, $selector = null, $compare = null) {
- if ($compare && ! method_exists($this, $compare))
- return false;
- $stack = array();
- if (! $this->elements)
- $this->debug('Stack empty, skipping...');
- // var_dump($this->elements[0]->nodeType);
- // element, document
- foreach($this->stack(array(1, 9, 13)) as $k => $stackNode) {
- $detachAfter = false;
- // to work on detached nodes we need temporary place them somewhere
- // thats because context xpath queries sucks ;]
- $testNode = $stackNode;
- while ($testNode) {
- if (! $testNode->parentNode && ! $this->isRoot($testNode)) {
- $this->root->appendChild($testNode);
- $detachAfter = $testNode;
- break;
- }
- $testNode = isset($testNode->parentNode)
- ? $testNode->parentNode
- : null;
- }
- // XXX tmp ?
- $xpath = $this->documentWrapper->isXHTML
- ? $this->getNodeXpath($stackNode, 'html')
- : $this->getNodeXpath($stackNode);
- // FIXME pseudoclasses-only query, support XML
- $query = $XQuery == '//' && $xpath == '/html[1]'
- ? '//*'
- : $xpath.$XQuery;
- $this->debug("XPATH: {$query}");
- // run query, get elements
- $nodes = $this->xpath->query($query);
- $this->debug("QUERY FETCHED");
- if (! $nodes->length )
- $this->debug('Nothing found');
- $debug = array();
- foreach($nodes as $node) {
- $matched = false;
- if ( $compare) {
- phpQuery::$debug ?
- $this->debug("Found: ".$this->whois( $node ).", comparing with {$compare}()")
- : null;
- $phpQueryDebug = phpQuery::$debug;
- phpQuery::$debug = false;
- // TODO ??? use phpQuery::callbackRun()
- if (call_user_func_array(array($this, $compare), array($selector, $node)))
- $matched = true;
- phpQuery::$debug = $phpQueryDebug;
- } else {
- $matched = true;
- }
- if ( $matched) {
- if (phpQuery::$debug)
- $debug[] = $this->whois( $node );
- $stack[] = $node;
- }
- }
- if (phpQuery::$debug) {
- $this->debug("Matched ".count($debug).": ".implode(', ', $debug));
- }
- if ($detachAfter)
- $this->root->removeChild($detachAfter);
- }
- $this->elements = $stack;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function find($selectors, $context = null, $noHistory = false) {
- if (!$noHistory)
- // backup last stack /for end()/
- $this->elementsBackup = $this->elements;
- // allow to define context
- // TODO combine code below with phpQuery::pq() context guessing code
- // as generic function
- if ($context) {
- if (! is_array($context) && $context instanceof DOMELEMENT)
- $this->elements = array($context);
- else if (is_array($context)) {
- $this->elements = array();
- foreach ($context as $c)
- if ($c instanceof DOMELEMENT)
- $this->elements[] = $c;
- } else if ( $context instanceof self )
- $this->elements = $context->elements;
- }
- $queries = $this->parseSelector($selectors);
- $this->debug(array('FIND', $selectors, $queries));
- $XQuery = '';
- // remember stack state because of multi-queries
- $oldStack = $this->elements;
- // here we will be keeping found elements
- $stack = array();
- foreach($queries as $selector) {
- $this->elements = $oldStack;
- $delimiterBefore = false;
- foreach($selector as $s) {
- // TAG
- $isTag = extension_loaded('mbstring') && phpQuery::$mbstringSupport
- ? mb_ereg_match('^[\w|\||-]+$', $s) || $s == '*'
- : preg_match('@^[\w|\||-]+$@', $s) || $s == '*';
- if ($isTag) {
- if ($this->isXML()) {
- // namespace support
- if (mb_strpos($s, '|') !== false) {
- $ns = $tag = null;
- list($ns, $tag) = explode('|', $s);
- $XQuery .= "$ns:$tag";
- } else if ($s == '*') {
- $XQuery .= "*";
- } else {
- $XQuery .= "*[local-name()='$s']";
- }
- } else {
- $XQuery .= $s;
- }
- // ID
- } else if ($s[0] == '#') {
- if ($delimiterBefore)
- $XQuery .= '*';
- $XQuery .= "[@id='".substr($s, 1)."']";
- // ATTRIBUTES
- } else if ($s[0] == '[') {
- if ($delimiterBefore)
- $XQuery .= '*';
- // strip side brackets
- $attr = trim($s, '][');
- $execute = false;
- // attr with specifed value
- if (mb_strpos($s, '=')) {
- $value = null;
- list($attr, $value) = explode('=', $attr);
- $value = trim($value, "'\"");
- if ($this->isRegexp($attr)) {
- // cut regexp character
- $attr = substr($attr, 0, -1);
- $execute = true;
- $XQuery .= "[@{$attr}]";
- } else {
- $XQuery .= "[@{$attr}='{$value}']";
- }
- // attr without specified value
- } else {
- $XQuery .= "[@{$attr}]";
- }
- if ($execute) {
- $this->runQuery($XQuery, $s, 'is');
- $XQuery = '';
- if (! $this->length())
- break;
- }
- // CLASSES
- } else if ($s[0] == '.') {
- // TODO use return $this->find("./self::*[contains(concat(\" \",@class,\" \"), \" $class \")]");
- // thx wizDom ;)
- if ($delimiterBefore)
- $XQuery .= '*';
- $XQuery .= '[@class]';
- $this->runQuery($XQuery, $s, 'matchClasses');
- $XQuery = '';
- if (! $this->length() )
- break;
- // ~ General Sibling Selector
- } else if ($s[0] == '~') {
- $this->runQuery($XQuery);
- $XQuery = '';
- $this->elements = $this
- ->siblings(
- substr($s, 1)
- )->elements;
- if (! $this->length() )
- break;
- // + Adjacent sibling selectors
- } else if ($s[0] == '+') {
- // TODO /following-sibling::
- $this->runQuery($XQuery);
- $XQuery = '';
- $subSelector = substr($s, 1);
- $subElements = $this->elements;
- $this->elements = array();
- foreach($subElements as $node) {
- // search first DOMElement sibling
- $test = $node->nextSibling;
- while($test && ! ($test instanceof DOMELEMENT))
- $test = $test->nextSibling;
- if ($test && $this->is($subSelector, $test))
- $this->elements[] = $test;
- }
- if (! $this->length() )
- break;
- // PSEUDO CLASSES
- } else if ($s[0] == ':') {
- // TODO optimization for :first :last
- if ($XQuery) {
- $this->runQuery($XQuery);
- $XQuery = '';
- }
- if (! $this->length())
- break;
- $this->pseudoClasses($s);
- if (! $this->length())
- break;
- // DIRECT DESCENDANDS
- } else if ($s == '>') {
- $XQuery .= '/';
- $delimiterBefore = 2;
- // ALL DESCENDANDS
- } else if ($s == ' ') {
- $XQuery .= '//';
- $delimiterBefore = 2;
- // ERRORS
- } else {
- phpQuery::debug("Unrecognized token '$s'");
- }
- $delimiterBefore = $delimiterBefore === 2;
- }
- // run query if any
- if ($XQuery && $XQuery != '//') {
- $this->runQuery($XQuery);
- $XQuery = '';
- }
- foreach($this->elements as $node)
- if (! $this->elementsContainsNode($node, $stack))
- $stack[] = $node;
- }
- $this->elements = $stack;
- return $this->newInstance();
- }
- /**
- * @todo create API for classes with pseudoselectors
- * @access private
- */
- protected function pseudoClasses($class) {
- // TODO clean args parsing ?
- $class = ltrim($class, ':');
- $haveArgs = mb_strpos($class, '(');
- if ($haveArgs !== false) {
- $args = substr($class, $haveArgs+1, -1);
- $class = substr($class, 0, $haveArgs);
- }
- switch($class) {
- case 'even':
- case 'odd':
- $stack = array();
- foreach($this->elements as $i => $node) {
- if ($class == 'even' && ($i%2) == 0)
- $stack[] = $node;
- else if ( $class == 'odd' && $i % 2 )
- $stack[] = $node;
- }
- $this->elements = $stack;
- break;
- case 'eq':
- $k = intval($args);
- $this->elements = isset( $this->elements[$k] )
- ? array( $this->elements[$k] )
- : array();
- break;
- case 'gt':
- $this->elements = array_slice($this->elements, $args+1);
- break;
- case 'lt':
- $this->elements = array_slice($this->elements, 0, $args+1);
- break;
- case 'first':
- if (isset($this->elements[0]))
- $this->elements = array($this->elements[0]);
- break;
- case 'last':
- if ($this->elements)
- $this->elements = array($this->elements[count($this->elements)-1]);
- break;
- /*case 'parent':
- $stack = array();
- foreach($this->elements as $node) {
- if ( $node->childNodes->length )
- $stack[] = $node;
- }
- $this->elements = $stack;
- break;*/
- case 'contains':
- $text = trim($args, "\"'");
- $stack = array();
- foreach($this->elements as $node) {
- if (mb_stripos($node->textContent, $text) === false)
- continue;
- $stack[] = $node;
- }
- $this->elements = $stack;
- break;
- case 'not':
- $selector = self::unQuote($args);
- $this->elements = $this->not($selector)->stack();
- break;
- case 'slice':
- // TODO jQuery difference ?
- $args = explode(',',
- str_replace(', ', ',', trim($args, "\"'"))
- );
- $start = $args[0];
- $end = isset($args[1])
- ? $args[1]
- : null;
- if ($end > 0)
- $end = $end-$start;
- $this->elements = array_slice($this->elements, $start, $end);
- break;
- case 'has':
- $selector = trim($args, "\"'");
- $stack = array();
- foreach($this->stack(1) as $el) {
- if ($this->find($selector, $el, true)->length)
- $stack[] = $el;
- }
- $this->elements = $stack;
- break;
- case 'submit':
- case 'reset':
- $this->elements = phpQuery::merge(
- $this->map(array($this, 'is'),
- "input[type=$class]", new CallbackParam()
- ),
- $this->map(array($this, 'is'),
- "button[type=$class]", new CallbackParam()
- )
- );
- break;
- // $stack = array();
- // foreach($this->elements as $node)
- // if ($node->is('input[type=submit]') || $node->is('button[type=submit]'))
- // $stack[] = $el;
- // $this->elements = $stack;
- case 'input':
- $this->elements = $this->map(
- array($this, 'is'),
- 'input', new CallbackParam()
- )->elements;
- break;
- case 'password':
- case 'checkbox':
- case 'radio':
- case 'hidden':
- case 'image':
- case 'file':
- $this->elements = $this->map(
- array($this, 'is'),
- "input[type=$class]", new CallbackParam()
- )->elements;
- break;
- case 'parent':
- $this->elements = $this->map(
- function ($node) {
- return $node instanceof DOMELEMENT && $node->childNodes->length
- ? $node : null;
- }
- )->elements;
- break;
- case 'empty':
- $this->elements = $this->map(
- function ($node) {
- return $node instanceof DOMELEMENT && $node->childNodes->length
- ? null : $node;
- }
- )->elements;
- break;
- case 'disabled':
- case 'selected':
- case 'checked':
- $this->elements = $this->map(
- array($this, 'is'),
- "[$class]", new CallbackParam()
- )->elements;
- break;
- case 'enabled':
- $this->elements = $this->map(
- function ($node) {
- return pq($node)->not(":disabled") ? $node : null;
- }
- )->elements;
- break;
- case 'header':
- $this->elements = $this->map(
- function ($node) {
- $isHeader = isset($node->tagName) && in_array($node->tagName, array(
- "h1", "h2", "h3", "h4", "h5", "h6", "h7"
- ));
- return $isHeader
- ? $node
- : null;
- }
- )->elements;
- // $this->elements = $this->map(
- // create_function('$node', '$node = pq($node);
- // return $node->is("h1")
- // || $node->is("h2")
- // || $node->is("h3")
- // || $node->is("h4")
- // || $node->is("h5")
- // || $node->is("h6")
- // || $node->is("h7")
- // ? $node
- // : null;')
- // )->elements;
- break;
- case 'only-child':
- $this->elements = $this->map(
- function ($node) {
- return pq($node)->siblings()->size() == 0 ? $node : null;
- }
- )->elements;
- break;
- case 'first-child':
- $this->elements = $this->map(
- function ($node) { return pq($node)->prevAll()->size() == 0 ? $node : null; }
- )->elements;
- break;
- case 'last-child':
- $this->elements = $this->map(
- function ($node) { return pq($node)->nextAll()->size() == 0 ? $node : null; }
- )->elements;
- break;
- case 'nth-child':
- $param = trim($args, "\"'");
- if (! $param)
- break;
- // nth-child(n+b) to nth-child(1n+b)
- if (substr($param, 0, 1) == 'n')
- $param = '1'.$param;
- // :nth-child(index/even/odd/equation)
- if ($param == 'even' || $param == 'odd')
- $mapped = $this->map(
- function ($node, $param) {
- $index = pq($node)->prevAll()->size()+1;
- if ($param == "even" && ($index%2) == 0)
- return $node;
- else if ($param == "odd" && $index%2 == 1)
- return $node;
- else
- return null;
- },
- new CallbackParam(), $param
- );
- else if (mb_strlen($param) > 1 && preg_match('/^(\d*)n([-+]?)(\d*)/', $param) === 1)
- // an+b
- $mapped = $this->map(
- function ($node, $param) {
- $prevs = pq($node)->prevAll()->size();
- $index = 1+$prevs;
- preg_match("/^(\d*)n([-+]?)(\d*)/", $param, $matches);
- $a = intval($matches[1]);
- $b = intval($matches[3]);
- if( $matches[2] === "-" ) {
- $b = -$b;
- }
- if ($a > 0) {
- return ($index-$b)%$a == 0
- ? $node
- : null;
- phpQuery::debug($a."*".floor($index/$a)."+$b-1 == ".($a*floor($index/$a)+$b-1)." ?= $prevs");
- return $a*floor($index/$a)+$b-1 == $prevs
- ? $node
- : null;
- } else if ($a == 0)
- return $index == $b
- ? $node
- : null;
- else
- // negative value
- return $index <= $b
- ? $node
- : null;
- // if (! $b)
- // return $index%$a == 0
- // ? $node
- // : null;
- // else
- // return ($index-$b)%$a == 0
- // ? $node
- // : null;
- },
- new CallbackParam(), $param
- );
- else
- // index
- $mapped = $this->map(
- function ($node, $index) {
- $prevs = pq($node)->prevAll()->size();
- if ($prevs && $prevs == $index-1)
- return $node;
- else if (! $prevs && $index == 1)
- return $node;
- else
- return null;
- },
- new CallbackParam(), $param
- );
- $this->elements = $mapped->elements;
- break;
- default:
- $this->debug("Unknown pseudoclass '{$class}', skipping...");
- }
- }
- /**
- * @access private
- */
- protected function __pseudoClassParam($paramsString) {
- // TODO;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function is($selector, $nodes = null) {
- phpQuery::debug(array("Is:", $selector));
- if (! $selector)
- return false;
- $oldStack = $this->elements;
- $returnArray = false;
- if ($nodes && is_array($nodes)) {
- $this->elements = $nodes;
- } else if ($nodes)
- $this->elements = array($nodes);
- $this->filter($selector, true);
- $stack = $this->elements;
- $this->elements = $oldStack;
- if ($nodes)
- return $stack ? $stack : null;
- return (bool)count($stack);
- }
- /**
- * Enter description here...
- * jQuery difference.
- *
- * Callback:
- * - $index int
- * - $node DOMNode
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @link http://docs.jquery.com/Traversing/filter
- */
- public function filterCallback($callback, $_skipHistory = false) {
- if (! $_skipHistory) {
- $this->elementsBackup = $this->elements;
- $this->debug("Filtering by callback");
- }
- $newStack = array();
- foreach($this->elements as $index => $node) {
- $result = phpQuery::callbackRun($callback, array($index, $node));
- if (is_null($result) || (! is_null($result) && $result))
- $newStack[] = $node;
- }
- $this->elements = $newStack;
- return $_skipHistory
- ? $this
- : $this->newInstance();
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @link http://docs.jquery.com/Traversing/filter
- */
- public function filter($selectors, $_skipHistory = false) {
- if ($selectors instanceof Callback OR $selectors instanceof Closure)
- return $this->filterCallback($selectors, $_skipHistory);
- if (! $_skipHistory)
- $this->elementsBackup = $this->elements;
- $notSimpleSelector = array(' ', '>', '~', '+', '/');
- if (! is_array($selectors))
- $selectors = $this->parseSelector($selectors);
- if (! $_skipHistory)
- $this->debug(array("Filtering:", $selectors));
- $finalStack = array();
- foreach($selectors as $selector) {
- $stack = array();
- if (! $selector)
- break;
- // avoid first space or /
- if (in_array($selector[0], $notSimpleSelector))
- $selector = array_slice($selector, 1);
- // PER NODE selector chunks
- foreach($this->stack() as $node) {
- $break = false;
- foreach($selector as $s) {
- if (!($node instanceof DOMELEMENT)) {
- // all besides DOMElement
- if ( $s[0] == '[') {
- $attr = trim($s, '[]');
- if ( mb_strpos($attr, '=')) {
- list( $attr, $val ) = explode('=', $attr);
- if ($attr == 'nodeType' && $node->nodeType != $val)
- $break = true;
- }
- } else
- $break = true;
- } else {
- // DOMElement only
- // ID
- if ( $s[0] == '#') {
- if ( $node->getAttribute('id') != substr($s, 1) )
- $break = true;
- // CLASSES
- } else if ( $s[0] == '.') {
- if (! $this->matchClasses( $s, $node ) )
- $break = true;
- // ATTRS
- } else if ( $s[0] == '[') {
- // strip side brackets
- $attr = trim($s, '[]');
- if (mb_strpos($attr, '=')) {
- list($attr, $val) = explode('=', $attr);
- $val = self::unQuote($val);
- if ($attr == 'nodeType') {
- if ($val != $node->nodeType)
- $break = true;
- } else if ($this->isRegexp($attr)) {
- $val = extension_loaded('mbstring') && phpQuery::$mbstringSupport
- ? quotemeta(trim($val, '"\''))
- : preg_quote(trim($val, '"\''), '@');
- // switch last character
- switch( substr($attr, -1)) {
- // quotemeta used insted of preg_quote
- // http://code.google.com/p/phpquery/issues/detail?id=76
- case '^':
- $pattern = '^'.$val;
- break;
- case '*':
- $pattern = '.*'.$val.'.*';
- break;
- case '$':
- $pattern = '.*'.$val.'$';
- break;
- }
- // cut last character
- $attr = substr($attr, 0, -1);
- $isMatch = extension_loaded('mbstring') && phpQuery::$mbstringSupport
- ? mb_ereg_match($pattern, $node->getAttribute($attr))
- : preg_match("@{$pattern}@", $node->getAttribute($attr));
- if (! $isMatch)
- $break = true;
- } else if ($node->getAttribute($attr) != $val)
- $break = true;
- } else if (! $node->hasAttribute($attr))
- $break = true;
- // PSEUDO CLASSES
- } else if ( $s[0] == ':') {
- // skip
- // TAG
- } else if (trim($s)) {
- if ($s != '*') {
- // TODO namespaces
- if (isset($node->tagName)) {
- if ($node->tagName != $s)
- $break = true;
- } else if ($s == 'html' && ! $this->isRoot($node))
- $break = true;
- }
- // AVOID NON-SIMPLE SELECTORS
- } else if (in_array($s, $notSimpleSelector)) {
- $break = true;
- $this->debug(array('Skipping non simple selector', $selector));
- }
- }
- if ($break)
- break;
- }
- // if element passed all chunks of selector - add it to new stack
- if (! $break )
- $stack[] = $node;
- }
- $tmpStack = $this->elements;
- $this->elements = $stack;
- // PER ALL NODES selector chunks
- foreach($selector as $s)
- // PSEUDO CLASSES
- if ($s[0] == ':')
- $this->pseudoClasses($s);
- foreach($this->elements as $node)
- // XXX it should be merged without duplicates
- // but jQuery doesnt do that
- $finalStack[] = $node;
- $this->elements = $tmpStack;
- }
- $this->elements = $finalStack;
- if ($_skipHistory) {
- return $this;
- } else {
- $this->debug("Stack length after filter(): ".count($finalStack));
- return $this->newInstance();
- }
- }
- /**
- *
- * @param $value
- * @return unknown_type
- * @TODO implement in all methods using passed parameters
- */
- protected static function unQuote($value) {
- return $value[0] == '\'' || $value[0] == '"'
- ? substr($value, 1, -1)
- : $value;
- }
- /**
- * Enter description here...
- *
- * @link http://docs.jquery.com/Ajax/load
- * @return phpQuery|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo Support $selector
- */
- public function load($url, $data = null, $callback = null) {
- if ($data && ! is_array($data)) {
- $callback = $data;
- $data = null;
- }
- if (mb_strpos($url, ' ') !== false) {
- $matches = null;
- if (extension_loaded('mbstring') && phpQuery::$mbstringSupport)
- mb_ereg('^([^ ]+) (.*)$', $url, $matches);
- else
- preg_match('@^([^ ]+) (.*)$@', $url, $matches);
- $url = $matches[1];
- $selector = $matches[2];
- // FIXME this sucks, pass as callback param
- $this->_loadSelector = $selector;
- }
- $ajax = array(
- 'url' => $url,
- 'type' => $data ? 'POST' : 'GET',
- 'data' => $data,
- 'complete' => $callback,
- 'success' => array($this, '__loadSuccess')
- );
- phpQuery::ajax($ajax);
- return $this;
- }
- /**
- * @access private
- * @param $html
- * @return unknown_type
- */
- public function __loadSuccess($html) {
- if ($this->_loadSelector) {
- $html = phpQuery::newDocument($html)->find($this->_loadSelector);
- unset($this->_loadSelector);
- }
- foreach($this->stack(1) as $node) {
- phpQuery::pq($node, $this->getDocumentID())
- ->markup($html);
- }
- }
- /**
- * Enter description here...
- *
- * @return phpQuery|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo
- */
- public function css() {
- // TODO
- return $this;
- }
- /**
- * @todo
- *
- */
- public function show(){
- // TODO
- return $this;
- }
- /**
- * @todo
- *
- */
- public function hide(){
- // TODO
- return $this;
- }
- /**
- * Trigger a type of event on every matched element.
- *
- * @param unknown_type $type
- * @param unknown_type $data
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @TODO support more than event in $type (space-separated)
- */
- public function trigger($type, $data = array()) {
- foreach($this->elements as $node)
- phpQueryEvents::trigger($this->getDocumentID(), $type, $data, $node);
- return $this;
- }
- /**
- * This particular method triggers all bound event handlers on an element (for a specific event type) WITHOUT executing the browsers default actions.
- *
- * @param unknown_type $type
- * @param unknown_type $data
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @TODO
- */
- public function triggerHandler($type, $data = array()) {
- // TODO;
- }
- /**
- * Binds a handler to one or more events (like click) for each matched element.
- * Can also bind custom events.
- *
- * @param unknown_type $type
- * @param unknown_type $data Optional
- * @param unknown_type $callback
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @TODO support '!' (exclusive) events
- * @TODO support more than event in $type (space-separated)
- */
- public function bind($type, $data, $callback = null) {
- // TODO check if $data is callable, not using is_callable
- if (! isset($callback)) {
- $callback = $data;
- $data = null;
- }
- foreach($this->elements as $node)
- phpQueryEvents::add($this->getDocumentID(), $node, $type, $data, $callback);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @param unknown_type $type
- * @param unknown_type $callback
- * @return unknown
- * @TODO namespace events
- * @TODO support more than event in $type (space-separated)
- */
- public function unbind($type = null, $callback = null) {
- foreach($this->elements as $node)
- phpQueryEvents::remove($this->getDocumentID(), $node, $type, $callback);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function change($callback = null) {
- if ($callback)
- return $this->bind('change', $callback);
- return $this->trigger('change');
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function submit($callback = null) {
- if ($callback)
- return $this->bind('submit', $callback);
- return $this->trigger('submit');
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function click($callback = null) {
- if ($callback)
- return $this->bind('click', $callback);
- return $this->trigger('click');
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrapAllOld($wrapper) {
- $wrapper = pq($wrapper)->_clone();
- if (! $wrapper->length() || ! $this->length() )
- return $this;
- $wrapper->insertBefore($this->elements[0]);
- $deepest = $wrapper->elements[0];
- while($deepest->firstChild && $deepest->firstChild instanceof DOMELEMENT)
- $deepest = $deepest->firstChild;
- pq($deepest)->append($this);
- return $this;
- }
- /**
- * Enter description here...
- *
- * TODO testme...
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrapAll($wrapper) {
- if (! $this->length())
- return $this;
- return phpQuery::pq($wrapper, $this->getDocumentID())
- ->clone()
- ->insertBefore($this->get(0))
- ->map(array($this, '___wrapAllCallback'))
- ->append($this);
- }
- /**
- *
- * @param $node
- * @return unknown_type
- * @access private
- */
- public function ___wrapAllCallback($node) {
- $deepest = $node;
- while($deepest->firstChild && $deepest->firstChild instanceof DOMELEMENT)
- $deepest = $deepest->firstChild;
- return $deepest;
- }
- /**
- * Enter description here...
- * NON JQUERY METHOD
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrapAllPHP($codeBefore, $codeAfter) {
- return $this
- ->slice(0, 1)
- ->beforePHP($codeBefore)
- ->end()
- ->slice(-1)
- ->afterPHP($codeAfter)
- ->end();
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrap($wrapper) {
- foreach($this->stack() as $node)
- phpQuery::pq($node, $this->getDocumentID())->wrapAll($wrapper);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrapPHP($codeBefore, $codeAfter) {
- foreach($this->stack() as $node)
- phpQuery::pq($node, $this->getDocumentID())->wrapAllPHP($codeBefore, $codeAfter);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrapInner($wrapper) {
- foreach($this->stack() as $node)
- phpQuery::pq($node, $this->getDocumentID())->contents()->wrapAll($wrapper);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function wrapInnerPHP($codeBefore, $codeAfter) {
- foreach($this->stack(1) as $node)
- phpQuery::pq($node, $this->getDocumentID())->contents()
- ->wrapAllPHP($codeBefore, $codeAfter);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @testme Support for text nodes
- */
- public function contents() {
- $stack = array();
- foreach($this->stack(1) as $el) {
- // FIXME (fixed) http://code.google.com/p/phpquery/issues/detail?id=56
- // if (! isset($el->childNodes))
- // continue;
- foreach($el->childNodes as $node) {
- $stack[] = $node;
- }
- }
- return $this->newInstance($stack);
- }
- /**
- * Enter description here...
- *
- * jQuery difference.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function contentsUnwrap() {
- foreach($this->stack(1) as $node) {
- if (! $node->parentNode )
- continue;
- $childNodes = array();
- // any modification in DOM tree breaks childNodes iteration, so cache them first
- foreach($node->childNodes as $chNode )
- $childNodes[] = $chNode;
- foreach($childNodes as $chNode )
- // $node->parentNode->appendChild($chNode);
- $node->parentNode->insertBefore($chNode, $node);
- $node->parentNode->removeChild($node);
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * jQuery difference.
- */
- public function switchWith($markup) {
- $markup = pq($markup, $this->getDocumentID());
- $content = null;
- foreach($this->stack(1) as $node) {
- pq($node)
- ->contents()->toReference($content)->end()
- ->replaceWith($markup->clone()->append($content));
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function eq($num) {
- $oldStack = $this->elements;
- $this->elementsBackup = $this->elements;
- $this->elements = array();
- if ( isset($oldStack[$num]) )
- $this->elements[] = $oldStack[$num];
- return $this->newInstance();
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
-
- #[\ReturnTypeWillChange]
- public function size() {
- return count($this->elements);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @deprecated Use length as attribute
- */
- #[\ReturnTypeWillChange]
- public function length() {
- return $this->size();
- }
-
- #[\ReturnTypeWillChange]
- public function count() {
- return $this->size();
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo $level
- */
-
- #[\ReturnTypeWillChange]
- public function end($level = 1) {
- // $this->elements = array_pop( $this->history );
- // return $this;
- // $this->previous->DOM = $this->DOM;
- // $this->previous->XPath = $this->XPath;
- return $this->previous
- ? $this->previous
- : $this;
- }
- /**
- * Enter description here...
- * Normal use ->clone() .
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @access private
- */
- public function _clone() {
- $newStack = array();
- //pr(array('copy... ', $this->whois()));
- //$this->dumpHistory('copy');
- $this->elementsBackup = $this->elements;
- foreach($this->elements as $node) {
- $newStack[] = $node->cloneNode(true);
- }
- $this->elements = $newStack;
- return $this->newInstance();
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function replaceWithPHP($code) {
- return $this->replaceWith(phpQuery::php($code));
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery $content
- * @link http://docs.jquery.com/Manipulation/replaceWith#content
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function replaceWith($content) {
- return $this->after($content)->remove();
- }
- /**
- * Enter description here...
- *
- * @param String $selector
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo this works ?
- */
- public function replaceAll($selector) {
- foreach(phpQuery::pq($selector, $this->getDocumentID()) as $node)
- phpQuery::pq($node, $this->getDocumentID())
- ->after($this->_clone())
- ->remove();
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function remove($selector = null) {
- $loop = $selector
- ? $this->filter($selector)->elements
- : $this->elements;
- foreach($loop as $node) {
- if (! $node->parentNode )
- continue;
- if (isset($node->tagName))
- $this->debug("Removing '{$node->tagName}'");
- $node->parentNode->removeChild($node);
- // Mutation event
- $event = new DOMEvent(array(
- 'target' => $node,
- 'type' => 'DOMNodeRemoved'
- ));
- phpQueryEvents::trigger($this->getDocumentID(),
- $event->type, array($event), $node
- );
- }
- return $this;
- }
- protected function markupEvents($newMarkup, $oldMarkup, $node) {
- if ($node->tagName == 'textarea' && $newMarkup != $oldMarkup) {
- $event = new DOMEvent(array(
- 'target' => $node,
- 'type' => 'change'
- ));
- phpQueryEvents::trigger($this->getDocumentID(),
- $event->type, array($event), $node
- );
- }
- }
- /**
- * jQuey difference
- *
- * @param $markup
- * @return unknown_type
- * @TODO trigger change event for textarea
- */
- public function markup($markup = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- $args = func_get_args();
- if ($this->documentWrapper->isXML)
- return call_user_func_array(array($this, 'xml'), $args);
- else
- return call_user_func_array(array($this, 'html'), $args);
- }
- /**
- * jQuey difference
- *
- * @param $markup
- * @return unknown_type
- */
- public function markupOuter($callback1 = null, $callback2 = null, $callback3 = null) {
- $args = func_get_args();
- if ($this->documentWrapper->isXML)
- return call_user_func_array(array($this, 'xmlOuter'), $args);
- else
- return call_user_func_array(array($this, 'htmlOuter'), $args);
- }
- /**
- * Enter description here...
- *
- * @param unknown_type $html
- * @return string|phpQuery|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @TODO force html result
- */
- public function html($html = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- if (isset($html)) {
- // INSERT
- $nodes = $this->documentWrapper->import($html);
- $this->empty();
- foreach($this->stack(1) as $alreadyAdded => $node) {
- // for now, limit events for textarea
- if (($this->isXHTML() || $this->isHTML()) && $node->tagName == 'textarea')
- $oldHtml = pq($node, $this->getDocumentID())->markup();
- foreach($nodes as $newNode) {
- $node->appendChild($alreadyAdded
- ? $newNode->cloneNode(true)
- : $newNode
- );
- }
- // for now, limit events for textarea
- if (($this->isXHTML() || $this->isHTML()) && $node->tagName == 'textarea')
- $this->markupEvents($html, $oldHtml, $node);
- }
- return $this;
- } else {
- // FETCH
- $return = $this->documentWrapper->markup($this->elements, true);
- $args = func_get_args();
- foreach(array_slice($args, 1) as $callback) {
- $return = phpQuery::callbackRun($callback, array($return));
- }
- return $return;
- }
- }
- /**
- * @TODO force xml result
- */
- public function xml($xml = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- $args = func_get_args();
- return call_user_func_array(array($this, 'html'), $args);
- }
- /**
- * Enter description here...
- * @TODO force html result
- *
- * @return String
- */
- public function htmlOuter($callback1 = null, $callback2 = null, $callback3 = null) {
- $markup = $this->documentWrapper->markup($this->elements);
- // pass thou callbacks
- $args = func_get_args();
- foreach($args as $callback) {
- $markup = phpQuery::callbackRun($callback, array($markup));
- }
- return $markup;
- }
- /**
- * @TODO force xml result
- */
- public function xmlOuter($callback1 = null, $callback2 = null, $callback3 = null) {
- $args = func_get_args();
- return call_user_func_array(array($this, 'htmlOuter'), $args);
- }
- public function __toString() {
- return $this->markupOuter();
- }
- /**
- * Just like html(), but returns markup with VALID (dangerous) PHP tags.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo support returning markup with PHP tags when called without param
- */
- public function php($code = null) {
- return $this->markupPHP($code);
- }
- /**
- * Enter description here...
- *
- * @param $code
- * @return unknown_type
- */
- public function markupPHP($code = null) {
- return isset($code)
- ? $this->markup(phpQuery::php($code))
- : phpQuery::markupToPHP($this->markup());
- }
- /**
- * Enter description here...
- *
- * @param $code
- * @return unknown_type
- */
- public function markupOuterPHP() {
- return phpQuery::markupToPHP($this->markupOuter());
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function children($selector = null) {
- $stack = array();
- foreach($this->stack(1) as $node) {
- // foreach($node->getElementsByTagName('*') as $newNode) {
- foreach($node->childNodes as $newNode) {
- if ($newNode->nodeType != 1)
- continue;
- if ($selector && ! $this->is($selector, $newNode))
- continue;
- if ($this->elementsContainsNode($newNode, $stack))
- continue;
- $stack[] = $newNode;
- }
- }
- $this->elementsBackup = $this->elements;
- $this->elements = $stack;
- return $this->newInstance();
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function ancestors($selector = null) {
- return $this->children( $selector );
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function append( $content) {
- return $this->insert($content, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function appendPHP( $content) {
- return $this->insert("<php><!-- {$content} --></php>", 'append');
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function appendTo( $seletor) {
- return $this->insert($seletor, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function prepend( $content) {
- return $this->insert($content, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @todo accept many arguments, which are joined, arrays maybe also
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function prependPHP( $content) {
- return $this->insert("<php><!-- {$content} --></php>", 'prepend');
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function prependTo( $seletor) {
- return $this->insert($seletor, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function before($content) {
- return $this->insert($content, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function beforePHP( $content) {
- return $this->insert("<php><!-- {$content} --></php>", 'before');
- }
- /**
- * Enter description here...
- *
- * @param String|phpQuery
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function insertBefore( $seletor) {
- return $this->insert($seletor, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function after( $content) {
- return $this->insert($content, __FUNCTION__);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function afterPHP( $content) {
- return $this->insert("<php><!-- {$content} --></php>", 'after');
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function insertAfter( $seletor) {
- return $this->insert($seletor, __FUNCTION__);
- }
- /**
- * Internal insert method. Don't use it.
- *
- * @param unknown_type $target
- * @param unknown_type $type
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @access private
- */
- public function insert($target, $type) {
- $this->debug("Inserting data with '{$type}'");
- $to = false;
- switch( $type) {
- case 'appendTo':
- case 'prependTo':
- case 'insertBefore':
- case 'insertAfter':
- $to = true;
- }
- switch(gettype($target)) {
- case 'string':
- $insertFrom = $insertTo = array();
- if ($to) {
- // INSERT TO
- $insertFrom = $this->elements;
- if (phpQuery::isMarkup($target)) {
- // $target is new markup, import it
- $insertTo = $this->documentWrapper->import($target);
- // insert into selected element
- } else {
- // $tagret is a selector
- $thisStack = $this->elements;
- $this->toRoot();
- $insertTo = $this->find($target)->elements;
- $this->elements = $thisStack;
- }
- } else {
- // INSERT FROM
- $insertTo = $this->elements;
- $insertFrom = $this->documentWrapper->import($target);
- }
- break;
- case 'object':
- $insertFrom = $insertTo = array();
- // phpQuery
- if ($target instanceof self) {
- if ($to) {
- $insertTo = $target->elements;
- if ($this->documentFragment && $this->stackIsRoot())
- // get all body children
- // $loop = $this->find('body > *')->elements;
- // TODO test it, test it hard...
- // $loop = $this->newInstance($this->root)->find('> *')->elements;
- $loop = $this->root->childNodes;
- else
- $loop = $this->elements;
- // import nodes if needed
- $insertFrom = $this->getDocumentID() == $target->getDocumentID()
- ? $loop
- : $target->documentWrapper->import($loop);
- } else {
- $insertTo = $this->elements;
- if ( $target->documentFragment && $target->stackIsRoot() )
- // get all body children
- // $loop = $target->find('body > *')->elements;
- $loop = $target->root->childNodes;
- else
- $loop = $target->elements;
- // import nodes if needed
- $insertFrom = $this->getDocumentID() == $target->getDocumentID()
- ? $loop
- : $this->documentWrapper->import($loop);
- }
- // DOMNODE
- } elseif ($target instanceof DOMNODE) {
- // import node if needed
- // if ( $target->ownerDocument != $this->DOM )
- // $target = $this->DOM->importNode($target, true);
- if ( $to) {
- $insertTo = array($target);
- if ($this->documentFragment && $this->stackIsRoot())
- // get all body children
- $loop = $this->root->childNodes;
- // $loop = $this->find('body > *')->elements;
- else
- $loop = $this->elements;
- foreach($loop as $fromNode)
- // import nodes if needed
- $insertFrom[] = ! $fromNode->ownerDocument->isSameNode($target->ownerDocument)
- ? $target->ownerDocument->importNode($fromNode, true)
- : $fromNode;
- } else {
- // import node if needed
- if (! $target->ownerDocument->isSameNode($this->document))
- $target = $this->document->importNode($target, true);
- $insertTo = $this->elements;
- $insertFrom[] = $target;
- }
- }
- break;
- }
- phpQuery::debug("From ".count($insertFrom)."; To ".count($insertTo)." nodes");
- foreach($insertTo as $insertNumber => $toNode) {
- // we need static relative elements in some cases
- switch( $type) {
- case 'prependTo':
- case 'prepend':
- $firstChild = $toNode->firstChild;
- break;
- case 'insertAfter':
- case 'after':
- $nextSibling = $toNode->nextSibling;
- break;
- }
- foreach($insertFrom as $fromNode) {
- // clone if inserted already before
- $insert = $insertNumber
- ? $fromNode->cloneNode(true)
- : $fromNode;
- switch($type) {
- case 'appendTo':
- case 'append':
- // $toNode->insertBefore(
- // $fromNode,
- // $toNode->lastChild->nextSibling
- // );
- $toNode->appendChild($insert);
- $eventTarget = $insert;
- break;
- case 'prependTo':
- case 'prepend':
- $toNode->insertBefore(
- $insert,
- $firstChild
- );
- break;
- case 'insertBefore':
- case 'before':
- if (! $toNode->parentNode)
- throw new Exception("No parentNode, can't do {$type}()");
- else
- $toNode->parentNode->insertBefore(
- $insert,
- $toNode
- );
- break;
- case 'insertAfter':
- case 'after':
- if (! $toNode->parentNode)
- throw new Exception("No parentNode, can't do {$type}()");
- else
- $toNode->parentNode->insertBefore(
- $insert,
- $nextSibling
- );
- break;
- }
- // Mutation event
- $event = new DOMEvent(array(
- 'target' => $insert,
- 'type' => 'DOMNodeInserted'
- ));
- phpQueryEvents::trigger($this->getDocumentID(),
- $event->type, array($event), $insert
- );
- }
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return Int
- */
- public function index($subject) {
- $index = -1;
- $subject = $subject instanceof phpQueryObject
- ? $subject->elements[0]
- : $subject;
- foreach($this->newInstance() as $k => $node) {
- if ($node->isSameNode($subject))
- $index = $k;
- }
- return $index;
- }
- /**
- * Enter description here...
- *
- * @param unknown_type $start
- * @param unknown_type $end
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @testme
- */
- public function slice($start, $end = null) {
- // $last = count($this->elements)-1;
- // $end = $end
- // ? min($end, $last)
- // : $last;
- // if ($start < 0)
- // $start = $last+$start;
- // if ($start > $last)
- // return array();
- if ($end > 0)
- $end = $end-$start;
- return $this->newInstance(
- array_slice($this->elements, $start, $end)
- );
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function reverse() {
- $this->elementsBackup = $this->elements;
- $this->elements = array_reverse($this->elements);
- return $this->newInstance();
- }
- /**
- * Return joined text content.
- * @return String
- */
- public function text($text = null, $callback1 = null, $callback2 = null, $callback3 = null) {
- if (isset($text))
- return $this->html(htmlspecialchars($text));
- $args = func_get_args();
- $args = array_slice($args, 1);
- $return = '';
- foreach($this->elements as $node) {
- $text = $node->textContent;
- if (count($this->elements) > 1 && $text)
- $text .= "\n";
- foreach($args as $callback) {
- $text = phpQuery::callbackRun($callback, array($text));
- }
- $return .= $text;
- }
- return $return;
- }
- /**
- * @return The text content of each matching element, like
- * text() but returns an array with one entry per matched element.
- * Read only.
- */
- public function texts($attr = null) {
- $results = array();
- foreach($this->elements as $node) {
- $results[] = $node->textContent;
- }
- return $results;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function plugin($class, $file = null) {
- phpQuery::plugin($class, $file);
- return $this;
- }
- /**
- * Deprecated, use $pq->plugin() instead.
- *
- * @deprecated
- * @param $class
- * @param $file
- * @return unknown_type
- */
- public static function extend($class, $file = null) {
- return $this->plugin($class, $file);
- }
- /**
- *
- * @access private
- * @param $method
- * @param $args
- * @return unknown_type
- */
- public function __call($method, $args) {
- $aliasMethods = array('clone', 'empty');
- if (isset(phpQuery::$extendMethods[$method])) {
- array_unshift($args, $this);
- return phpQuery::callbackRun(
- phpQuery::$extendMethods[$method], $args
- );
- } else if (isset(phpQuery::$pluginsMethods[$method])) {
- array_unshift($args, $this);
- $class = phpQuery::$pluginsMethods[$method];
- $realClass = "phpQueryObjectPlugin_$class";
- $return = call_user_func_array(
- array($realClass, $method),
- $args
- );
- // XXX deprecate ?
- return is_null($return)
- ? $this
- : $return;
- } else if (in_array($method, $aliasMethods)) {
- return call_user_func_array(array($this, '_'.$method), $args);
- } else
- throw new Exception("Method '{$method}' doesnt exist");
- }
- /**
- * Safe rename of next().
- *
- * Use it ONLY when need to call next() on an iterated object (in same time).
- * Normaly there is no need to do such thing ;)
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @access private
- */
- public function _next($selector = null) {
- return $this->newInstance(
- $this->getElementSiblings('nextSibling', $selector, true)
- );
- }
- /**
- * Use prev() and next().
- *
- * @deprecated
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @access private
- */
- public function _prev($selector = null) {
- return $this->prev($selector);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function prev($selector = null) {
- return $this->newInstance(
- $this->getElementSiblings('previousSibling', $selector, true)
- );
- }
- /**
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo
- */
- public function prevAll($selector = null) {
- return $this->newInstance(
- $this->getElementSiblings('previousSibling', $selector)
- );
- }
- /**
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo FIXME: returns source elements insted of next siblings
- */
- public function nextAll($selector = null) {
- return $this->newInstance(
- $this->getElementSiblings('nextSibling', $selector)
- );
- }
- /**
- * @access private
- */
- protected function getElementSiblings($direction, $selector = null, $limitToOne = false) {
- $stack = array();
- $count = 0;
- foreach($this->stack() as $node) {
- $test = $node;
- while( isset($test->{$direction}) && $test->{$direction}) {
- $test = $test->{$direction};
- if (! $test instanceof DOMELEMENT)
- continue;
- $stack[] = $test;
- if ($limitToOne)
- break;
- }
- }
- if ($selector) {
- $stackOld = $this->elements;
- $this->elements = $stack;
- $stack = $this->filter($selector, true)->stack();
- $this->elements = $stackOld;
- }
- return $stack;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function siblings($selector = null) {
- $stack = array();
- $siblings = array_merge(
- $this->getElementSiblings('previousSibling', $selector),
- $this->getElementSiblings('nextSibling', $selector)
- );
- foreach($siblings as $node) {
- if (! $this->elementsContainsNode($node, $stack))
- $stack[] = $node;
- }
- return $this->newInstance($stack);
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function not($selector = null) {
- if (is_string($selector))
- phpQuery::debug(array('not', $selector));
- else
- phpQuery::debug('not');
- $stack = array();
- if ($selector instanceof self || $selector instanceof DOMNODE) {
- foreach($this->stack() as $node) {
- if ($selector instanceof self) {
- $matchFound = false;
- foreach($selector->stack() as $notNode) {
- if ($notNode->isSameNode($node))
- $matchFound = true;
- }
- if (! $matchFound)
- $stack[] = $node;
- } else if ($selector instanceof DOMNODE) {
- if (! $selector->isSameNode($node))
- $stack[] = $node;
- } else {
- if (! $this->is($selector))
- $stack[] = $node;
- }
- }
- } else {
- $orgStack = $this->stack();
- $matched = $this->filter($selector, true)->stack();
- // $matched = array();
- // // simulate OR in filter() instead of AND 5y
- // foreach($this->parseSelector($selector) as $s) {
- // $matched = array_merge($matched,
- // $this->filter(array($s))->stack()
- // );
- // }
- foreach($orgStack as $node)
- if (! $this->elementsContainsNode($node, $matched))
- $stack[] = $node;
- }
- return $this->newInstance($stack);
- }
- /**
- * Enter description here...
- *
- * @param string|phpQueryObject
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function add($selector = null) {
- if (! $selector)
- return $this;
- $stack = array();
- $this->elementsBackup = $this->elements;
- $found = phpQuery::pq($selector, $this->getDocumentID());
- $this->merge($found->elements);
- return $this->newInstance();
- }
- /**
- * @access private
- */
- protected function merge() {
- foreach(func_get_args() as $nodes)
- foreach($nodes as $newNode )
- if (! $this->elementsContainsNode($newNode) )
- $this->elements[] = $newNode;
- }
- /**
- * @access private
- * TODO refactor to stackContainsNode
- */
- protected function elementsContainsNode($nodeToCheck, $elementsStack = null) {
- $loop = ! is_null($elementsStack)
- ? $elementsStack
- : $this->elements;
- foreach($loop as $node) {
- if ( $node->isSameNode( $nodeToCheck ) )
- return true;
- }
- return false;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function parent($selector = null) {
- $stack = array();
- foreach($this->elements as $node )
- if ( $node->parentNode && ! $this->elementsContainsNode($node->parentNode, $stack) )
- $stack[] = $node->parentNode;
- $this->elementsBackup = $this->elements;
- $this->elements = $stack;
- if ( $selector )
- $this->filter($selector, true);
- return $this->newInstance();
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function parents($selector = null) {
- $stack = array();
- if (! $this->elements )
- $this->debug('parents() - stack empty');
- foreach($this->elements as $node) {
- $test = $node;
- while( $test->parentNode) {
- $test = $test->parentNode;
- if ($this->isRoot($test))
- break;
- if (! $this->elementsContainsNode($test, $stack)) {
- $stack[] = $test;
- continue;
- }
- }
- }
- $this->elementsBackup = $this->elements;
- $this->elements = $stack;
- if ( $selector )
- $this->filter($selector, true);
- return $this->newInstance();
- }
- /**
- * Internal stack iterator.
- *
- * @access private
- * @return {Array.<DOMNode>}
- */
- public function stack($nodeTypes = null) {
- if (!isset($nodeTypes))
- return $this->elements;
- if (!is_array($nodeTypes))
- $nodeTypes = array($nodeTypes);
- $return = array();
- foreach($this->elements as $node) {
- if (in_array($node->nodeType, $nodeTypes))
- $return[] = $node;
- }
- return $return;
- }
- // TODO phpdoc; $oldAttr is result of hasAttribute, before any changes
- protected function attrEvents($attr, $oldAttr, $oldValue, $node) {
- // skip events for XML documents
- if (! $this->isXHTML() && ! $this->isHTML())
- return;
- $event = null;
- // identify
- $isInputValue = $node->tagName == 'input'
- && (
- in_array($node->getAttribute('type'),
- array('text', 'password', 'hidden'))
- || !$node->getAttribute('type')
- );
- $isRadio = $node->tagName == 'input'
- && $node->getAttribute('type') == 'radio';
- $isCheckbox = $node->tagName == 'input'
- && $node->getAttribute('type') == 'checkbox';
- $isOption = $node->tagName == 'option';
- if ($isInputValue && $attr == 'value' && $oldValue != $node->getAttribute($attr)) {
- $event = new DOMEvent(array(
- 'target' => $node,
- 'type' => 'change'
- ));
- } else if (($isRadio || $isCheckbox) && $attr == 'checked' && (
- // check
- (! $oldAttr && $node->hasAttribute($attr))
- // un-check
- || (! $node->hasAttribute($attr) && $oldAttr)
- )) {
- $event = new DOMEvent(array(
- 'target' => $node,
- 'type' => 'change'
- ));
- } else if ($isOption && $node->parentNode && $attr == 'selected' && (
- // select
- (! $oldAttr && $node->hasAttribute($attr))
- // un-select
- || (! $node->hasAttribute($attr) && $oldAttr)
- )) {
- $event = new DOMEvent(array(
- 'target' => $node->parentNode,
- 'type' => 'change'
- ));
- }
- if ($event) {
- phpQueryEvents::trigger($this->getDocumentID(),
- $event->type, array($event), $node
- );
- }
- }
- public function attr($attr = null, $value = null) {
- foreach($this->stack(1) as $node) {
- if (! is_null($value)) {
- $loop = $attr == '*'
- ? $this->getNodeAttrs($node)
- : array($attr);
- foreach($loop as $a) {
- $oldValue = $node->getAttribute($a);
- $oldAttr = $node->hasAttribute($a);
- // TODO raises an error when charset other than UTF-8
- // while document's charset is also not UTF-8
- @$node->setAttribute($a, $value);
- $this->attrEvents($a, $oldAttr, $oldValue, $node);
- }
- } else if ($attr == '*') {
- // jQuery difference
- $return = array();
- foreach($node->attributes as $n => $v)
- $return[$n] = $v->value;
- return $return;
- } else
- return $node->hasAttribute($attr)
- ? $node->getAttribute($attr)
- : null;
- }
- return is_null($value)
- ? '' : $this;
- }
- /**
- * @return The same attribute of each matching element, like
- * attr() but returns an array with one entry per matched element.
- * Read only.
- */
- public function attrs($attr = null) {
- $results = array();
- foreach($this->stack(1) as $node) {
- $results[] = $node->hasAttribute($attr)
- ? $node->getAttribute($attr)
- : null;
- }
- return $results;
- }
- /**
- * @access private
- */
- protected function getNodeAttrs($node) {
- $return = array();
- foreach($node->attributes as $n => $o)
- $return[] = $n;
- return $return;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo check CDATA ???
- */
- public function attrPHP($attr, $code) {
- if (! is_null($code)) {
- $value = '<'.'?php '.$code.' ?'.'>';
- // TODO tempolary solution
- // http://code.google.com/p/phpquery/issues/detail?id=17
- // if (function_exists('mb_detect_encoding') && mb_detect_encoding($value) == 'ASCII')
- // $value = mb_convert_encoding($value, 'UTF-8', 'HTML-ENTITIES');
- }
- foreach($this->stack(1) as $node) {
- if (! is_null($code)) {
- // $attrNode = $this->DOM->createAttribute($attr);
- $node->setAttribute($attr, $value);
- // $attrNode->value = $value;
- // $node->appendChild($attrNode);
- } else if ( $attr == '*') {
- // jQuery diff
- $return = array();
- foreach($node->attributes as $n => $v)
- $return[$n] = $v->value;
- return $return;
- } else
- return $node->getAttribute($attr);
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function removeAttr($attr) {
- foreach($this->stack(1) as $node) {
- $loop = $attr == '*'
- ? $this->getNodeAttrs($node)
- : array($attr);
- foreach($loop as $a) {
- $oldValue = $node->getAttribute($a);
- $node->removeAttribute($a);
- $this->attrEvents($a, $oldValue, null, $node);
- }
- }
- return $this;
- }
- /**
- * Return form element value.
- *
- * @return String Fields value.
- */
- public function val($val = null) {
- if (! isset($val)) {
- if ($this->eq(0)->is('select')) {
- $selected = $this->eq(0)->find('option[selected=selected]');
- if ($selected->is('[value]'))
- return $selected->attr('value');
- else
- return $selected->text();
- } else if ($this->eq(0)->is('textarea'))
- return $this->eq(0)->markup();
- else
- return $this->eq(0)->attr('value');
- } else {
- $_val = null;
- foreach($this->stack(1) as $node) {
- $node = pq($node, $this->getDocumentID());
- if (is_array($val) && in_array($node->attr('type'), array('checkbox', 'radio'))) {
- $isChecked = in_array($node->attr('value'), $val)
- || in_array($node->attr('name'), $val);
- if ($isChecked)
- $node->attr('checked', 'checked');
- else
- $node->removeAttr('checked');
- } else if ($node->get(0)->tagName == 'select') {
- if (! isset($_val)) {
- $_val = array();
- if (! is_array($val))
- $_val = array((string)$val);
- else
- foreach($val as $v)
- $_val[] = $v;
- }
- foreach($node['option']->stack(1) as $option) {
- $option = pq($option, $this->getDocumentID());
- $selected = false;
- // XXX: workaround for string comparsion, see issue #96
- // http://code.google.com/p/phpquery/issues/detail?id=96
- $selected = is_null($option->attr('value'))
- ? in_array($option->markup(), $_val)
- : in_array($option->attr('value'), $_val);
- // $optionValue = $option->attr('value');
- // $optionText = $option->text();
- // $optionTextLenght = mb_strlen($optionText);
- // foreach($_val as $v)
- // if ($optionValue == $v)
- // $selected = true;
- // else if ($optionText == $v && $optionTextLenght == mb_strlen($v))
- // $selected = true;
- if ($selected)
- $option->attr('selected', 'selected');
- else
- $option->removeAttr('selected');
- }
- } else if ($node->get(0)->tagName == 'textarea')
- $node->markup($val);
- else
- $node->attr('value', $val);
- }
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function andSelf() {
- if ( $this->previous )
- $this->elements = array_merge($this->elements, $this->previous->elements);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function addClass( $className) {
- if (! $className)
- return $this;
- foreach($this->stack(1) as $node) {
- if (! $this->is(".$className", $node))
- $node->setAttribute(
- 'class',
- trim($node->getAttribute('class').' '.$className)
- );
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function addClassPHP( $className) {
- foreach($this->stack(1) as $node) {
- $classes = $node->getAttribute('class');
- $newValue = $classes
- ? $classes.' <'.'?php '.$className.' ?'.'>'
- : '<'.'?php '.$className.' ?'.'>';
- $node->setAttribute('class', $newValue);
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @param string $className
- * @return bool
- */
- public function hasClass($className) {
- foreach($this->stack(1) as $node) {
- if ( $this->is(".$className", $node))
- return true;
- }
- return false;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function removeClass($className) {
- foreach($this->stack(1) as $node) {
- $classes = explode( ' ', $node->getAttribute('class'));
- if ( in_array($className, $classes)) {
- $classes = array_diff($classes, array($className));
- if ( $classes )
- $node->setAttribute('class', implode(' ', $classes));
- else
- $node->removeAttribute('class');
- }
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function toggleClass($className) {
- foreach($this->stack(1) as $node) {
- if ( $this->is( $node, '.'.$className ))
- $this->removeClass($className);
- else
- $this->addClass($className);
- }
- return $this;
- }
- /**
- * Proper name without underscore (just ->empty()) also works.
- *
- * Removes all child nodes from the set of matched elements.
- *
- * Example:
- * pq("p")._empty()
- *
- * HTML:
- * <p>Hello, <span>Person</span> <a href="#">and person</a></p>
- *
- * Result:
- * [ <p></p> ]
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @access private
- */
- public function _empty() {
- foreach($this->stack(1) as $node) {
- // thx to 'dave at dgx dot cz'
- $node->nodeValue = '';
- }
- return $this;
- }
- /**
- * Enter description here...
- *
- * @param array|string $callback Expects $node as first param, $index as second
- * @param array $scope External variables passed to callback. Use compact('varName1', 'varName2'...) and extract($scope)
- * @param array $arg1 Will ba passed as third and futher args to callback.
- * @param array $arg2 Will ba passed as fourth and futher args to callback, and so on...
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function each($callback, $param1 = null, $param2 = null, $param3 = null) {
- $paramStructure = null;
- if (func_num_args() > 1) {
- $paramStructure = func_get_args();
- $paramStructure = array_slice($paramStructure, 1);
- }
- foreach($this->elements as $v)
- phpQuery::callbackRun($callback, array($v), $paramStructure);
- return $this;
- }
- /**
- * Run callback on actual object.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- public function callback($callback, $param1 = null, $param2 = null, $param3 = null) {
- $params = func_get_args();
- $params[0] = $this;
- phpQuery::callbackRun($callback, $params);
- return $this;
- }
- /**
- * Enter description here...
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- * @todo add $scope and $args as in each() ???
- */
- public function map($callback, $param1 = null, $param2 = null, $param3 = null) {
- // $stack = array();
- //// foreach($this->newInstance() as $node) {
- // foreach($this->newInstance() as $node) {
- // $result = call_user_func($callback, $node);
- // if ($result)
- // $stack[] = $result;
- // }
- $params = func_get_args();
- array_unshift($params, $this->elements);
- return $this->newInstance(
- call_user_func_array(array('phpQuery', 'map'), $params)
- // phpQuery::map($this->elements, $callback)
- );
- }
- /**
- * Enter description here...
- *
- * @param <type> $key
- * @param <type> $value
- */
- public function data($key, $value = null) {
- if (! isset($value)) {
- // TODO? implement specific jQuery behavior od returning parent values
- // is child which we look up doesn't exist
- return phpQuery::data($this->get(0), $key, $value, $this->getDocumentID());
- } else {
- foreach($this as $node)
- phpQuery::data($node, $key, $value, $this->getDocumentID());
- return $this;
- }
- }
- /**
- * Enter description here...
- *
- * @param <type> $key
- */
- public function removeData($key) {
- foreach($this as $node)
- phpQuery::removeData($node, $key, $this->getDocumentID());
- return $this;
- }
- // INTERFACE IMPLEMENTATIONS
- // ITERATOR INTERFACE
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function rewind(){
- $this->debug('iterating foreach');
- // phpQuery::selectDocument($this->getDocumentID());
- $this->elementsBackup = $this->elements;
- $this->elementsInterator = $this->elements;
- $this->valid = isset( $this->elements[0] )
- ? 1 : 0;
- // $this->elements = $this->valid
- // ? array($this->elements[0])
- // : array();
- $this->current = 0;
- }
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function current(){
- return $this->elementsInterator[ $this->current ];
- }
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function key(){
- return $this->current;
- }
- /**
- * Double-function method.
- *
- * First: main iterator interface method.
- * Second: Returning next sibling, alias for _next().
- *
- * Proper functionality is choosed automagicaly.
- *
- * @see phpQueryObject::_next()
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- */
- #[\ReturnTypeWillChange]
- public function next($cssSelector = null){
- // if ($cssSelector || $this->valid)
- // return $this->_next($cssSelector);
- $this->valid = isset( $this->elementsInterator[ $this->current+1 ] )
- ? true
- : false;
- if (! $this->valid && $this->elementsInterator) {
- $this->elementsInterator = null;
- } else if ($this->valid) {
- $this->current++;
- } else {
- return $this->_next($cssSelector);
- }
- }
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function valid(){
- return $this->valid;
- }
- // ITERATOR INTERFACE END
- // ARRAYACCESS INTERFACE
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function offsetExists($offset) {
- return $this->find($offset)->size() > 0;
- }
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function offsetGet($offset) {
- return $this->find($offset);
- }
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function offsetSet($offset, $value) {
- // $this->find($offset)->replaceWith($value);
- $this->find($offset)->html($value);
- }
- /**
- * @access private
- */
- #[\ReturnTypeWillChange]
- public function offsetUnset($offset) {
- // empty
- throw new Exception("Can't do unset, use array interface only for calling queries and replacing HTML.");
- }
- // ARRAYACCESS INTERFACE END
- /**
- * Returns node's XPath.
- *
- * @param unknown_type $oneNode
- * @return string
- * @TODO use native getNodePath is avaible
- * @access private
- */
- protected function getNodeXpath($oneNode = null, $namespace = null) {
- $return = array();
- $loop = $oneNode
- ? array($oneNode)
- : $this->elements;
- // if ($namespace)
- // $namespace .= ':';
- foreach($loop as $node) {
- if ($node instanceof DOMDOCUMENT) {
- $return[] = '';
- continue;
- }
- $xpath = array();
- while(! ($node instanceof DOMDOCUMENT)) {
- $i = 1;
- $sibling = $node;
- while($sibling->previousSibling) {
- $sibling = $sibling->previousSibling;
- $isElement = $sibling instanceof DOMELEMENT;
- if ($isElement && $sibling->tagName == $node->tagName)
- $i++;
- }
- $xpath[] = $this->isXML()
- ? "*[local-name()='{$node->tagName}'][{$i}]"
- : "{$node->tagName}[{$i}]";
- $node = $node->parentNode;
- }
- $xpath = implode('/', array_reverse($xpath));
- $return[] = '/'.$xpath;
- }
- return $oneNode
- ? $return[0]
- : $return;
- }
- // HELPERS
- public function whois($oneNode = null) {
- $return = array();
- $loop = $oneNode
- ? array( $oneNode )
- : $this->elements;
- foreach($loop as $node) {
- if (isset($node->tagName)) {
- $tag = in_array($node->tagName, array('php', 'js'))
- ? strtoupper($node->tagName)
- : $node->tagName;
- $return[] = $tag
- .($node->getAttribute('id')
- ? '#'.$node->getAttribute('id'):'')
- .($node->getAttribute('class')
- ? '.'.implode('.', explode(' ', $node->getAttribute('class'))):'')
- .($node->getAttribute('name')
- ? '[name="'.$node->getAttribute('name').'"]':'')
- .($node->getAttribute('value') && strpos($node->getAttribute('value'), '<'.'?php') === false
- ? '[value="'.substr(str_replace("\n", '', $node->getAttribute('value')), 0, 15).'"]':'')
- .($node->getAttribute('value') && strpos($node->getAttribute('value'), '<'.'?php') !== false
- ? '[value=PHP]':'')
- .($node->getAttribute('selected')
- ? '[selected]':'')
- .($node->getAttribute('checked')
- ? '[checked]':'')
- ;
- } else if ($node instanceof DOMTEXT) {
- if (trim($node->textContent))
- $return[] = 'Text:'.substr(str_replace("\n", ' ', $node->textContent), 0, 15);
- } else {
- }
- }
- return $oneNode && isset($return[0])
- ? $return[0]
- : $return;
- }
- /**
- * Dump htmlOuter and preserve chain. Usefull for debugging.
- *
- * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
- *
- */
- public function dump() {
- print 'DUMP #'.(phpQuery::$dumpCount++).' ';
- $debug = phpQuery::$debug;
- phpQuery::$debug = false;
- // print __FILE__.':'.__LINE__."\n";
- var_dump($this->htmlOuter());
- return $this;
- }
- public function dumpWhois() {
- print 'DUMP #'.(phpQuery::$dumpCount++).' ';
- $debug = phpQuery::$debug;
- phpQuery::$debug = false;
- // print __FILE__.':'.__LINE__."\n";
- var_dump('whois', $this->whois());
- phpQuery::$debug = $debug;
- return $this;
- }
- public function dumpLength() {
- print 'DUMP #'.(phpQuery::$dumpCount++).' ';
- $debug = phpQuery::$debug;
- phpQuery::$debug = false;
- // print __FILE__.':'.__LINE__."\n";
- var_dump('length', $this->length());
- phpQuery::$debug = $debug;
- return $this;
- }
- public function dumpTree($html = true, $title = true) {
- $output = $title
- ? 'DUMP #'.(phpQuery::$dumpCount++)." \n" : '';
- $debug = phpQuery::$debug;
- phpQuery::$debug = false;
- foreach($this->stack() as $node)
- $output .= $this->__dumpTree($node);
- phpQuery::$debug = $debug;
- print $html
- ? nl2br(str_replace(' ', ' ', $output))
- : $output;
- return $this;
- }
- private function __dumpTree($node, $intend = 0) {
- $whois = $this->whois($node);
- $return = '';
- if ($whois)
- $return .= str_repeat(' - ', $intend).$whois."\n";
- if (isset($node->childNodes))
- foreach($node->childNodes as $chNode)
- $return .= $this->__dumpTree($chNode, $intend+1);
- return $return;
- }
- /**
- * Dump htmlOuter and stop script execution. Usefull for debugging.
- *
- */
- public function dumpDie() {
- print __FILE__.':'.__LINE__;
- var_dump($this->htmlOuter());
- die();
- }
- }
|