Skip to content

Commit

Permalink
Merge pull request #77 from jolicode/php8.1
Browse files Browse the repository at this point in the history
Some maintenance work
  • Loading branch information
damienalexandre authored Nov 20, 2022
2 parents ab79a6e + 8c00c9d commit 9d0b3cc
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
php-versions:
- '7.4'
- '8.0'
- '8.1'

steps:
- uses: actions/checkout@v2
Expand All @@ -43,6 +44,7 @@ jobs:
php-versions:
- '7.4'
- '8.0'
- '8.1'

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ CHANGELOG

### 1.4.0 (not released yet) ###

* Drop support for Symfony < 4.4
* Add support for Symfony ^6.0
* Run tests on PHP 8.1

### 1.3.0 (2022-05-30) ###

* Add PHAR generation to use the library in CLI context
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"php": ">=7.4",
"ext-mbstring": "*",
"lib-libxml": "*",
"org_heigl/hyphenator": "~2.6.0"
"org_heigl/hyphenator": "^2.6 || ^3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.3.2",
"symfony/phpunit-bridge": "^5.0",
"symfony/framework-bundle": "^3.4.26|^4.1.12|^5.0",
"symfony/twig-bundle": "^3.4.26|^4.1.12|^5.0",
"symfony/yaml": "^3.4.26|^4.1.12|^5.0"
"symfony/phpunit-bridge": "^5.4.8 || ^6.0",
"symfony/framework-bundle": "^4.4 || ^5.0 || ^6.0",
"symfony/twig-bundle": "^4.4 || ^5.0 || ^6.0",
"symfony/yaml": "^4.4 || ^5.0 || ^6.0"
},
"conflict": {
"ext-apc": "3.1.11"
Expand All @@ -35,7 +35,7 @@
"psr-4": { "JoliTypo\\Tests\\": "tests/JoliTypo/Tests" }
},
"scripts": {
"test": "vendor/bin/simple-phpunit -c phpunit.xml.dist",
"test": "vendor/bin/simple-phpunit",
"cs": "vendor/bin/php-cs-fixer fix"
}
}
35 changes: 17 additions & 18 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public function __construct($rules)
/**
* @param string $content HTML content to fix
*
* @throws Exception\BadRuleSetException
*
* @return string Fixed content
*
* @throws Exception\BadRuleSetException
*/
public function fix($content)
{
Expand Down Expand Up @@ -173,8 +173,6 @@ public function setLocale($locale)
/**
* Get language part of a Locale string (fr_FR => fr).
*
* @param $locale
*
* @return string
*/
public static function getLanguageFromLocale($locale)
Expand All @@ -191,8 +189,6 @@ public static function getLanguageFromLocale($locale)
/**
* Build the _rules array of Fixer.
*
* @param $rules
*
* @throws Exception\BadRuleSetException
*/
private function compileRules($rules)
Expand Down Expand Up @@ -290,11 +286,9 @@ private function doFix(\DOMText $childNode, \DOMNode $node, \DOMDocument $dom)
}

/**
* @param $content
* @return \DOMDocument
*
* @throws Exception\InvalidMarkupException
*
* @return \DOMDocument
*/
private function loadDOMDocument($content)
{
Expand All @@ -305,16 +299,13 @@ private function loadDOMDocument($content)
$dom->substituteEntities = false;
$dom->formatOutput = false;

// Change mb and libxml config
// Change libxml config
$libxmlCurrent = libxml_use_internal_errors(true);
$mbDetectCurrent = mb_detect_order();
mb_detect_order('ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15');

$loaded = $dom->loadHTML($this->fixContentEncoding($content));

// Restore mb and libxml config
// Restore libxml config
libxml_use_internal_errors($libxmlCurrent);
mb_detect_order(implode(',', $mbDetectCurrent));

if (!$loaded) {
throw new InvalidMarkupException("Can't load the given HTML via DomDocument");
Expand All @@ -329,8 +320,6 @@ private function loadDOMDocument($content)
* @see http://php.net/manual/en/domdocument.loadhtml.php#91513
* @see https://github.com/jolicode/JoliTypo/issues/7
*
* @param $content
*
* @return string
*/
private function fixContentEncoding($content)
Expand All @@ -345,7 +334,15 @@ private function fixContentEncoding($content)
$content = $hack . $content;
}

$encoding = mb_detect_encoding($content);
$encoding = null;
foreach (['UTF-8', 'ASCII', 'ISO-8859-1', 'windows-1252', 'iso-8859-15'] as $testedEncoding) {
if (mb_detect_encoding($content, $testedEncoding, true)) {
$encoding = $testedEncoding;

break;
}
}

$headPos = mb_strpos($content, '<head>');

// Add a meta to the <head> section
Expand All @@ -356,7 +353,9 @@ private function fixContentEncoding($content)
mb_substr($content, $headPos);
}

