Skip to content

Commit

Permalink
Add strip_tags option
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Mar 21, 2018
1 parent 91184c0 commit 5b76799
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/CocurSlugifyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function load(array $configs, ContainerBuilder $container)
}

// Extract slugify arguments from config
$slugifyArguments = array_intersect_key($config, array_flip(['lowercase', 'separator', 'regexp', 'rulesets']));
$slugifyArguments = array_intersect_key($config, array_flip(['lowercase', 'trim', 'strip_tags', 'separator', 'regexp', 'rulesets']));

$container->setDefinition('cocur_slugify', new Definition('Cocur\Slugify\Slugify', [$slugifyArguments]));
$container
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getConfigTreeBuilder()
->children()
->booleanNode('lowercase')->end()
->booleanNode('trim')->end()
->booleanNode('strip_tags')->end()
->scalarNode('separator')->end()
->scalarNode('regexp')->end()
->arrayNode('rulesets')->prototype('scalar')->end()
Expand Down
5 changes: 5 additions & 0 deletions src/Slugify.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Slugify implements SlugifyInterface
'separator' => '-',
'lowercase' => true,
'trim' => true,
'strip_tags' => false,
'rulesets' => [
'default',
// Languages are preferred if they appear later, list is ordered by number of
Expand Down Expand Up @@ -111,6 +112,10 @@ public function slugify($string, $options = null)
$rules = $this->rules;
}

$string = ($options['strip_tags'])
? strip_tags($string)
: $string;

$string = strtr($string, $rules);
unset($rules);

Expand Down
10 changes: 10 additions & 0 deletions tests/Bridge/Symfony/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testAll()
$configs = [
[
'lowercase' => true,
'strip_tags' => false,
'separator' => '_',
'regexp' => 'abcd',
'rulesets' => ['burmese', 'hindi']
Expand All @@ -39,6 +40,15 @@ public function testLowercaseOnlyAcceptsBoolean()
$this->process($configs);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testStripTagsOnlyAcceptsBoolean()
{
$configs = [['strip_tags' => 'abc']];
$this->process($configs);
}

/**
* Processes an array of configurations and returns a compiled version.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/SlugifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public function slugifyOptionsArray()

$this->assertEquals('file-name', $this->slugify->slugify('file name '));
$this->assertEquals('file-name-', $this->slugify->slugify('file name ', ['trim' => false]));

$this->assertEquals('file-name', $this->slugify->slugify('<file name'));
$this->assertEquals('p-file-p-foo-a-href-bar-name-a', $this->slugify->slugify('<p>file</p><!-- foo --> <a href="#bar">name</a>'));
$this->assertEquals('file-name', $this->slugify->slugify('<p>file</p><!-- foo --> <a href="#bar">name</a>', ['strip_tags' => true]));
}

/**
Expand Down

0 comments on commit 5b76799

Please sign in to comment.