080600
This commit is contained in:
@@ -1,152 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace pagelayerQuery;
|
||||
|
||||
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 separating them with spaces.
|
||||
* @return IQuery
|
||||
*/
|
||||
function addClass($classname);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, after each element in the set of matched elements.
|
||||
* @param string $content The content to add.
|
||||
* @return IQuery
|
||||
*/
|
||||
function after($content);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, to the end of each element in the set of matched elements.
|
||||
* @param string $content The content to append.
|
||||
* @return IQuery
|
||||
*/
|
||||
function append($content);
|
||||
|
||||
/**
|
||||
* Get the value of an attribute for the first element in the set of matched elements or set one
|
||||
* or more attributes for every matched element.
|
||||
* @param string $name The name of the attribute.
|
||||
* @param null|string $value The value to set or null to get the current attribute value.
|
||||
* @return string|IQuery
|
||||
*/
|
||||
function attr($name, $value = null);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, before each element in the set of matched elements.
|
||||
* @param string $content The content to add.
|
||||
* @return IQuery
|
||||
*/
|
||||
function before($content);
|
||||
|
||||
/**
|
||||
* Remove all child nodes of the set of matched elements from the DOM.
|
||||
* @return IQuery;
|
||||
*/
|
||||
function clear();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
// function css($name, $value = null);
|
||||
|
||||
/**
|
||||
* Determine whether any of the matched elements are assigned the given class.
|
||||
* @param string $classname The name of the class to check.
|
||||
*/
|
||||
function hasClass($classname);
|
||||
|
||||
/**
|
||||
* Get the HTML contents of the first element in the set of matched elements
|
||||
* or set the HTML contents of every matched element.
|
||||
* @param string|null $value The value to set.
|
||||
*/
|
||||
function html($value = null);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
|
||||
* @param string $content The content to add.
|
||||
*/
|
||||
function prepend($content);
|
||||
|
||||
/**
|
||||
* Get the value of a property for the first element in the set of matched elements
|
||||
* or set one or more properties for every matched element.
|
||||
* @param string $name The name of the property.
|
||||
* The currently supported properties are `tagname`, `selected`, and `checked`.
|
||||
* @param null|string $value The value to set or null to get the current property value.
|
||||
*/
|
||||
function prop($name, $value = null);
|
||||
|
||||
/**
|
||||
* Remove the set of matched elements from the DOM.
|
||||
* @param null|string $selector A css query to filter the set of removed nodes.
|
||||
*/
|
||||
function remove($selector = null);
|
||||
|
||||
/**
|
||||
* Remove an attribute from each element in the set of matched elements.
|
||||
* @param string $name The name of the attribute to remove.
|
||||
*/
|
||||
function removeAttr($name);
|
||||
|
||||
/**
|
||||
* Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
|
||||
* @param string $classname The name of the class to remove.
|
||||
*/
|
||||
function removeClass($classname);
|
||||
|
||||
/**
|
||||
* Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.
|
||||
* @param string $content The content that will replace the nodes.
|
||||
*/
|
||||
function replaceWith($content);
|
||||
|
||||
/**
|
||||
* Returns the name of the element.
|
||||
* @param null|string $tagName A new tag name or null to return the current tag name.
|
||||
*/
|
||||
function tagName($value = null);
|
||||
|
||||
/**
|
||||
* Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.
|
||||
* @param null|string $value A string to set the text or null to return the current text.
|
||||
*/
|
||||
function text($value = null);
|
||||
|
||||
/**
|
||||
* Add or remove one or more classes from each element in the set of matched elements,
|
||||
* depending on either the class’s presence or the value of the switch argument.
|
||||
* @param string $classname
|
||||
* @param bool|null
|
||||
*/
|
||||
function toggleClass($classname, $switch = null);
|
||||
|
||||
/**
|
||||
* Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
|
||||
*/
|
||||
function unwrap();
|
||||
|
||||
/**
|
||||
* Get the current value of the first element in the set of matched elements or set the value of every matched element.
|
||||
* @param string|null $value The new value of the element or null to return the current value.
|
||||
*/
|
||||
function val($value = null);
|
||||
|
||||
/**
|
||||
* Wrap an HTML structure around each element in the set of matched elements.
|
||||
* @param string A tag name or html string specifying the structure to wrap around the matched elements.
|
||||
*/
|
||||
function wrap($wrapping_element);
|
||||
|
||||
/**
|
||||
* Wrap an HTML structure around the content of each element in the set of matched elements.
|
||||
* @param string A tag name or html string specifying the structure to wrap around the content of the matched elements.
|
||||
*/
|
||||
function wrapInner($wrapping_element);
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
namespace pagelayerQuery;
|
||||
|
||||
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 separating them with spaces.
|
||||
* @return IQuery
|
||||
*/
|
||||
function addClass($classname);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, after each element in the set of matched elements.
|
||||
* @param string $content The content to add.
|
||||
* @return IQuery
|
||||
*/
|
||||
function after($content);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, to the end of each element in the set of matched elements.
|
||||
* @param string $content The content to append.
|
||||
* @return IQuery
|
||||
*/
|
||||
function append($content);
|
||||
|
||||
/**
|
||||
* Get the value of an attribute for the first element in the set of matched elements or set one
|
||||
* or more attributes for every matched element.
|
||||
* @param string $name The name of the attribute.
|
||||
* @param null|string $value The value to set or null to get the current attribute value.
|
||||
* @return string|IQuery
|
||||
*/
|
||||
function attr($name, $value = null);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, before each element in the set of matched elements.
|
||||
* @param string $content The content to add.
|
||||
* @return IQuery
|
||||
*/
|
||||
function before($content);
|
||||
|
||||
/**
|
||||
* Remove all child nodes of the set of matched elements from the DOM.
|
||||
* @return IQuery;
|
||||
*/
|
||||
function clear();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
// function css($name, $value = null);
|
||||
|
||||
/**
|
||||
* Determine whether any of the matched elements are assigned the given class.
|
||||
* @param string $classname The name of the class to check.
|
||||
*/
|
||||
function hasClass($classname);
|
||||
|
||||
/**
|
||||
* Get the HTML contents of the first element in the set of matched elements
|
||||
* or set the HTML contents of every matched element.
|
||||
* @param string|null $value The value to set.
|
||||
*/
|
||||
function html($value = null);
|
||||
|
||||
/**
|
||||
* Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
|
||||
* @param string $content The content to add.
|
||||
*/
|
||||
function prepend($content);
|
||||
|
||||
/**
|
||||
* Get the value of a property for the first element in the set of matched elements
|
||||
* or set one or more properties for every matched element.
|
||||
* @param string $name The name of the property.
|
||||
* The currently supported properties are `tagname`, `selected`, and `checked`.
|
||||
* @param null|string $value The value to set or null to get the current property value.
|
||||
*/
|
||||
function prop($name, $value = null);
|
||||
|
||||
/**
|
||||
* Remove the set of matched elements from the DOM.
|
||||
* @param null|string $selector A css query to filter the set of removed nodes.
|
||||
*/
|
||||
function remove($selector = null);
|
||||
|
||||
/**
|
||||
* Remove an attribute from each element in the set of matched elements.
|
||||
* @param string $name The name of the attribute to remove.
|
||||
*/
|
||||
function removeAttr($name);
|
||||
|
||||
/**
|
||||
* Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
|
||||
* @param string $classname The name of the class to remove.
|
||||
*/
|
||||
function removeClass($classname);
|
||||
|
||||
/**
|
||||
* Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.
|
||||
* @param string $content The content that will replace the nodes.
|
||||
*/
|
||||
function replaceWith($content);
|
||||
|
||||
/**
|
||||
* Returns the name of the element.
|
||||
* @param null|string $tagName A new tag name or null to return the current tag name.
|
||||
*/
|
||||
function tagName($value = null);
|
||||
|
||||
/**
|
||||
* Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.
|
||||
* @param null|string $value A string to set the text or null to return the current text.
|
||||
*/
|
||||
function text($value = null);
|
||||
|
||||
/**
|
||||
* Add or remove one or more classes from each element in the set of matched elements,
|
||||
* depending on either the class’s presence or the value of the switch argument.
|
||||
* @param string $classname
|
||||
* @param bool|null
|
||||
*/
|
||||
function toggleClass($classname, $switch = null);
|
||||
|
||||
/**
|
||||
* Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
|
||||
*/
|
||||
function unwrap();
|
||||
|
||||
/**
|
||||
* Get the current value of the first element in the set of matched elements or set the value of every matched element.
|
||||
* @param string|null $value The new value of the element or null to return the current value.
|
||||
*/
|
||||
function val($value = null);
|
||||
|
||||
/**
|
||||
* Wrap an HTML structure around each element in the set of matched elements.
|
||||
* @param string A tag name or html string specifying the structure to wrap around the matched elements.
|
||||
*/
|
||||
function wrap($wrapping_element);
|
||||
|
||||
/**
|
||||
* Wrap an HTML structure around the content of each element in the set of matched elements.
|
||||
* @param string A tag name or html string specifying the structure to wrap around the content of the matched elements.
|
||||
*/
|
||||
function wrapInner($wrapping_element);
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,49 +1,49 @@
|
||||
# pQuery
|
||||
|
||||
[](https://travis-ci.org/tburry/pquery)
|
||||
[](https://scrutinizer-ci.com/g/tburry/pquery/)
|
||||
[](https://packagist.org/packages/tburry/pquery)
|
||||
|
||||
pQuery is a jQuery like html dom parser written in php. It is a fork of the [ganon dom parser](https://code.google.com/p/ganon/).
|
||||
|
||||
## Basic usage
|
||||
|
||||
To get started using pQuery do the following.
|
||||
|
||||
1. Require the pQuery library into your project using [composer](http://getcomposer.org/doc/01-basic-usage.md#the-require-key).
|
||||
2. Parse a snippet of html using `pQuery::parseStr()` or `pQuery::parseFile()` to return a document object model (DOM).
|
||||
3. Run jQuery like functions on the DOM.
|
||||
|
||||
## Example
|
||||
|
||||
The following example parses an html string and does some manipulation on it.
|
||||
|
||||
```php
|
||||
$html = '<div class="container">
|
||||
<div class="inner verb">Hello</div>
|
||||
<div class="inner adj">Cruel</div>
|
||||
<div class="inner obj">World</div>
|
||||
</div>';
|
||||
|
||||
$dom = pQuery::parseStr($html);
|
||||
|
||||
$dom->query('.inner')
|
||||
->tagName('span');
|
||||
|
||||
$dom->query('.adj')
|
||||
->html('Beautiful')
|
||||
->tagName('i');
|
||||
|
||||
echo $dom->html();
|
||||
```
|
||||
|
||||
## Differences between pQuery and ganon
|
||||
|
||||
pQuery is a fork of the [ganon php processor](https://code.google.com/p/ganon/). Most of the functionality is identical to ganon with the following exceptions.
|
||||
|
||||
* pQuery is a composer package.
|
||||
* pQuery renames ganon's classes and puts them into a namespace.
|
||||
* pQuery is used only with objects rather than functions so that it can be autoloaded.
|
||||
* pQuery Adds the `IQuery` interface and the `pQuery` object that define the jQuery-like interface for querying the dom.
|
||||
* pQuery implements more of jQuery's methods. See the `IQuery` interface for a list of methods.
|
||||
* pQuery supports adding tags to the dom using the `<div class="something"></div>` notation rather than just `div`.
|
||||
# pQuery
|
||||
|
||||
[](https://travis-ci.org/tburry/pquery)
|
||||
[](https://scrutinizer-ci.com/g/tburry/pquery/)
|
||||
[](https://packagist.org/packages/tburry/pquery)
|
||||
|
||||
pQuery is a jQuery like html dom parser written in php. It is a fork of the [ganon dom parser](https://code.google.com/p/ganon/).
|
||||
|
||||
## Basic usage
|
||||
|
||||
To get started using pQuery do the following.
|
||||
|
||||
1. Require the pQuery library into your project using [composer](http://getcomposer.org/doc/01-basic-usage.md#the-require-key).
|
||||
2. Parse a snippet of html using `pQuery::parseStr()` or `pQuery::parseFile()` to return a document object model (DOM).
|
||||
3. Run jQuery like functions on the DOM.
|
||||
|
||||
## Example
|
||||
|
||||
The following example parses an html string and does some manipulation on it.
|
||||
|
||||
```php
|
||||
$html = '<div class="container">
|
||||
<div class="inner verb">Hello</div>
|
||||
<div class="inner adj">Cruel</div>
|
||||
<div class="inner obj">World</div>
|
||||
</div>';
|
||||
|
||||
$dom = pQuery::parseStr($html);
|
||||
|
||||
$dom->query('.inner')
|
||||
->tagName('span');
|
||||
|
||||
$dom->query('.adj')
|
||||
->html('Beautiful')
|
||||
->tagName('i');
|
||||
|
||||
echo $dom->html();
|
||||
```
|
||||
|
||||
## Differences between pQuery and ganon
|
||||
|
||||
pQuery is a fork of the [ganon php processor](https://code.google.com/p/ganon/). Most of the functionality is identical to ganon with the following exceptions.
|
||||
|
||||
* pQuery is a composer package.
|
||||
* pQuery renames ganon's classes and puts them into a namespace.
|
||||
* pQuery is used only with objects rather than functions so that it can be autoloaded.
|
||||
* pQuery Adds the `IQuery` interface and the `pQuery` object that define the jQuery-like interface for querying the dom.
|
||||
* pQuery implements more of jQuery's methods. See the `IQuery` interface for a list of methods.
|
||||
* pQuery supports adding tags to the dom using the `<div class="something"></div>` notation rather than just `div`.
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
{
|
||||
"name": "tburry/pquery",
|
||||
"type": "library",
|
||||
"description": "A jQuery like html dom parser written in php.",
|
||||
"keywords": ["php", "dom", "ganon"],
|
||||
"license": "LGPL-2.1",
|
||||
"authors": [
|
||||
{ "name": "Todd Burry", "email": "todd@vanillaforums.com", "role": "Developer" }
|
||||
],
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"IQuery.php",
|
||||
"gan_formatter.php",
|
||||
"gan_node_html.php",
|
||||
"gan_parser_html.php",
|
||||
"gan_selector_html.php",
|
||||
"gan_tokenizer.php",
|
||||
"gan_xml2array.php",
|
||||
"pQuery.php"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"htmlawed/htmlawed": "dev-master"
|
||||
}
|
||||
{
|
||||
"name": "tburry/pquery",
|
||||
"type": "library",
|
||||
"description": "A jQuery like html dom parser written in php.",
|
||||
"keywords": ["php", "dom", "ganon"],
|
||||
"license": "LGPL-2.1",
|
||||
"authors": [
|
||||
{ "name": "Todd Burry", "email": "todd@vanillaforums.com", "role": "Developer" }
|
||||
],
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"IQuery.php",
|
||||
"gan_formatter.php",
|
||||
"gan_node_html.php",
|
||||
"gan_parser_html.php",
|
||||
"gan_selector_html.php",
|
||||
"gan_tokenizer.php",
|
||||
"gan_xml2array.php",
|
||||
"pQuery.php"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"htmlawed/htmlawed": "dev-master"
|
||||
}
|
||||
}
|
||||
@@ -1,381 +1,381 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
namespace pagelayerQuery;
|
||||
|
||||
/**
|
||||
* Indents text
|
||||
* @param string $text
|
||||
* @param int $indent
|
||||
* @param string $indent_string
|
||||
* @return string
|
||||
*/
|
||||
function indent_text($text, $indent, $indent_string = ' ') {
|
||||
if ($indent && $indent_string) {
|
||||
return str_replace("\n", "\n".str_repeat($indent_string, $indent), $text);
|
||||
} else {
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class used to format/minify HTML nodes
|
||||
*
|
||||
* Used like:
|
||||
* <code>
|
||||
* <?php
|
||||
* $formatter = new HtmlFormatter();
|
||||
* $formatter->format($root);
|
||||
* ?>
|
||||
* </code>
|
||||
*/
|
||||
class HtmlFormatter {
|
||||
|
||||
/**
|
||||
* Determines which elements start on a new line and which function as block
|
||||
* @var array('element' => array('new_line' => true, 'as_block' => true, 'format_inside' => true))
|
||||
*/
|
||||
var $block_elements = array(
|
||||
'p' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h1' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h2' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h3' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h4' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h5' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h6' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
|
||||
'form' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'fieldset' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'legend' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
'dl' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
'dt' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
'dd' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'ol' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'ul' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'li' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
|
||||
'table' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'tr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
|
||||
'dir' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'menu' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'address' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'blockquote' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'center' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'del' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
//'div' => array('new_line' => false, 'as_block' => true, 'format_inside' => true),
|
||||
'hr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'ins' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'noscript' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'pre' => array('new_line' => true, 'as_block' => true, 'format_inside' => false),
|
||||
'script' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'style' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
|
||||
'html' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'head' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'body' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'title' => array('new_line' => true, 'as_block' => false, 'format_inside' => false)
|
||||
);
|
||||
|
||||
/**
|
||||
* Determines which characters are considered whitespace
|
||||
* @var array("\t" => true) True to recognize as new line
|
||||
*/
|
||||
var $whitespace = array(
|
||||
' ' => false,
|
||||
"\t" => false,
|
||||
"\x0B" => false,
|
||||
"\0" => false,
|
||||
"\n" => true,
|
||||
"\r" => true
|
||||
);
|
||||
|
||||
/**
|
||||
* String that is used to generate correct indenting
|
||||
* @var string
|
||||
*/
|
||||
var $indent_string = ' ';
|
||||
|
||||
/**
|
||||
* String that is used to break lines
|
||||
* @var string
|
||||
*/
|
||||
var $linebreak_string = "\n";
|
||||
|
||||
/**
|
||||
* Other formatting options
|
||||
* @var array
|
||||
*/
|
||||
public $options = array(
|
||||
'img_alt' => '',
|
||||
'self_close_str' => null,
|
||||
'attribute_shorttag' => false,
|
||||
'sort_attributes' => false,
|
||||
'attributes_case' => CASE_LOWER,
|
||||
'minify_script' => true
|
||||
);
|
||||
|
||||
/**
|
||||
* Errors found during formatting
|
||||
* @var array
|
||||
*/
|
||||
var $errors = array();
|
||||
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
* @param array $options {@link $options}
|
||||
*/
|
||||
function __construct($options = array()) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
|
||||
if (isset($options['indent_str']))
|
||||
$this->indent_string = $options['indent_str'];
|
||||
|
||||
if (isset($options['linebreak_str']))
|
||||
$this->linebreak_string = $options['linebreak_str'];
|
||||
}
|
||||
|
||||
#php4 PHP4 class constructor compatibility
|
||||
#function HtmlFormatter($options = array()) {return $this->__construct($options);}
|
||||
#php4e
|
||||
|
||||
/**
|
||||
* Class magic invoke method, performs {@link format()}
|
||||
* @access private
|
||||
*/
|
||||
function __invoke(&$node) {
|
||||
return $this->format($node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies HTML / removes unneeded whitespace
|
||||
* @param DomNode $root
|
||||
* @param bool $strip_comments
|
||||
* @param bool $recursive
|
||||
*/
|
||||
static function minify_html(&$root, $strip_comments = true, $recursive = true) {
|
||||
if ($strip_comments) {
|
||||
foreach($root->select(':comment', false, $recursive, true) as $c) {
|
||||
$prev = $c->getSibling(-1);
|
||||
$next = $c->getSibling(1);
|
||||
$c->delete();
|
||||
if ($prev && $next && ($prev->isText()) && ($next->isText())) {
|
||||
$prev->text .= $next->text;
|
||||
$next->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach($root->select('(!pre + !xmp + !style + !script + !"?php" + !"~text~" + !"~comment~"):not-empty > "~text~"', false, $recursive, true) as $c) {
|
||||
$c->text = preg_replace('`\s+`', ' ', $c->text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies javascript using JSMin+
|
||||
* @param DomNode $root
|
||||
* @param string $indent_string
|
||||
* @param bool $wrap_comment Wrap javascript in HTML comments (<!-- ~text~ //-->)
|
||||
* @param bool $recursive
|
||||
* @return bool|array Array of errors on failure, true on succes
|
||||
*/
|
||||
static function minify_javascript(&$root, $indent_string = ' ', $wrap_comment = true, $recursive = true) {
|
||||
#php4 JSMin+ doesn't support PHP4
|
||||
#return true;
|
||||
#php4e
|
||||
#php5
|
||||
include_once('third party/jsminplus.php');
|
||||
|
||||
$errors = array();
|
||||
foreach($root->select('script:not-empty > "~text~"', false, $recursive, true) as $c) {
|
||||
try {
|
||||
$text = $c->text;
|
||||
while ($text) {
|
||||
$text = trim($text);
|
||||
//Remove comment/CDATA tags at begin and end
|
||||
if (substr($text, 0, 4) === '<!--') {
|
||||
$text = substr($text, 5);
|
||||
continue;
|
||||
} elseif (strtolower(substr($text, 0, 9)) === '<![cdata[') {
|
||||
$text = substr($text, 10);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($end = substr($text, -3)) && (($end === '-->') || ($end === ']]>'))) {
|
||||
$text = substr($text, 0, -3);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (trim($text)) {
|
||||
$text = \JSMinPlus::minify($text);
|
||||
if ($wrap_comment) {
|
||||
$text = "<!--\n".$text."\n//-->";
|
||||
}
|
||||
if ($indent_string && ($wrap_comment || (strpos($text, "\n") !== false))) {
|
||||
$text = indent_text("\n".$text, $c->indent(), $indent_string);
|
||||
}
|
||||
}
|
||||
$c->text = $text;
|
||||
} catch (\Exception $e) {
|
||||
$errors[] = array($e, $c->parent->dumpLocation());
|
||||
}
|
||||
}
|
||||
|
||||
return (($errors) ? $errors : true);
|
||||
#php5e
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats HTML
|
||||
* @param DomNode $root
|
||||
* @param bool $recursive
|
||||
* @access private
|
||||
*/
|
||||
function format_html(&$root, $recursive = null) {
|
||||
if ($recursive === null) {
|
||||
$recursive = true;
|
||||
self::minify_html($root);
|
||||
} elseif (is_int($recursive)) {
|
||||
$recursive = (($recursive > 1) ? $recursive - 1 : false);
|
||||
}
|
||||
|
||||
$root_tag = strtolower($root->tag);
|
||||
$in_block = isset($this->block_elements[$root_tag]) && $this->block_elements[$root_tag]['as_block'];
|
||||
$child_count = count($root->children);
|
||||
|
||||
if (isset($this->options['attributes_case']) && $this->options['attributes_case']) {
|
||||
$root->attributes = array_change_key_case($root->attributes, $this->options['attributes_case']);
|
||||
$root->attributes_ns = null;
|
||||
}
|
||||
|
||||
if (isset($this->options['sort_attributes']) && $this->options['sort_attributes']) {
|
||||
if ($this->options['sort_attributes'] === 'reverse') {
|
||||
krsort($root->attributes);
|
||||
} else {
|
||||
ksort($root->attributes);
|
||||
}
|
||||
}
|
||||
|
||||
if ($root->select(':element', true, false, true)) {
|
||||
$root->setTag(strtolower($root->tag), true);
|
||||
if (($this->options['img_alt'] !== null) && ($root_tag === 'img') && (!isset($root->alt))) {
|
||||
$root->setAttribute('alt', $this->options['img_alt']);
|
||||
}
|
||||
}
|
||||
if ($this->options['self_close_str'] !== null) {
|
||||
$root->self_close_str = $this->options['self_close_str'];
|
||||
}
|
||||
if ($this->options['attribute_shorttag'] !== null) {
|
||||
$root->attribute_shorttag = $this->options['attribute_shorttag'];
|
||||
}
|
||||
|
||||
$prev = null;
|
||||
$n_tag = '';
|
||||
// $prev_tag = '';
|
||||
$as_block = false;
|
||||
$prev_asblock = false;
|
||||
for($i = 0; $i < $child_count; $i++) {
|
||||
$n =& $root->children[$i];
|
||||
$indent = $n->indent();
|
||||
|
||||
if (!$n->isText()) {
|
||||
$n_tag = strtolower($n->tag);
|
||||
$new_line = isset($this->block_elements[$n_tag]) && $this->block_elements[$n_tag]['new_line'];
|
||||
$as_block = isset($this->block_elements[$n_tag]) && $this->block_elements[$n_tag]['as_block'];
|
||||
$format_inside = ((!isset($this->block_elements[$n_tag])) || $this->block_elements[$n_tag]['format_inside']);
|
||||
|
||||
if ($prev && ($prev->isText()) && $prev->text && ($char = $prev->text[strlen($prev->text) - 1]) && isset($this->whitespace[$char])) {
|
||||
if ($this->whitespace[$char]) {
|
||||
$prev->text .= str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$prev->text = substr_replace($prev->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), -1, 1);
|
||||
}
|
||||
} elseif (($new_line || $prev_asblock || ($in_block && ($i === 0)))){
|
||||
if ($prev && ($prev->isText())) {
|
||||
$prev->text .= $this->linebreak_string.str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$root->addText($this->linebreak_string.str_repeat($this->indent_string, $indent), $i);
|
||||
++$child_count;
|
||||
}
|
||||
}
|
||||
|
||||
if ($format_inside && count($n->children)) {
|
||||
//$last = end($n->children);
|
||||
$last = $n->children[count($n->children) - 1];
|
||||
$last_tag = ($last) ? strtolower($last->tag) : '';
|
||||
$last_asblock = ($last_tag && isset($this->block_elements[$last_tag]) && $this->block_elements[$last_tag]['as_block']);
|
||||
|
||||
if (($n->childCount(true) > 0) || (trim($n->getPlainText()))) {
|
||||
if ($last && ($last->isText()) && $last->text && ($char = $last->text[strlen($last->text) - 1]) && isset($this->whitespace[$char])) {
|
||||
if ($as_block || ($last->index() > 0) || isset($this->whitespace[$last->text[0]])) {
|
||||
if ($this->whitespace[$char]) {
|
||||
$last->text .= str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$last->text = substr_replace($last->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), -1, 1);
|
||||
}
|
||||
}
|
||||
} elseif (($as_block || $last_asblock || ($in_block && ($i === 0))) && $last) {
|
||||
if ($last && ($last->isText())) {
|
||||
$last->text .= $this->linebreak_string.str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$n->addText($this->linebreak_string.str_repeat($this->indent_string, $indent));
|
||||
}
|
||||
}
|
||||
} elseif (!trim($n->getInnerText())) {
|
||||
$n->clear();
|
||||
}
|
||||
|
||||
if ($recursive) {
|
||||
$this->format_html($n, $recursive);
|
||||
}
|
||||
}
|
||||
|
||||
} elseif (trim($n->text) && ((($i - 1 < $child_count) && ($char = $n->text[0]) && isset($this->whitespace[$char])) || ($in_block && ($i === 0)))) {
|
||||
if (isset($this->whitespace[$char])) {
|
||||
if ($this->whitespace[$char]) {
|
||||
$n->text = str_repeat($this->indent_string, $indent).$n->text;
|
||||
} else {
|
||||
$n->text = substr_replace($n->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), 0, 1);
|
||||
}
|
||||
} else {
|
||||
$n->text = $this->linebreak_string.str_repeat($this->indent_string, $indent).$n->text;
|
||||
}
|
||||
}
|
||||
|
||||
$prev = $n;
|
||||
// $prev_tag = $n_tag;
|
||||
$prev_asblock = $as_block;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats HTML/Javascript
|
||||
* @param DomNode $root
|
||||
* @see format_html()
|
||||
*/
|
||||
function format(&$node) {
|
||||
$this->errors = array();
|
||||
if ($this->options['minify_script']) {
|
||||
$a = self::minify_javascript($node, $this->indent_string, true, true);
|
||||
if (is_array($a)) {
|
||||
foreach($a as $error) {
|
||||
$this->errors[] = $error[0]->getMessage().' >>> '.$error[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->format_html($node);
|
||||
}
|
||||
}
|
||||
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
namespace pagelayerQuery;
|
||||
|
||||
/**
|
||||
* Indents text
|
||||
* @param string $text
|
||||
* @param int $indent
|
||||
* @param string $indent_string
|
||||
* @return string
|
||||
*/
|
||||
function indent_text($text, $indent, $indent_string = ' ') {
|
||||
if ($indent && $indent_string) {
|
||||
return str_replace("\n", "\n".str_repeat($indent_string, $indent), $text);
|
||||
} else {
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class used to format/minify HTML nodes
|
||||
*
|
||||
* Used like:
|
||||
* <code>
|
||||
* <?php
|
||||
* $formatter = new HtmlFormatter();
|
||||
* $formatter->format($root);
|
||||
* ?>
|
||||
* </code>
|
||||
*/
|
||||
class HtmlFormatter {
|
||||
|
||||
/**
|
||||
* Determines which elements start on a new line and which function as block
|
||||
* @var array('element' => array('new_line' => true, 'as_block' => true, 'format_inside' => true))
|
||||
*/
|
||||
var $block_elements = array(
|
||||
'p' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h1' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h2' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h3' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h4' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h5' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'h6' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
|
||||
'form' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'fieldset' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'legend' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
'dl' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
'dt' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
'dd' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'ol' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'ul' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'li' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
|
||||
'table' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'tr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
|
||||
'dir' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'menu' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'address' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'blockquote' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'center' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'del' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||||
//'div' => array('new_line' => false, 'as_block' => true, 'format_inside' => true),
|
||||
'hr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'ins' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'noscript' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'pre' => array('new_line' => true, 'as_block' => true, 'format_inside' => false),
|
||||
'script' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'style' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
|
||||
'html' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'head' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'body' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||||
'title' => array('new_line' => true, 'as_block' => false, 'format_inside' => false)
|
||||
);
|
||||
|
||||
/**
|
||||
* Determines which characters are considered whitespace
|
||||
* @var array("\t" => true) True to recognize as new line
|
||||
*/
|
||||
var $whitespace = array(
|
||||
' ' => false,
|
||||
"\t" => false,
|
||||
"\x0B" => false,
|
||||
"\0" => false,
|
||||
"\n" => true,
|
||||
"\r" => true
|
||||
);
|
||||
|
||||
/**
|
||||
* String that is used to generate correct indenting
|
||||
* @var string
|
||||
*/
|
||||
var $indent_string = ' ';
|
||||
|
||||
/**
|
||||
* String that is used to break lines
|
||||
* @var string
|
||||
*/
|
||||
var $linebreak_string = "\n";
|
||||
|
||||
/**
|
||||
* Other formatting options
|
||||
* @var array
|
||||
*/
|
||||
public $options = array(
|
||||
'img_alt' => '',
|
||||
'self_close_str' => null,
|
||||
'attribute_shorttag' => false,
|
||||
'sort_attributes' => false,
|
||||
'attributes_case' => CASE_LOWER,
|
||||
'minify_script' => true
|
||||
);
|
||||
|
||||
/**
|
||||
* Errors found during formatting
|
||||
* @var array
|
||||
*/
|
||||
var $errors = array();
|
||||
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
* @param array $options {@link $options}
|
||||
*/
|
||||
function __construct($options = array()) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
|
||||
if (isset($options['indent_str']))
|
||||
$this->indent_string = $options['indent_str'];
|
||||
|
||||
if (isset($options['linebreak_str']))
|
||||
$this->linebreak_string = $options['linebreak_str'];
|
||||
}
|
||||
|
||||
#php4 PHP4 class constructor compatibility
|
||||
#function HtmlFormatter($options = array()) {return $this->__construct($options);}
|
||||
#php4e
|
||||
|
||||
/**
|
||||
* Class magic invoke method, performs {@link format()}
|
||||
* @access private
|
||||
*/
|
||||
function __invoke(&$node) {
|
||||
return $this->format($node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies HTML / removes unneeded whitespace
|
||||
* @param DomNode $root
|
||||
* @param bool $strip_comments
|
||||
* @param bool $recursive
|
||||
*/
|
||||
static function minify_html(&$root, $strip_comments = true, $recursive = true) {
|
||||
if ($strip_comments) {
|
||||
foreach($root->select(':comment', false, $recursive, true) as $c) {
|
||||
$prev = $c->getSibling(-1);
|
||||
$next = $c->getSibling(1);
|
||||
$c->delete();
|
||||
if ($prev && $next && ($prev->isText()) && ($next->isText())) {
|
||||
$prev->text .= $next->text;
|
||||
$next->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach($root->select('(!pre + !xmp + !style + !script + !"?php" + !"~text~" + !"~comment~"):not-empty > "~text~"', false, $recursive, true) as $c) {
|
||||
$c->text = preg_replace('`\s+`', ' ', $c->text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies javascript using JSMin+
|
||||
* @param DomNode $root
|
||||
* @param string $indent_string
|
||||
* @param bool $wrap_comment Wrap javascript in HTML comments (<!-- ~text~ //-->)
|
||||
* @param bool $recursive
|
||||
* @return bool|array Array of errors on failure, true on succes
|
||||
*/
|
||||
static function minify_javascript(&$root, $indent_string = ' ', $wrap_comment = true, $recursive = true) {
|
||||
#php4 JSMin+ doesn't support PHP4
|
||||
#return true;
|
||||
#php4e
|
||||
#php5
|
||||
include_once('third party/jsminplus.php');
|
||||
|
||||
$errors = array();
|
||||
foreach($root->select('script:not-empty > "~text~"', false, $recursive, true) as $c) {
|
||||
try {
|
||||
$text = $c->text;
|
||||
while ($text) {
|
||||
$text = trim($text);
|
||||
//Remove comment/CDATA tags at begin and end
|
||||
if (substr($text, 0, 4) === '<!--') {
|
||||
$text = substr($text, 5);
|
||||
continue;
|
||||
} elseif (strtolower(substr($text, 0, 9)) === '<![cdata[') {
|
||||
$text = substr($text, 10);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($end = substr($text, -3)) && (($end === '-->') || ($end === ']]>'))) {
|
||||
$text = substr($text, 0, -3);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (trim($text)) {
|
||||
$text = \JSMinPlus::minify($text);
|
||||
if ($wrap_comment) {
|
||||
$text = "<!--\n".$text."\n//-->";
|
||||
}
|
||||
if ($indent_string && ($wrap_comment || (strpos($text, "\n") !== false))) {
|
||||
$text = indent_text("\n".$text, $c->indent(), $indent_string);
|
||||
}
|
||||
}
|
||||
$c->text = $text;
|
||||
} catch (\Exception $e) {
|
||||
$errors[] = array($e, $c->parent->dumpLocation());
|
||||
}
|
||||
}
|
||||
|
||||
return (($errors) ? $errors : true);
|
||||
#php5e
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats HTML
|
||||
* @param DomNode $root
|
||||
* @param bool $recursive
|
||||
* @access private
|
||||
*/
|
||||
function format_html(&$root, $recursive = null) {
|
||||
if ($recursive === null) {
|
||||
$recursive = true;
|
||||
self::minify_html($root);
|
||||
} elseif (is_int($recursive)) {
|
||||
$recursive = (($recursive > 1) ? $recursive - 1 : false);
|
||||
}
|
||||
|
||||
$root_tag = strtolower($root->tag);
|
||||
$in_block = isset($this->block_elements[$root_tag]) && $this->block_elements[$root_tag]['as_block'];
|
||||
$child_count = count($root->children);
|
||||
|
||||
if (isset($this->options['attributes_case']) && $this->options['attributes_case']) {
|
||||
$root->attributes = array_change_key_case($root->attributes, $this->options['attributes_case']);
|
||||
$root->attributes_ns = null;
|
||||
}
|
||||
|
||||
if (isset($this->options['sort_attributes']) && $this->options['sort_attributes']) {
|
||||
if ($this->options['sort_attributes'] === 'reverse') {
|
||||
krsort($root->attributes);
|
||||
} else {
|
||||
ksort($root->attributes);
|
||||
}
|
||||
}
|
||||
|
||||
if ($root->select(':element', true, false, true)) {
|
||||
$root->setTag(strtolower($root->tag), true);
|
||||
if (($this->options['img_alt'] !== null) && ($root_tag === 'img') && (!isset($root->alt))) {
|
||||
$root->setAttribute('alt', $this->options['img_alt']);
|
||||
}
|
||||
}
|
||||
if ($this->options['self_close_str'] !== null) {
|
||||
$root->self_close_str = $this->options['self_close_str'];
|
||||
}
|
||||
if ($this->options['attribute_shorttag'] !== null) {
|
||||
$root->attribute_shorttag = $this->options['attribute_shorttag'];
|
||||
}
|
||||
|
||||
$prev = null;
|
||||
$n_tag = '';
|
||||
// $prev_tag = '';
|
||||
$as_block = false;
|
||||
$prev_asblock = false;
|
||||
for($i = 0; $i < $child_count; $i++) {
|
||||
$n =& $root->children[$i];
|
||||
$indent = $n->indent();
|
||||
|
||||
if (!$n->isText()) {
|
||||
$n_tag = strtolower($n->tag);
|
||||
$new_line = isset($this->block_elements[$n_tag]) && $this->block_elements[$n_tag]['new_line'];
|
||||
$as_block = isset($this->block_elements[$n_tag]) && $this->block_elements[$n_tag]['as_block'];
|
||||
$format_inside = ((!isset($this->block_elements[$n_tag])) || $this->block_elements[$n_tag]['format_inside']);
|
||||
|
||||
if ($prev && ($prev->isText()) && $prev->text && ($char = $prev->text[strlen($prev->text) - 1]) && isset($this->whitespace[$char])) {
|
||||
if ($this->whitespace[$char]) {
|
||||
$prev->text .= str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$prev->text = substr_replace($prev->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), -1, 1);
|
||||
}
|
||||
} elseif (($new_line || $prev_asblock || ($in_block && ($i === 0)))){
|
||||
if ($prev && ($prev->isText())) {
|
||||
$prev->text .= $this->linebreak_string.str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$root->addText($this->linebreak_string.str_repeat($this->indent_string, $indent), $i);
|
||||
++$child_count;
|
||||
}
|
||||
}
|
||||
|
||||
if ($format_inside && count($n->children)) {
|
||||
//$last = end($n->children);
|
||||
$last = $n->children[count($n->children) - 1];
|
||||
$last_tag = ($last) ? strtolower($last->tag) : '';
|
||||
$last_asblock = ($last_tag && isset($this->block_elements[$last_tag]) && $this->block_elements[$last_tag]['as_block']);
|
||||
|
||||
if (($n->childCount(true) > 0) || (trim($n->getPlainText()))) {
|
||||
if ($last && ($last->isText()) && $last->text && ($char = $last->text[strlen($last->text) - 1]) && isset($this->whitespace[$char])) {
|
||||
if ($as_block || ($last->index() > 0) || isset($this->whitespace[$last->text[0]])) {
|
||||
if ($this->whitespace[$char]) {
|
||||
$last->text .= str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$last->text = substr_replace($last->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), -1, 1);
|
||||
}
|
||||
}
|
||||
} elseif (($as_block || $last_asblock || ($in_block && ($i === 0))) && $last) {
|
||||
if ($last && ($last->isText())) {
|
||||
$last->text .= $this->linebreak_string.str_repeat($this->indent_string, $indent);
|
||||
} else {
|
||||
$n->addText($this->linebreak_string.str_repeat($this->indent_string, $indent));
|
||||
}
|
||||
}
|
||||
} elseif (!trim($n->getInnerText())) {
|
||||
$n->clear();
|
||||
}
|
||||
|
||||
if ($recursive) {
|
||||
$this->format_html($n, $recursive);
|
||||
}
|
||||
}
|
||||
|
||||
} elseif (trim($n->text) && ((($i - 1 < $child_count) && ($char = $n->text[0]) && isset($this->whitespace[$char])) || ($in_block && ($i === 0)))) {
|
||||
if (isset($this->whitespace[$char])) {
|
||||
if ($this->whitespace[$char]) {
|
||||
$n->text = str_repeat($this->indent_string, $indent).$n->text;
|
||||
} else {
|
||||
$n->text = substr_replace($n->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), 0, 1);
|
||||
}
|
||||
} else {
|
||||
$n->text = $this->linebreak_string.str_repeat($this->indent_string, $indent).$n->text;
|
||||
}
|
||||
}
|
||||
|
||||
$prev = $n;
|
||||
// $prev_tag = $n_tag;
|
||||
$prev_asblock = $as_block;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats HTML/Javascript
|
||||
* @param DomNode $root
|
||||
* @see format_html()
|
||||
*/
|
||||
function format(&$node) {
|
||||
$this->errors = array();
|
||||
if ($this->options['minify_script']) {
|
||||
$a = self::minify_javascript($node, $this->indent_string, true, true);
|
||||
if (is_array($a)) {
|
||||
foreach($a as $error) {
|
||||
$this->errors[] = $error[0]->getMessage().' >>> '.$error[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->format_html($node);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,101 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
namespace pagelayerQuery;
|
||||
|
||||
/**
|
||||
* Converts a XML document to an array
|
||||
*/
|
||||
class XML2ArrayParser extends HtmlParserBase {
|
||||
|
||||
/**
|
||||
* Holds the document structure
|
||||
* @var array array('name' => 'tag', 'attrs' => array('attr' => 'val'), 'childen' => array())
|
||||
*/
|
||||
var $root = array(
|
||||
'name' => '',
|
||||
'attrs' => array(),
|
||||
'children' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* Current parsing hierarchy
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
var $hierarchy = array();
|
||||
|
||||
protected function parse_hierarchy($self_close) {
|
||||
if ($this->status['closing_tag']) {
|
||||
$found = false;
|
||||
for ($count = count($this->hierarchy), $i = $count - 1; $i >= 0; $i--) {
|
||||
if (strcasecmp($this->hierarchy[$i]['name'], $this->status['tag_name']) === 0) {
|
||||
|
||||
for($ii = ($count - $i - 1); $ii >= 0; $ii--) {
|
||||
$e = array_pop($this->hierarchy);
|
||||
if ($ii > 0) {
|
||||
$this->addError('Closing tag "'.$this->status['tag_name'].'" while "'.$e['name'].'" is not closed yet');
|
||||
}
|
||||
}
|
||||
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$found) {
|
||||
$this->addError('Closing tag "'.$this->status['tag_name'].'" which is not open');
|
||||
}
|
||||
} else {
|
||||
$tag = array(
|
||||
'name' => $this->status['tag_name'],
|
||||
'attrs' => $this->status['attributes'],
|
||||
'children' => array()
|
||||
);
|
||||
if ($this->hierarchy) {
|
||||
$current =& $this->hierarchy[count($this->hierarchy) - 1];
|
||||
$current['children'][] = $tag;
|
||||
$tag =& $current['children'][count($current['children']) - 1];
|
||||
unset($current['tagData']);
|
||||
} else {
|
||||
$this->root = $tag;
|
||||
$tag =& $this->root;
|
||||
$self_close = false;
|
||||
}
|
||||
if (!$self_close) {
|
||||
$this->hierarchy[] =& $tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parse_tag_default() {
|
||||
if (!parent::parse_tag_default()) {return false;}
|
||||
|
||||
if ($this->status['tag_name'][0] !== '?') {
|
||||
$this->parse_hierarchy(($this->status['self_close']) ? true : null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function parse_text() {
|
||||
parent::parse_text();
|
||||
if (($this->status['text'] !== '') && $this->hierarchy) {
|
||||
$current =& $this->hierarchy[count($this->hierarchy) - 1];
|
||||
if (!$current['children']) {
|
||||
$current['tagData'] = $this->status['text'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parse_all() {
|
||||
return ((parent::parse_all()) ? $this->root : false);
|
||||
}
|
||||
}
|
||||
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
namespace pagelayerQuery;
|
||||
|
||||
/**
|
||||
* Converts a XML document to an array
|
||||
*/
|
||||
class XML2ArrayParser extends HtmlParserBase {
|
||||
|
||||
/**
|
||||
* Holds the document structure
|
||||
* @var array array('name' => 'tag', 'attrs' => array('attr' => 'val'), 'childen' => array())
|
||||
*/
|
||||
var $root = array(
|
||||
'name' => '',
|
||||
'attrs' => array(),
|
||||
'children' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* Current parsing hierarchy
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
var $hierarchy = array();
|
||||
|
||||
protected function parse_hierarchy($self_close) {
|
||||
if ($this->status['closing_tag']) {
|
||||
$found = false;
|
||||
for ($count = count($this->hierarchy), $i = $count - 1; $i >= 0; $i--) {
|
||||
if (strcasecmp($this->hierarchy[$i]['name'], $this->status['tag_name']) === 0) {
|
||||
|
||||
for($ii = ($count - $i - 1); $ii >= 0; $ii--) {
|
||||
$e = array_pop($this->hierarchy);
|
||||
if ($ii > 0) {
|
||||
$this->addError('Closing tag "'.$this->status['tag_name'].'" while "'.$e['name'].'" is not closed yet');
|
||||
}
|
||||
}
|
||||
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$found) {
|
||||
$this->addError('Closing tag "'.$this->status['tag_name'].'" which is not open');
|
||||
}
|
||||
} else {
|
||||
$tag = array(
|
||||
'name' => $this->status['tag_name'],
|
||||
'attrs' => $this->status['attributes'],
|
||||
'children' => array()
|
||||
);
|
||||
if ($this->hierarchy) {
|
||||
$current =& $this->hierarchy[count($this->hierarchy) - 1];
|
||||
$current['children'][] = $tag;
|
||||
$tag =& $current['children'][count($current['children']) - 1];
|
||||
unset($current['tagData']);
|
||||
} else {
|
||||
$this->root = $tag;
|
||||
$tag =& $this->root;
|
||||
$self_close = false;
|
||||
}
|
||||
if (!$self_close) {
|
||||
$this->hierarchy[] =& $tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parse_tag_default() {
|
||||
if (!parent::parse_tag_default()) {return false;}
|
||||
|
||||
if ($this->status['tag_name'][0] !== '?') {
|
||||
$this->parse_hierarchy(($this->status['self_close']) ? true : null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function parse_text() {
|
||||
parent::parse_text();
|
||||
if (($this->status['text'] !== '') && $this->hierarchy) {
|
||||
$current =& $this->hierarchy[count($this->hierarchy) - 1];
|
||||
if (!$current['children']) {
|
||||
$current['tagData'] = $this->status['text'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parse_all() {
|
||||
return ((parent::parse_all()) ? $this->root : false);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,101 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
use pagelayerQuery\Html5Parser;
|
||||
use pagelayerQuery\HtmlFormatter;
|
||||
|
||||
/**
|
||||
* Returns HTML DOM from string
|
||||
* @param string $str
|
||||
* @param bool $return_root Return root node or return parser object
|
||||
* @return Html5Parser|DomNode
|
||||
*/
|
||||
function str_get_dom($str, $return_root = true) {
|
||||
$a = new Html5Parser($str);
|
||||
return (($return_root) ? $a->root : $a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTML DOM from file/website
|
||||
* @param string $str
|
||||
* @param bool $return_root Return root node or return parser object
|
||||
* @param bool $use_include_path Use include path search in file_get_contents
|
||||
* @param resource $context Context resource used in file_get_contents (PHP >= 5.0.0)
|
||||
* @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);
|
||||
}
|
||||
|
||||
return (($f === false) ? false : str_get_dom($f, $return_root));
|
||||
}
|
||||
|
||||
/**
|
||||
* Format/beautify DOM
|
||||
* @param DomNode $root
|
||||
* @param array $options Extra formatting options {@link Formatter::$options}
|
||||
* @return bool
|
||||
*/
|
||||
function dom_format(&$root, $options = array()) {
|
||||
$formatter = new HtmlFormatter($options);
|
||||
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);
|
||||
include_once('IQuery.php');
|
||||
include_once('gan_tokenizer.php');
|
||||
include_once('gan_parser_html.php');
|
||||
include_once('gan_node_html.php');
|
||||
include_once('gan_selector_html.php');
|
||||
include_once('gan_formatter.php');
|
||||
}
|
||||
#!
|
||||
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
use pagelayerQuery\Html5Parser;
|
||||
use pagelayerQuery\HtmlFormatter;
|
||||
|
||||
/**
|
||||
* Returns HTML DOM from string
|
||||
* @param string $str
|
||||
* @param bool $return_root Return root node or return parser object
|
||||
* @return Html5Parser|DomNode
|
||||
*/
|
||||
function str_get_dom($str, $return_root = true) {
|
||||
$a = new Html5Parser($str);
|
||||
return (($return_root) ? $a->root : $a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTML DOM from file/website
|
||||
* @param string $str
|
||||
* @param bool $return_root Return root node or return parser object
|
||||
* @param bool $use_include_path Use include path search in file_get_contents
|
||||
* @param resource $context Context resource used in file_get_contents (PHP >= 5.0.0)
|
||||
* @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);
|
||||
}
|
||||
|
||||
return (($f === false) ? false : str_get_dom($f, $return_root));
|
||||
}
|
||||
|
||||
/**
|
||||
* Format/beautify DOM
|
||||
* @param DomNode $root
|
||||
* @param array $options Extra formatting options {@link Formatter::$options}
|
||||
* @return bool
|
||||
*/
|
||||
function dom_format(&$root, $options = array()) {
|
||||
$formatter = new HtmlFormatter($options);
|
||||
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);
|
||||
include_once('IQuery.php');
|
||||
include_once('gan_tokenizer.php');
|
||||
include_once('gan_parser_html.php');
|
||||
include_once('gan_node_html.php');
|
||||
include_once('gan_selector_html.php');
|
||||
include_once('gan_formatter.php');
|
||||
}
|
||||
#!
|
||||
|
||||
?>
|
||||
@@ -1,281 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
use pagelayerQuery\IQuery;
|
||||
|
||||
/**
|
||||
* A jQuery-like object for php.
|
||||
*/
|
||||
class pagelayerQuery implements ArrayAccess, IteratorAggregate, IQuery {
|
||||
/// Properties ///
|
||||
|
||||
/**
|
||||
* @var IQuery[]
|
||||
*/
|
||||
protected $nodes = array();
|
||||
|
||||
/// Methods ///
|
||||
|
||||
public function __construct($nodes = array()) {
|
||||
$this->nodes = $nodes;
|
||||
}
|
||||
|
||||
public function addClass($classname) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->addClass($classname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function after($content) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->after($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function append($content) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->append($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function attr($name, $value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->attr($name);
|
||||
$value = str_replace('<', '<', $value);
|
||||
$value = str_replace('>', '>', $value);
|
||||
$node->attr($name, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function before($content) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->before($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->clear();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of matched elements.
|
||||
*
|
||||
* @return int Returns the count of matched elements.
|
||||
*/
|
||||
public function count() {
|
||||
return count($this->nodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format/beautify a DOM.
|
||||
*
|
||||
* @param pagelayerQuery\DomNode $dom The dom to format.
|
||||
* @param array $options Extra formatting options. See {@link pagelayerQuery\HtmlFormatter::$options}.
|
||||
* @return bool Returns `true` on sucess and `false` on failure.
|
||||
*/
|
||||
// public static function format($dom, $options = array()) {
|
||||
// $formatter = new pagelayerQuery\HtmlFormatter($options);
|
||||
// return $formatter->format($dom);
|
||||
// }
|
||||
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->nodes);
|
||||
}
|
||||
|
||||
public function hasClass($classname) {
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($node->hasClass($classname))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function html($value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->html();
|
||||
$node->html($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->nodes[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value) {
|
||||
|
||||
if (is_null($offset) || !isset($this->nodes[$offset])) {
|
||||
throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
|
||||
} else {
|
||||
$this->nodes[$offset]->replaceWith($value);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
if (isset($this->nodes[$offset])) {
|
||||
$this->nodes[$offset]->remove();
|
||||
unset($this->nodes[$offset]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query a file or url.
|
||||
*
|
||||
* @param string $path The path to the url.
|
||||
* @param resource $context A context suitable to be passed into {@link file_get_contents}
|
||||
* @return pagelayerQuery\DomNode Returns the root dom node for the html file.
|
||||
*/
|
||||
public static function parseFile($path, $context = null) {
|
||||
$html_str = file_get_contents($path, false, $context);
|
||||
return static::parseStr($html_str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query a string of html.
|
||||
*
|
||||
* @param string $html
|
||||
* @return pagelayerQuery\DomNode Returns the root dom node for the html string.
|
||||
*/
|
||||
public static function parseStr($html) {
|
||||
$parser = new pagelayerQuery\Html5Parser($html);
|
||||
return $parser->root;
|
||||
}
|
||||
|
||||
public function prepend($content = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->prepend($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function prop($name, $value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->prop($name);
|
||||
$node->prop($name, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function remove($selector = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->remove($selector);
|
||||
}
|
||||
if ($selector === null)
|
||||
$this->nodes = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeAttr($name) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->removeAttr($name);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeClass($classname) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->removeClass($classname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function replaceWith($content) {
|
||||
foreach ($this->nodes as &$node) {
|
||||
$node = $node->replaceWith($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function tagName($value = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->tagName();
|
||||
$node->tagName($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function text($value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->text();
|
||||
$node->text($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toggleClass($classname, $switch = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->toggleClass($classname, $switch);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unwrap() {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->unwrap();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function val($value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->val();
|
||||
$node->val($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function wrap($wrapping_element) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->wrap($wrapping_element);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function wrapInner($wrapping_element) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->wrapInner($wrapping_element);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* @author Niels A.D.
|
||||
* @author Todd Burry <todd@vanillaforums.com>
|
||||
* @copyright 2010 Niels A.D., 2014 Todd Burry
|
||||
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
|
||||
* @package pQuery
|
||||
*/
|
||||
|
||||
use pagelayerQuery\IQuery;
|
||||
|
||||
/**
|
||||
* A jQuery-like object for php.
|
||||
*/
|
||||
class pagelayerQuery implements ArrayAccess, IteratorAggregate, IQuery {
|
||||
/// Properties ///
|
||||
|
||||
/**
|
||||
* @var IQuery[]
|
||||
*/
|
||||
protected $nodes = array();
|
||||
|
||||
/// Methods ///
|
||||
|
||||
public function __construct($nodes = array()) {
|
||||
$this->nodes = $nodes;
|
||||
}
|
||||
|
||||
public function addClass($classname) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->addClass($classname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function after($content) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->after($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function append($content) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->append($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function attr($name, $value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->attr($name);
|
||||
$value = str_replace('<', '<', $value);
|
||||
$value = str_replace('>', '>', $value);
|
||||
$node->attr($name, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function before($content) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->before($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->clear();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of matched elements.
|
||||
*
|
||||
* @return int Returns the count of matched elements.
|
||||
*/
|
||||
public function count() {
|
||||
return count($this->nodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format/beautify a DOM.
|
||||
*
|
||||
* @param pagelayerQuery\DomNode $dom The dom to format.
|
||||
* @param array $options Extra formatting options. See {@link pagelayerQuery\HtmlFormatter::$options}.
|
||||
* @return bool Returns `true` on sucess and `false` on failure.
|
||||
*/
|
||||
// public static function format($dom, $options = array()) {
|
||||
// $formatter = new pagelayerQuery\HtmlFormatter($options);
|
||||
// return $formatter->format($dom);
|
||||
// }
|
||||
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->nodes);
|
||||
}
|
||||
|
||||
public function hasClass($classname) {
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($node->hasClass($classname))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function html($value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->html();
|
||||
$node->html($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->nodes[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value) {
|
||||
|
||||
if (is_null($offset) || !isset($this->nodes[$offset])) {
|
||||
throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
|
||||
} else {
|
||||
$this->nodes[$offset]->replaceWith($value);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
if (isset($this->nodes[$offset])) {
|
||||
$this->nodes[$offset]->remove();
|
||||
unset($this->nodes[$offset]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query a file or url.
|
||||
*
|
||||
* @param string $path The path to the url.
|
||||
* @param resource $context A context suitable to be passed into {@link file_get_contents}
|
||||
* @return pagelayerQuery\DomNode Returns the root dom node for the html file.
|
||||
*/
|
||||
public static function parseFile($path, $context = null) {
|
||||
$html_str = file_get_contents($path, false, $context);
|
||||
return static::parseStr($html_str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query a string of html.
|
||||
*
|
||||
* @param string $html
|
||||
* @return pagelayerQuery\DomNode Returns the root dom node for the html string.
|
||||
*/
|
||||
public static function parseStr($html) {
|
||||
$parser = new pagelayerQuery\Html5Parser($html);
|
||||
return $parser->root;
|
||||
}
|
||||
|
||||
public function prepend($content = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->prepend($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function prop($name, $value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->prop($name);
|
||||
$node->prop($name, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function remove($selector = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->remove($selector);
|
||||
}
|
||||
if ($selector === null)
|
||||
$this->nodes = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeAttr($name) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->removeAttr($name);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeClass($classname) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->removeClass($classname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function replaceWith($content) {
|
||||
foreach ($this->nodes as &$node) {
|
||||
$node = $node->replaceWith($content);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function tagName($value = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->tagName();
|
||||
$node->tagName($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function text($value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->text();
|
||||
$node->text($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toggleClass($classname, $switch = null) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->toggleClass($classname, $switch);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unwrap() {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->unwrap();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function val($value = null) {
|
||||
if (empty($this->nodes) && $value === null)
|
||||
return '';
|
||||
|
||||
foreach ($this->nodes as $node) {
|
||||
if ($value === null)
|
||||
return $node->val();
|
||||
$node->val($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function wrap($wrapping_element) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->wrap($wrapping_element);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function wrapInner($wrapping_element) {
|
||||
foreach ($this->nodes as $node) {
|
||||
$node->wrapInner($wrapping_element);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user