@@ -26,7 +26,13 @@ final class SubsetHtmlParser
2626 public function parse (string $ html ): array
2727 {
2828 $ dom = new DOMDocument ('1.0 ' , 'UTF-8 ' );
29- @$ dom ->loadHTML ('<?xml encoding="utf-8" ?><body> ' . $ html . '</body> ' , LIBXML_NOERROR | LIBXML_NOWARNING );
29+ $ previousUseInternalErrors = libxml_use_internal_errors (true );
30+ $ dom ->loadHTML (
31+ '<?xml encoding="utf-8" ?><body> ' . $ html . '</body> ' ,
32+ LIBXML_NOERROR | LIBXML_NOWARNING ,
33+ );
34+ libxml_clear_errors ();
35+ libxml_use_internal_errors ($ previousUseInternalErrors );
3036
3137 $ body = $ dom ->getElementsByTagName ('body ' )->item (0 );
3238 if (!$ body instanceof DOMElement) {
@@ -49,12 +55,14 @@ private function parseDomNode(DOMNode $node, string $inheritedStyle): ?Node
4955 if ($ node instanceof DOMElement) {
5056 $ tag = strtolower ($ node ->tagName );
5157 if (!isset ($ this ->allowedTags [$ tag ])) {
52- throw new UnsupportedSubsetException (sprintf ('Tag <%s> is not supported in MVP subset . ' , $ tag ));
58+ throw new UnsupportedSubsetException (sprintf ('Tag <%s> is not supported. ' , $ tag ));
5359 }
5460
5561 $ attributes = [];
56- foreach ($ node ->attributes as $ attribute ) {
57- $ attributes [strtolower ($ attribute ->name )] = $ attribute ->value ;
62+ if ($ node ->attributes !== null ) {
63+ foreach ($ node ->attributes as $ attribute ) {
64+ $ attributes [strtolower ($ attribute ->name )] = $ attribute ->value ;
65+ }
5866 }
5967
6068 $ ownStyle = $ attributes ['style ' ] ?? '' ;
@@ -79,7 +87,7 @@ private function parseDomNode(DOMNode $node, string $inheritedStyle): ?Node
7987 );
8088 }
8189
82- $ text = trim ($ node ->textContent ?? '' );
90+ $ text = trim ($ node ->textContent );
8391 if ($ text === '' ) {
8492 return null ;
8593 }
0 commit comments