Skip to content

Commit 733b51f

Browse files
authored
Merge pull request #952 from thephpleague/fix-mbstring-usage
Fix mbstring calls without explicit encoding
2 parents 8e37e27 + 9f6b98a commit 733b51f

File tree

16 files changed

+169
-15
lines changed

16 files changed

+169
-15
lines changed

.github/workflows/backwards-compatibility.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ jobs:
1818
with:
1919
fetch-depth: 0
2020

21+
# User in the container seems to be different than the cloned repo's owner.
22+
# Git doesn't like that as the repo will then be unusable by the owner.
23+
# We don't care about this here since this is only used for running one test.
24+
# See https://github.com/actions/runner/issues/2033
25+
- name: Workaround directory permissions
26+
run: mkdir -p /home/runner/work/_temp/_github_home && printf "[safe]\n\tdirectory = /github/workspace" > /home/runner/work/_temp/_github_home/.gitconfig
27+
2128
- name: "BC Check"
2229
uses: docker://nyholm/roave-bc-check-ga
2330
with:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
66

77
## [Unreleased][unreleased]
88

9+
### Fixed
10+
11+
- Fixed parsing issues when `mb_internal_encoding()` is set to something other than `UTF-8` (#951)
12+
913
## [2.3.7] - 2022-11-03
1014

1115
### Fixed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@
9494
"autoload-dev": {
9595
"psr-4": {
9696
"League\\CommonMark\\Tests\\Unit\\": "tests/unit",
97-
"League\\CommonMark\\Tests\\Functional\\": "tests/functional"
97+
"League\\CommonMark\\Tests\\Functional\\": "tests/functional",
98+
"League\\CommonMark\\Tests\\PHPStan\\": "tests/phpstan"
9899
}
99100
},
100101
"scripts": {

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ parameters:
77
message: '#Parameter .+ of class .+Reference constructor expects string, string\|null given#'
88
- path: src/Util/RegexHelper.php
99
message: '#Method .+RegexHelper::unescape\(\) should return string but returns string\|null#'
10+
rules:
11+
- 'League\CommonMark\Tests\PHPStan\MbstringFunctionCallRule'

src/Extension/Autolink/UrlAutolinkParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function parse(InlineParserContext $inlineContext): bool
105105
$url = \substr($url, 0, -$diff);
106106
}
107107

108-
$cursor->advanceBy(\mb_strlen($url));
108+
$cursor->advanceBy(\mb_strlen($url, 'UTF-8'));
109109

110110
// Auto-prefix 'http://' onto 'www' URLs
111111
if (\substr($url, 0, 4) === 'www.') {

src/Extension/Footnote/Renderer/FootnoteBackrefRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): s
4444

4545
$attrs->append('class', $this->config->get('footnote/backref_class'));
4646
$attrs->set('rev', 'footnote');
47-
$attrs->set('href', \mb_strtolower($node->getReference()->getDestination()));
47+
$attrs->set('href', \mb_strtolower($node->getReference()->getDestination(), 'UTF-8'));
4848
$attrs->set('role', 'doc-backlink');
4949

5050
$symbol = $this->config->get('footnote/backref_symbol');

src/Extension/Footnote/Renderer/FootnoteRefRenderer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
4040

4141
$attrs = $node->data->getData('attributes');
4242
$attrs->append('class', $this->config->get('footnote/ref_class'));
43-
$attrs->set('href', \mb_strtolower($node->getReference()->getDestination()));
43+
$attrs->set('href', \mb_strtolower($node->getReference()->getDestination(), 'UTF-8'));
4444
$attrs->set('role', 'doc-noteref');
4545

4646
$idPrefix = $this->config->get('footnote/ref_id_prefix');
4747

4848
return new HtmlElement(
4949
'sup',
5050
[
51-
'id' => $idPrefix . \mb_strtolower($node->getReference()->getLabel()),
51+
'id' => $idPrefix . \mb_strtolower($node->getReference()->getLabel(), 'UTF-8'),
5252
],
5353
new HtmlElement(
5454
'a',

src/Extension/Footnote/Renderer/FootnoteRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
4141
$attrs = $node->data->getData('attributes');
4242

4343
$attrs->append('class', $this->config->get('footnote/footnote_class'));
44-
$attrs->set('id', $this->config->get('footnote/footnote_id_prefix') . \mb_strtolower($node->getReference()->getLabel()));
44+
$attrs->set('id', $this->config->get('footnote/footnote_id_prefix') . \mb_strtolower($node->getReference()->getLabel(), 'UTF-8'));
4545
$attrs->set('role', 'doc-endnote');
4646

4747
return new HtmlElement(

src/Normalizer/SlugNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public function normalize(string $text, array $context = []): string
4141
// Trim whitespace
4242
$slug = \trim($slug);
4343
// Convert to lowercase
44-
$slug = \mb_strtolower($slug);
44+
$slug = \mb_strtolower($slug, 'UTF-8');
4545
// Try replacing whitespace with a dash
4646
$slug = \preg_replace('/\s+/u', '-', $slug) ?? $slug;
4747
// Try removing characters other than letters, numbers, and marks.
4848
$slug = \preg_replace('/[^\p{L}\p{Nd}\p{Nl}\p{M}-]+/u', '', $slug) ?? $slug;
4949
// Trim to requested length if given
5050
if ($length = $context['length'] ?? $this->defaultMaxLength) {
51-
$slug = \mb_substr($slug, 0, $length);
51+
$slug = \mb_substr($slug, 0, $length, 'UTF-8');
5252
}
5353

5454
return $slug;

src/Parser/InlineParserContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getFullMatch(): string
8383
*/
8484
public function getFullMatchLength(): int
8585
{
86-
return \mb_strlen($this->matches[0]);
86+
return \mb_strlen($this->matches[0], 'UTF-8');
8787
}
8888

8989
/**

0 commit comments

Comments
 (0)