Skip to content

report(dom): support code snippets within markdown links #14121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2022
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
8 changes: 6 additions & 2 deletions report/renderer/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ export class DOM {
const element = this.createElement('span');

for (const segment of Util.splitMarkdownLink(text)) {
const processedSegment = segment.text.includes('`') ?
this.convertMarkdownCodeSnippets(segment.text) :
segment.text;

if (!segment.isLink) {
// Plain text segment.
element.append(this._document.createTextNode(segment.text));
element.append(processedSegment);
continue;
}

Expand All @@ -151,7 +155,7 @@ export class DOM {
const a = this.createElement('a');
a.rel = 'noopener';
a.target = '_blank';
a.textContent = segment.text;
a.append(processedSegment);
this.safelySetHref(a, url.href);
element.append(a);
}
Expand Down
20 changes: 18 additions & 2 deletions report/test/renderer/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ describe('DOM', () => {
'and some text afterwards.', 'link with spaces in brackets');
});

it('correctly converts code snippets', () => {
let result = dom.convertMarkdownLinkSnippets(
'Some `code`. [Learn more](http://example.com).');
assert.equal(result.innerHTML,
'<span>Some <code>code</code>. </span>' +
'<a rel="noopener" target="_blank" href="http://example.com/">Learn more</a>.');

result = dom.convertMarkdownLinkSnippets(
'[link with `code`](https://example.com/foo) and some text afterwards.');
assert.equal(result.innerHTML,
'<a rel="noopener" target="_blank" href="https://example.com/foo">' +
'<span>link with <code>code</code></span>' +
'</a> and some text afterwards.', 'link with code snippet inside');
});

it('handles invalid urls', () => {
const text = 'Text has [bad](https:///) link.';
assert.throws(() => {
Expand All @@ -120,8 +135,9 @@ describe('DOM', () => {
const text = 'Ensuring `<td>` cells using the `[headers]` are good. ' +
'[Learn more](https://dequeuniversity.com/rules/axe/3.1/td-headers-attr).';
const result = dom.convertMarkdownLinkSnippets(text);
assert.equal(result.innerHTML, 'Ensuring `&lt;td&gt;` cells using the `[headers]` are ' +
'good. <a rel="noopener" target="_blank" href="https://dequeuniversity.com/rules/axe/3.1/td-headers-attr">Learn more</a>.');
assert.equal(result.innerHTML,
'<span>Ensuring <code>&lt;td&gt;</code> cells using the <code>[headers]</code> are ' +
'good. </span><a rel="noopener" target="_blank" href="https://dequeuniversity.com/rules/axe/3.1/td-headers-attr">Learn more</a>.');
});

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