Skip to content

Commit

Permalink
fix coding style
Browse files Browse the repository at this point in the history
closes #47
  • Loading branch information
g105b committed Jul 17, 2016
1 parent 5415f57 commit 54a9b1b
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 181 deletions.
9 changes: 5 additions & 4 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function matches(string $selector):bool {
$i = $matches->length;
while(--$i >= 0 && $matches->item($i) !== $this);

return ($i >= 0);
return($i >= 0);
}

/**
Expand All @@ -57,11 +57,12 @@ public function getElementsByClassName(string $names):HTMLCollection {
* @return Element|null
*/
public function closest(string $selectors) {
$collection = $this->css($selectors, 'ancestor-or-self::');
return $collection->item(count($collection) - 1);
$collection = $this->css($selectors, "ancestor-or-self::");
return $collection->item(count($collection) - 1);
}

private function css(string $selector, string $prefix = 'descendant-or-self::'):HTMLCollection {
private function css(
string $selector, string $prefix = "descendant-or-self::"):HTMLCollection {
$converter = new CssSelectorConverter();
$xPathSelector = $converter->toXPath($selector, $prefix);
return $this->xPath($xPathSelector);
Expand Down
17 changes: 9 additions & 8 deletions src/HTMLCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Iterator;
use ArrayAccess;
use Countable;
use DOMNodeList;

/**
* Represents a Node list that can only contain Element nodes. Internally,
Expand All @@ -19,7 +20,7 @@ class HTMLCollection implements Iterator, ArrayAccess, Countable {
private $domNodeList;
private $key = 0;

public function __construct(\DOMNodeList $domNodeList) {
public function __construct(DOMNodeList $domNodeList) {
$this->domNodeList = $domNodeList;
}

Expand All @@ -31,7 +32,7 @@ public function __construct(\DOMNodeList $domNodeList) {
*/
public function item(int $index) {
$count = 0;
foreach ($this as $element) {
foreach($this as $element) {
if($index === $count) {
return $element;
}
Expand All @@ -53,8 +54,8 @@ public function item(int $index) {
public function namedItem(string $name) {
$namedElement = null;

// Iterating all elements is costly. Room for improvement here?
foreach ($this as $element) {
// ENHANCEMENT: Iterating all elements is costly. Room for improvement here?
foreach($this as $element) {
if($element->getAttribute("id") === $name) {
return $element;
}
Expand Down Expand Up @@ -84,10 +85,10 @@ private function prop_get_length():int {
}

public function count():int {
return $this->length;
return $this->length;
}

// Iterator implementation /////////////////////////////////////////////////////
// Iterator implementation -----------------------------------------------------

public function current():Element {
return $this->domNodeList[$this->key];
Expand Down Expand Up @@ -124,7 +125,7 @@ private function incrementKeyToNextElement() {
* @return boolean
*/
public function offsetExists($offset):bool {
return isset($offset, $this->domNodeList);
return isset($offset, $this->domNodeList);
}

/**
Expand All @@ -133,7 +134,7 @@ public function offsetExists($offset):bool {
* @return phpgt\dom\Element
*/
public function offsetGet($offset):Element {
return $this->item($offset);
return $this->item($offset);
}

public function offsetSet($offset, $value) {
Expand Down
13 changes: 8 additions & 5 deletions src/HTMLDocument.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace phpgt\dom;

use DOMDocument;

/**
* Provides access to special properties and methods not present by default
* on a regular (XML) document.
Expand All @@ -22,14 +24,15 @@ class HTMLDocument extends Document {
use LiveProperty, ParentNode;

public function __construct($document) {
parent::__construct($document);
if (!($document instanceof \DOMDocument)) {
$this->loadHTML($document);
}
parent::__construct($document);

if(!($document instanceof DOMDocument)) {
$this->loadHTML($document);
}
}

public function querySelector(string $selectors) {
return $this->documentElement->querySelector($selectors);
return $this->documentElement->querySelector($selectors);
}

public function querySelectorAll(string $selectors):HTMLCollection {
Expand Down
20 changes: 10 additions & 10 deletions src/XMLDocument.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

namespace phpgt\dom;

class XMLDocument extends Document
{
use DOMDocument;

class XMLDocument extends Document {
use LiveProperty, ParentNode;

public function __construct($document)
{
parent::__construct($document);
if (!($document instanceof \DOMDocument)) {
$this->loadXML($document);
}
}
public function __construct($document) {
parent::__construct($document);

if(!$document instanceof DOMDocument) {
$this->loadXML($document);
}
}

}#
38 changes: 16 additions & 22 deletions test/Element.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,35 @@ public function testChildElementCount() {
}

public function testElementClosest() {
$document = new HTMLDocument(test\Helper::HTML_NESTED);
$document = new HTMLDocument(test\Helper::HTML_NESTED);

$p = $document->querySelector('.inner-list p');
// test if p is an element
$this->assertInstanceOf('\phpgt\dom\Element', $p);
$p = $document->querySelector('.inner-list p');
$this->assertInstanceOf('\phpgt\dom\Element', $p);

$innerList = $document->querySelector('.inner-list');
// test if .inner-list is an element
$this->assertInstanceOf('\phpgt\dom\Element', $innerList);
$innerList = $document->querySelector('.inner-list');
$this->assertInstanceOf('\phpgt\dom\Element', $innerList);

$closestUl = $innerList->closest('ul');
// test if the closest element is .inner-list
$this->assertEquals($innerList, $closestUl);
$closestUl = $innerList->closest('ul');
$this->assertEquals($innerList, $closestUl);

// test something far just to be sure
$container = $p->closest('.container');
$this->assertInstanceOf('\phpgt\dom\Element', $container);
$container = $p->closest('.container');
$this->assertInstanceOf('\phpgt\dom\Element', $container);

$nonExistentClosestElement = $p->closest('br');
$this->assertNull($nonExistentClosestElement);

// test if closest is returning null for unknown element
$nonExistentClosestElement = $p->closest('br');
$this->assertNull($nonExistentClosestElement);

// test false-positive matches on a selector matching multiple ancestors.
$innerPost = $document->querySelector("div.post.inner");
$innerListItem = $document->querySelector(".inner-item-1");
$outerPost = $document->querySelector("div.post.outer");
$this->assertInstanceOf("\phpgt\dom\Element", $innerPost);
$this->assertInstanceOf("\phpgt\dom\Element", $outerPost);

$closestDivToInnerListItem = $innerListItem->closest("div");
$closestDivToInnerPost = $innerPost->closest("div");
// ..the inner post should match itself, as it is a div.
// ..the inner post should match itself, as it is a div.
$this->assertSame($closestDivToInnerPost, $innerPost);
// ..but the inner list item should match up the tree to the outer post
// ..missing the other divs in the tree.
// ..but the inner list item should match up the tree to the outer post
// ..missing the other divs in the tree.
$this->assertSame($closestDivToInnerListItem, $outerPost);
}

Expand Down
61 changes: 30 additions & 31 deletions test/HTMLCollection.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,53 @@ public function testNonElementsRemoved() {
}

public function testArrayAccessImplementation() {
$document = new HTMLDocument(test\Helper::HTML_MORE);
$collection = $document->body->children;
$document = new HTMLDocument(test\Helper::HTML_MORE);
$collection = $document->body->children;

// test if the collection implements ArrayAccess
$this->assertInstanceOf('\ArrayAccess', $collection);
// test if the collection implements ArrayAccess
$this->assertInstanceOf('\ArrayAccess', $collection);

// test if offset 0 exists
$this->assertArrayHasKey(0, $collection);
// test if offset 0 exists
$this->assertArrayHasKey(0, $collection);

// test if the first item is an Element instance
$first = $collection[0];
$this->assertInstanceOf('\phpgt\dom\Element', $first);
// test if the first item is an Element instance
$first = $collection[0];
$this->assertInstanceOf('\phpgt\dom\Element', $first);

// test if the collection is read only
$this->setExpectedException(\BadMethodCallException::class);
$collection[$collection->length] = new Element('br');
unset($collection[0]);
// test if the collection is read only
$this->setExpectedException(\BadMethodCallException::class);
$collection[$collection->length] = new Element('br');
unset($collection[0]);

}

public function testCountMethod() {
$document = new HTMLDocument(test\Helper::HTML_MORE);
$childrenCount = count($document->body->children);
// must have 11 childrens
$this->assertEquals(11, $childrenCount);
$document = new HTMLDocument(test\Helper::HTML_MORE);
$childrenCount = count($document->body->children);
$this->assertEquals(11, $childrenCount);
}

public function testNamedItem() {
$document = new HTMLDocument(test\Helper::HTML_MORE);
$whoNamed = $document->body->children->namedItem("who");
$whoH2 = $document->getElementById("who");
$document = new HTMLDocument(test\Helper::HTML_MORE);
$whoNamed = $document->body->children->namedItem("who");
$whoH2 = $document->getElementById("who");

$this->assertSame($whoNamed, $whoH2,
"Named item should match by id first");
$this->assertSame($whoNamed, $whoH2,
"Named item should match by id first");

$formsNamed = $document->body->children->namedItem("forms");
$formsAnchor = $document->querySelector("a[name='forms']");
$formsNamed = $document->body->children->namedItem("forms");
$formsAnchor = $document->querySelector("a[name='forms']");

$this->assertSame($formsNamed, $formsAnchor,
"Named item should match name when no id match");
$this->assertSame($formsNamed, $formsAnchor,
"Named item should match name when no id match");
}

public function testIteration() {
$document = new HTMLDocument(test\Helper::HTML_MORE);
foreach($document->querySelectorAll("p") as $i => $p) {
$paragraphItem = $document->getElementsByTagName("p")[$i];
$this->assertSame($paragraphItem, $p);
}
$document = new HTMLDocument(test\Helper::HTML_MORE);
foreach($document->querySelectorAll("p") as $i => $p) {
$paragraphItem = $document->getElementsByTagName("p")[$i];
$this->assertSame($paragraphItem, $p);
}
}

}#
48 changes: 19 additions & 29 deletions test/HTMLDocument.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ class HTMLDocumentTest extends \PHPUnit_Framework_TestCase {

public function testConstruction()
{
// test construction from raw HTML
$fromRawHTML = new HTMLDocument(test\Helper::HTML);
$this->assertInstanceOf('phpgt\dom\HTMLDocument', $fromRawHTML);
// test construction from raw HTML
$fromRawHTML = new HTMLDocument(test\Helper::HTML);
$this->assertInstanceOf('phpgt\dom\HTMLDocument', $fromRawHTML);

// test construction from a DOMDocument object
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$domDocument->loadHTML(test\Helper::HTML);
$fromDOMDocument = new HTMLDocument($domDocument);
$this->assertInstanceOf('phpgt\dom\HTMLDocument', $fromDOMDocument);
// test construction from a DOMDocument object
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$domDocument->loadHTML(test\Helper::HTML);
$fromDOMDocument = new HTMLDocument($domDocument);
$this->assertInstanceOf('phpgt\dom\HTMLDocument', $fromDOMDocument);

// test construction from a HTMLDocument object, just to be sure
$fromHTMLDocument = new HTMLDocument($fromRawHTML);
$this->assertInstanceOf('phpgt\dom\HTMLDocument', $fromHTMLDocument);
// test construction from a HTMLDocument object, just to be sure
$fromHTMLDocument = new HTMLDocument($fromRawHTML);
$this->assertInstanceOf('phpgt\dom\HTMLDocument', $fromHTMLDocument);

// test if elements are consistent
$h2FromRawHTML = $fromRawHTML->querySelector('h2');
$h2FromDOMDocument = $fromDOMDocument->querySelector('h2');
$this->assertSame($h2FromRawHTML, $h2FromDOMDocument);
// test if elements are consistent
$h2FromRawHTML = $fromRawHTML->querySelector('h2');
$h2FromDOMDocument = $fromDOMDocument->querySelector('h2');
$this->assertSame($h2FromRawHTML, $h2FromDOMDocument);

}

Expand Down Expand Up @@ -72,7 +72,7 @@ public function testHeadElement() {
}

public function testHeadElementAutomaticallyCreated() {
// test\Helper::HTML does not explicitly define a <head>
// test\Helper::HTML does not explicitly define a <head>
$document = new HTMLDocument(test\Helper::HTML);
$this->assertInstanceOf("\phpgt\dom\Element", $document->head);
}
Expand All @@ -83,7 +83,7 @@ public function testBodyElement() {
}

public function testBodyElementAutomaticallyCreated() {
// test\Helper::HTML does not explicitly define a <body>
// test\Helper::HTML does not explicitly define a <body>
$document = new HTMLDocument(test\Helper::HTML);
$this->assertInstanceOf("\phpgt\dom\Element", $document->body);
}
Expand All @@ -107,8 +107,8 @@ public function testAnchorsPropertyWhenNoAnchors() {

public function testAnchorsPropertyWhenAnchors() {
$documentWith = new HTMLDocument(test\Helper::HTML_MORE);
// There are actually 3 "a" elements, but only two are anchors - the
// other is a link.
// There are actually 3 "a" elements, but only two are anchors - the
// other is a link.
$this->assertEquals(2, $documentWith->anchors->length);
}

Expand All @@ -132,16 +132,6 @@ public function testLinksPropertyWhenLinks() {
$this->assertEquals(1, $documentWith->links->length);
}

// public function testTitleGetterSetter() {
// $document = new HTMLDocument(test\Helper::HTML_MORE);
// $this->assertEquals("Test HTML", $document->title);

// $newTitle = $document->title . " (New Title)";
// $document->title = $newTitle;

// $this->assertEquals($newTitle, $document->title);
// }

public function testTitleWhenNoTitle() {
$document = new HTMLDocument(test\Helper::HTML);
$this->assertEmpty($document->title);
Expand Down
Loading

0 comments on commit 54a9b1b

Please sign in to comment.