Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getNodes() #20

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Added getNodes() #20

wants to merge 19 commits into from

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.

None yet

2 participants