$content = mb_convert_encoding($content, 'HTML-ENTITIES', $encoding);
if ('UTF-8' !== $encoding) {
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
}
}

return $content;
Expand Down
5 changes: 0 additions & 5 deletions src/JoliTypo/Fixer/Hyphen.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ public function __construct($locale)
$this->setLocale($locale);
}

/**
* @param $locale
*/
public function setLocale($locale)
{
$this->hyphenator = Hyphenator::factory(null, $this->fixLocale($locale));
Expand All @@ -82,8 +79,6 @@ protected function setOptions()
/**
* Transform fr_FR to fr to fit the list of supported locales.
*
* @param $locale
*
* @return mixed
*/
protected function fixLocale($locale)
Expand Down
10 changes: 5 additions & 5 deletions src/JoliTypo/Fixer/SmartQuotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setLocale($locale)
$this->closingPrefix = '';

return;
// «…»
// «…»
case 'de-ch':
$this->opening = Fixer::LAQUO;
$this->openingSuffix = '';
Expand All @@ -93,7 +93,7 @@ public function setLocale($locale)
$this->closingPrefix = Fixer::NO_BREAK_SPACE;

break;
// «…»
// «…»
case 'hy':
case 'az':
case 'hz':
Expand All @@ -115,7 +115,7 @@ public function setLocale($locale)
$this->closingPrefix = '';

break;
// „…“
// „…“
case 'de':
case 'ka':
case 'cs':
Expand All @@ -133,7 +133,7 @@ public function setLocale($locale)
$this->closingPrefix = '';

break;
// “…”
// “…”
case 'en':
case 'us':
case 'gb':
Expand All @@ -153,7 +153,7 @@ public function setLocale($locale)
$this->closingPrefix = '';

break;
// ”…”
// ”…”
case 'fi':
case 'sv':
case 'bs':
Expand Down
1 change: 1 addition & 0 deletions tests/JoliTypo/Tests/Bridge/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ framework:
resource: "%kernel.project_dir%/tests/JoliTypo/Tests/Bridge/app/config/routing.yml"
strict_requirements: ~
secret: "ThisTokenIsEnoughSecretForOurTests"
http_method_override: false
twig:
strict_variables: '%kernel.debug%'

Expand Down
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/Fixer/DashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testSimpleString()
$this->assertEquals('M. Jackson: 1964' . Fixer::NDASH . '2009', $fixer->fix('M. Jackson: 1964-2009'));
$this->assertEquals('M. Jackson: 1964 ' . Fixer::NDASH . ' 2009', $fixer->fix('M. Jackson: 1964 - 2009'));
$this->assertEquals('Style ' . Fixer::NDASH . ' not sincerity ' . Fixer::NDASH . ' is the vital thing.', $fixer->fix('Style - not sincerity - is the vital thing.'));
//$this->assertEquals("Style ".Fixer::MDASH." not sincerity ".Fixer::MDASH." is the vital thing.", $fixer->fix("Style -not sincerity- is the vital thing."));
// $this->assertEquals("Style ".Fixer::MDASH." not sincerity ".Fixer::MDASH." is the vital thing.", $fixer->fix("Style -not sincerity- is the vital thing."));

$this->assertEquals('Style' . Fixer::MDASH . 'you have it.', $fixer->fix('Style -- you have it.'));
$this->assertEquals('Style' . Fixer::MDASH . 'you have it.', $fixer->fix('Style--you have it.'));
Expand Down
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/Html5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testFullPageMarkup()
HTML;

$fixed = <<<'STRING'
&#8220;Who Let the Dogs Out?&#8221; is a song written and originally recorded by Anslem Douglas (titled &#8220;Doggie&#8221;).
Who Let the Dogs Out? is a song written and originally recorded by Anslem Douglas (titled Doggie).
STRING;

$this->assertEquals($fixed, $fixer->fix($html));
Expand Down
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/JoliTypoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testHtmlComments()
$this->assertEquals('<p>Coucou&hellip;</p> <!-- Not Coucou... -->', $fixer->fix('<p>Coucou...</p> <!-- Not Coucou... -->'));

// This test can't be ok, DomDocument is encoding entities even in comments (╯°□°)╯︵ ┻━┻
//$this->assertEquals("<p>Coucou&hellip;</p> <!-- abusé -->", $fixer->fix("<p>Coucou...</p> <!-- abusé -->"));
// $this->assertEquals("<p>Coucou&hellip;</p> <!-- abusé -->", $fixer->fix("<p>Coucou...</p> <!-- abusé -->"));
}

public function testBadRuleSets()
Expand Down

0 comments on commit 9d0b3cc

Please sign in to comment.