Skip to content

Commit

Permalink
Package independent to Nette/Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubboucek committed Feb 28, 2022
1 parent 0b98a8d commit 3d18846
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
],
"require": {
"php": ">= 7.2",
"nette/utils": "^3.2.5"
"ext-json": "*"
},
"require-dev": {
"phpstan/phpstan": "^0.12.98",
"nette/tester": "^2.4"
"nette/tester": "^2.4",
"nette/utils": "^3.2.5",
"phpstan/phpstan": "^0.12.98"
},
"suggest": {
"nette/utils": "Allows to safe escape HTML with markup: https://doc.nette.org/en/utils/html-elements"
},
"autoload": {
"psr-4": {
Expand All @@ -30,5 +34,8 @@
"scripts": {
"phpstan": "phpstan analyze src -c phpstan.neon --level 7",
"tester": "tester tests"
},
"config": {
"sort-packages": true
}
}
8 changes: 6 additions & 2 deletions src/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use Nette\HtmlStringable;
use Nette\Utils\IHtmlString;
use Nette\Utils\Json;
use RuntimeException;

/**
* Escape funxtions. Uses UTF-8 only.
Expand Down Expand Up @@ -99,7 +99,11 @@ public static function js($data): string
$data = (string)$data;
}

$json = Json::encode($data);
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);

if ($json === false) {
throw new RuntimeException("JSON encode failed: " . json_last_error_msg(), json_last_error());
}

return str_replace([']]>', '<!', '</'], [']]\u003E', '\u003C!', '<\/'], $json);
}
Expand Down
10 changes: 4 additions & 6 deletions src/EscapeCss.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace JakubBoucek\Escape;

use Nette\Utils\Strings;

class EscapeCss
{
/**
Expand Down Expand Up @@ -43,12 +41,12 @@ class EscapeCss
*/
public static function color(string $color): string
{
$valid = (bool)Strings::match(
$color,
'/^\s*(?:[-a-zA-Z]+|#[\da-fA-F]{3,8}|(?:rgba?|hsla?|lch|lab)\([\d,.%\\/ ]+\))\s*$/D'
$result = preg_match(
'/^\s*(?:[-a-zA-Z]+|#[\da-fA-F]{3,8}|(?:rgba?|hsla?|lch|lab)\([\d,.%\\/ ]+\))\s*$/D',
$color
);

if ($valid === false) {
if ($result !== 1) {
return '';
}

Expand Down

0 comments on commit 3d18846

Please sign in to comment.