Skip to content

Commit ff4869b

Browse files
authored
Merge pull request #14 from nswdpc/feat-ci-files
Automated updates
2 parents 33953af + b61de5f commit ff4869b

File tree

8 files changed

+101
-82
lines changed

8 files changed

+101
-82
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/.editorconfig export-ignore
55
/.gitattributes export-ignore
66
/.gitignore export-ignore
7-
/.php-cs-fixer.dist.php export-ignore
7+
/.github export-ignore
88
/phpunit.xml.dist export-ignore
99
/.waratah export-ignore
1010
/code-of-conduct.md export-ignore

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: CI
2+
3+
on:
4+
pull_request: null
5+
6+
jobs:
7+
Silverstripe:
8+
name: 'Silverstripe (bundle)'
9+
uses: nswdpc/ci-files/.github/workflows/silverstripe_53_83.yml@v-4
10+
PHPStan:
11+
name: 'PHPStan (analyse)'
12+
uses: nswdpc/ci-files/.github/workflows/phpstan.silverstripe_83.yml@v-4
13+
needs: Silverstripe

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/client/node_modules
22
/vendor/
33
.DS_Store
4-
/.php_cs.fixer.cache
4+
/.php-cs-fixer.cache
5+
/composer.lock
6+
/public/

.php-cs-fixer.dist.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

composer.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,21 @@
3636
"ezyang/htmlpurifier" : "^4.17"
3737
},
3838
"require-dev": {
39+
"cambis/silverstripe-rector": "^2",
3940
"phpunit/phpunit": "^9.5",
40-
"friendsofphp/php-cs-fixer": "^3"
41+
"cambis/silverstan": "^2",
42+
"nswdpc/ci-files": "dev-v-4",
43+
"friendsofphp/php-cs-fixer": "^3",
44+
"phpstan/phpstan": "^2",
45+
"phpstan/phpstan-phpunit": "^2",
46+
"rector/rector": "^2"
47+
},
48+
"config": {
49+
"allow-plugins": {
50+
"composer/installers": true,
51+
"silverstripe/vendor-plugin": true,
52+
"silverstripe/recipe-plugin": true,
53+
"phpstan/extension-installer": true
54+
}
4155
}
4256
}

src/Fields/TrumbowygEditorField.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@
22

33
namespace NSWDPC\Utilities\Trumbowyg;
44

5-
use SilverStripe\Core\Convert;
6-
use SilverStripe\Forms\FormField;
75
use SilverStripe\Forms\TextareaField;
86
use SilverStripe\View\ArrayData;
97
use SilverStripe\View\Requirements;
10-
use Exception;
11-
use DOMDocument;
128

