Masterminds/html5-php

Prevent wrapping in <!DOCTYPE html> <html>

Yahav opened this issue · 3 comments

Yahav commented

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

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

str_replace(
  ['<!DOCTYPE html>', '<html>', '<body>', '</body>', '</html>'],
  '',
  $html->saveHTML($dom)
)

that is not cool

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());