Skip to content

Commit

Permalink
Merge pull request #9 from tburry/hotfix/issues-cleanup
Browse files Browse the repository at this point in the history
Issues cleanup
  • Loading branch information
tburry committed Jan 14, 2016
2 parents e13ec88 + b4da371 commit 872339f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 41 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

sudo: false

matrix:
fast_finish: true

install:
- composer install
Expand Down
10 changes: 2 additions & 8 deletions IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace pQuery;

interface IQuery {
interface IQuery extends \Countable {
/// Methods ///

/**
* Adds the specified class(es) to each of the set of matched elements.
* @param string $classname The name of the class to add. You can add multiple classes by seperating them with spaces.
* @param string $classname The name of the class to add. You can add multiple classes by separating them with spaces.
* @return IQuery
*/
function addClass($classname);
Expand Down Expand Up @@ -48,12 +48,6 @@ function before($content);
*/
function clear();

/**
* Gets the count of matched elements.
* @return int Returns the number of matched elements.
*/
function count();

/**
* Get the value of a style property for the first element in the set of matched elements or
* set one or more CSS properties for every matched element.
Expand Down
38 changes: 19 additions & 19 deletions gan_node_html.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class DomNode implements IQuery {
var $attributes_ns = null;

/**
* Array of childnodes
* Array of child nodes
* @var array
* @internal Public for faster access!
* @see childCount()
Expand Down Expand Up @@ -186,7 +186,7 @@ class DomNode implements IQuery {
var $self_close_str = ' /';

/**
* Use shorttags for attributes? If true, then attributes
* Use short tags for attributes? If true, then attributes
* with values equal to the attribute name will not output
* the value, e.g. selected="selected" will be selected.
* @var bool
Expand Down Expand Up @@ -361,7 +361,7 @@ protected function toString_content($attributes = true, $recursive = true, $cont
/**
* Returns the node as string
* @param bool $attributes Print attributes (of child tags)
* @param bool|int $recursive How many sublevels of childtags to print. True for all.
* @param bool|int $recursive How many sub-levels of child tags to print. True for all.
* @param bool|int $content_only Only print text, false will print tags too.
* @return string
*/
Expand Down Expand Up @@ -398,7 +398,7 @@ function getOuterText() {
}

/**
* Similar to JavaScript outerText, will replace node (and childnodes) with new text
* Similar to JavaScript outerText, will replace node (and child nodes) with new text
* @param string $text
* @param HtmlParserBase $parser Null to auto create instance
* @return bool|array True on succeed, array with errors on failure
Expand Down Expand Up @@ -440,7 +440,7 @@ function getInnerText() {
}

/**
* Similar to JavaScript innerText, will replace childnodes with new text
* Similar to JavaScript innerText, will replace child nodes with new text
* @param string $text
* @param HtmlParserBase $parser Null to auto create instance
* @return bool|array True on succeed, array with errors on failure
Expand Down Expand Up @@ -480,7 +480,7 @@ function getPlainTextUTF8() {
}

/**
* Similar to JavaScript plainText, will replace childnodes with new text (literal)
* Similar to JavaScript plainText, will replace child nodes with new text (literal)
* @param string $text
*/
function setPlainText($text) {
Expand All @@ -504,7 +504,7 @@ function delete() {

/**
* Detach node from parent
* @param bool $move_children_up Only detach current node and replace it with childnodes
* @param bool $move_children_up Only detach current node and replace it with child nodes
* @internal jquery (naming) compatibility
* @see delete()
*/
Expand Down Expand Up @@ -549,7 +549,7 @@ function getRoot() {
/**
* Change parent
* @param null|DomNode $to New parent, null if none
* @param false|int $index Add child to parent if not present at index, false to not add, negative to cound from end, null to append
* @param false|int $index Add child to parent if not present at index, false to not add, negative to count from end, null to append
*/
#php4
#function changeParent($to, &$index) {
Expand Down Expand Up @@ -627,7 +627,7 @@ function isTextOrComment() {
/**
* Move node to other node
* @param DomNode $to New parent, null if none
* @param int $new_index Add child to parent at index if not present, null to not add, negative to cound from end
* @param int $new_index Add child to parent at index if not present, null to not add, negative to count from end
* @internal Performs {@link changeParent()}
*/
#php4
Expand All @@ -640,9 +640,9 @@ function move($to, &$new_index = -1) {
}

/**
* Move childnodes to other node
* Move child nodes to other node
* @param DomNode $to New parent, null if none
* @param int $new_index Add child to new node at index if not present, null to not add, negative to cound from end
* @param int $new_index Add child to new node at index if not present, null to not add, negative to count from end
* @param int $start Index from child node where to start wrapping, 0 for first element
* @param int $end Index from child node where to end wrapping, -1 for last element
*/
Expand Down Expand Up @@ -962,8 +962,8 @@ function &getChild($child, $ignore_text_comments = false) {
}

/**
* Add childnode
* @param string|DomNode $tag Tagname or object
* Add child node
* @param string|DomNode $tag Tag name or object
* @param int $offset Position to insert node, negative to count from end, null to append
* @return DomNode Added node
*/
Expand Down Expand Up @@ -1030,7 +1030,7 @@ function &insertChild($tag, $index) {
}

/**
* Add textnode
* Add text node
* @param string $text
* @param int $offset Position to insert node, negative to count from end, null to append
* @return DomNode Added node
Expand Down Expand Up @@ -1112,7 +1112,7 @@ function &addDoctype($dtd, &$offset = null) {

/**
* Add xml node
* @param string $tag Tagname after "?", e.g. "php" or "xml"
* @param string $tag Tag name after "?", e.g. "php" or "xml"
* @param string $text
* @param array $attributes Array of attributes (array('attribute' => 'value'))
* @param int $offset Position to insert node, negative to count from end, null to append
Expand All @@ -1130,7 +1130,7 @@ function &addXML($tag = 'xml', $text = '', $attributes = array(), &$offset = nul

/**
* Add ASP node
* @param string $tag Tagname after "%"
* @param string $tag Tag name after "%"
* @param string $text
* @param array $attributes Array of attributes (array('attribute' => 'value'))
* @param int $offset Position to insert node, negative to count from end, null to append
Expand All @@ -1147,7 +1147,7 @@ function &addASP($tag = '', $text = '', $attributes = array(), &$offset = null)
}

/**
* Delete a childnode
* Delete a child node
* @param int|DomNode $child Child(index) to delete, negative to count from end
* @param bool $soft_delete False to call {@link delete()} from child
*/
Expand Down Expand Up @@ -1198,7 +1198,7 @@ function wrap($node, $wrap_index = -1, $node_index = null) {
}

/**
* Wrap childnodes
* Wrap child nodes
* @param string|DomNode $node Wrapping node, string to create new element node
* @param int $start Index from child node where to start wrapping, 0 for first element
* @param int $end Index from child node where to end wrapping, -1 for last element
Expand Down Expand Up @@ -1932,7 +1932,7 @@ public function query($query = '*') {
* @param int|bool $index True to return node instead of array if only 1 match,
* false to return array, int to return match at index, negative int to count from end
* @param bool|int $recursive
* @param bool $check_self Include this node in search or only search childnodes
* @param bool $check_self Include this node in search or only search child nodes
* @return DomNode[]|DomNode Returns an array of matching {@link DomNode} objects
* or a single {@link DomNode} if `$index` is not false.
*/
Expand Down
6 changes: 5 additions & 1 deletion gan_parser_html.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,18 @@ function parse_attributes() {
if ($this->next_no_whitespace() === self::TOK_STRING) {
$val = $this->getTokenString(1, -1);
} else {
$this->token_start = $this->pos;
if (!isset($stop)) {
$stop = $this->whitespace;
$stop['<'] = true;
$stop['>'] = true;
}

while ((++$this->pos < $this->size) && (!isset($stop[$this->doc[$this->pos]]))) {}
while ((++$this->pos < $this->size) && (!isset($stop[$this->doc[$this->pos]]))) {
// Do nothing.
}
--$this->pos;

$val = $this->getTokenString();

if (trim($val) === '') {
Expand Down
18 changes: 9 additions & 9 deletions gan_tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TokenizerBase {
var $token = self::TOK_NULL;

/**
* Startposition of token. If NULL, then current position is used.
* Start position of token. If NULL, then current position is used.
* @var int
* @internal Public for faster access!
* @see getTokenString()
Expand All @@ -107,7 +107,7 @@ class TokenizerBase {
/**
* List with all the character that can be considered as whitespace
* @var array|string
* @internal Variable is public + asscociated array for faster access!
* @internal Variable is public + associated array for faster access!
* @internal array(' ' => true) will recognize space (' ') as whitespace
* @internal String will be converted to array in constructor
* @internal Result token will be {@link self::TOK_WHITESPACE};
Expand All @@ -120,8 +120,8 @@ class TokenizerBase {
/**
* List with all the character that can be considered as identifier
* @var array|string
* @internal Variable is public + asscociated array for faster access!
* @internal array('a' => true) will recognize 'a' as identifer
* @internal Variable is public + associated array for faster access!
* @internal array('a' => true) will recognize 'a' as identifier
* @internal String will be converted to array in constructor
* @internal Result token will be {@link self::TOK_IDENTIFIER};
* @see setIdentifiers()
Expand All @@ -133,7 +133,7 @@ class TokenizerBase {
/**
* All characters that should be mapped to a token/function that cannot be considered as whitespace or identifier
* @var array
* @internal Variable is public + asscociated array for faster access!
* @internal Variable is public + associated array for faster access!
* @internal array('a' => 'parse_a') will call $this->parse_a() if it matches the character 'a'
* @internal array('a' => self::TOK_A) will set token to TOK_A if it matches the character 'a'
* @see mapChar()
Expand All @@ -143,7 +143,7 @@ class TokenizerBase {
var $custom_char_map = array();

/**
* Automaticly built character map. Built using {@link $identifiers}, {@link $whitespace} and {@link $custom_char_map}
* Automatically built character map. Built using {@link $identifiers}, {@link $whitespace} and {@link $custom_char_map}
* @var array
* @internal Public for faster access!
* @access private
Expand Down Expand Up @@ -304,7 +304,7 @@ function getIdentifiers($as_string = true) {
* Maps a custom character to a token/function
*
* Used like: mapChar('a', self::{@link TOK_IDENTIFIER}) or mapChar('a', 'parse_identifier');
* @param string $char Character that should be mapped. If set, it will be overriden
* @param string $char Character that should be mapped. If set, it will be overridden
* @param int|string $map If function name, then $this->function will be called, otherwise token is set to $map
* @see unmapChar()
*/
Expand Down Expand Up @@ -449,9 +449,9 @@ function next_no_whitespace() {
}

/**
* Finds the next token using stopcharacters
* Finds the next token using stop characters.
*
* Used like: next_search('abc') or next_seach(array('a' => true, 'b' => true, 'c' => true));
* Used like: next_search('abc') or next_search(array('a' => true, 'b' => true, 'c' => true));
* @param string|array $characters Characters to search for
* @param bool $callback Should the function check the charmap after finding a character?
* @return int Next token ({@link TOK_NULL} if none)
Expand Down
7 changes: 6 additions & 1 deletion pQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* A jQuery-like object for php.
*/
class pQuery implements ArrayAccess, Countable, IteratorAggregate, IQuery {
class pQuery implements ArrayAccess, IteratorAggregate, IQuery {
/// Properties ///

/**
Expand Down Expand Up @@ -73,6 +73,11 @@ public function clear() {
return $this;
}

/**
* Get the count of matched elements.
*
* @return int Returns the count of matched elements.
*/
public function count() {
return count($this->nodes);
}
Expand Down
1 change: 0 additions & 1 deletion tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ public function testHelloWorld() {

$this->assertHtmlStringEqualsHtmlString($expected, $dom->html());
}

}
41 changes: 41 additions & 0 deletions tests/IssueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @author Todd Burry <todd@vanillaforums.com>
* @copyright 2009-2016 Vanilla Forums Inc.
* @license LGPLv2
*/

/**
* Contains tests for github issues.
*/
class IssueTest extends pQueryTestCase {
/**
* Test attributes with no quotes.
*
* @link https://github.com/tburry/pquery/issues/4
*/
public function testAttrWithoutQuotes() {
$html = '<a href=/index.php/example>Example</a>';
$dom = pQuery::parseStr($html);

$this->assertSame('/index.php/example', $dom->query('a')->attr('href'));
}

/**
* Make sure that the {@link pQuery} class is still {@link Countable}.
*
* @link https://github.com/tburry/pquery/issues/8
*/
public function testStillCountable() {
$html = 'Is <b>this</b> what you <b>want</b>?';
$dom = pQuery::parseStr($html);

$pq = $dom->query('b');

$this->assertTrue($pq instanceof \pQuery\IQuery);
$this->assertTrue($pq instanceof \Countable);
$count = count($pq);
$this->assertSame(2, $count);
}
}

0 comments on commit 872339f

Please sign in to comment.