Skip to content

Commit b9f57ba

Browse files
committed
Add custom tag support and mark inline tag
1 parent e73e420 commit b9f57ba

File tree

4 files changed

+81
-11
lines changed

4 files changed

+81
-11
lines changed

EditorJS/BlockHandler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ private function getPurifier($allowedTags)
225225

226226
$sanitizer->set('HTML.Allowed', $allowedTags);
227227

228+
/**
229+
* Define custom HTML Definition for mark tool
230+
*/
231+
if ($def = $sanitizer->maybeGetRawHTMLDefinition()) {
232+
$def->addElement('mark', 'Inline', 'Inline', 'Common');
233+
}
234+
228235
$purifier = new \HTMLPurifier($sanitizer);
229236

230237
return $purifier;
@@ -240,6 +247,7 @@ private function getDefaultPurifier()
240247
$sanitizer->set('HTML.TargetBlank', true);
241248
$sanitizer->set('URI.AllowedSchemes', ['http' => true, 'https' => true]);
242249
$sanitizer->set('AutoFormat.RemoveEmpty', true);
250+
$sanitizer->set('HTML.DefinitionID', 'html5-definitions');
243251

244252
if (!is_dir('/tmp/purifier')) {
245253
mkdir('/tmp/purifier', 0777, true);

tests/BlockHandlerTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,4 @@ 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-
}
8574
}

tests/PurifierTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use EditorJS\EditorJS;
4+
5+
/**
6+
* Class PurifierTest
7+
*
8+
* Check HTML Purification cases
9+
*/
10+
class PurifierTest extends TestCase
11+
{
12+
const CONFIGURATION_FILE = TESTS_DIR . "/samples/purify-test-config.json";
13+
14+
/**
15+
* Sample configuration
16+
*/
17+
public $configuration;
18+
19+
/**
20+
* Setup configuration
21+
*/
22+
public function setUp()
23+
{
24+
$this->configuration = file_get_contents(PurifierTest::CONFIGURATION_FILE);
25+
}
26+
27+
public function testHtmlPurifier()
28+
{
29+
$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"}}]}';
30+
$editor = new EditorJS($data, $this->configuration);
31+
$result = $editor->getBlocks();
32+
33+
$this->assertEquals(2, count($result));
34+
$this->assertEquals('test', $result[0]['data']['text']);
35+
$this->assertEquals('<b>t</b><i>e</i><u>st</u>', $result[1]['data']['text']);
36+
}
37+
38+
public function testCustomTagPurifier()
39+
{
40+
$data = '{"time":1539180803359,"blocks":[{"type":"header","data":{"text":"<b>t</b><mark>e</mark><u>st</u>","level":2}}]}';
41+
$editor = new EditorJS($data, $this->configuration);
42+
$result = $editor->getBlocks();
43+
44+
$this->assertEquals('t<mark>e</mark>st', $result[0]['data']['text']);
45+
}
46+
}

tests/samples/purify-test-config.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"tools": {
3+
"header": {
4+
"text": {
5+
"type": "string",
6+
"allowedTags": "mark"
7+
},
8+
"level": {
9+
"type": "int",
10+
"canBeOnly": [2, 3, 4]
11+
}
12+
},
13+
"quote": {
14+
"text": {
15+
"type": "string",
16+
"allowedTags": "i,b,u"
17+
},
18+
"caption": {
19+
"type": "string"
20+
},
21+
"alignment": {
22+
"type": "string",
23+
"canBeOnly": ["left", "right"]
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)