-
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.
- Loading branch information
Showing
7 changed files
with
365 additions
and
0 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
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,147 @@ | ||
/** | ||
* @license Copyright 2018 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'; | ||
|
||
const Audit = require('../audit'); | ||
const URL = require('../../lib/url-shim'); | ||
|
||
const JAVA_APPLET_TYPE = 'application/x-java-applet'; | ||
const JAVA_BEAN_TYPE = 'application/x-java-bean'; | ||
const TYPE_BLOCKLIST = new Set([ | ||
'application/x-shockwave-flash', | ||
// See https://docs.oracle.com/cd/E19683-01/816-0378/using_tags/index.html | ||
JAVA_APPLET_TYPE, | ||
JAVA_BEAN_TYPE, | ||
// See https://msdn.microsoft.com/es-es/library/cc265156(v=vs.95).aspx | ||
'application/x-silverlight', | ||
'application/x-silverlight-2', | ||
]); | ||
const FILE_EXTENSION_BLOCKLIST = new Set([ | ||
'swf', | ||
'flv', | ||
'class', | ||
'xap', | ||
]); | ||
const SOURCE_PARAMS = new Set([ | ||
'code', | ||
'movie', | ||
'source', | ||
'src', | ||
]); | ||
|
||
/** | ||
* Verifies if given MIME type matches any known plugin MIME type | ||
* @param {string} type | ||
*/ | ||
function isPluginType(type) { | ||
type = type.trim().toLowerCase(); | ||
|
||
return TYPE_BLOCKLIST.has(type) || | ||
type.startsWith(JAVA_APPLET_TYPE) || // e.g. "application/x-java-applet;jpi-version=1.4" | ||
type.startsWith(JAVA_BEAN_TYPE); | ||
} | ||
|
||
/** | ||
* Verifies if given url points to a file that has a known plugin extension | ||
* @param {string} url | ||
*/ | ||
function isPluginURL(url) { | ||
try { | ||
// in order to support relative URLs we need to provied a base URL | ||
const filePath = new URL(url, 'http://example.com').pathname; | ||
const parts = filePath.split('.'); | ||
|
||
return parts.length > 1 && FILE_EXTENSION_BLOCKLIST.has(parts.pop().trim().toLowerCase()); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
class Plugins extends Audit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
name: 'plugins', | ||
description: 'Document avoids plugins.', | ||
failureDescription: 'Document uses plugins', | ||
helpText: 'Some types of media or content are not playable on mobile devices. ' + | ||
'[Learn more](https://developers.google.com/speed/docs/insights/AvoidPlugins).', | ||
requiredArtifacts: ['EmbeddedContent'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
const plugins = artifacts.EmbeddedContent | ||
.filter(item => { | ||
if (item.tagName === 'APPLET') { | ||
return true; | ||
} | ||
|
||
if ( | ||
(item.tagName === 'EMBED' || item.tagName === 'OBJECT') && | ||
item.type && | ||
isPluginType(item.type) | ||
) { | ||
return true; | ||
} | ||
|
||
const embedSrc = item.src || item.code; | ||
if (item.tagName === 'EMBED' && embedSrc && isPluginURL(embedSrc)) { | ||
return true; | ||
} | ||
|
||
if (item.tagName === 'OBJECT' && item.data && isPluginURL(item.data)) { | ||
return true; | ||
} | ||
|
||
const failingParams = item.params.filter(param => | ||
SOURCE_PARAMS.has(param.name.trim().toLowerCase()) && isPluginURL(param.value) | ||
); | ||
|
||
return failingParams.length > 0; | ||
}) | ||
.map(plugin => { | ||
const tagName = plugin.tagName.toLowerCase(); | ||
const attributes = ['src', 'data', 'code', 'type'] | ||
.reduce((result, attr) => { | ||
if (plugin[attr] !== null) { | ||
result += ` ${attr}="${plugin[attr]}"`; | ||
} | ||
return result; | ||
}, ''); | ||
const params = plugin.params | ||
.filter(param => SOURCE_PARAMS.has(param.name.trim().toLowerCase())) | ||
.map(param => `<param ${param.name}="${param.value}" />`) | ||
.join(''); | ||
|
||
return { | ||
source: { | ||
type: 'node', | ||
snippet: `<${tagName}${attributes}>${params}</${tagName}>`, | ||
}, | ||
}; | ||
}); | ||
|
||
const headings = [ | ||
{key: 'source', itemType: 'code', text: 'Source'}, | ||
]; | ||
|
||
const details = Audit.makeTableDetails(headings, plugins); | ||
|
||
return { | ||
rawValue: plugins.length === 0, | ||
details, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Plugins; |
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,41 @@ | ||
/** | ||
* @license Copyright 2018 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'; | ||
|
||
const Gatherer = require('../gatherer'); | ||
const DOMHelpers = require('../../../lib/dom-helpers.js'); | ||
|
||
class EmbeddedContent extends Gatherer { | ||
/** | ||
* @param {{driver: !Driver}} options Run options | ||
* @return {!Promise<Array<{tagName: string, type: ?string, src: ?string, data: ?string, code: ?string, params: Array<{name: string, value: string}>}>>} All <object>s, <embed>s and <applet>s with list of relevant attributes and child properties | ||
*/ | ||
afterPass(options) { | ||
const expression = `(function() { | ||
${DOMHelpers.getElementsInDocumentFnString}; // define function on page | ||
const selector = 'object, embed, applet'; | ||
const elements = getElementsInDocument(selector); | ||
return elements | ||
.map(node => ({ | ||
tagName: node.tagName, | ||
type: node.getAttribute('type'), | ||
src: node.getAttribute('src'), | ||
data: node.getAttribute('data'), | ||
code: node.getAttribute('code'), | ||
params: Array.from(node.children) | ||
.filter(el => el.tagName === 'PARAM') | ||
.map(el => ({ | ||
name: el.getAttribute('name') || '', | ||
value: el.getAttribute('value') || '', | ||
})), | ||
})); | ||
})()`; | ||
|
||
return options.driver.evaluateAsync(expression); | ||
} | ||
} | ||
|
||
module.exports = EmbeddedContent; |
Oops, something went wrong.