Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Oct 11, 2019
0 parents commit 280f6c9
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_name: travis-ci
coverage_clover: clover.xml # file generated by phpunit
json_path: coverage.json # file generated by php-coveralls
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/tests export-ignore
/phpunit.xml.dist export-ignore
/phpcs.xml.dist export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php
php:
- 7.2
- 7.3
- 7.4snapshot
dist: xenial
before_script:
- composer install --prefer-source --no-interaction
script:
- vendor/bin/phpunit --coverage-clover=clover.xml
after_script:
- wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
- php php-coveralls.phar --verbose
cache:
directories:
- $HOME/.composer/cache/files
after_success:
- bash <(curl -s https://codecov.io/bash)
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2019-10-11

This component has been decoupled from the [OriginPHP framework](https://www.originphp.com/).
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2019] [Jamiel Sharief]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# DOM

![license](https://img.shields.io/badge/license-MIT-brightGreen.svg)
[![build](https://travis-ci.org/originphp/dom.svg?branch=masterhttps://travis-ci.org/originphp/dom.svg?branch=master)](https://travis-ci.org/originphp/dom)
[![coverage](https://coveralls.io/repos/github/originphp/dom/badge.svg?branch=master)](https://coveralls.io/github/originphp/dom?branch=master)

The DOM utility extends the dom extension and provides javascript style query selectors (even when xpath fails).

The two functions functions that it adds to the PHP dom extension are `querySelector` and `querySelectorAll`. Unlike most other dom libraries, this does not use xpath to find elements. This is because during development, I found that translating my selectors from existing javascript code to xpath, i was not able to find the elements. When I tried downloading other libraries the same problem occurred. So I had to build from ground up so that the selector works the same as in javascript.

Currently it supports the following selectors:

- `.class` selects by class name
- `#id` selects by id
- `*` selects all elements
- `element` selects by tag name e.g. h1
- `element.class` selects a tag name with a class e.g. h1.heading will return all the h1 elements with the class heading
- `element,element` - Selects either elements e.g div,p will select all divs and paragraphs
- `element element` - e.g. div p will select all p in a div
- `[attribute=value]` - e.g. a[target='_blank'] will select all links with target = _blank
- `:first-child` - e.g. p:first-child will select the first child element from the p
- `:last-child` - e.g. p:last-child will select the last child element from the p
- `:nth-child(x)` = e.g. p:3 will select the third child.

At the moment it does not support the `~>+^|$` selectors.

## Installation

To install this package

```linux
$ composer require origin/dom
```

## Usage

Example usage:

```php
use Origin\Dom\Dom;
$dom = new Dom('1.0','UTF-8');
$dom->loadHtml($html); // this is a dom function
$element = $dom->querySelector('div.foo div.sub h1');
$paragraphs = $dom->querySelectorAll('div.p');
```
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "originphp/dom",
"description": "Origin DOM",
"type": "library",
"keywords": [
"originphp"
],
"homepage": "https://www.originphp.com",
"license": "MIT",
"authors": [
{
"name": "Jamiel Sharief",
"email": "js@originphp.com"
}
],
"autoload": {
"psr-4": {
"Origin\\Dom\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Origin\\Test\\Dom\\": "tests/"
}
},
"require": {
"php": "^7.2.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
}
}
7 changes: 7 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<ruleset name="OriginPHP">
<description>The coding standard for OriginPHP.</description>
<rule ref="PSR2"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
</ruleset>
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
backupGlobals="true"
>
<testsuites>
<testsuite name="Origin DOM">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<php>
<const name="PHPUNIT" value="true"/>
<env name="ORIGIN_ENV" value="test"/>
</php>
</phpunit>
36 changes: 36 additions & 0 deletions src/Dom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types = 1);
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
*
* Licensed under The MIT License
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* @copyright Copyright (c) Jamiel Sharief
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

/**
* This extends the DOM extension, it gives the ability to use a javascript style query selectors
* to find elements.
*/

namespace Origin\Dom;

use DOMDocument;
use Origin\Dom\DomElement;
use Origin\Dom\QuerySelectorTrait;

class Dom extends DOMDocument
{
use QuerySelectorTrait;

public function __construct(string $version = '', string $encoding = '')
{
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', DomElement::class);
}
}
24 changes: 24 additions & 0 deletions src/DomElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
*
* Licensed under The MIT License
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* @copyright Copyright (c) Jamiel Sharief
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Origin\Dom;

/**
* Turbocharged Dom Element
*/
class DomElement extends \DOMElement
{
use QuerySelectorTrait;
}
Loading

0 comments on commit 280f6c9

Please sign in to comment.