Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Nov 28, 2023
2 parents c245c33 + 99ec04a commit 0c946d2
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,52 @@ class TrixPurifierDefinitions implements Definition
}
```


#### Custom CSS definitions

It's possible to override the CSS definitions, this allows you to customize what
inline styles you allow and their properties and values. This can help fill in
missing values for properties such as text-align, which by default is missing start
and end values. You can do this by creating a CSS definition.

To create your own CSS definition, create a new class and have it implement `CssDefinition`:

```php
namespace App;

use HTMLPurifier_CSSDefinition;
use Stevebauman\Purify\Definitions\CssDefinition;

class CustomCssDefinition implements CssDefinition
{
/**
* Apply rules to the CSS Purifier definition.
*
* @param HTMLPurifier_CSSDefinition $definition
*
* @return void
*/
public static function apply(HTMLPurifier_CSSDefinition $definition)
{
// Customize the CSS purifier definition.
$definition->info['text-align'] = new \HTMLPurifier_AttrDef_Enum(
['right', 'left', 'center', 'start', 'end'],
false,
);
}
}
```

Then, reference this class in the `config/purify.php` file in the `css-definitions` key:

```php
// config/purify.php

'css-definitions' => \App\CustomCssDefinition::class,
```

See the class HTMLPurifier_CSSDefinition in the HTMLPurifier library for other examples of what can be changed.

### Upgrading from v4 to v5

To upgrade from v4, install the latest version by running the below command in the root of your project:
Expand Down
20 changes: 20 additions & 0 deletions config/purify.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@

'definitions' => Html5Definition::class,

/*
|--------------------------------------------------------------------------
| HTMLPurifier CSS definitions
|--------------------------------------------------------------------------
|
| Here you may specify a class that augments the CSS definitions used by
| HTMLPurifier. When specifying a custom class, make sure it implements
| the interface:
|
| \Stevebauman\Purify\Definitions\CssDefinition
|
| Note that these definitions are applied to every Purifier instance.
|
| CSS should be extending $definition->info['css-attribute'] = values
| See HTMLPurifier_CSSDefinition for further explanation
|
*/

'css-definitions' => null,

/*
|--------------------------------------------------------------------------
| Serializer
Expand Down
17 changes: 17 additions & 0 deletions src/Definitions/CssDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Stevebauman\Purify\Definitions;

use HTMLPurifier_CSSDefinition;

interface CssDefinition
{
/**
* Apply rules to the CSS Purifier definition.
*
* @param HTMLPurifier_CSSDefinition $definition
*
* @return void
*/
public static function apply(HTMLPurifier_CSSDefinition $definition);
}
9 changes: 9 additions & 0 deletions src/PurifyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Manager;
use InvalidArgumentException;
use Stevebauman\Purify\Definitions\CssDefinition;
use Stevebauman\Purify\Definitions\Definition;

class PurifyManager extends Manager
Expand Down Expand Up @@ -201,6 +202,14 @@ protected function createHtmlConfig($config)
}
}

if ($definition = $htmlConfig->getCSSDefinition()) {
$definitionsClass = $this->config->get('purify.css-definitions');

if ($definitionsClass && is_a($definitionsClass, CssDefinition::class, true)) {
$definitionsClass::apply($definition);
}
}

return $htmlConfig;
}
}
43 changes: 43 additions & 0 deletions tests/PurifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Stevebauman\Purify\Tests;

use HTMLPurifier_CSSDefinition;
use HTMLPurifier_HTMLDefinition;
use Illuminate\Support\Facades\File;
use Stevebauman\Purify\Cache\CacheDefinitionCache;
use Stevebauman\Purify\Definitions\CssDefinition;
use Stevebauman\Purify\Definitions\Definition;
use Stevebauman\Purify\Facades\Purify;
use Stevebauman\Purify\PurifyServiceProvider;
Expand Down Expand Up @@ -131,6 +133,36 @@ public function test_custom_definitions_are_applied()
Purify::config(['HTML.Allowed' => 'span[class]'])->clean('<span class="bar">Test</span>')
);
}

public function test_custom_css_definitions_are_applied()
{
$this->app['config']->set('purify.css-definitions', FooCssDefinition::class);

$this->assertEquals(
'<p>Test</p>',
Purify::clean('<p style="text-align:left">Test</p>')
);

$this->assertEquals(
'<p>Test</p>',
Purify::clean('<p style="text-align:right">Test</p>')
);

$this->assertEquals(
'<p style="text-align:center;">Test</p>',
Purify::clean('<p style="text-align:center;">Test</p>')
);

$this->assertEquals(
'<p style="text-align:start;">Test</p>',
Purify::clean('<p style="text-align:start;">Test</p>')
);

$this->assertEquals(
'<p style="text-align:end;">Test</p>',
Purify::clean('<p style="text-align:end;">Test</p>')
);
}
}

class FooDefinition implements Definition
Expand All @@ -140,3 +172,14 @@ public static function apply(HTMLPurifier_HTMLDefinition $definition)
$definition->addAttribute('span', 'class', 'Enum#foo');
}
}

class FooCssDefinition implements CssDefinition
{
public static function apply(HTMLPurifier_CSSDefinition $definition)
{
$definition->info['text-align'] = new \HTMLPurifier_AttrDef_Enum(
['center', 'start', 'end'],
false,
);
}
}

0 comments on commit 0c946d2

Please sign in to comment.