Debian Codegraph @

vendor/wikimedia/zest-css/src/Zest.php

raw
1 <?php
2
3 namespace Wikimedia\Zest;
4
5 use DOMDocument;
6 use DOMDocumentFragment;
7 use DOMElement;
8 use DOMNode;
9
10 /**
11 * Zest.php (https://github.com/wikimedia/zest.php)
12 * Copyright (c) 2019, C. Scott Ananian. (MIT licensed)
13 * PHP port based on:
14 *
15 * Zest (https://github.com/chjj/zest)
16 * A css selector engine.
17 * Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed)
18 * Domino version based on Zest v0.1.3 with bugfixes applied.
19 */
20
21 class Zest {
22
23 /** @var ZestInst */
24 private static $singleton = null;
25
26 private static function singleton() {
27 if ( !self::$singleton ) {
28 self::$singleton = new ZestInst();
29 }
30 return self::$singleton;
31 }
32
33 /**
34 * Find elements matching a CSS selector underneath $context.
35 * @param string $sel The CSS selector string
36 * @param DOMDocument|DOMDocumentFragment|DOMElement $context
37 * The scoping root for the search
38 * @param array $opts Additional match-context options (optional)
39 * @return array Elements matching the CSS selector
40 */
41 public static function find( string $sel, $context, array $opts = [] ): array {
42 return self::singleton()->find( $sel, $context, $opts );
43 }
44
45 /**
46 * Determine whether an element matches the given selector.
47 * @param DOMNode $el The element to be tested
48 * @param string $sel The CSS selector string
49 * @param array $opts Additional match-context options (optional)
50 * @return bool True iff the element matches the selector
51 */
52 public static function matches( $el, string $sel, array $opts = [] ): bool {
53 return self::singleton()->matches( $el, $sel, $opts );
54 }
55
56 /**
57 * Get descendants by ID.
58 * The PHP DOM doesn't provide this method for DOMElement, and the
59 * implementation in DOMDocument is broken.
60 *
61 * @param DOMDocument|DOMDocumentFragment|DOMElement $context
62 * The scoping root for the search
63 * @param string $id
64 * @param array $opts Additional match-context options (optional)
65 * @return array<DOMElement> A list of the elements with the given ID. When there are more
66 * than one, this method might return all of them or only the first one.
67 */
68 public static function getElementsById( $context, string $id, array $opts = [] ): array {
69 return self::singleton()->getElementsById( $context, $id, $opts );
70 }
71
72 /**
73 * Get descendants by tag name.
74 * The PHP DOM doesn't provide this method for DOMElement, and the
75 * implementation in DOMDocument has performance issues.
76 *
77 * @param DOMDocument|DOMDocumentFragment|DOMElement $context
78 * @param string $tagName
79 * @param array $opts Additional match-context options (optional)
80 * @return array<DOMElement>
81 */
82 public static function getElementsByTagName( $context, string $tagName, array $opts = [] ) {
83 return self::singleton()->getElementsByTagName( $context, $tagName, $opts );
84 }
85
86 }