Skip to content

Commit f877807

Browse files
committed
Updated usage and README
1 parent 5ab5c6c commit f877807

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,52 @@ composer require mirazmac/html-sanitizer dev-main
2525
## Usage
2626

2727
```php
28-
use MirazMac\HtmlSanitizer\BasicWhitelist;
28+
use MirazMac\HtmlSanitizer\Whitelist;
2929
use MirazMac\HtmlSanitizer\Sanitizer;
3030

3131
require_once '../vendor/autoload.php';
3232

33-
// A basic pre-defined whitelist, you can off course customize, add, remove or create your own whitelist
34-
$whitelist = new BasicWhitelist;
33+
$whitelist = new Whitelist;
34+
35+
// Allow the anchor tag with specific attributes
36+
$whitelist->allowTag('a', ['href', 'title', 'download', 'data-url', 'data-loaded']);
37+
38+
// You can add multiple tags at once as well if that's what you prefer
39+
$whitelist->setTags(
40+
[
41+
// allows the `abbr` tag and it's title attribute
42+
'abbr' => ['title'],
43+
// allows only the em tag, any attributes would be stripped off
44+
'em' => [],
45+
],
46+
true
47+
);
48+
49+
// Set allowed hosts for the URL attributes on the `a` tag
50+
$whitelist->setAllowedHosts('a', ['google.com', 'facebook.com']);
51+
52+
// Set the allowed protocols for this document
53+
$whitelist->setProtocols(['http', '//', 'https']);
54+
55+
// Set a list of allowed values for an attribute's tag
56+
$whitelist->setAllowedValues('abbr', 'title', ['one', 'two', 'three']);
57+
58+
// Set a list of custom attributes to be treated as URL (i.e to use the host & protocol filter)
59+
$whitelist->treatAttributesAsUrl(['data-url']);
60+
61+
// Set a list of custom attributes to be treated as HTML Boolean (Not true/false ) (i.e their values would be set to blank or the name of the attribute itself)
62+
$whitelist->treatAttributesAsBoolean(['data-load']);
3563

3664
// Create the sanitizer instance that uses this whitelist
3765
$htmlsanitizer = new Sanitizer($whitelist);
3866

3967
// returns sanitized string
40-
$sanitizedHTML = $htmlsanitizer->sanitize('....HTML STRING...');
68+
$sanitizedHTML = $htmlsanitizer->sanitize('<a href="//google.com" data-download="">Google</a> <a href="https://bing.com" data-url="https://bing.com">My URL would be removed</a>');
69+
70+
echo "HTML Source Output: <pre>";
71+
echo htmlspecialchars($sanitizedHTML);
72+
echo "</pre><br>Rendered Output:<br>" . $sanitizedHTML;
73+
4174

4275
```
4376

usage/usage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
require_once '../vendor/autoload.php';
1010

1111
$whitelist = new BasicWhitelist;
12-
$whitelist->setAllowedHosts('a', ['google.com']);
12+
$whitelist->setAllowedValues('a', 'href', ['#', '#2']);
13+
$whitelist->setAllowedValues('a', 'title', ['No more']);
1314

1415
$htmlsanitizer = new Sanitizer($whitelist);
1516

0 commit comments

Comments
 (0)