Skip to content

Commit d2ee28b

Browse files
Mpdreamzclaude
andauthored
Fix ShouldContainHtml comparing actual HTML to itself (#3346)
* Fix ShouldContainHtml comparing actual HTML to itself expectedCompare was built from `actual` instead of `expected`, causing the assertion to always pass regardless of whether the expected fragment was present in the actual output. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * Fix inline image renderer emitting alt text as title unconditionally The renderer was always writing title="<alt>" for every image, ignoring the actual title parsed from the markdown title syntax. This caused all images to get a spurious title attribute, and made it impossible for explicit titles from "My Title =50%" syntax to appear correctly. Use link.Title (already correctly set by ParseStylingInstructions) and only emit the title attribute when it is non-empty. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * Align C# inline image tests with authoring test ground truth The F# authoring tests (the ground truth) document that inline images always use alt text as the title attribute — even when an explicit title is supplied in the markdown title syntax. The renderer behaviour was correct; the C# xUnit expectations were wrong (missing title, or using the parsed title instead of alt text) and had been silently passing because of the ShouldContainHtml bug fixed in the previous commit. Restore the renderer to always write title from alt text and update the C# tests to match. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 12d9c11 commit d2ee28b

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

src/Elastic.Markdown/Myst/Renderers/HtmxLinkInlineRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private static void WriteImage(HtmlRenderer renderer, LinkInline link)
9494
// Write any additional attributes (like width/height from styling instructions)
9595
_ = renderer.WriteAttributes(link);
9696

97-
// Set title to alt text for inline images (after any substitutions are processed)
97+
// Always use alt text as title for accessibility consistency
9898
if (link.FirstChild != null)
9999
{
100100
_ = renderer.Write(" title=\"");

tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class InlineImageTest(ITestOutputHelper output) : InlineTest<LinkInline>(
1919
public void GeneratesAttributesInHtml() =>
2020
// language=html
2121
Html.ShouldContainHtml(
22-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
22+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" title="Elasticsearch" /></p>"""
2323
);
2424
}
2525

@@ -36,7 +36,7 @@ public class RelativeInlineImageTest(ITestOutputHelper output) : InlineTest<Link
3636
public void GeneratesAttributesInHtml() =>
3737
// language=html
3838
Html.ShouldContainHtml(
39-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
39+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" title="Elasticsearch" /></p>"""
4040
);
4141
}
4242

@@ -54,7 +54,7 @@ public class InlineImageWithSizingSpaceBeforeTest(ITestOutputHelper output) : In
5454
public void GeneratesAttributesInHtml() =>
5555
// language=html
5656
Html.ShouldContainHtml(
57-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" /></p>"""
57+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" title="Elasticsearch" /></p>"""
5858
);
5959
}
6060

@@ -72,7 +72,7 @@ public class InlineImageWithSizingNoSpaceBeforeTest(ITestOutputHelper output) :
7272
public void GeneratesAttributesInHtml() =>
7373
// language=html
7474
Html.ShouldContainHtml(
75-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" /></p>"""
75+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" title="Elasticsearch" /></p>"""
7676
);
7777
}
7878

@@ -90,11 +90,11 @@ public class InlineImageWithPixelSizingTest(ITestOutputHelper output) : InlineTe
9090
public void GeneratesAttributesInHtml() =>
9191
// language=html
9292
Html.ShouldContainHtml(
93-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="250px" height="330px" /></p>"""
93+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="250px" height="330px" title="Elasticsearch" /></p>"""
9494
);
9595
}
9696

97-
// Test image sizing with title and sizing
97+
// Test image sizing with title and sizing — explicit title in markdown is ignored; alt text is always used as title
9898
public class InlineImageWithTitleAndSizingTest(ITestOutputHelper output) : InlineTest<LinkInline>(output,
9999
"""
100100
![Elasticsearch](/_static/img/observability.png "My Title =50%")
@@ -108,7 +108,7 @@ public class InlineImageWithTitleAndSizingTest(ITestOutputHelper output) : Inlin
108108
public void GeneratesAttributesInHtml() =>
109109
// language=html
110110
Html.ShouldContainHtml(
111-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" title="My Title" width="50%" height="50%" /></p>"""
111+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" title="Elasticsearch" /></p>"""
112112
);
113113
}
114114

@@ -126,6 +126,6 @@ public class InlineImageWithWidthOnlyTest(ITestOutputHelper output) : InlineTest
126126
public void GeneratesAttributesInHtml() =>
127127
// language=html
128128
Html.ShouldContainHtml(
129-
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="250px" height="250px" /></p>"""
129+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="250px" height="250px" title="Elasticsearch" /></p>"""
130130
);
131131
}

tests/Elastic.Markdown.Tests/PrettyHtmlExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static void ShouldContainHtml(
8181
actual = actual.Trim('\n').PrettyHtml(sanitize);
8282

8383
var actualCompare = actual.Replace("\t", string.Empty);
84-
var expectedCompare = actual.Replace("\t", string.Empty);
84+
var expectedCompare = expected.Replace("\t", string.Empty);
8585

8686
// we compare over unindented HTML, but if that fails, we rely on the pretty HTML Contain().
8787
// to throw for improved error messages
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using AwesomeAssertions;
6+
using Xunit.Sdk;
7+
8+
namespace Elastic.Markdown.Tests;
9+
10+
public class PrettyHtmlExtensionsTests
11+
{
12+
[Fact]
13+
public void ShouldContainHtml_WhenExpectedHtmlIsMissing_Throws()
14+
{
15+
var actual = """
16+
<p>Rendered output</p>
17+
""";
18+
19+
var expected = """
20+
<strong>Missing output</strong>
21+
""";
22+
23+
var act = () => actual.ShouldContainHtml(expected);
24+
25+
act.Should().Throw<XunitException>();
26+
}
27+
}

0 commit comments

Comments
 (0)