-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved html check in serializiers.
- Loading branch information
mustapayev
committed
Dec 24, 2024
1 parent
1f460d3
commit d04cd35
Showing
5 changed files
with
50 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* @license MIT | ||
*/ | ||
|
||
namespace Mews\Pos\Serializer; | ||
|
||
/** | ||
* Serializer utility methods. | ||
*/ | ||
trait SerializerUtilTrait | ||
{ | ||
/** | ||
* @param string $string | ||
* | ||
* @return bool | ||
*/ | ||
private function isHTML(string $string): bool | ||
{ | ||
if ('' === \trim($string)) { | ||
return false; | ||
} | ||
|
||
// Suppress errors for invalid HTML | ||
$previousLibxmlState = \libxml_use_internal_errors(true); | ||
|
||
// Create a new DOMDocument | ||
$dom = new \DOMDocument(); | ||
|
||
// Attempt to load the string as HTML | ||
$isValidHTML = $dom->loadHTML($string, LIBXML_NOERROR | LIBXML_NOWARNING); | ||
|
||
// Clear any libxml errors | ||
\libxml_clear_errors(); | ||
|
||
// Restore the previous libxml error handling state | ||
\libxml_use_internal_errors($previousLibxmlState); | ||
|
||
// Check if the string has recognizable HTML elements | ||
return $isValidHTML && $dom->getElementsByTagName('html')->length > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters