Skip to content

Commit

Permalink
Match attribute names in a case-insensitive manner. (#219)
Browse files Browse the repository at this point in the history
* Match attribute values in a case-insensitive manner.

* wip: test upgraded ci runners

* wip: upgrade upload/download artifact action

* wip: match artifact runner on phpstan

* wip: matrix builds

* wip: name artifacts with php version

* wip: hardcode phpunit version

* wip: cache v3

* wip: remove old artifactgs

* wip: remove old artifacts

* wip: allow failure of deleting old artifacts

* Add htmlMode contructor argument to Translator.

Also add tests for XML-mode case-sensitivity.

* build: upgrade minimum PHP version

* docs: mention xml documents in readme

---------

Co-authored-by: Greg Bowler <[email protected]>
  • Loading branch information
chrishow and g105b authored Jan 3, 2024
1 parent d99d35f commit aa941bb
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 134 deletions.
51 changes: 40 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,44 @@ on: [push, pull_request]
jobs:
composer:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 8.0, 8.1, 8.2, 8.3 ]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Cache Composer dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }}

- uses: php-actions/composer@v6
- name: Composer
uses: php-actions/composer@run-as-current-user
with:
php_version: ${{ matrix.php }}

- name: Archive build
run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./

- name: Upload build archive for test runners
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: build-artifact
name: build-php${{ matrix.php }}
path: /tmp/github-actions

phpunit:
runs-on: ubuntu-latest
needs: [composer]
strategy:
matrix:
php: [ 8.0, 8.1, 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: build-artifact
name: build-php${{ matrix.php }}
path: /tmp/github-actions

- name: Extract build archive
Expand All @@ -42,19 +51,23 @@ jobs:
- name: PHP Unit tests
uses: php-actions/phpunit@v3
with:
php_version: 7.3
version: 9
php_version: ${{ matrix.php }}
php_extensions: xdebug
configuration: test/phpunit/phpunit.xml
bootstrap: vendor/autoload.php

phpstan:
runs-on: ubuntu-latest
needs: [composer]
strategy:
matrix:
php: [ 8.0, 8.1, 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: build-artifact
name: build-php${{ matrix.php }}
path: /tmp/github-actions

- name: Extract build archive
Expand All @@ -64,3 +77,19 @@ jobs:
uses: php-actions/phpstan@v3
with:
path: src/
php_version: ${{ matrix.php }}

remove_old_artifacts:
runs-on: ubuntu-latest

steps:
- name: Remove old artifacts for prior workflow runs on this repository
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api "/repos/${{ github.repository }}/actions/artifacts" | jq ".artifacts[] | .id" > artifact-id-list.txt
while read id
do
echo -n "Deleting artifact ID $id ... "
gh api --method DELETE /repos/${{ github.repository }}/actions/artifacts/$id && echo "Done" || true
done <artifact-id-list.txt
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ $xpath = new DOMXPath($document);
$inputElementList = $xpath->query(new Translator("form>label>input");
```

## Using this library with XML Documents

To correctly work with XML documents, where the attributes are case-sensitive, pass `false` to the `htmlMode` property of the constructor.

```php
$translator = new Translator("[data-FOO='bar']", htmlMode: false);
```

It's perhaps worth noting that for XML-style matching to work, you must load the document content with DOMDocument->load/DOMDocument->loadXML instead of DOMDocument->loadHTMLFile/DOMDocument->loadHTML, as the HTML loading methods automatically convert the tags and attribute names to lowercase. This is handled automatically when using [PHP.Gt/Dom][gt-dom].

[qsa]: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
[gt-dom]: https://www.php.gt/dom
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",

"require": {
"php": ">=7.3"
"php": ">=8.0"
},

"require-dev": {
Expand Down
Loading

0 comments on commit aa941bb

Please sign in to comment.