Could you provide some input HTML that's valid, and explain by what rules the <h3> got wrapped up in a <div> along with the two <p>'s, instead of the <p> preceding the <h3> being wrapped along with the preceding <h2>?
Anyway, I agree that Mojo::DOM is nice. Maybe this is something to get started with:
use warnings; use strict; use Mojo::DOM; my $html = "<h1>a</h1>\n<p>b</p>\n<h2>c</h2>\n<p>d</p>\n" ."<h3>e</h3>\n<p>f</p>\n<h2>g</h2>\n<p>h</p>\n<h2>i</h2>\n" ."<h1>j</h1>\n<h2>k</h2>\n"; my $dom = Mojo::DOM->new($html); $dom->find('h2')->each(sub { my $next = $_->next; my $new = $_->wrap('<div></div>'); if ( $next && !$next->matches('h1') ) { $new->append($next); $next->remove; } }); print "$dom";
In reply to Re: Wrapping HTML "sections" with a div
by haukex
in thread Wrapping HTML "sections" with a div
by nysus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |