Skip to content

Commit e73e420

Browse files
committed
Move purifier init to the blockHandler. Provide purifier test. Fix bug #27
1 parent 6d0d6f1 commit e73e420

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

EditorJS/BlockHandler.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,16 @@ class BlockHandler
1919
*/
2020
private $rules = null;
2121

22-
/**
23-
* @var \HTMLPurifier_Config
24-
*/
25-
private $sanitizer;
26-
2722
/**
2823
* BlockHandler constructor
2924
*
30-
* @param \HTMLPurifier_Config $sanitizer
31-
* @param string $configuration
25+
* @param string $configuration
3226
*
3327
* @throws EditorJSException
3428
*/
35-
public function __construct($configuration, $sanitizer)
29+
public function __construct($configuration)
3630
{
3731
$this->rules = new ConfigLoader($configuration);
38-
$this->sanitizer = $sanitizer;
3932
}
4033

4134
/**
@@ -228,11 +221,32 @@ private function sanitize($rules, $blockData)
228221
*/
229222
private function getPurifier($allowedTags)
230223
{
231-
$sanitizer = clone $this->sanitizer;
224+
$sanitizer = $this->getDefaultPurifier();
225+
232226
$sanitizer->set('HTML.Allowed', $allowedTags);
233227

234228
$purifier = new \HTMLPurifier($sanitizer);
235229

236230
return $purifier;
237231
}
232+
233+
/**
234+
* Initialize HTML Purifier with default settings
235+
*/
236+
private function getDefaultPurifier()
237+
{
238+
$sanitizer = \HTMLPurifier_Config::createDefault();
239+
240+
$sanitizer->set('HTML.TargetBlank', true);
241+
$sanitizer->set('URI.AllowedSchemes', ['http' => true, 'https' => true]);
242+
$sanitizer->set('AutoFormat.RemoveEmpty', true);
243+
244+
if (!is_dir('/tmp/purifier')) {
245+
mkdir('/tmp/purifier', 0777, true);
246+
}
247+
248+
$sanitizer->set('Cache.SerializerPath', '/tmp/purifier');
249+
250+
return $sanitizer;
251+
}
238252
}

EditorJS/EditorJS.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class EditorJS
2424
*/
2525
public $handler;
2626

27-
/**
28-
* @var \HTMLPurifier_Config
29-
*/
30-
public $sanitizer;
31-
3227
/**
3328
* EditorJS constructor.
3429
* Splits JSON string to separate blocks
@@ -40,8 +35,7 @@ class EditorJS
4035
*/
4136
public function __construct($json, $configuration)
4237
{
43-
$this->initPurifier();
44-
$this->handler = new BlockHandler($configuration, $this->sanitizer);
38+
$this->handler = new BlockHandler($configuration);
4539

4640
/**
4741
* Check if json string is empty
@@ -102,24 +96,6 @@ public function __construct($json, $configuration)
10296
$this->validateBlocks();
10397
}
10498

105-
/**
106-
* Initialize HTML Purifier with default settings
107-
*/
108-
private function initPurifier()
109-
{
110-
$this->sanitizer = \HTMLPurifier_Config::createDefault();
111-
112-
$this->sanitizer->set('HTML.TargetBlank', true);
113-
$this->sanitizer->set('URI.AllowedSchemes', ['http' => true, 'https' => true]);
114-
$this->sanitizer->set('AutoFormat.RemoveEmpty', true);
115-
116-
if (!is_dir('/tmp/purifier')) {
117-
mkdir('/tmp/purifier', 0777, true);
118-
}
119-
120-
$this->sanitizer->set('Cache.SerializerPath', '/tmp/purifier');
121-
}
122-
12399
/**
124100
* Sanitize and return array of blocks according to the Handler's rules.
125101
*

tests/BlockHandlerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,15 @@ public function testListTool()
7171
$this->assertEquals("second", $result[0]['data']['items'][1]);
7272
$this->assertEquals("third", $result[0]['data']['items'][2]);
7373
}
74+
75+
public function testHtmlPurifier()
76+
{
77+
$data = '{"time":1539180803359,"blocks":[{"type":"header","data":{"text":"<b>t</b><i>e</i><u>st</u>","level":2}}, {"type":"quote","data":{"text":"<b>t</b><i>e</i><u>st</u>","caption":"", "alignment":"left"}}]}';
78+
$editor = new EditorJS($data, $this->configuration);
79+
$result = $editor->getBlocks();
80+
81+
$this->assertEquals(2, count($result));
82+
$this->assertEquals('test', $result[0]['data']['text']);
83+
$this->assertEquals('<b>t</b><i>e</i><u>st</u>', $result[1]['data']['text']);
84+
}
7485
}

tests/samples/test-config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
},
3434
"quote": {
3535
"text": {
36-
"type": "string"
36+
"type": "string",
37+
"allowedTags": "i,b,u"
3738
},
3839
"caption": {
3940
"type": "string"
4041
},
41-
"size": {
42+
"alignment": {
4243
"type": "string",
4344
"canBeOnly": ["left", "right"]
4445
}

0 commit comments

Comments
 (0)