Skip to content

Commit

Permalink
add transformer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
trsteel88 committed May 4, 2012
1 parent d0f068c commit 23ca5ef
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 7 deletions.
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ 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(
'document', 'clipboard', 'editing', '/',
'basicstyles', 'paragraph', 'links', '/',
'insert', 'styles', 'tools'
))

->end()
->end()
->children()
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/TrsteelCkeditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
28 changes: 24 additions & 4 deletions Form/CkeditorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,49 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Symfony\Component\Form\DataTransformerInterface;

/**
* CKEditor type
*
*/
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'])
Expand Down Expand Up @@ -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'),
Expand All @@ -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)
);
}

Expand Down
15 changes: 15 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
13 changes: 11 additions & 2 deletions Tests/Form/CkeditorTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Tests/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
framework:
secret: 'secret'
router: { resource: null }
templating: { engines: ['twig'] }
templating: { engines: ['twig'] }
24 changes: 24 additions & 0 deletions Transformer/StripCSS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Trsteel\CkeditorBundle\Transformer;

use Symfony\Component\Form\DataTransformerInterface;

class StripCSS implements DataTransformerInterface
{
/**
* {@inheritDoc}
*/
public function transform($data)
{
return $data;
}

/**
* {@inheritDoc}
*/
public function reverseTransform($data)
{
return preg_replace('/<style[^>]*>(.*?)<\/style>/is', '', $data);
}
}
24 changes: 24 additions & 0 deletions Transformer/StripComments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Trsteel\CkeditorBundle\Transformer;

use Symfony\Component\Form\DataTransformerInterface;

class StripComments implements DataTransformerInterface
{
/**
* {@inheritDoc}
*/
public function transform($data)
{
return $data;
}

/**
* {@inheritDoc}
*/
public function reverseTransform($data)
{
return preg_replace('/<!--(.*?)-->/is', '', $data);
}
}
24 changes: 24 additions & 0 deletions Transformer/StripJS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Trsteel\CkeditorBundle\Transformer;

use Symfony\Component\Form\DataTransformerInterface;

class StripJS implements DataTransformerInterface
{
/**
* {@inheritDoc}
*/
public function transform($data)
{
return $data;
}

/**
* {@inheritDoc}
*/
public function reverseTransform($data)
{
return preg_replace('/<script[^>]*>(.*?)<\/script>/is', '', $data);
}
}
9 changes: 9 additions & 0 deletions TrsteelCkeditorBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 23ca5ef

Please sign in to comment.