1<?php 2 3namespace ComboStrap; 4 5 6use DOMDocument; 7use Exception; 8use LibXMLError; 9 10require_once(__DIR__ . '/XmlUtility.php'); 11 12/** 13 * Class HtmlUtility 14 * Static HTML utility 15 * 16 * On HTML as string, if you want to work on HTML as XML, see the {@link XmlUtility} class 17 * 18 * @package ComboStrap 19 * 20 * This class is based on {@link XmlDocument} 21 * 22 */ 23class XhtmlUtility 24{ 25 26 27 /** 28 * Return a diff 29 * @param string $left 30 * @param string $right 31 * @return string 32 * DOMDocument supports formatted XML while SimpleXMLElement does not. 33 * @noinspection PhpComposerExtensionStubsInspection 34 * @throws ExceptionCombo 35 */ 36 public static function diffMarkup($left, $right, $xhtml = true, $excludedAttributes = null): string 37 { 38 if (empty($right)) { 39 throw new \RuntimeException("The right text should not be empty"); 40 } 41 if (empty($left)) { 42 throw new \RuntimeException("The left text should not be empty"); 43 } 44 $loading = XmlDocument::XML_TYPE; 45 if (!$xhtml) { 46 $loading = XmlDocument::HTML_TYPE; 47 } 48 49 $leftDocument = (new XmlDocument($left, $loading))->getXmlDom(); 50 $rightDocument = (new XmlDocument($right, $loading))->getXmlDom(); 51 52 $error = ""; 53 XmlUtility::diffNode( 54 $leftDocument, 55 $rightDocument, 56 $error, 57 $excludedAttributes 58 ); 59 60 return $error; 61 62 } 63 64 /** 65 * @param $text 66 * @return int the number of lines estimated 67 */ 68 public static function countLines($text) 69 { 70 return count(preg_split("/<\/p>|<\/h[1-9]{1}>|<br|<\/tr>|<\/li>|<hr>|<\/pre>/", $text)) - 1; 71 } 72 73 74 public static function normalize($htmlText) 75 { 76 if (empty($htmlText)) { 77 throw new \RuntimeException("The text should not be empty"); 78 } 79 $xmlDoc = new XmlDocument($htmlText, XmlDocument::HTML_TYPE); 80 return $xmlDoc->getXmlTextNormalized(); 81 } 82 83 84} 85