From 1ff6fde1b149ba54954e2b18cee2e5d132c3dd48 Mon Sep 17 00:00:00 2001 From: Jennifer Mourek Date: Mon, 29 Oct 2018 15:35:09 +0100 Subject: [PATCH] Add distinction whether an element is phrasing content --- src/BaseHtmlElement.php | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/BaseHtmlElement.php b/src/BaseHtmlElement.php index 1bcfe981..354a65e7 100644 --- a/src/BaseHtmlElement.php +++ b/src/BaseHtmlElement.php @@ -42,6 +42,64 @@ abstract class BaseHtmlElement extends HtmlDocument 'wbr' => 1 ]; + /** + * List of elements that fall under 'phrasing content' and should not have a content separator. + * + * If {@link $contentSeparator} is null, this property should be used to decide whether there should be a newline or + * no content separator rendered. + * + * @var array + * + * @see https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content + */ + protected static $phrasingContent = [ + 'abbr' => 1, + 'audio' => 1, + 'b' => 1, + 'bdo' => 1, + 'br' => 1, + 'button' => 1, + 'canvas' => 1, + 'cite' => 1, + 'code' => 1, + 'command' => 1, + 'data' => 1, + 'datalist' => 1, + 'dfn' => 1, + 'em' => 1, + 'embed' => 1, + 'i' => 1, + 'iframe' => 1, + 'img' => 1, + 'input' => 1, + 'kbd' => 1, + 'keygen' => 1, + 'label' => 1, + 'mark' => 1, + 'math' => 1, + 'meter' => 1, + 'noscript' => 1, + 'object' => 1, + 'output' => 1, + 'progress' => 1, + 'q' => 1, + 'ruby' => 1, + 'samp' => 1, + 'script' => 1, + 'select' => 1, + 'small' => 1, + 'span' => 1, + 'strong' => 1, + 'sub' => 1, + 'sup' => 1, + 'svg' => 1, + 'textarea' => 1, + 'time' => 1, + 'var' => 1, + 'video' => 1, + 'wbr' => 1 + ]; + /** @var bool|null Whether the element is void. If null, void check should use {@link $voidElements} */ protected $isVoid; @@ -197,6 +255,14 @@ public function renderUnwrapped() $attributes = $this->getAttributes()->render(); $content = $this->renderContent(); + if ($this->contentSeparator == null) { + if ($this->isPhrasingContent()) { + $this->setSeparator(''); + } else { + $this->setSeparator("\n"); + } + } + if (strlen($this->contentSeparator)) { $length = strlen($content); if ($length > 0) { @@ -227,6 +293,19 @@ public function renderUnwrapped() ); } + /** + * Determines whether this element falls under 'phrasing content' + * and should therefore not be rendered with a content separator + * + * @see https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content + * + * @return bool + */ + public function isPhrasingContent() + { + return isset(self::$phrasingContent[$this->getTag()]); + } + /** * Use this element to wrap the given document *