Skip to content

Commit

Permalink
Add htmlMode contructor argument to Translator.
Browse files Browse the repository at this point in the history
Also add tests for XML-mode case-sensitivity.
  • Loading branch information
chrishow committed Jan 3, 2024
1 parent 1633803 commit 68b23ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
17 changes: 8 additions & 9 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ class Translator {
const EQUALS_STARTS_WITH_OR_STARTS_WITH_HYPHENATED = "|=";
const EQUALS_STARTS_WITH = "^=";

/** @var string */
protected $cssSelector;
/** @var string */
protected $prefix;

public function __construct(string $cssSelector, string $prefix = ".//") {
$this->cssSelector = $cssSelector;
$this->prefix = $prefix;
public function __construct(

Check failure on line 26 in src/Translator.php

View workflow job for this annotation

GitHub Actions / phpstan (7.3)

Promoted properties are supported only on PHP 8.0 and later.

Check failure on line 26 in src/Translator.php

View workflow job for this annotation

GitHub Actions / phpstan (7.4)

Promoted properties are supported only on PHP 8.0 and later.
protected string $cssSelector,
protected string $prefix = ".//",
protected bool $htmlMode = true
) {
}

public function __toString():string {
Expand Down Expand Up @@ -197,7 +194,9 @@ protected function convertSingleSelector(string $css):string {
$hasElement = true;
}

$currentThreadItem['content'] = strtolower($currentThreadItem['content']);
if($this->htmlMode) {
$currentThreadItem['content'] = strtolower($currentThreadItem['content']);
}

/** @var null|array<int, array<string, string>> $detail */
$detail = $currentThreadItem["detail"] ?? null;
Expand Down
40 changes: 39 additions & 1 deletion test/phpunit/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function testAttribute() {
);
}

public function testCaseSensitivity() {
public function testCaseSensitivityHtmlMode() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML("<div data-FOO='bar'>baz</div>");

Expand Down Expand Up @@ -214,6 +214,44 @@ public function testCaseSensitivity() {

}

public function testCaseSensitivityXmlMode() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadXML('<div data-FOO="bar">baz</div>');

$xpath = new DOMXPath($document);

$attributeNameAndValueIsCaseSensitive = new Translator(
"[data-FOO='bar']",
prefix: '//',
htmlMode: false
);
self::assertEquals(
1,
$xpath->query($attributeNameAndValueIsCaseSensitive)->length
);

$attributeNameIsCaseSensitive = new Translator(
"[data-foo='bar']",
prefix: '//',
htmlMode: false
);
self::assertEquals(
0,
$xpath->query($attributeNameIsCaseSensitive)->length
);

$attributeValueCaseSensitive = new Translator(
"[data-FOO='BAR']",
prefix: '//',
htmlMode: false
);
self::assertEquals(
0,
$xpath->query($attributeValueCaseSensitive)->length
);

}

public function testAttributeStarSelector() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML(Helper::HTML_COMPLEX);
Expand Down

0 comments on commit 68b23ac

Please sign in to comment.