Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Converter/HtmlToDjot.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ protected function processNode(DOMNode $node): string
'kbd' => $this->processSemanticSpan($node, 'kbd'),
'dfn' => $this->processSemanticSpan($node, 'dfn'),
'abbr' => $this->processSemanticSpan($node, 'abbr'),
'samp' => $this->processSemanticSpan($node, 'samp'),
'var' => $this->processSemanticSpan($node, 'var'),
'q' => $this->processInlineQuote($node),
'code' => $this->processCode($node),
'pre' => $this->processPreBlock($node),
Expand Down Expand Up @@ -1625,7 +1627,7 @@ protected function processRawInline(DOMElement $node): string
* for round-trip support with SemanticSpanExtension.
*
* @param \DOMElement $node The semantic element
* @param string $type The element type (kbd, dfn, abbr)
* @param string $type The element type (kbd, dfn, abbr, samp, var)
*/
protected function processSemanticSpan(DOMElement $node, string $type): string
{
Expand Down Expand Up @@ -1672,9 +1674,10 @@ protected function processSemanticSpan(DOMElement $node, string $type): string
protected function processInlineQuote(DOMElement $node): string
{
$content = $this->processChildren($node);
$escapedContent = str_replace(['\\', '"'], ['\\\\', '\\"'], $content);

// Wrap in quotes
$quoted = '"' . $content . '"';
$quoted = '"' . $escapedContent . '"';

// If there's a cite attribute, wrap in span with the attribute
$cite = $node->getAttribute('cite');
Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/Converter/HtmlToDjotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,15 @@ public function testQElement(): void
$this->assertSame('She said "Hello" to me.', $result);
}

public function testQElementEscapesInnerQuotes(): void
{
$html = '<p><q>He said "hi"</q></p>';
$result = trim($this->converter->convert($html));

$this->assertSame('"He said \\"hi\\""', $result);
$this->assertStringContainsString('He said "hi"', (new DjotConverter())->convert($result));
}

public function testQElementWithCite(): void
{
$html = '<p>As stated: <q cite="https://example.com">Quote here</q>.</p>';
Expand All @@ -1633,6 +1642,15 @@ public function testQElementWithCite(): void
$this->assertSame('As stated: ["Quote here"]{cite="https://example.com"}.', $result);
}

public function testQElementWithCiteEscapesInnerQuotes(): void
{
$html = '<p><q cite="https://example.com">He said "hi"</q></p>';
$result = trim($this->converter->convert($html));

$this->assertSame('["He said \\"hi\\""]{cite="https://example.com"}', $result);
$this->assertStringContainsString('He said "hi"', (new DjotConverter())->convert($result));
}

public function testSemanticSpanWithAdditionalAttributes(): void
{
$html = '<p>Press <kbd id="shortcut" class="key">Ctrl+S</kbd> to save.</p>';
Expand All @@ -1659,4 +1677,39 @@ public function testAbbrTitleWithQuotes(): void

$this->assertStringContainsString('[TBP]{abbr="The \\"Best\\" Practice"}', $result);
}

public function testSampElement(): void
{
$html = '<p>The output was <samp>Hello World</samp>.</p>';
$result = trim($this->converter->convert($html));

$this->assertSame('The output was [Hello World]{samp}.', $result);
}

public function testSampElementWithAttributes(): void
{
$html = '<p>Output: <samp class="output" id="result">Success</samp></p>';
$result = trim($this->converter->convert($html));

$this->assertStringContainsString('[Success]{samp', $result);
$this->assertStringContainsString('.output', $result);
$this->assertStringContainsString('#result', $result);
}

public function testVarElement(): void
{
$html = '<p>The variable <var>x</var> represents a number.</p>';
$result = trim($this->converter->convert($html));

$this->assertSame('The variable [x]{var} represents a number.', $result);
}

public function testVarElementWithAttributes(): void
{
$html = '<p>Set <var class="math">y</var> to 5.</p>';
$result = trim($this->converter->convert($html));

$this->assertStringContainsString('[y]{var', $result);
$this->assertStringContainsString('.math', $result);
}
}
Loading