diff --git a/src/Converter/EmphasisConverter.php b/src/Converter/EmphasisConverter.php
index 359b5ee..2150f8b 100644
--- a/src/Converter/EmphasisConverter.php
+++ b/src/Converter/EmphasisConverter.php
@@ -40,7 +40,11 @@ class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterfa
$tag = $this->getNormTag($element);
$value = $element->getValue();
- if (! \trim($value)) {
+ // Unlike trim(), this also strips non-breaking spaces, which would keep the delimiters from flanking the text
+ $content = \preg_replace('~^\s+|\s+$~u', '', $value);
+ \assert(\is_string($content));
+
+ if (! $content) {
return $value;
}
@@ -50,8 +54,8 @@ class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterfa
$style = Coerce::toString($this->config->getOption('bold_style'));
}
- $prefix = \ltrim($value) !== $value ? ' ' : '';
- $suffix = \rtrim($value) !== $value ? ' ' : '';
+ $prefix = \preg_match('~^\s~u', $value) === 1 ? ' ' : '';
+ $suffix = \preg_match('~\s$~u', $value) === 1 ? ' ' : '';
/* If this node is immediately preceded or followed by one of the same type don't emit
* the start or end $style, respectively. This prevents foobar from
@@ -60,7 +64,7 @@ class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterfa
$preStyle = $this->getNormTag($element->getPreviousSibling()) === $tag ? '' : $style;
$postStyle = $this->getNormTag($element->getNextSibling()) === $tag ? '' : $style;
- return $prefix . $preStyle . \trim($value) . $postStyle . $suffix;
+ return $prefix . $preStyle . $content . $postStyle . $suffix;
}
/**
diff --git a/src/Converter/TextConverter.php b/src/Converter/TextConverter.php
index 465dd48..05ba6ac 100644
--- a/src/Converter/TextConverter.php
+++ b/src/Converter/TextConverter.php
@@ -15,8 +15,8 @@ class TextConverter implements ConverterInterface
// Remove leftover \n at the beginning of the line
$markdown = \ltrim($markdown, "\n");
- // Replace sequences of invisible characters with spaces
- $markdown = \preg_replace('~\s+~u', ' ', $markdown);
+ // Replace sequences of invisible characters with spaces, keeping non-breaking spaces
+ $markdown = \preg_replace('~[^\S\x{00A0}]+~u', ' ', $markdown);
\assert(\is_string($markdown));
// Escape the following characters: '*', '_', '[', ']' and '\'
diff --git a/tests/HtmlConverterTest.php b/tests/HtmlConverterTest.php
index e62247e..5eeadac 100644
--- a/tests/HtmlConverterTest.php
+++ b/tests/HtmlConverterTest.php
@@ -378,6 +378,14 @@ EOT;
$this->assertHtmlGivesMarkdown('<p>Some sample HTML</p>', '`
Some sample HTML
`'); } + public function testNonBreakingSpaces(): void + { + $this->assertHtmlGivesMarkdown('This is a test and test
', "This is a test and\u{00A0}test"); + $this->assertHtmlGivesMarkdown("12,50\u{00A0}\u{00A0}€ \n total
", "12,50\u{00A0}\u{00A0}€ total"); + $this->assertHtmlGivesMarkdown('Total: 12,50 €
', "**Total:** 12,50\u{00A0}€"); + $this->assertHtmlGivesMarkdown('a b
', "a\u{00A0}b"); + } + public function testSetOption(): void { $markdown = new HtmlConverter();