PHP code example of wikimedia / zest-css

1. Go to this page and download the library: Download wikimedia/zest-css library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wikimedia / zest-css example snippets


use Wikimedia\Zest\Zest;

$els = Zest::find('section! > div[title="hello" i] > :local-link /href/ h1', $doc);

use Wikimedia\Zest\ZestInst;

$z = new ZestInst;
$z->addSelector1( ':name', function( string $param ):callable {
  return function ( $el, array $opts ) use ( $param ):bool {
    if ($el->getAttribute('name') === $param) return true;
    return false;
  };
} );

// Use it!
$z->find( 'h1:name(foo)', $document );

$z = new ZestInst;
// `$attr` is the attribute
// `$val` is the value to match
$z->addOperator( '!=', function( string $attr, string $val ):bool {
  return $attr !== $val;
} );

// Use it!
$z->find( 'h1[name != "foo"]', $document );

use Wikimedia\Zest\ZestJQ;

$z = ZestJQ::register(new ZestInst);

use Wikimedia\Zest\ZestJQ;

// Match all {{Citation needed}} transclusion markers in a MediaWiki document
$els = ZestJQ::find(
    '[typeof="mw:Transclusion"]' .
    '[data-mw/.parts[].template?.target.href == "./Template:Citation_needed"]',
    $doc
);

// Match elements whose data-mw JSON contains a `parserfunction` key anywhere
// in the parts array (path-only query: matches if the path yields any value)
// https://www.mediawiki.org/wiki/Parsoid/MediaWiki_DOM_spec/Parser_Functions
$els = ZestJQ::find( '[data-mw/.parts[].parserfunction?]', $doc );
js
$z = new ZestInst;
$z->addCombinator( '<', function( callable $test ): callable {
  return function( $el, array $opts ) use ( $test ): ?DOMNode {
    // `$el` is the current element
    $el = $el->firstChild;
    while ($el) {
      // return the relevant element
      // if it passed the test
      if ($el->nodeType === 1 && call_user_func($test, $el, $opts)) {
        return $el;
      }
      $el = $el->nextSibling;
    }
    return null;
  };
} );

// Use it!
$z->find( 'h1 < section', $document );