From 23ca5ef248ecce0c54f3e441e2f9aa58965de76b Mon Sep 17 00:00:00 2001 From: Trent Steel Date: Fri, 4 May 2012 22:58:26 +1000 Subject: [PATCH] add transformer functionality --- DependencyInjection/Configuration.php | 8 ++++++ .../TrsteelCkeditorExtension.php | 1 + Form/CkeditorType.php | 28 ++++++++++++++++--- Resources/config/services.yml | 15 ++++++++++ Tests/Form/CkeditorTypeTest.php | 13 +++++++-- Tests/config.yml | 2 +- Transformer/StripCSS.php | 24 ++++++++++++++++ Transformer/StripComments.php | 24 ++++++++++++++++ Transformer/StripJS.php | 24 ++++++++++++++++ TrsteelCkeditorBundle.php | 9 ++++++ 10 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 Transformer/StripCSS.php create mode 100644 Transformer/StripComments.php create mode 100644 Transformer/StripJS.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4034ca1f..ddd62ec1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -26,6 +26,13 @@ public function getConfigTreeBuilder() ->defaultValue('Trsteel\CkeditorBundle\Form\CkeditorType') ->end() ->end() + ->children() + ->variableNode('transformers') + ->defaultValue(array( + 'strip_js', 'strip_css', 'strip_comments' + )) + ->end() + ->end() ->children() ->variableNode('toolbar') ->defaultValue(array( @@ -33,6 +40,7 @@ public function getConfigTreeBuilder() 'basicstyles', 'paragraph', 'links', '/', 'insert', 'styles', 'tools' )) + ->end() ->end() ->children() diff --git a/DependencyInjection/TrsteelCkeditorExtension.php b/DependencyInjection/TrsteelCkeditorExtension.php index 0f965731..485450e8 100644 --- a/DependencyInjection/TrsteelCkeditorExtension.php +++ b/DependencyInjection/TrsteelCkeditorExtension.php @@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container) )); $container->setParameter('trsteel_ckeditor.form.type.class', $config['class']); + $container->setParameter('trsteel_ckeditor.ckeditor.transformers', $config['transformers']); $container->setParameter('trsteel_ckeditor.ckeditor.toolbar', $config['toolbar']); $container->setParameter('trsteel_ckeditor.ckeditor.toolbar_groups', array_merge($this->getDefaultGroups(), $config['toolbar_groups'])); $container->setParameter('trsteel_ckeditor.ckeditor.startup_outline_blocks', $config['startup_outline_blocks']); diff --git a/Form/CkeditorType.php b/Form/CkeditorType.php index a28864b0..4936bbf8 100644 --- a/Form/CkeditorType.php +++ b/Form/CkeditorType.php @@ -8,6 +8,8 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Form\DataTransformerInterface; + /** * CKEditor type * @@ -15,23 +17,40 @@ class CkeditorType extends AbstractType { protected $container; + protected $transformers; public function __construct(ContainerInterface $container) { $this->container = $container; } + + public function addTransformer(DataTransformerInterface $transformer, $alias) + { + if (isset($this->transformers[$alias])) { + throw new \Exception('Transformer alias must be unique.'); + } + $this->transformers[$alias] = $transformer; + } /** * {@inheritdoc} */ public function buildForm(FormBuilder $builder, array $options) { + foreach($options['transformers'] as $transformer_alias) { + if (isset($this->transformers[$transformer_alias])) { + $builder->appendClientTransformer($this->transformers[$transformer_alias]); + } else { + throw new \Exception(sprintf("'%s' is not a valid transformer.", $transformer_alias)); + } + } + $default_toolbar_groups = $this->getDefaultOptions(); - $default_toolbar_groups = $default_toolbar_groups['toolbar_groups']; + $default_toolbar_groups = array_merge($default_toolbar_groups['toolbar_groups'], $options['toolbar_groups']); $builder ->setAttribute('toolbar', $options['toolbar']) - ->setAttribute('toolbar_groups', array_merge($default_toolbar_groups, $options['toolbar_groups'])) + ->setAttribute('toolbar_groups', $default_toolbar_groups) ->setAttribute('ui_color', $options['ui_color'] ? '#'.ltrim($options['ui_color'], '#') : null) ->setAttribute('startup_outline_blocks', $options['startup_outline_blocks']) ->setAttribute('width', $options['width']) @@ -98,6 +117,7 @@ public function getDefaultOptions() { return array( 'required' => false, + 'transformers' => $this->container->getParameter('trsteel_ckeditor.ckeditor.transformers'), 'toolbar' => $this->container->getParameter('trsteel_ckeditor.ckeditor.toolbar'), 'toolbar_groups' => $this->container->getParameter('trsteel_ckeditor.ckeditor.toolbar_groups'), 'startup_outline_blocks' => $this->container->getParameter('trsteel_ckeditor.ckeditor.startup_outline_blocks'), @@ -124,8 +144,8 @@ public function getDefaultOptions() public function getAllowedOptionValues() { return array( - 'required' => array(false), - 'startup_outline_blocks' => array(true, false) + 'required' => array(false), + 'startup_outline_blocks' => array(true, false) ); } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index f70bc58a..b6e9d52c 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -10,3 +10,18 @@ services: tags: - { name: form.type, alias: ckeditor } arguments: [ @service_container ] + + trsteel_ckeditor.transformer.strip_js: + class: Trsteel\CkeditorBundle\Transformer\StripJS + tags: + - { name: trsteel_ckeditor.transformer, alias: strip_js } + + trsteel_ckeditor.transformer.strip_css: + class: Trsteel\CkeditorBundle\Transformer\StripCSS + tags: + - { name: trsteel_ckeditor.transformer, alias: strip_css } + + trsteel_ckeditor.transformer.strip_comments: + class: Trsteel\CkeditorBundle\Transformer\StripComments + tags: + - { name: trsteel_ckeditor.transformer, alias: strip_comments } \ No newline at end of file diff --git a/Tests/Form/CkeditorTypeTest.php b/Tests/Form/CkeditorTypeTest.php index 1c79b914..157ef8ab 100644 --- a/Tests/Form/CkeditorTypeTest.php +++ b/Tests/Form/CkeditorTypeTest.php @@ -4,6 +4,9 @@ use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase; use Trsteel\CkeditorBundle\Form\CkeditorType; +use Trsteel\CkeditorBundle\Transformer\StripJS; +use Trsteel\CkeditorBundle\Transformer\StripCSS; +use Trsteel\CkeditorBundle\Transformer\StripComments; class CkeditorTypeTest extends TypeTestCase { @@ -26,8 +29,14 @@ public function get($serviceId) public function setUp() { parent::setUp(); + + $CkeditorType = new CkeditorType($this->get('service_container')); + + $CkeditorType->addTransformer(new StripJS(), 'strip_js'); + $CkeditorType->addTransformer(new StripCSS(), 'strip_css'); + $CkeditorType->addTransformer(new StripComments(), 'strip_comments'); - $this->factory->addType(new CkeditorType($this->get('service_container'))); + $this->factory->addType($CkeditorType); } /** @@ -405,7 +414,7 @@ public function testFilebrowserFlashBrowseUrl() public function testFilebrowserFlashUploadUrl() { $form = $this->factory->create('ckeditor', null, array( - 'filebrowser_flash_upload_url' => '/myfilebrowser/uploads' + 'filebrowser_flash_upload_url' => '/myfilebrowser/uploads', )); $view = $form->createView(); diff --git a/Tests/config.yml b/Tests/config.yml index 365a2b89..ecedb8b9 100644 --- a/Tests/config.yml +++ b/Tests/config.yml @@ -1,4 +1,4 @@ framework: secret: 'secret' router: { resource: null } - templating: { engines: ['twig'] } \ No newline at end of file + templating: { engines: ['twig'] } diff --git a/Transformer/StripCSS.php b/Transformer/StripCSS.php new file mode 100644 index 00000000..a6548e46 --- /dev/null +++ b/Transformer/StripCSS.php @@ -0,0 +1,24 @@ +]*>(.*?)<\/style>/is', '', $data); + } +} \ No newline at end of file diff --git a/Transformer/StripComments.php b/Transformer/StripComments.php new file mode 100644 index 00000000..7c36ecee --- /dev/null +++ b/Transformer/StripComments.php @@ -0,0 +1,24 @@ +/is', '', $data); + } +} \ No newline at end of file diff --git a/Transformer/StripJS.php b/Transformer/StripJS.php new file mode 100644 index 00000000..e356a7b9 --- /dev/null +++ b/Transformer/StripJS.php @@ -0,0 +1,24 @@ +]*>(.*?)<\/script>/is', '', $data); + } +} \ No newline at end of file diff --git a/TrsteelCkeditorBundle.php b/TrsteelCkeditorBundle.php index eac3ee4b..2af3fe0a 100644 --- a/TrsteelCkeditorBundle.php +++ b/TrsteelCkeditorBundle.php @@ -3,7 +3,16 @@ namespace Trsteel\CkeditorBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +use Trsteel\CkeditorBundle\DependencyInjection\Compiler\TransformerCompilerPass; class TrsteelCkeditorBundle extends Bundle { + public function build(ContainerBuilder $container) + { + parent::build($container); + + $container->addCompilerPass(new TransformerCompilerPass()); + } }