Skip to content

Commit

Permalink
Adds Configuration to skipp html tags (#5)
Browse files Browse the repository at this point in the history
* added logic to skipp tags

* added script tag to be skipped default

* adjusted tests

* added ci workflow configuration
  • Loading branch information
AlexejKossmann committed Apr 12, 2021
1 parent 01831d1 commit 737ec72
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 6 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,56 @@
name: CI

on: [ push ]

jobs:
tests:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ 7.2., 7.3, 7.4 ]
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib
tools: phpunit
coverage: none

- name: Checkout
uses: actions/checkout@v2

- name: Install the dependencies
run: composer install --no-interaction

- name: Run the unit tests
run: php vendor/bin/phpunit -c phpunit.xml.dist --colors=always

coverage:
runs-on: ubuntu-latest
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib
coverage: xdebug
tools: php-cs-fixer, phpunit

- name: Checkout
uses: actions/checkout@v2

- name: Install the dependencies
run: composer install --no-interaction

- name: Generate the coverage report
run: php vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/logs/clover.xml

- name: Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/logs/clover.xml -v
13 changes: 12 additions & 1 deletion README.md
Expand Up @@ -29,6 +29,18 @@ hyphenator_locale_language_mapping | array | ['en' => 'en-us', 'cz' => 'cs'] | M

If you want to skip several tags from hyphenation simply add `hyphen-none` as css-class to the appropriate element or use the `tl_page.hyphenation` field.

You also can add tags to be skipped to your project configuration. See configuration reference below

## Configuration reference

```yaml
# Default configuration for extension with alias: "huh_hyphenator"
huh_hyphenator:

# Add tags you want to be skipped from hyphenating, to array (string without <>)
skip_tags: []

```

## Line break exceptions

Expand All @@ -44,4 +56,3 @@ As you can see, if you provide an replace pattern, than an regular expression wi

* [vanderlee/phpSyllable](https://github.com/vanderlee/phpSyllable)
* [wa72/htmlpagedom](https://github.com/wasinger/htmlpagedom)

9 changes: 7 additions & 2 deletions phpunit.xml.dist
Expand Up @@ -17,8 +17,9 @@
<ini name="error_reporting" value="-1"/>
<ini name="display_errors" value="1"/>
<ini name="display_startup_errors" value="1"/>
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
</php>

<testsuites>
Expand All @@ -35,4 +36,8 @@
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
44 changes: 44 additions & 0 deletions src/DependencyInjection/Configuration.php
@@ -0,0 +1,44 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\HyphenatorBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
const ROOT_ID = 'huh_hyphenator';

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder(static::ROOT_ID);

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->root(static::ROOT_ID);
} else {
$rootNode = $treeBuilder->getRootNode();
}

// $rootNode = $treeBuilder->root(static::ROOT_ID);
$rootNode
->children()
->arrayNode('skip_tags')
->scalarPrototype()
->defaultValue(['script'])
->end()
->end()
->end();

return $treeBuilder;
}
}
9 changes: 8 additions & 1 deletion src/DependencyInjection/HyphenatorExtension.php
Expand Up @@ -20,8 +20,15 @@ class HyphenatorExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$configuration = new Configuration();
$container->setParameter(Configuration::ROOT_ID, $this->processConfiguration($configuration, $configs));

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}

public function getAlias()
{
return Configuration::ROOT_ID;
}
}
39 changes: 38 additions & 1 deletion src/Hyphenator/FrontendHyphenator.php
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (c) 2020 Heimrich & Hannot GmbH
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
Expand Down Expand Up @@ -108,10 +108,47 @@ function (HtmlPageCrawler $node, $i) use ($h, &$cache, $cacheEnabled, $objPage)
$html = $this->handleLineBreakExceptions($html, $objPage);

if (!$this->isHyphenationDisabled($objPage, Config::get('hyphenator_skipPages'))) {
// mask tags configured to be skipped
$skipTagCache = [];
$skipTagCacheIndex = 0;

foreach ($this->container->getParameter('huh_hyphenator')['skip_tags'] as $tag) {
if (\in_array($tag, $this->voidElements)) {
$html = preg_replace_callback(
'#<\s*?'.$tag.'\b[^>]*>#s',
function ($matches) use (&$skipTagCache, $skipTagCacheIndex) {
$skipTagCache[$skipTagCacheIndex] = $matches[0];
++$skipTagCacheIndex;

return '####skip:open####'.($skipTagCacheIndex - 1).'####skip:close####';
}, $html
);
} else {
$html = preg_replace_callback(
'#<\s*?'.$tag.'\b[^>]*>(.*?)</'.$tag.'\b[^>]*>#s',
function ($matches) use (&$skipTagCache, &$skipTagCacheIndex) {
$skipTagCache[$skipTagCacheIndex] = $matches[0];
++$skipTagCacheIndex;

return '####skip:open####'.($skipTagCacheIndex - 1).'####skip:close####';
}, $html
);
}
}

// if html contains nested tags, use the hyphenateHtml that excludes HTML tags and attributes
libxml_use_internal_errors(true); // disable error reporting when potential using HTML5 tags
$html = $h->hyphenateHtml($html);
libxml_clear_errors();

// replace skipped tags
$html = preg_replace_callback(
'/####skip:open####(.*)####skip:close####/',
function ($matches) use ($skipTagCache) {
return $skipTagCache[$matches[1]];
},
$html
);
}

if (false === preg_match('#<body>(<p>)?(?<content>.+?)(<\/p>)?<\/body>#is', $html, $matches) || !isset($matches['content'])) {
Expand Down
15 changes: 14 additions & 1 deletion tests/Hyphenator/FrontendHyphenatorTest.php
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (c) 2020 Heimrich & Hannot GmbH
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
Expand Down Expand Up @@ -68,6 +68,7 @@ public function testHyphenate($buffer, array $pageData, array $config, $expected
$modelUtil = $this->createMock(ModelUtil::class);

$this->container->set('huh.utils.model', $modelUtil);
$this->container->setParameter('huh_hyphenator', ['skip_tags' => ['script', 'img']]);

$listener = new FrontendHyphenator($this->container);

Expand Down Expand Up @@ -171,6 +172,18 @@ public function hyphenationProvider()
$this->getConfig(),
'<picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture>',
],
[
'<a><script>document.querySelectorAll("#id").addEventListener("click", e => {console.log(e.target})</script></a>',
$this->getPage(),
$this->getConfig(),
'<a><script>document.querySelectorAll("#id").addEventListener("click", e => {console.log(e.target})</script></a>',
],
[
'<a><picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture></a>',
$this->getPage(),
$this->getConfig(),
'<a><picture><!--[if IE 9]><video style="display: none;"><![endif]--><source srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" media="(min-width: 853px)"><!--[if IE 9]></video><![endif]--><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=" data-wrapper="#image-wrapper-1368074709" class="image" alt=""></picture></a>',
],
];
}

Expand Down

0 comments on commit 737ec72

Please sign in to comment.