Skip to content

Conversation

@shtse8
Copy link

@shtse8 shtse8 commented Feb 28, 2017

In original version, if we want to loop all the elements, we have code like this:

use \Wa72\HtmlPageDom\HtmlPageCrawler;

$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
$c->filter('p')->each(function($element) {
	$element->html('hi');
});
echo $c->saveHTML();

Yes, it is very convenient. But if we got lots of global variables, we have to pass it one by one using use.

use \Wa72\HtmlPageDom\HtmlPageCrawler;

$var = 'hi';
$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
$c->filter('p')->each(function($element) use ($var) {
	$element->html($var);
});
echo $c->saveHTML();

or, somethings like:

use \Wa72\HtmlPageDom\HtmlPageCrawler;

$var = 'hi';
$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
foreach($c->filter('p') as $element) {
	(new HtmlPageCrawler($element))->html($var);
};
echo $c->saveHTML();

That's why I created this method for another option.
Now, we can code like this:

use \Wa72\HtmlPageDom\HtmlPageCrawler;

$var = 'hi';
$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
foreach($c->filter('p')->getNodes() as $element) {
	$element->html($var);
}
echo $c->saveHTML();

Replace `_root` by constant `self::FRAGMENT_ROOT_TAGNAME`
Try to fix utf-8 encoding problem.
Don't output DocType if only `saveHTML` of elements.
I think we should keep the newline and whitespace.
Syntax:
```
use \Wa72\HtmlPageDom\HtmlPageCrawler;

$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
foreach($c->filter('p')->getNodes() as $element) {
	$element->html('hi');
}
echo $c->saveHTML();
```
@glensc
Copy link
Contributor

glensc commented Jun 11, 2018

the PRs seems broken because created from @shtse8 master branch, thus changes from #19 and #20 mixed in both pull requests. and perhaps even changes not related to neither of the PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants