-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding audit for theme-color in HTML and moving to jsdom for testing …
…html elements
- Loading branch information
Matt Gaunt
committed
Mar 24, 2016
1 parent
eb9f9ea
commit a04ab43
Showing
10 changed files
with
206 additions
and
25 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
class ThemeColor { | ||
|
||
static get tags() { | ||
return ['HTML']; | ||
} | ||
|
||
static get description() { | ||
return 'Site has a theme-color meta tag'; | ||
} | ||
|
||
static audit(inputs) { | ||
let hasThemeColor = false; | ||
if (inputs.window) { | ||
const themeColorElements = | ||
inputs.window.document.querySelectorAll('head meta[name="theme-color"]'); | ||
|
||
if (themeColorElements.length === 1 && | ||
themeColorElements[0].getAttribute('content').length > 0) { | ||
hasThemeColor = true; | ||
} | ||
} | ||
|
||
return { | ||
value: hasThemeColor, | ||
tags: ThemeColor.tags, | ||
description: ThemeColor.description | ||
}; | ||
} | ||
} | ||
|
||
module.exports = ThemeColor; |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const Audit = require('../../../audits/html/theme-color.js'); | ||
const assert = require('assert'); | ||
const mockHtml = require('../../helpers/mock-html.js'); | ||
|
||
/* global describe, it*/ | ||
|
||
describe('HTML: theme-color audit', () => { | ||
it('fails when no window or html present', () => { | ||
return assert.equal(Audit.audit({}).value, false); | ||
}); | ||
|
||
it('fails when invalid HTML and window given', () => { | ||
return assert.equal(Audit.audit({ | ||
window: null, | ||
html: null | ||
}).value, false); | ||
}); | ||
|
||
it('fails when no theme-color is present in the html', () => { | ||
return assert.equal(Audit.audit(mockHtml()).value, false); | ||
}); | ||
|
||
it('fails when theme-color has no content value', () => { | ||
return assert.equal(Audit.audit( | ||
mockHtml('<meta name="theme-color" content="">')).value, false); | ||
}); | ||
|
||
it('fails when multiple theme-colors exist', () => { | ||
return assert.equal(Audit.audit( | ||
mockHtml(`<meta name="theme-color" content="#ffffff"> | ||
<meta name="theme-color" content="#ffffff">`)).value, false); | ||
}); | ||
|
||
it('fails when theme-color exists in the body', () => { | ||
return assert.equal(Audit.audit( | ||
mockHtml('', '<meta name="theme-color" content="#ffffff">') | ||
).value, false); | ||
}); | ||
|
||
it('succeeds when theme-color present in the html', () => { | ||
return assert.equal(Audit.audit( | ||
mockHtml('<meta name="theme-color" content="#ffffff">') | ||
).value, true); | ||
}); | ||
|
||
it('succeeds when theme-color present in the html with id', () => { | ||
return assert.equal(Audit.audit( | ||
mockHtml('<meta id="my-theme-color" name="theme-color" content="#ffffff">') | ||
).value, true); | ||
}); | ||
|
||
it('succeeds when theme-color has a CSS name content value', () => { | ||
return assert.equal(Audit.audit( | ||
mockHtml('<meta id="my-theme-color" name="theme-color" content="red">') | ||
).value, true); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const jsdom = require('jsdom').jsdom; | ||
|
||
module.exports = (headInsert, bodyInsert) => { | ||
headInsert = headInsert || ''; | ||
bodyInsert = bodyInsert || ''; | ||
|
||
const htmlString = `<!doctype html> | ||
<html> | ||
<head> | ||
${headInsert} | ||
<title>Sample page</title> | ||
</head> | ||
<body> | ||
${bodyInsert} | ||
</body> | ||
</html>`; | ||
|
||
const doc = jsdom(htmlString); | ||
|
||
return { | ||
window: doc.defaultView, | ||
html: htmlString | ||
}; | ||
}; |