From 737ec72b652f0cb1e84d6d39aff451a59f2b92f8 Mon Sep 17 00:00:00 2001 From: Alexej Kossmann <32612134+AlexejKossmann@users.noreply.github.com> Date: Mon, 12 Apr 2021 15:08:36 +0200 Subject: [PATCH] Adds Configuration to skipp html tags (#5) * added logic to skipp tags * added script tag to be skipped default * adjusted tests * added ci workflow configuration --- .github/workflows/ci.yml | 56 +++++++++++++++++++ README.md | 13 ++++- phpunit.xml.dist | 9 ++- src/DependencyInjection/Configuration.php | 44 +++++++++++++++ .../HyphenatorExtension.php | 9 ++- src/Hyphenator/FrontendHyphenator.php | 39 ++++++++++++- tests/Hyphenator/FrontendHyphenatorTest.php | 15 ++++- 7 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 src/DependencyInjection/Configuration.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6fc83f7 --- /dev/null +++ b/.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 \ No newline at end of file diff --git a/README.md b/README.md index 6468114..f801a81 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) - diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a27a59c..337c4e6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,8 +17,9 @@ - - + + + @@ -35,4 +36,8 @@ + + + + \ No newline at end of file diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..926c844 --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/src/DependencyInjection/HyphenatorExtension.php b/src/DependencyInjection/HyphenatorExtension.php index d65ac6c..386c145 100644 --- a/src/DependencyInjection/HyphenatorExtension.php +++ b/src/DependencyInjection/HyphenatorExtension.php @@ -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; + } } diff --git a/src/Hyphenator/FrontendHyphenator.php b/src/Hyphenator/FrontendHyphenator.php index 063b220..437022e 100644 --- a/src/Hyphenator/FrontendHyphenator.php +++ b/src/Hyphenator/FrontendHyphenator.php @@ -1,7 +1,7 @@ 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[^>]*>(.*?)]*>#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('#(

)?(?.+?)(<\/p>)?<\/body>#is', $html, $matches) || !isset($matches['content'])) { diff --git a/tests/Hyphenator/FrontendHyphenatorTest.php b/tests/Hyphenator/FrontendHyphenatorTest.php index e46bdda..ed3cb96 100644 --- a/tests/Hyphenator/FrontendHyphenatorTest.php +++ b/tests/Hyphenator/FrontendHyphenatorTest.php @@ -1,7 +1,7 @@ createMock(ModelUtil::class); $this->container->set('huh.utils.model', $modelUtil); + $this->container->setParameter('huh_hyphenator', ['skip_tags' => ['script', 'img']]); $listener = new FrontendHyphenator($this->container); @@ -171,6 +172,18 @@ public function hyphenationProvider() $this->getConfig(), '', ], + [ + '', + $this->getPage(), + $this->getConfig(), + '', + ], + [ + '', + $this->getPage(), + $this->getConfig(), + '', + ], ]; }