Skip to content

Commit efb6e44

Browse files
authored
Fix UTF-8 support in splitCodeIntoArray() (#76)
1 parent ed72b21 commit efb6e44

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

HighlightUtilities/functions.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ function splitCodeIntoArray($html)
175175

176176
$dom = new \DOMDocument();
177177

178-
if (!$dom->loadHTML($html)) {
178+
// https://stackoverflow.com/a/8218649
179+
if (!$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
179180
throw new \UnexpectedValueException("The given HTML could not be parsed correctly.");
180181
}
181182

@@ -186,15 +187,15 @@ function splitCodeIntoArray($html)
186187
$classes = $span->getAttribute("class");
187188
$renderedSpan = $dom->saveHTML($span);
188189

189-
if (preg_match('/\R/', $renderedSpan)) {
190+
if (preg_match('/\R/u', $renderedSpan)) {
190191
$finished = preg_replace(
191-
'/\R/',
192+
'/\R/u',
192193
sprintf('</span>%s<span class="%s">', PHP_EOL, $classes),
193194
$renderedSpan
194195
);
195196
$html = str_replace($renderedSpan, $finished, $html);
196197
}
197198
}
198199

199-
return preg_split('/\R/', $html);
200+
return preg_split('/\R/u', $html);
200201
}

test/HighlightUtilitiesTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ public function testSplitCodeIntoArray_MultilineComment()
116116
}
117117
}
118118

119+
public function testSplitCodeIntoArray_Emojis()
120+
{
121+
$raw = <<<'PHP'
122+
// ✅ ...
123+
$user = new \stdClass();
124+
$isUserPending = $user->isStatus('pending');
125+
PHP;
126+
$highlighted = $this->hl->highlight('php', $raw);
127+
$split = \HighlightUtilities\splitCodeIntoArray($highlighted->value);
128+
129+
$this->assertEquals(
130+
$split,
131+
array(
132+
'<span class="hljs-comment">// ✅ ...</span>',
133+
'$user = <span class="hljs-keyword">new</span> \stdClass();',
134+
'$isUserPending = $user-&gt;isStatus(<span class="hljs-string">\'pending\'</span>);',
135+
)
136+
);
137+
}
138+
119139
public function testGetThemeBackgroundColorSingleColor()
120140
{
121141
$theme = 'atom-one-dark';

0 commit comments

Comments
 (0)