<commit_before>HtmlPageDom
===========

[![Build Status](https://secure.travis-ci.org/wasinger/htmlpagedom.png?branch=master)](http://travis-ci.org/wasinger/htmlpagedom)

Wa72\HtmlPageDom is a PHP library for easy manipulation of HTML documents using DOM.
It requires [DomCrawler from Symfony2 components](https://github.com/symfony/DomCrawler) for traversing 
the DOM tree and extends it by adding methods for manipulating the DOM tree of HTML documents.

It's useful when you need to not just extract information from an HTML file (what DomCrawler does) but
also to modify HTML pages. It is usable as a template engine: load your HTML template file, set new
HTML content on certain elements such as the page title, "div#content" or "ul#menu" and print out
the modified page.

Wa72\HtmlPageDom consists of two classes:

-   *HtmlPageCrawler* extends Symfony\Components\DomCrawler by adding jQuery inspired, HTML specific 
    DOM *manipulation* functions such as html(), before(), append(), wrap(), addClass() or css().
    It's like jQuery for PHP: simply select elements of an HTML page using CSS selectors and change their attributes and content.

-   *HtmlPage* represents one complete HTML document and offers convenience functions like setTitle($title),
    setMeta('description', $description), getBody(). It uses HtmlPageCrawler for filtering and manipulation DOM Elements.
 

Requirements
------------

-   PHP 5.3+
-   [Symfony\Components\DomCrawler](https://github.com/symfony/DomCrawler)
-   [Symfony\Components\CssSelector](https://github.com/symfony/CssSelector)

Installation
------------

-   using [composer] (http://getcomposer.org): add "wa72/htmlpagedom": "dev-master" to the "require" section of your composer.json

-   using other [PSR-0] (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) compliant autoloader:
    clone this project to where your included libraries are and point your autoloader to look for the 
    "\Wa72\HtmlPageDom" namespace in the "src" directory of this project

Usage
-----

HtmlPageCrawler is a wrapper around DOMNodes. HtmlPageCrawler objects can be created using "new" or the static function
HtmlPageCrawler::create(), which accepts an HTML string or a DOMNode (or an array of DOMNodes or a DOMNodeList)
as arguments.

Afterwards you can select nodes from the added DOM tree by calling filter() (equivalent to find() in jQuery) and alter
the selected elements using the following jQuery-like manipulation functions:

-   addClass(), hasClass(), removeClass(), toggleClass()
-   after()
-   append()
-   appendTo()
-   before()
-   css() (same as getStyle() / setStyle())
-   html() (same as getInnerHtml() / setInnerHtml())
-   insertAfter()
-   insertBefore()
-   makeEmpty() (equivalent to emtpy() in jQuery)
-   prepend()
-   prependTo()
-   remove()
-   removeAttribute(), getAttribute(), setAttribute()
-   replaceAll()
-   replaceWith()
-   unwrap()
-   wrap()
-   wrapInner()
-   wrapAll()

To get the modified DOM as HTML code use html() (returns innerHTML of the first node in your crawler object)
or saveHTML() (returns combined "outer" HTML code of all elements in the list).

Example:

```php

// create an object from a fragment of HTML code as you would do with jQuery's $() function
$c = \Wa72\HtmlPageDom\HtmlPageCrawler::create('<div id="content"><h1>Title</h1></div>');

// the above is the same as calling:
$c = new \Wa72\HtmlPageDom\HtmlPageCrawler('<div id="content"><h1>Title</h1></div>');

// filter for h1 elements and wrap them with an HTML structure
$c->filter('h1')->wrap('<div class="innercontent">');

// return the modified HTML
echo $c->saveHTML();
// or simply:
echo $c; // implicit __toString() calls saveHTML()
// will output: <div id="content"><div class="innercontent"><h1>Title</h1></div></div>

```

Usage examples for HtmlPage:

```php
// create a new HtmlPage object with an empty HTML skeleton
$page = new \Wa72\HtmlPageDom\HtmlPage();

// or create a HtmlPage object from an existing page
$page = new \Wa72\HtmlPageDom\HtmlPage(file_get_contents('http://www.heise.de'));

// get or set page title
echo $page->getTitle();
$page->setTitle('New page title');
echo $page->getTitle();


// add HTML content<commit_msg>examples changed<commit_after>HtmlPageDom
===========

[![Build Status](https://secure.travis-ci.org/wasinger/htmlpagedom.png?branch=master)](http://travis-ci.org/wasinger/htmlpagedom)

Wa72\HtmlPageDom is a PHP library for easy manipulation of HTML documents using DOM.
It requires [DomCrawler from Symfony2 components](https://github.com/symfony/DomCrawler) for traversing 
the DOM tree and extends it by adding methods for manipulating the DOM tree of HTML documents.

It's useful when you need to not just extract information from an HTML file (what DomCrawler does) but
also to modify HTML pages. It is usable as a template engine: load your HTML template file, set new
HTML content on certain elements such as the page title, "div#content" or "ul#menu" and print out
the modified page.

Wa72\HtmlPageDom consists of two classes:

-   *HtmlPageCrawler* extends Symfony\Components\DomCrawler by adding jQuery inspired, HTML specific 
    DOM *manipulation* functions such as html(), before(), append(), wrap(), addClass() or css().
    It's like jQuery for PHP: simply select elements of an HTML page using CSS selectors and change their attributes and content.

-   *HtmlPage* represents one complete HTML document and offers convenience functions like setTitle($title),
    setMeta('description', $description), getBody(). It uses HtmlPageCrawler for filtering and manipulation DOM Elements.
 

Requirements
------------

-   PHP 5.3+
-   [Symfony\Components\DomCrawler](https://github.com/symfony/DomCrawler)
-   [Symfony\Components\CssSelector](https://github.com/symfony/CssSelector)

Installation
------------

-   using [composer] (http://getcomposer.org): add "wa72/htmlpagedom": "dev-master" to the "require" section of your composer.json

-   using other [PSR-0] (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) compliant autoloader:
    clone this project to where your included libraries are and point your autoloader to look for the 
    "\Wa72\HtmlPageDom" namespace in the "src" directory of this project

Usage
-----

HtmlPageCrawler is a wrapper around DOMNodes. HtmlPageCrawler objects can be created using "new" or the static function
HtmlPageCrawler::create(), which accepts an HTML string or a DOMNode (or an array of DOMNodes or a DOMNodeList)
as arguments.

Afterwards you can select nodes from the added DOM tree by calling filter() (equivalent to find() in jQuery) and alter
the selected elements using the following jQuery-like manipulation functions:

-   addClass(), hasClass(), removeClass(), toggleClass()
-   after()
-   append()
-   appendTo()
-   before()
-   css() (same as getStyle() / setStyle())
-   html() (same as getInnerHtml() / setInnerHtml())
-   insertAfter()
-   insertBefore()
-   makeEmpty() (equivalent to emtpy() in jQuery)
-   prepend()
-   prependTo()
-   remove()
-   removeAttribute(), getAttribute(), setAttribute()
-   replaceAll()
-   replaceWith()
-   unwrap()
-   wrap()
-   wrapInner()
-   wrapAll()

To get the modified DOM as HTML code use html() (returns innerHTML of the first node in your crawler object)
or saveHTML() (returns combined "outer" HTML code of all elements in the list).

Example:

```php
use \Wa72\HtmlPageDom\HtmlPageCrawler;

// create an object from a fragment of HTML code as you would do with jQuery's $() function
$c = HtmlPageCrawler::create('<div id="content"><h1>Title</h1></div>');

// the above is the same as calling:
$c = new HtmlPageCrawler('<div id="content"><h1>Title</h1></div>');

// filter for h1 elements and wrap them with an HTML structure
$c->filter('h1')->wrap('<div class="innercontent">');

// return the modified HTML
echo $c->saveHTML();
// or simply:
echo $c; // implicit __toString() calls saveHTML()
// will output: <div id="content"><div class="innercontent"><h1>Title</h1></div></div>

```

Usage examples for HtmlPage:

```php
use \Wa72\HtmlPageDom\HtmlPage;

// create a new HtmlPage object with an empty HTML skeleton
$page = new HtmlPage();

// or create a HtmlPage object from an existing page
$page = new HtmlPage(file_get_contents('http://www.heise.de'));

// get or set page title
echo $page->getTitle();
$page->setTitle('New page title');
echo $page->getTitle();


// add HTML content
{'fraction_non_alphanumeric': 0.12454212454212454, 'fraction_numerical': 0.006380716058135413, 'mean_word_length': 4.2669570628500315, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 45, 'https://': 10, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1808750', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2465, 'n_words': 934}