'af-fieldset' => [
'model' => 'text',
],
+ 'area' => ['#selfClose' => TRUE],
+ 'base' => ['#selfClose' => TRUE],
+ 'br' => ['#selfClose' => TRUE],
+ 'col' => ['#selfClose' => TRUE],
+ 'command' => ['#selfClose' => TRUE],
+ 'embed' => ['#selfClose' => TRUE],
+ 'hr' => ['#selfClose' => TRUE],
+ 'iframe' => ['#selfClose' => TRUE],
+ 'img' => ['#selfClose' => TRUE],
+ 'input' => ['#selfClose' => TRUE],
+ 'keygen' => ['#selfClose' => TRUE],
+ 'link' => ['#selfClose' => TRUE],
+ 'meta' => ['#selfClose' => TRUE],
+ 'param' => ['#selfClose' => TRUE],
+ 'source' => ['#selfClose' => TRUE],
+ 'track' => ['#selfClose' => TRUE],
+ 'wbr' => ['#selfClose' => TRUE],
];
/**
]);
}
}
- $buf .= '>';
- $buf .= $this->convertArraysToHtml($children);
- $buf .= '</' . $tag . '>';
+
+ if (empty($children) && $this->isSelfClosing($tag)) {
+ $buf .= ' />';
+ }
+ else {
+ $buf .= '>';
+ $buf .= $this->convertArraysToHtml($children);
+ $buf .= '</' . $tag . '>';
+ }
return $buf;
}
return $children;
}
+ /**
+ * @param string $tag
+ * Ex: 'img', 'div'
+ * @return bool
+ * TRUE if the tag should look like '<img/>'.
+ * FALSE if the tag should look like '<div></div>'.
+ */
+ protected function isSelfClosing($tag) {
+ return $this->protoSchema[$tag]['#selfClose'] ?? FALSE;
+ }
+
/**
* Determine the type of data that is stored in an attribute.
*
public function getFormatExamples() {
$es = [];
- foreach (['empty', 'string', 'comments', 'apple', 'banana', 'cherry'] as $exampleName) {
+ foreach (['empty', 'string', 'comments', 'self-closing', 'apple', 'banana', 'cherry'] as $exampleName) {
$exampleFile = '/formatExamples/' . $exampleName . '.php';
$example = require __DIR__ . $exampleFile;
$formats = ['html', 'shallow', 'deep'];
--- /dev/null
+<?php
+
+return [
+ 'html' => '<span class="one"></span><img class="two" /><div><br class="three" /><br /></div>',
+ 'shallow' => [
+ ['#tag' => 'span', 'class' => 'one'],
+ ['#tag' => 'img', 'class' => 'two'],
+ [
+ '#tag' => 'div',
+ '#children' => [
+ ['#tag' => 'br', 'class' => 'three'],
+ ['#tag' => 'br'],
+ ],
+ ],
+ ],
+ 'deep' => [
+ ['#tag' => 'span', 'class' => 'one'],
+ ['#tag' => 'img', 'class' => 'two'],
+ [
+ '#tag' => 'div',
+ '#children' => [
+ ['#tag' => 'br', 'class' => 'three'],
+ ['#tag' => 'br'],
+ ],
+ ],
+ ],
+];