13-
14-
class TrumbowygEditorField extends TextareaField {
15-
16-
private static $casting = [
9+
class TrumbowygEditorField extends TextareaField
10+
{
11+
private static array $casting = [
1712
'Value' => 'HTMLText',
1813
];
1914

20-
private static $include_own_jquery = true;
15+
private static bool $include_own_jquery = true;
2116

2217
/**
2318
* Get field options
2419
* @return array
2520
*/
26-
protected function getFieldOptions() {
21+
protected function getFieldOptions()
22+
{
2723
$options = $this->config()->get('editor_options');
28-
if( empty($options) || !is_array($options) ) {
24+
if (empty($options) || !is_array($options)) {
2925
// Fallback options in case of none configured
3026
$options = [
3127
"fixedBtnPane" => true,
@@ -49,15 +45,16 @@ protected function getFieldOptions() {
4945
]
5046
];
5147
}
48+
5249
$options['tagsToRemove'] = self::getDeniedTags();
5350
return $options;
5451
}
5552

5653
/**
5754
* These tags are denied by default
58-
* @return array
5955
*/
60-
public static function getDeniedTags() {
56+
public static function getDeniedTags(): array
57+
{
6158
return [
6259
'form',
6360
'script',
@@ -79,10 +76,12 @@ public static function getDeniedTags() {
7976
/**
8077
* Returns the field
8178
*/
82-
public function Field($properties = []) {
83-
$this->setAttribute('data-tw','1');
79+
#[\Override]
80+
public function Field($properties = [])
81+
{
82+
$this->setAttribute('data-tw', '1');
8483

85-
if($this->config()->get('include_own_jquery')) {
84+
if ($this->config()->get('include_own_jquery')) {
8685
Requirements::javascript(
8786
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js",
8887
[
@@ -91,6 +90,7 @@ public function Field($properties = []) {
9190
]
9291
);
9392
}
93+
9494
Requirements::javascript(
9595
"https://cdn.jsdelivr.net/npm/trumbowyg@2.31.0/dist/trumbowyg.min.js",
9696
[
@@ -101,7 +101,7 @@ public function Field($properties = []) {
101101
// import template with options
102102
$custom_script = ArrayData::create([
103103
'ID' => $this->ID(),
104-
'Options' => json_encode( $this->getFieldOptions() )
104+
'Options' => json_encode($this->getFieldOptions())
105105
])->renderWith('NSWDPC/Utilities/Trumbowyg/Script');
106106
Requirements::customScript(
107107
$custom_script,
@@ -121,16 +121,24 @@ public function Field($properties = []) {
121121
/**
122122
* Return the value, sanitised
123123
*/
124-
public function Value() {
124+
#[\Override]
125+
public function Value()
126+
{
125127
return $this->dataValue();
126128
}
127129

128130
/**
129131
* Return cleaned data value
130132
*/
131-
public function dataValue() {
132-
$sanitiser = new ContentSanitiser();
133-
$this->value = $sanitiser->clean($this->value);
133+
#[\Override]
134+
public function dataValue()
135+
{
136+
$value = $this->value;
137+
if (!is_string($value)) {
138+
$value = "";
139+
}
140+
141+
$this->value = ContentSanitiser::clean($value);
134142
return $this->value;
135143
}
136144

src/Models/ContentSanitiser.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,58 @@
33
namespace NSWDPC\Utilities\Trumbowyg;
44

55
use SilverStripe\Assets\Filesystem;
6-
use Silverstripe\Core\Config\Configurable;
7-
use Silverstripe\Core\Config\Config;
8-
use Silverstripe\ORM\ValidationException;
6+
use SilverStripe\Core\Config\Configurable;
7+
use SilverStripe\Core\Config\Config;
98

109
/**
1110
* Sanitise content provided by a trumbowyg field
12-
* @author James <james@dcs>
11+
* @author James
1312
*/
14-
class ContentSanitiser {
15-
13+
class ContentSanitiser
14+
{
1615
use Configurable;
1716

1817
/**
1918
* @var string
2019
* default allowed tags, if none are specified in configuration
2120
*/
22-
private static $default_allowed_html_tags = "<p><i><blockquote>"
21+
private static string $default_allowed_html_tags = "<p><i><blockquote>"
2322
. "<b><strong><em><br>"
2423
. "<h2><h3><h4><h5><h6>"
2524
. "<ol><ul><li><a><strike>";
2625

2726
/**
2827
* Return tags suitable for strip_tags
29-
* @return string
3028
*/
31-
public static function getAllowedHTMLTags() : string {
29+
public static function getAllowedHTMLTags(): string
30+
{
3231
$allowedHTMLTags = Config::inst()->get(self::class, 'default_allowed_html_tags');
33-
if($allowedHTMLTags == "") {
32+
if ($allowedHTMLTags == "") {
3433
$allowedHTMLTags = "<p>";// disallow all
3534
}
35+
3636
return $allowedHTMLTags;
3737
}
3838

3939
/**
4040
* Return tags suitable for strip_tags
41-
* @return array
4241
*/
43-
public static function getAllowedHTMLTagsAsArray() : array {
42+
public static function getAllowedHTMLTagsAsArray(): array
43+
{
4444
$allowedHTMLTags = trim(self::getAllowedHTMLTags(), "<>");
4545
return explode("><", $allowedHTMLTags);
4646
}
4747

4848
/**
4949
* Generate a strict configuration for handling incoming user content
50-
* @return array
5150
*/
52-
public static function generateConfig() : array {
51+
public static function generateConfig(): array
52+
{
5353
$serializerPath = TEMP_PATH . "/HtmlPurifier/Serializer";
54-
if(!is_dir($serializerPath)) {
54+
if (!is_dir($serializerPath)) {
5555
Filesystem::makeFolder($serializerPath);
5656
}
57+
5758
return [
5859
'Core.Encoding' => 'UTF-8',
5960
'HTML.AllowedElements' => self::getAllowedHTMLTagsAsArray(),
@@ -70,26 +71,25 @@ public static function generateConfig() : array {
7071
/**
7172
* Clean dirty HTML using HTML purifier
7273
* If the purification fails in any way, an entitised version of the HTML is returned
73-
* @param string $html
74-
* @return string
7574
*/
76-
public static function clean($dirtyHtml) : string {
75+
public static function clean(string $dirtyHtml): string
76+
{
7777
try {
7878
$htmlPurifierConfig = \HTMLPurifier_Config::createDefault();
7979
$configuration = self::generateConfig();
8080
foreach ($configuration as $key => $value) {
8181
$htmlPurifierConfig->set($key, $value);
8282
}
83+
8384
$purifier = new \HTMLPurifier($htmlPurifierConfig);
8485
$cleaned = $purifier->purify($dirtyHtml);
85-
if(trim(strip_tags($cleaned ?? '')) === '') {
86+
if (trim(strip_tags($cleaned ?? '')) === '') {
8687
return '';
8788
} else {
88-
return $cleaned;
89+
return trim($cleaned);
8990
}
90-
return $cleaned;
91-
} catch (\Exception $e) {
92-
return htmlentities($dirtyHtml, ENT_QUOTES|ENT_HTML5, "UTF-8");
91+
} catch (\Exception) {
92+
return htmlentities($dirtyHtml, ENT_QUOTES | ENT_HTML5, "UTF-8");
9393
}
9494
}
9595
}

0 commit comments

Comments
 (0)