Skip to content

Commit

Permalink
Maintenance & github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Sep 18, 2023
1 parent 54781cd commit 4307350
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 152 deletions.
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# perform LF normalization
*.php text eol=lf
*.js text eol=lf
*.json text eol=lf
*.md text eol=lf
*.xml text eol=lf
*.yml text eol=lf

*.png binary

/.code-climate.json export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
49 changes: 49 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: PHPUnit
on: [push, pull_request]
jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
# windows-latest, macOS-latest
operating-system: [ubuntu-latest]
php-version: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
name: PHP ${{ matrix.php-version }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2 # https://github.com/marketplace/actions/setup-php-action
with:
php-version: ${{ matrix.php-version }}
extensions: intl #optional
ini-values: "post_max_size=256M, memory_limit=512M"
coverage: xdebug #optional
- name: Check PHP Version
run: php -v
- name: Validate composer.json and composer.lock
run: composer validate --strict
- name: Cache composer packages
uses: actions/cache@v3
id: composer-cache
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: |
composer --version
composer install --prefer-dist --no-progress
- name: Unit test
run: composer run test
- name: Publish code coverage
uses: paambaati/[email protected]
if: matrix.php-version == '8.2' && ${{ github.ref_name == 'master' }}
continue-on-error: true
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
debug: false
coverageCommand: vendor/bin/phpunit --coverage-clover coverage/clover.xml
coverageLocations: coverage/clover.xml:clover
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.phpunit.result.cache
/composer.lock
/coverage
/vendor
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ CssXpath
* Provide PHPUnit Assertions (once provided by PHPUnit)

![No Dependencies](https://img.shields.io/badge/dependencies-none-333333.svg)
[![Build Status](https://img.shields.io/travis/com/bkdotcom/CssXpath.svg)](https://travis-ci.com/bkdotcom/CssXpath)
![Supported PHP versions](https://img.shields.io/static/v1?label=PHP&message=5.4%20-%208.2&color=blue)
![Build Status](https://img.shields.io/github/actions/workflow/status/bkdotcom/CssXpath/phpunit.yml.svg?logo=github)
[![Maintainability](https://img.shields.io/codeclimate/maintainability/bkdotcom/CssXpath.svg?logo=codeclimate)](https://codeclimate.com/github/bkdotcom/CssXpath)
[![Coverage](https://img.shields.io/codeclimate/coverage-letter/bkdotcom/CssXpath.svg?logo=codeclimate)](https://codeclimate.com/github/bkdotcom/CssXpath)

## Installation

Expand Down
22 changes: 20 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,27 @@
}
],
"autoload": {
"psr-4": { "bdk\\CssXpath\\": "src/" }
"psr-4": {
"bdk\\CssXpath\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"bdk\\Test\\": "tests/"
}
},
"require": {
"php": ">=5.3.0"
}
},
"require-dev": {
"phpunit/phpunit": "^4.0 | ^5.0 | ^6.0 | ^7.0 | ^8.0 | ^9.0"
},
"scripts": {
"coverage" : [
"vendor/bin/phpunit -v --coverage-clover coverage/clover.xml --coverage-html coverage/html"
],
"test" : [
"vendor/bin/phpunit -v"
]
}
}
17 changes: 16 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
beStrictAboutCoversAnnotation="false"
bootstrap="tests/bootstrap.php"
colors="true"
forceCoversAnnotation="true"
testdox="true"
verbose="true"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="CssXpath">
<directory>tests</directory>
Expand Down
41 changes: 19 additions & 22 deletions src/CssSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @package CssXPath
* @author Brad Kent <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2018 Brad Kent
* @copyright 2018-2023 Brad Kent
* @version 1.0
*
* @link http://www.github.com/bkdotcom/CssXpath
Expand All @@ -15,6 +15,10 @@
namespace bdk\CssXpath;

use bdk\CssXpath\CssXpath;
use DOMDocument;
use DOMElement;
use DOMNodeList;
use DOMXpath;

/**
* CSS selector class
Expand All @@ -33,7 +37,6 @@
*/
class CssSelect
{

protected $domXpath;

/**
Expand Down Expand Up @@ -146,11 +149,11 @@ protected function selectNonStatic($selector, $asDomList = false)
/**
* Convert DOMNodeList to an array.
*
* @param \DOMNodeList $elements elements
* @param DOMNodeList $elements elements
*
* @return array
*/
protected static function elementsToArray(\DOMNodeList $elements)
protected static function elementsToArray(DOMNodeList $elements)
{
$array = array();
for ($i = 0, $length = $elements->length; $i < $length; ++$i) {
Expand All @@ -164,16 +167,16 @@ protected static function elementsToArray(\DOMNodeList $elements)
/**
* Convert DOMElement to an array.
*
* @param \DOMElement $element element
* @param DOMElement $element element
*
* @return array
*/
protected static function elementToArray(\DOMElement $element)
protected static function elementToArray(DOMElement $element)
{
$array = array(
'name' => $element->nodeName,
'attributes' => array(),
'innerHTML' => self::domInnerHtml($element),
'name' => $element->nodeName,
);
foreach ($element->attributes as $key => $attr) {
$array['attributes'][$key] = $attr->value;
Expand All @@ -184,48 +187,42 @@ protected static function elementToArray(\DOMElement $element)
/**
* Build inner html for given DOMElement
*
* @param \DOMElement $element dom element
* @param DOMElement $element dom element
*
* @return string html
*/
protected static function domInnerHtml(\DOMElement $element)
protected static function domInnerHtml(DOMElement $element)
{
$innerHTML = '';
foreach ($element->childNodes as $child) {
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
$innerHTML = \preg_replace('/{amp}([0-9a-z]+);/i', '&\1;', $innerHTML);
// $innerHTML = str_replace("\xc2\xa0", ' ', $innerHTML); // &nbsp; && &#160; get converted to UTF-8 \xc2\xa0
/*
saveHTML doesn't close "void" tags :(
*/
$voidTags = array('area','base','br','col','command','embed','hr','img','input','keygen','link','meta','param','source','track','wbr');
$voidTags = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');
$regEx = '#<(' . \implode('|', $voidTags) . ')(\b[^>]*)>#';
$innerHTML = \preg_replace($regEx, '<\\1\\2 />', $innerHTML);
return \trim($innerHTML);
}

/**
* Return \DOMXpath object
* Return DOMXpath object
*
* @param string|\DOMDocument $html HTML string or \DOMDocument object
* @param string|DOMDocument $html HTML string or DOMDocument object
*
* @return DOUMXpath
*/
protected static function getDomXpath($html)
{
if ($html instanceof \DOMDocument) {
return new \DOMXpath($html);
if ($html instanceof DOMDocument) {
return new DOMXpath($html);
}
\libxml_use_internal_errors(true);
if (empty($html)) {
$html = '<!-- empty document -->';
}
$dom = new \DOMDocument();
/*
PHP bug: entities get converted
*/
// $html = preg_replace('/&([0-9a-z]+);/i', '{amp}\1;', $html);
$dom = new DOMDocument();
$dom->loadHTML('<?xml encoding="UTF-8">' . $html); // seriously?
foreach ($dom->childNodes as $node) {
if ($node->nodeType === XML_PI_NODE) {
Expand All @@ -234,6 +231,6 @@ protected static function getDomXpath($html)
}
}
$dom->encoding = 'UTF-8';
return new \DOMXpath($dom);
return new DOMXpath($dom);
}
}
Loading

0 comments on commit 4307350

Please sign in to comment.