return $buf . $end;
}
+ /**
+ * Converts a subset of items into html markup
+ *
+ * @param array $children
+ * @return string html
+ */
public function convertArraysToHtml($children) {
$buf = '';
$this->indent++;
return $buf;
}
+ /**
+ * Converts a full array of items into html markup
+ *
+ * @param array $tree
+ * @return string html
+ */
+ public function convertTreeToHtml($tree) {
+ $this->indent = -1;
+ return $this->replaceUnicodeChars($this->convertArraysToHtml($tree));
+ }
+
/**
* @param string $html
* Ex: '<div class="greeting">Hello world</div>'
$doc = new DOMDocument();
$doc->preserveWhiteSpace = !$this->formatWhitespace;
- @$doc->loadHTML("<html><body>$html</body></html>");
+ @$doc->loadHTML("<?xml encoding=\"utf-8\" ?><html><body>$html</body></html>");
// FIXME: Validate expected number of child nodes
if (!$this->deepCoding && !$this->isNodeEditable($arr)) {
$arr['#markup'] = '';
foreach ($node->childNodes as $child) {
- $arr['#markup'] .= $child->ownerDocument->saveXML($child);
+ $arr['#markup'] .= $this->replaceUnicodeChars($child->ownerDocument->saveXML($child));
}
}
else {
return $arr;
}
elseif ($node instanceof DOMText) {
- return ['#text' => $node->textContent];
+ return ['#text' => $this->replaceUnicodeChars($node->textContent)];
}
elseif ($node instanceof DOMComment) {
return ['#comment' => $node->nodeValue];
}
}
+ /**
+ * Convert non-breaking space character to html notation.
+ *
+ * Makes html files easier to read.
+ *
+ * Note: This function does NOT convert all html entities (< to <, etc.)
+ * as the input string is assumed to already be valid markup.
+ *
+ * @param string $markup - some html
+ * @return string
+ */
+ public function replaceUnicodeChars($markup) {
+ // TODO: Potentially replace other unicode characters that can be represented as html entities
+ $replace = [
+ ["\xc2\xa0", ' '],
+ ];
+ return str_replace(array_column($replace, 0), array_column($replace, 1), $markup);
+ }
+
/**
* Determine if a node is recognized by the gui editor.
*
return $mixed;
}
$converter = new \CRM_Afform_ArrayHtml($this->layoutFormat !== 'shallow', $this->formatWhitespace);
- return $converter->convertArraysToHtml($mixed);
+ return $converter->convertTreeToHtml($mixed);
}
}
<?php
return [
- 'html' => '<strong>New text!</strong>',
- 'pretty' => "<strong>New text!</strong>\n",
+ 'html' => '<strong>New text!</strong>',
+ 'pretty' => "<strong>New text!</strong>\n",
'shallow' => [
- ['#tag' => 'strong', '#markup' => 'New text!'],
+ ['#tag' => 'strong', '#markup' => 'New text!'],
],
'deep' => [
- ['#tag' => 'strong', '#children' => [['#text' => 'New text!']]],
+ ['#tag' => 'strong', '#children' => [['#text' => 'New text!']]],
],
];
<?php
return [
- 'html' => '<div class="af-block"><strong> New text!</strong><strong class="af-text"> No whitespace! </strong><af-field name="do_not_sms" defn="{label: \'Do not do any of the emailing\'}" /></div>',
+ 'html' => '<div class="af-block"><strong> New text!</strong><strong class="af-text"> Get a trim! </strong><af-field name="do_not_sms" defn="{label: \'Do not do any of the emailing\'}" /></div>',
'pretty' => '<div class="af-block">
<strong> New text!</strong>
- <strong class="af-text">No whitespace!</strong>
+ <strong class="af-text"> Get a trim!</strong>
<af-field name="do_not_sms" defn="{label: \'Do not do any of the emailing\'}" />
</div>
',
'class' => 'af-block',
'#children' => [
['#tag' => 'strong', '#markup' => ' New text!'],
- ['#tag' => 'strong', 'class' => 'af-text', '#children' => [['#text' => 'No whitespace!']]],
+ ['#tag' => 'strong', 'class' => 'af-text', '#children' => [['#text' => " Get a trim!"]]],
['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => "{label: 'Do not do any of the emailing'}"],
],
],
'class' => 'af-block',
'#children' => [
['#tag' => 'strong', '#markup' => ' New text!'],
- ['#tag' => 'strong', 'class' => 'af-text', '#children' => [['#text' => ' No whitespace! ']]],
+ ['#tag' => 'strong', 'class' => 'af-text', '#children' => [['#text' => " Get a trim! "]]],
['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => "{label: 'Do not do any of the emailing'}"],
],
],
'class' => 'af-block',
'#children' => [
['#tag' => 'strong', '#children' => [['#text' => ' New text!']]],
- ['#tag' => 'strong', 'class' => 'af-text', '#children' => [['#text' => ' No whitespace! ']]],
+ ['#tag' => 'strong', 'class' => 'af-text', '#children' => [['#text' => " Get a trim! "]]],
['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => ['label' => 'Do not do any of the emailing']],
],
],