Skip to content

Commit 16a4ccf

Browse files
authored
report(dom): support code snippets within markdown links (#14121)
1 parent 1c324e7 commit 16a4ccf

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

report/renderer/dom.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ export class DOM {
133133
const element = this.createElement('span');
134134

135135
for (const segment of Util.splitMarkdownLink(text)) {
136+
const processedSegment = segment.text.includes('`') ?
137+
this.convertMarkdownCodeSnippets(segment.text) :
138+
segment.text;
139+
136140
if (!segment.isLink) {
137141
// Plain text segment.
138-
element.append(this._document.createTextNode(segment.text));
142+
element.append(processedSegment);
139143
continue;
140144
}
141145

@@ -151,7 +155,7 @@ export class DOM {
151155
const a = this.createElement('a');
152156
a.rel = 'noopener';
153157
a.target = '_blank';
154-
a.textContent = segment.text;
158+
a.append(processedSegment);
155159
this.safelySetHref(a, url.href);
156160
element.append(a);
157161
}

report/test/renderer/dom-test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ describe('DOM', () => {
9696
'and some text afterwards.', 'link with spaces in brackets');
9797
});
9898

99+
it('correctly converts code snippets', () => {
100+
let result = dom.convertMarkdownLinkSnippets(
101+
'Some `code`. [Learn more](http://example.com).');
102+
assert.equal(result.innerHTML,
103+
'<span>Some <code>code</code>. </span>' +
104+
'<a rel="noopener" target="_blank" href="http://example.com/">Learn more</a>.');
105+
106+
result = dom.convertMarkdownLinkSnippets(
107+
'[link with `code`](https://example.com/foo) and some text afterwards.');
108+
assert.equal(result.innerHTML,
109+
'<a rel="noopener" target="_blank" href="https://example.com/foo">' +
110+
'<span>link with <code>code</code></span>' +
111+
'</a> and some text afterwards.', 'link with code snippet inside');
112+
});
113+
99114
it('handles invalid urls', () => {
100115
const text = 'Text has [bad](https:///) link.';
101116
assert.throws(() => {
@@ -120,8 +135,9 @@ describe('DOM', () => {
120135
const text = 'Ensuring `<td>` cells using the `[headers]` are good. ' +
121136
'[Learn more](https://dequeuniversity.com/rules/axe/3.1/td-headers-attr).';
122137
const result = dom.convertMarkdownLinkSnippets(text);
123-
assert.equal(result.innerHTML, 'Ensuring `&lt;td&gt;` cells using the `[headers]` are ' +
124-
'good. <a rel="noopener" target="_blank" href="https://dequeuniversity.com/rules/axe/3.1/td-headers-attr">Learn more</a>.');
138+
assert.equal(result.innerHTML,
139+
'<span>Ensuring <code>&lt;td&gt;</code> cells using the <code>[headers]</code> are ' +
140+
'good. </span><a rel="noopener" target="_blank" href="https://dequeuniversity.com/rules/axe/3.1/td-headers-attr">Learn more</a>.');
125141
});
126142

127143
it('appends utm params to the URLs with https://developers.google.com origin', () => {

0 commit comments

Comments
 (0)