Masterminds / html5-php

An HTML5 parser and serializer for PHP.
http://masterminds.github.io/html5-php/
Other
1.55k stars 114 forks source link

Prevent wrapping in <!DOCTYPE html> <html> #226

Open Yahav opened 1 year ago

Yahav commented 1 year ago

Any way to prevent the saveHTML method from wrapping the output in <!DOCTYPE html> ? inb4 loadHTMLFragment, can't use it since it won't allow to use the getElementsByTagName method.

thunderdw commented 1 year ago

It's a workaround but here's how I'm achieving this:

str_replace(
  ['<!DOCTYPE html>', '<html>', '<body>', '</body>', '</html>'],
  '',
  $html->saveHTML($dom)
)
jason-engage commented 1 year ago

that is not cool

Boldewyn commented 1 year ago

In my code I can prevent the wrapper markup when I render only the fragment that was parsed:

$events = new DOMTreeBuilder(true, $options);
$scanner = new Scanner('<p>Hello World!</p>');
$parser = new Tokenizer($scanner, $events);

$parser->parse();

$html5 = new HTML5();
echo $html5->saveHTML($events->fragment());