Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ To get started using pQuery do the following.
The following example parses an html string and does some manipulation on it.

```php

// composer autoload
require "vendor/autoload.php";

$html = '<div class="container">
<div class="inner verb">Hello</div>
<div class="inner adj">Cruel</div>
Expand Down
12 changes: 11 additions & 1 deletion gan_tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class TokenizerBase {
* @access private
*/
var $doc = '';

/**
* The document as string in lowercase for faster search in next_pos()
* @var string
*/
var $doc_lower = '';

/**
* The size of the document (length of string)
Expand Down Expand Up @@ -184,6 +190,7 @@ function __construct($doc = '', $pos = 0) {
*/
function setDoc($doc, $pos = 0) {
$this->doc = $doc;
$this->doc_lower = strtolower($doc);
$this->size = strlen($doc);
$this->setPos($pos);
}
Expand Down Expand Up @@ -489,7 +496,10 @@ function next_search($characters, $callback = true) {
*/
function next_pos($needle, $callback = true) {
$this->token_start = $this->pos;
if (($this->pos < $this->size) && (($p = stripos($this->doc, $needle, $this->pos + 1)) !== false)) {

$needle_lower = strtolower($needle);

if (($this->pos < $this->size) && (($p = strpos($this->doc_lower, $needle_lower, $this->pos + 1)) !== false)) {

$len = $p - $this->pos - 1;
if ($len > 0) {
Expand Down
43 changes: 1 addition & 42 deletions ganon.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ function str_get_dom($str, $return_root = true) {
* @return Html5Parser|DomNode
*/
function file_get_dom($file, $return_root = true, $use_include_path = false, $context = null) {
if (version_compare(PHP_VERSION, '5.0.0', '>='))
$f = file_get_contents($file, $use_include_path, $context);
else {
if ($context !== null)
trigger_error('Context parameter not supported in this PHP version');
$f = file_get_contents($file, $use_include_path);
}

$f = file_get_contents($file, $use_include_path, $context);
return (($f === false) ? false : str_get_dom($f, $return_root));
}

Expand All @@ -52,40 +45,6 @@ function dom_format(&$root, $options = array()) {
return $formatter->format($root);
}

if (version_compare(PHP_VERSION, '5.0.0', '<')) {
/**
* PHP alternative to str_split, for backwards compatibility
* @param string $string
* @return string
*/
function str_split($string) {
$res = array();
$size = strlen($string);
for ($i = 0; $i < $size; $i++) {
$res[] = $string[$i];
}

return $res;
}
}

if (version_compare(PHP_VERSION, '5.2.0', '<')) {
/**
* PHP alternative to array_fill_keys, for backwards compatibility
* @param array $keys
* @param mixed $value
* @return array
*/
function array_fill_keys($keys, $value) {
$res = array();
foreach($keys as $k) {
$res[$k] = $value;
}

return $res;
}
}

#!! <- Ignore when converting to single file
if (!defined('GANON_NO_INCLUDES')) {
define('GANON_NO_INCLUDES', true);
Expand Down
39 changes: 26 additions & 13 deletions pQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public function append($content) {
}

public function attr($name, $value = null) {
if (empty($this->nodes) && $value === null)
if (empty($this->nodes) && $value === null){
return '';
}

foreach ($this->nodes as $node) {
if ($value === null)
if ($value === null){
return $node->attr($name);
}
$node->attr($name, $value);
}
return $this;
Expand Down Expand Up @@ -100,19 +102,22 @@ public function getIterator() {

public function hasClass($classname) {
foreach ($this->nodes as $node) {
if ($node->hasClass($classname))
if ($node->hasClass($classname)){
return true;
}
}
return false;
}

public function html($value = null) {
if (empty($this->nodes) && $value === null)
if (empty($this->nodes) && $value === null){
return '';
}

foreach ($this->nodes as $node) {
if ($value === null)
if ($value === null){
return $node->html();
}
$node->html($value);
}
return $this;
Expand Down Expand Up @@ -173,12 +178,14 @@ public function prepend($content = null) {
}

public function prop($name, $value = null) {
if (empty($this->nodes) && $value === null)
if (empty($this->nodes) && $value === null){
return '';
}

foreach ($this->nodes as $node) {
if ($value === null)
if ($value === null){
return $node->prop($name);
}
$node->prop($name, $value);
}
return $this;
Expand All @@ -188,8 +195,9 @@ public function remove($selector = null) {
foreach ($this->nodes as $node) {
$node->remove($selector);
}
if ($selector === null)
if ($selector === null){
$this->nodes = array();
}

return $this;
}
Expand Down Expand Up @@ -217,20 +225,23 @@ public function replaceWith($content) {

public function tagName($value = null) {
foreach ($this->nodes as $node) {
if ($value === null)
if ($value === null){
return $node->tagName();
}
$node->tagName($value);
}
return $this;
}

public function text($value = null) {
if (empty($this->nodes) && $value === null)
if (empty($this->nodes) && $value === null){
return '';
}

foreach ($this->nodes as $node) {
if ($value === null)
if ($value === null){
return $node->text();
}
$node->text($value);
}
return $this;
Expand All @@ -252,12 +263,14 @@ public function unwrap() {
}

public function val($value = null) {
if (empty($this->nodes) && $value === null)
if (empty($this->nodes) && $value === null){
return '';
}

foreach ($this->nodes as $node) {
if ($value === null)
if ($value === null){
return $node->val();
}
$node->val($value);
}
return $this;
Expand Down