add html to custom elements formatter
3 unresolved threads
3 unresolved threads
Closes #3494426
Merge request reports
Activity
116 * Converts a DOM node into a custom element with all attributes. 117 * 118 * @param \DOMNode $node 119 * The DOM node to convert. 120 * 121 * @return \Drupal\custom_elements\CustomElement|null 122 * Returns the converted CustomElement or NULL. 123 */ 124 protected function convertNodeToCustomElement(\DOMNode $node): ?CustomElement { 125 if ($node->nodeType === XML_TEXT_NODE) { 126 $trimmedText = trim(preg_replace('/\s+/', ' ', $node->nodeValue)); 127 if (!empty($trimmedText)) { 128 $customElement = CustomElement::create('text'); 129 $customElement->addSlot('text', $trimmedText); 130 return $customElement; 131 } 142 } 143 144 $hasContent = FALSE; 145 $content = []; 146 foreach ($node->childNodes as $childNode) { 147 $childElement = $this->convertNodeToCustomElement($childNode); 148 if (!empty($childElement)) { 149 $slot = $childNode->nodeName === 'img' ? 'image' : 'content'; 150 $content[] = ['slot' => $slot, 'element' => $childElement]; 151 $hasContent = TRUE; 152 } 153 } 154 155 // Skip if paragraphs and divs have no content and no attributes. 156 if (($tagName === 'p' || $tagName === 'div') && !$hasContent && empty($customElement->getAttributes())) { 157 return NULL; 147 $childElement = $this->convertNodeToCustomElement($childNode); 148 if (!empty($childElement)) { 149 $slot = $childNode->nodeName === 'img' ? 'image' : 'content'; 150 $content[] = ['slot' => $slot, 'element' => $childElement]; 151 $hasContent = TRUE; 152 } 153 } 154 155 // Skip if paragraphs and divs have no content and no attributes. 156 if (($tagName === 'p' || $tagName === 'div') && !$hasContent && empty($customElement->getAttributes())) { 157 return NULL; 158 } 159 160 // Skip if element has no content, no attributes, 161 // and is not self-closing (like <br> or <hr>). 162 if (!$hasContent && empty($customElement->getAttributes()) && !in_array($tagName, ['br', 'hr', 'img', 'input'])) {
Please register or sign in to reply