|
| 1 | +# DOM Classes |
| 2 | + |
| 3 | +PurePHP provides two DOM representation classes for different use cases. |
| 4 | + |
| 5 | +## PDom Class |
| 6 | + |
| 7 | +`Pure\Core\PDom` is a DOM representation class for string output, extending the Dom abstract class. |
| 8 | + |
| 9 | +### Output Methods |
| 10 | + |
| 11 | +#### `__toString(): string` |
| 12 | + |
| 13 | +Converts the PDom object to HTML/XML string. |
| 14 | + |
| 15 | +```php |
| 16 | +<?php |
| 17 | + |
| 18 | +use function Pure\HTML\div; |
| 19 | + |
| 20 | +$element = div('Content')->class('container'); |
| 21 | +$pdom = $element->toPDom(); |
| 22 | +echo $pdom; // Output: <div class="container">Content</div> |
| 23 | +``` |
| 24 | + |
| 25 | +### Use Cases |
| 26 | + |
| 27 | +PDom is optimized for string output and is the default choice for most scenarios: |
| 28 | + |
| 29 | +```php |
| 30 | +<?php |
| 31 | + |
| 32 | +use function Pure\HTML\{html, head, title, body, div, h1, p}; |
| 33 | + |
| 34 | +$page = html( |
| 35 | + head(title('My Page')), |
| 36 | + body( |
| 37 | + div( |
| 38 | + h1('Welcome'), |
| 39 | + p('This is my website.') |
| 40 | + )->class('container') |
| 41 | + ) |
| 42 | +); |
| 43 | + |
| 44 | +// Convert to PDom for string output |
| 45 | +$pdom = $page->toPDom(); |
| 46 | +echo $pdom; // Outputs complete HTML |
| 47 | +``` |
| 48 | + |
| 49 | +## NDom Class |
| 50 | + |
| 51 | +`Pure\Core\NDom` is a DOM representation class based on DOMDocument, extending the Dom abstract class. |
| 52 | + |
| 53 | +### Output Methods |
| 54 | + |
| 55 | +#### `__toString(): string` |
| 56 | + |
| 57 | +Converts the NDom object to HTML/XML string. |
| 58 | + |
| 59 | +```php |
| 60 | +<?php |
| 61 | + |
| 62 | +use function Pure\HTML\div; |
| 63 | + |
| 64 | +$element = div('Content')->class('container'); |
| 65 | +$ndom = $element->toNDom(); |
| 66 | +echo $ndom; // Output: <div class="container">Content</div> |
| 67 | +``` |
| 68 | + |
| 69 | +#### `toDom(): DOMElement` |
| 70 | + |
| 71 | +Gets the underlying DOMElement object. |
| 72 | + |
| 73 | +```php |
| 74 | +<?php |
| 75 | + |
| 76 | +use function Pure\HTML\div; |
| 77 | + |
| 78 | +$element = div('Content'); |
| 79 | +$ndom = $element->toNDom(); |
| 80 | +$domElement = $ndom->toDom(); // Returns DOMElement object |
| 81 | +``` |
| 82 | + |
| 83 | +### Use Cases |
| 84 | + |
| 85 | +NDom is useful when you need to interact with PHP's DOMDocument: |
| 86 | + |
| 87 | +```php |
| 88 | +<?php |
| 89 | + |
| 90 | +use function Pure\HTML\{div, p}; |
| 91 | + |
| 92 | +$element = div( |
| 93 | + p('First paragraph'), |
| 94 | + p('Second paragraph') |
| 95 | +); |
| 96 | + |
| 97 | +$ndom = $element->toNDom(); |
| 98 | +$domElement = $ndom->toDom(); |
| 99 | + |
| 100 | +// Use DOMDocument methods |
| 101 | +$document = $domElement->ownerDocument; |
| 102 | +$xpath = new DOMXPath($document); |
| 103 | +$paragraphs = $xpath->query('//p'); |
| 104 | + |
| 105 | +foreach ($paragraphs as $p) { |
| 106 | + echo $p->textContent . "\n"; |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +## Choosing Between PDom and NDom |
| 111 | + |
| 112 | +### Use PDom when: |
| 113 | +- You need simple string output |
| 114 | +- Performance is important |
| 115 | +- You're generating HTML/XML for web responses |
| 116 | +- You don't need DOM manipulation |
| 117 | + |
| 118 | +### Use NDom when: |
| 119 | +- You need to manipulate the DOM after creation |
| 120 | +- You want to use XPath queries |
| 121 | +- You need to integrate with existing DOMDocument code |
| 122 | +- You need advanced DOM features |
| 123 | + |
| 124 | +## Examples |
| 125 | + |
| 126 | +### Performance Comparison |
| 127 | + |
| 128 | +```php |
| 129 | +<?php |
| 130 | + |
| 131 | +use function Pure\HTML\div; |
| 132 | + |
| 133 | +$element = div('Content')->class('container'); |
| 134 | + |
| 135 | +// PDom - faster for simple output |
| 136 | +$pdom = $element->toPDom(); |
| 137 | +$html1 = (string)$pdom; |
| 138 | + |
| 139 | +// NDom - more features but slower |
| 140 | +$ndom = $element->toNDom(); |
| 141 | +$html2 = (string)$ndom; |
| 142 | + |
| 143 | +// Both produce the same output |
| 144 | +assert($html1 === $html2); |
| 145 | +``` |
| 146 | + |
| 147 | +### DOM Manipulation with NDom |
| 148 | + |
| 149 | +```php |
| 150 | +<?php |
| 151 | + |
| 152 | +use function Pure\HTML\{div, p}; |
| 153 | + |
| 154 | +$container = div( |
| 155 | + p('Original content') |
| 156 | +)->class('container'); |
| 157 | + |
| 158 | +$ndom = $container->toNDom(); |
| 159 | +$domElement = $ndom->toDom(); |
| 160 | +$document = $domElement->ownerDocument; |
| 161 | + |
| 162 | +// Add a new paragraph using DOMDocument |
| 163 | +$newP = $document->createElement('p', 'Added via DOM'); |
| 164 | +$domElement->appendChild($newP); |
| 165 | + |
| 166 | +echo $ndom; // Outputs container with both paragraphs |
| 167 | +``` |
| 168 | + |
| 169 | +### XPath Queries |
| 170 | + |
| 171 | +```php |
| 172 | +<?php |
| 173 | + |
| 174 | +use function Pure\HTML\{div, p, span}; |
| 175 | + |
| 176 | +$content = div( |
| 177 | + p('First paragraph'), |
| 178 | + p(span('Highlighted text'), ' in second paragraph'), |
| 179 | + p('Third paragraph') |
| 180 | +)->class('content'); |
| 181 | + |
| 182 | +$ndom = $content->toNDom(); |
| 183 | +$domElement = $ndom->toDom(); |
| 184 | +$document = $domElement->ownerDocument; |
| 185 | + |
| 186 | +// Use XPath to find specific elements |
| 187 | +$xpath = new DOMXPath($document); |
| 188 | + |
| 189 | +// Find all paragraphs |
| 190 | +$paragraphs = $xpath->query('//p'); |
| 191 | +echo "Found {$paragraphs->length} paragraphs\n"; |
| 192 | + |
| 193 | +// Find spans inside paragraphs |
| 194 | +$spans = $xpath->query('//p/span'); |
| 195 | +foreach ($spans as $span) { |
| 196 | + echo "Span content: {$span->textContent}\n"; |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +### Integration with Existing DOM Code |
| 201 | + |
| 202 | +```php |
| 203 | +<?php |
| 204 | + |
| 205 | +use function Pure\HTML\{table, tr, td}; |
| 206 | + |
| 207 | +// Create table with PurePHP |
| 208 | +$table = table( |
| 209 | + tr(td('Cell 1'), td('Cell 2')), |
| 210 | + tr(td('Cell 3'), td('Cell 4')) |
| 211 | +)->class('data-table'); |
| 212 | + |
| 213 | +// Convert to NDom for DOM manipulation |
| 214 | +$ndom = $table->toNDom(); |
| 215 | +$domTable = $ndom->toDom(); |
| 216 | +$document = $domTable->ownerDocument; |
| 217 | + |
| 218 | +// Add attributes using DOM methods |
| 219 | +$domTable->setAttribute('border', '1'); |
| 220 | +$domTable->setAttribute('cellpadding', '5'); |
| 221 | + |
| 222 | +// Add a new row |
| 223 | +$newRow = $document->createElement('tr'); |
| 224 | +$cell1 = $document->createElement('td', 'Cell 5'); |
| 225 | +$cell2 = $document->createElement('td', 'Cell 6'); |
| 226 | +$newRow->appendChild($cell1); |
| 227 | +$newRow->appendChild($cell2); |
| 228 | +$domTable->appendChild($newRow); |
| 229 | + |
| 230 | +echo $ndom; // Outputs modified table |
| 231 | +``` |
0 commit comments