-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Generate DOM for markdown links in help text #1979
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
719ff97
Generate DOM for markdown links in help text
ebidel 2cb25c4
Add test
ebidel f3ad0be
Feedback
ebidel 23715f8
Check for valid url
ebidel 692cdd0
Node doesnt have URL
ebidel ec93a12
Allow to throw if invalid url
ebidel 56b96e5
Verify invalid url throws
ebidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,16 +20,87 @@ | |
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const jsdom = require('jsdom'); | ||
const URL = require('../../../lib/url-shim'); | ||
const ReportRenderer = require('../../../report/v2/report-renderer.js'); | ||
const sampleResults = require('../../results/sample_v2.json'); | ||
|
||
const TEMPLATE_FILE = fs.readFileSync(__dirname + '/../../../report/v2/templates.html', 'utf8'); | ||
|
||
describe('ReportRenderer V2', () => { | ||
describe('renderReport', () => { | ||
const document = jsdom.jsdom(TEMPLATE_FILE); | ||
const renderer = new ReportRenderer(document); | ||
before(() => { | ||
global.URL = URL; | ||
}); | ||
|
||
after(() => { | ||
global.URL = undefined; | ||
}); | ||
|
||
const document = jsdom.jsdom(TEMPLATE_FILE); | ||
const renderer = new ReportRenderer(document); | ||
|
||
describe('createElement', () => { | ||
it('creates a simple element using default values', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's new and what's not is very confusing when you move things around :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like to keep you on your toes. |
||
const el = renderer._createElement('div'); | ||
assert.equal(el.localName, 'div'); | ||
assert.equal(el.className, ''); | ||
assert.equal(el.className, el.attributes.length); | ||
}); | ||
|
||
it('creates an element from parameters', () => { | ||
const el = renderer._createElement( | ||
'div', 'class1 class2', {title: 'title attr', tabindex: 0}); | ||
assert.equal(el.localName, 'div'); | ||
assert.equal(el.className, 'class1 class2'); | ||
assert.equal(el.getAttribute('title'), 'title attr'); | ||
assert.equal(el.getAttribute('tabindex'), '0'); | ||
}); | ||
}); | ||
|
||
describe('cloneTemplate', () => { | ||
it('should clone a template', () => { | ||
const clone = renderer._cloneTemplate('#tmpl-lighthouse-audit-score'); | ||
assert.ok(clone.querySelector('.lighthouse-score')); | ||
}); | ||
|
||
it('fails when template cannot be found', () => { | ||
assert.throws(() => renderer._cloneTemplate('#unknown-selector')); | ||
}); | ||
}); | ||
|
||
describe('_convertMarkdownLinksToElement', () => { | ||
it('correctly converts links', () => { | ||
let result = renderer._convertMarkdownLinksToElement( | ||
'Some [link](https://example.com/foo). [Learn more](http://example.com).'); | ||
assert.equal(result.innerHTML, | ||
'Some <a rel="noopener" target="_blank" href="https://example.com/foo">link</a>. ' + | ||
'<a rel="noopener" target="_blank" href="http://example.com/">Learn more</a>.'); | ||
|
||
result = renderer._convertMarkdownLinksToElement('[link](https://example.com/foo)'); | ||
assert.equal(result.innerHTML, | ||
'<a rel="noopener" target="_blank" href="https://example.com/foo">link</a>', | ||
'just a link'); | ||
|
||
result = renderer._convertMarkdownLinksToElement( | ||
'[ Link ](https://example.com/foo) and some text afterwards.'); | ||
assert.equal(result.innerHTML, | ||
'<a rel="noopener" target="_blank" href="https://example.com/foo"> Link </a> ' + | ||
'and some text afterwards.', 'link with spaces in brackets'); | ||
}); | ||
|
||
it('handles invalid urls', () => { | ||
const text = 'Text has [bad](https:///) link.'; | ||
const result = renderer._convertMarkdownLinksToElement(text); | ||
assert.equal(result.innerHTML, 'Text has bad link.'); | ||
}); | ||
|
||
it('ignores links that do not start with http', () => { | ||
const text = 'Sentence with [link](/local/path).'; | ||
const result = renderer._convertMarkdownLinksToElement(text); | ||
assert.equal(result.innerHTML, text); | ||
}); | ||
}); | ||
|
||
describe('renderReport', () => { | ||
it('should render a report', () => { | ||
const output = renderer.renderReport(sampleResults); | ||
assert.ok(output.classList.contains('lighthouse-report')); | ||
|
@@ -44,35 +115,6 @@ describe('ReportRenderer V2', () => { | |
assert.ok(output.classList.contains('lighthouse-exception')); | ||
}); | ||
|
||
describe('createElement', () => { | ||
it('creates a simple element using default values', () => { | ||
const el = renderer._createElement('div'); | ||
assert.equal(el.localName, 'div'); | ||
assert.equal(el.className, ''); | ||
assert.equal(el.className, el.attributes.length); | ||
}); | ||
|
||
it('creates an element from parameters', () => { | ||
const el = renderer._createElement( | ||
'div', 'class1 class2', {title: 'title attr', tabindex: 0}); | ||
assert.equal(el.localName, 'div'); | ||
assert.equal(el.className, 'class1 class2'); | ||
assert.equal(el.getAttribute('title'), 'title attr'); | ||
assert.equal(el.getAttribute('tabindex'), '0'); | ||
}); | ||
}); | ||
|
||
describe('cloneTemplate', () => { | ||
it('should clone a template', () => { | ||
const clone = renderer._cloneTemplate('#tmpl-lighthouse-audit-score'); | ||
assert.ok(clone.querySelector('.lighthouse-score')); | ||
}); | ||
|
||
it('fails when template cannot be found', () => { | ||
assert.throws(() => renderer._cloneTemplate('#unknown-selector')); | ||
}); | ||
}); | ||
|
||
it('renders an audit', () => { | ||
const audit = sampleResults.reportCategories[0].audits[0]; | ||
const auditDOM = renderer._renderAudit(audit); | ||
|
@@ -82,7 +124,7 @@ describe('ReportRenderer V2', () => { | |
const score = auditDOM.querySelector('.lighthouse-score__value'); | ||
|
||
assert.equal(title.textContent, audit.result.description); | ||
assert.equal(description.textContent, audit.result.helpText); | ||
assert.ok(description.querySelector('a'), 'audit help text contains coverted markdown links'); | ||
assert.equal(score.textContent, '0'); | ||
assert.ok(score.classList.contains('lighthouse-score__value--fail')); | ||
assert.ok(score.classList.contains(`lighthouse-score__value--${audit.result.scoringMode}`)); | ||
|
@@ -102,7 +144,7 @@ describe('ReportRenderer V2', () => { | |
'category score is numeric'); | ||
assert.equal(value.textContent, Math.round(category.score), 'category score is rounded'); | ||
assert.equal(title.textContent, category.name, 'title is set'); | ||
assert.equal(description.textContent, category.description, 'description is set'); | ||
assert.ok(description.querySelector('a'), 'description contains converted markdown links'); | ||
|
||
const audits = categoryDOM.querySelectorAll('.lighthouse-category > .lighthouse-audit'); | ||
assert.equal(audits.length, category.audits.length, 'renders correct number of audits'); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we fail silently here? Seems like a big enough mistake that it should complain loudly and end up in
_renderException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to fail silently here. If a link is busted for some reason, it shouldn't block the whole report from being generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but why is the link busted? Right now, at least, they're all coming from helpText. An error in one of those should be fixed immediately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
FWIW, b/c of the regex, the only time we'd get an invalid URL is if someone wrote
https://
. or used something crazy in help text like[boom](🍣🍺)
.