|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Pest\Browser\Support; |
| 6 | + |
| 7 | +final class Selector |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Get selector by attribute text. |
| 11 | + */ |
| 12 | + public static function getByAttributeTextSelector(string $attrName, string $text, bool $exact = false): string |
| 13 | + { |
| 14 | + return "attr=[{$attrName}=".self::escapeForAttributeSelectorOrRegex($text, $exact).']'; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Get selector by test ID. |
| 19 | + */ |
| 20 | + public static function getByTestIdSelector(string $testIdAttributeName, string $testId): string |
| 21 | + { |
| 22 | + return "testid=[{$testIdAttributeName}=".self::escapeForAttributeSelectorOrRegex($testId, true).']'; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Get selector by label. |
| 27 | + */ |
| 28 | + public static function getByLabelSelector(string $text, bool $exact): string |
| 29 | + { |
| 30 | + return 'label='.self::escapeForTextSelector($text, $exact); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Get selector by alt text. |
| 35 | + */ |
| 36 | + public static function getByAltTextSelector(string $text, bool $exact): string |
| 37 | + { |
| 38 | + return self::getByAttributeTextSelector('alt', $text, $exact); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get selector by title. |
| 43 | + */ |
| 44 | + public static function getByTitleSelector(string $text, bool $exact): string |
| 45 | + { |
| 46 | + return self::getByAttributeTextSelector('title', $text, $exact); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get selector by placeholder. |
| 51 | + */ |
| 52 | + public static function getByPlaceholderSelector(string $text, bool $exact): string |
| 53 | + { |
| 54 | + return self::getByAttributeTextSelector('placeholder', $text, $exact); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get selector by text. |
| 59 | + */ |
| 60 | + public static function getByTextSelector(string $text, bool $exact): string |
| 61 | + { |
| 62 | + return 'text='.self::escapeForTextSelector($text, $exact); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Escape text for regex. |
| 67 | + */ |
| 68 | + public static function escapeForRegex(string $text): string |
| 69 | + { |
| 70 | + return preg_quote($text, '/'); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Escape for text selector. |
| 75 | + */ |
| 76 | + public static function escapeForTextSelector(string $text, bool $exact = false): string |
| 77 | + { |
| 78 | + if ($exact) { |
| 79 | + return json_encode($text).'s'; |
| 80 | + } |
| 81 | + |
| 82 | + return json_encode($text).'i'; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Escape for attribute selector or regex. |
| 87 | + */ |
| 88 | + public static function escapeForAttributeSelectorOrRegex(string $text, bool $exact = false): string |
| 89 | + { |
| 90 | + return self::escapeForAttributeSelector($text, $exact); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Escape for attribute selector. |
| 95 | + */ |
| 96 | + public static function escapeForAttributeSelector(string $text, bool $exact = false): string |
| 97 | + { |
| 98 | + $escapedText = str_replace('\\', '\\\\', $text); |
| 99 | + $escapedText = str_replace('"', '\\"', $escapedText); |
| 100 | + |
| 101 | + if ($exact) { |
| 102 | + return "\"{$escapedText}\""; |
| 103 | + } |
| 104 | + |
| 105 | + return "\"{$escapedText}\"i"; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + public static function getByRoleSelector(string $role, array $options = []): string |
| 110 | + { |
| 111 | + $props = []; |
| 112 | + |
| 113 | + if (isset($options['checked'])) { |
| 114 | + $props['checked'] = $options['checked'] ? 'true' : 'false'; |
| 115 | + } |
| 116 | + if (isset($options['disabled'])) { |
| 117 | + $props['disabled'] = $options['disabled'] ? 'true' : 'false'; |
| 118 | + } |
| 119 | + if (isset($options['selected'])) { |
| 120 | + $props['selected'] = $options['selected'] ? 'true' : 'false'; |
| 121 | + } |
| 122 | + if (isset($options['expanded'])) { |
| 123 | + $props['expanded'] = $options['expanded'] ? 'true' : 'false'; |
| 124 | + } |
| 125 | + if (isset($options['includeHidden'])) { |
| 126 | + $props['include-hidden'] = $options['includeHidden'] ? 'true' : 'false'; |
| 127 | + } |
| 128 | + if (isset($options['level'])) { |
| 129 | + $props['level'] = (string) $options['level']; |
| 130 | + } |
| 131 | + if (isset($options['name'])) { |
| 132 | + $exact = $options['exact'] ?? false; |
| 133 | + $props['name'] = self::escapeForAttributeSelector($options['name'], $exact); |
| 134 | + } |
| 135 | + if (isset($options['pressed'])) { |
| 136 | + $props['pressed'] = $options['pressed'] ? 'true' : 'false'; |
| 137 | + } |
| 138 | + |
| 139 | + $propsStr = ''; |
| 140 | + foreach ($props as $k => $v) { |
| 141 | + $propsStr .= '['.$k.'='.$v.']'; |
| 142 | + } |
| 143 | + |
| 144 | + return "role={$role}{$propsStr}"; |
| 145 | + } |
| 146 | +} |
0 commit comments