Skip to content

Commit 0881681

Browse files
authored
Merge pull request #31 from codex-editor/allow-all-tags
Add 'all' to allowedTags
2 parents 950d08f + 59d8ace commit 0881681

File tree

7 files changed

+167
-5
lines changed

7 files changed

+167
-5
lines changed

EditorJS/BlockHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ private function sanitize($rules, $blockData)
198198
*/
199199
if ($elementType == 'string') {
200200
$allowedTags = isset($rule['allowedTags']) ? $rule['allowedTags'] : '';
201-
$blockData[$key] = $this->getPurifier($allowedTags)->purify($value);
201+
if ($allowedTags !== '*') {
202+
$blockData[$key] = $this->getPurifier($allowedTags)->purify($value);
203+
}
202204
}
203205

204206
/**

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,108 @@ Where:
8383

8484
`allowedTags` param should follow [HTMLPurifier](https://github.com/ezyang/htmlpurifier]) format.
8585

86+
#### There are three common parameters for every block:
87+
88+
1. `type` (**required**) — type of the block
89+
90+
|value|description|
91+
|---|---|
92+
|`string`|field with string value|
93+
|`int`/`integer`|field with integer value|
94+
|`bool`/`boolean`|field with boolean value|
95+
|`array`|field with nested fields|
96+
97+
2. `allowedTags` (optional) — HTML tags in string that won't be removed
98+
99+
|value|default|description|
100+
|---|---|---|
101+
|`empty`|yes|all tags will be removed|
102+
|`*`|no|all tags are allowed|
103+
104+
Other values are allowed according to the [HTMLPurifier](https://github.com/ezyang/htmlpurifier]) format.
105+
106+
Example:
107+
```
108+
"paragraph": {
109+
"text": {
110+
"type": "string",
111+
"allowedTags": "i,b,u,a[href]"
112+
}
113+
}
114+
```
115+
116+
3. `canBeOnly` (optional) — define set of allowed values
117+
118+
Example:
119+
```
120+
"quote": {
121+
"text": {
122+
"type": "string"
123+
},
124+
"caption": {
125+
"type": "string"
126+
},
127+
"alignment": {
128+
"type": "string",
129+
"canBeOnly": ["left", "center"]
130+
}
131+
}
132+
```
133+
134+
### Nested tools
135+
136+
Tools can contain nested values. It is possible with the `array` type.
137+
138+
Let the JSON input be the following:
139+
```
140+
{
141+
"blocks": [
142+
"type": list,
143+
"data": {
144+
"items": [
145+
"first", "second", "third"
146+
],
147+
"style": {
148+
"background-color": "red",
149+
"font-color": "black"
150+
}
151+
}
152+
]
153+
}
154+
```
155+
156+
We can define validation rules for this input in the config:
157+
```
158+
"list": {
159+
"items": {
160+
"type": "array",
161+
"data": {
162+
"-": {
163+
"type": "string",
164+
"allowedTags": "i,b,u"
165+
}
166+
}
167+
},
168+
"style": {
169+
"type": "array",
170+
"data": {
171+
"background-color": {
172+
"type": "string",
173+
"canBeOnly": ["red", "blue", "green"]
174+
},
175+
"font-color": {
176+
"type": "string",
177+
"canBeOnly": ["black", "white"]
178+
}
179+
}
180+
}
181+
}
182+
```
183+
184+
where `data` is the container for values of the array and `-` is the special shortcut for values if the array is sequential.
185+
186+
187+
86188
Another configuration example: [/tests/samples/test-config.json](/tests/samples/test-config.json)
87189

88190
# Exceptions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "library",
44
"description": "PHP backend implementation for the Editor.js",
55
"license": "MIT",
6-
"version": "2.0.0",
6+
"version": "2.0.1",
77
"authors": [
88
{
99
"name": "CodeX Team",

tests/GeneralTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,21 @@ public function testBlocksContent()
9696

9797
$this->assertException($callable, EditorJSException::class, null, 'Block must be an Array');
9898
}
99+
100+
public function testNested()
101+
{
102+
$data = '{"blocks":[{"type":"table","data":{"header": {"description":"a table", "author": "codex"}, "rows": [["name", "age", "sex"],["Paul", "24", "male"],["Ann", "26", "female"]]}}]}';
103+
$editor = new EditorJS($data, $this->config);
104+
$result = $editor->getBlocks();
105+
106+
$valid_rows = [["name", "age", "sex"],["Paul", "24", "male"],["Ann", "26", "female"]];
107+
108+
$this->assertEquals('a table', $result[0]['data']['header']['description']);
109+
$this->assertEquals('codex', $result[0]['data']['header']['author']);
110+
$this->assertEquals(3, count($result[0]['data']['rows']));
111+
112+
$this->assertEquals('name', $result[0]['data']['rows'][0][0]);
113+
$this->assertEquals('24', $result[0]['data']['rows'][1][1]);
114+
$this->assertEquals('female', $result[0]['data']['rows'][2][2]);
115+
}
99116
}

tests/PurifierTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ public function testCustomTagPurifier()
4343

4444
$this->assertEquals('t<mark>e</mark>st', $result[0]['data']['text']);
4545
}
46+
47+
public function testAllTagsPurifier()
48+
{
49+
$data = '{"time":1539180803359,"blocks":[{"type":"raw","data":{"html": "<div style=\"background: #000; color: #fff; font-size: 30px; padding: 50px;\">Any HTML code</div>"}}]}';
50+
$editor = new EditorJS($data, $this->configuration);
51+
$result = $editor->getBlocks();
52+
53+
$this->assertEquals('<div style="background: #000; color: #fff; font-size: 30px; padding: 50px;">Any HTML code</div>', $result[0]['data']['html']);
54+
}
4655
}

tests/samples/purify-test-config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
},
2121
"alignment": {
2222
"type": "string",
23-
"canBeOnly": ["left", "right"]
23+
"canBeOnly": ["left", "center"]
24+
}
25+
},
26+
"raw": {
27+
"html": {
28+
"type": "string",
29+
"allowedTags": "*"
2430
}
2531
}
2632
}

tests/samples/test-config.json

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"list": {
2020
"style": {
2121
"type": "string",
22-
"canBeOnly": ["ordered", "numbered"]
22+
"canBeOnly": ["ordered", "unordered"]
2323
},
2424
"items": {
2525
"type": "array",
@@ -41,7 +41,33 @@
4141
},
4242
"alignment": {
4343
"type": "string",
44-
"canBeOnly": ["left", "right"]
44+
"canBeOnly": ["left", "center"]
45+
}
46+
},
47+
"table": {
48+
"header": {
49+
"type": "array",
50+
"data": {
51+
"description": {
52+
"type": "string"
53+
},
54+
"author": {
55+
"type": "string"
56+
}
57+
}
58+
},
59+
"rows": {
60+
"type": "array",
61+
"data": {
62+
"-": {
63+
"type": "array",
64+
"data": {
65+
"-": {
66+
"type": "string"
67+
}
68+
}
69+
}
70+
}
4571
}
4672
}
4773
}

0 commit comments

Comments
 (0)