Skip to content
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

core: use cssstyle to parse CSS colors instead of WebInspector #6091

Merged
merged 1 commit into from
Sep 22, 2018
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
12 changes: 10 additions & 2 deletions lighthouse-core/audits/themed-omnibox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
'use strict';

const MultiCheckAudit = require('./multi-check-audit');
const validColor = require('../lib/web-inspector').Color.parse;
const ManifestValues = require('../gather/computed/manifest-values');
const cssParsers = require('cssstyle/lib/parsers');

/**
* @fileoverview
Expand All @@ -34,14 +34,22 @@ class ThemedOmnibox extends MultiCheckAudit {
};
}

/**
* @param {string} color
* @return {boolean}
*/
static isValidColor(color) {
return cssParsers.valueType(color) === cssParsers.TYPES.COLOR;
}

/**
* @param {LH.Artifacts['ThemeColor']} themeColorMeta
* @param {Array<string>} failures
*/
static assessMetaThemecolor(themeColorMeta, failures) {
if (themeColorMeta === null) {
failures.push('No `<meta name="theme-color">` tag found');
} else if (!validColor(themeColorMeta)) {
} else if (!ThemedOmnibox.isValidColor(themeColorMeta)) {
failures.push('The theme-color meta tag did not contain a valid CSS color');
}
}
Expand Down
15 changes: 11 additions & 4 deletions lighthouse-core/lib/manifest-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const URL = require('./url-shim');
const validateColor = require('./web-inspector').Color.parse;
const cssParsers = require('cssstyle/lib/parsers');

const ALLOWED_DISPLAY_VALUES = [
'fullscreen',
Expand All @@ -31,6 +31,14 @@ const ALLOWED_ORIENTATION_VALUES = [
'landscape-secondary',
];

/**
* @param {string} color
* @return {boolean}
*/
function isValidColor(color) {
return cssParsers.valueType(color) === cssParsers.TYPES.COLOR;
}

/**
* @param {*} raw
* @param {boolean=} trim
Expand Down Expand Up @@ -66,9 +74,8 @@ function parseColor(raw) {
return color;
}

// Use DevTools's color parser to check CSS3 Color parsing.
const validatedColor = validateColor(color.raw);
if (!validatedColor) {
// Use color parser to check CSS3 Color parsing.
if (!isValidColor(color.raw)) {
color.value = undefined;
color.warning = 'ERROR: color parsing failed.';
}
Expand Down
3 changes: 0 additions & 3 deletions lighthouse-core/lib/web-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ module.exports = (function() {
Log: 'log',
};

// Dependencies for color parsing.
require('chrome-devtools-frontend/front_end/common/Color.js');

// Dependencies for effective CSS rule calculation.
require('chrome-devtools-frontend/front_end/common/TextRange.js');
require('chrome-devtools-frontend/front_end/sdk/CSSMatchedStyles.js');
Expand Down
1 change: 0 additions & 1 deletion lighthouse-core/test/lib/web-inspector-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const assert = require('assert');
describe('Web Inspector lib', function() {
it('WebInspector exported is the real one', () => {
assert.equal(typeof WebInspector, 'object');
assert.ok(WebInspector.Color);
assert.ok(WebInspector.CSSMetadata);
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"chrome-devtools-frontend": "1.0.422034",
"chrome-launcher": "^0.10.4",
"configstore": "^3.1.1",
"cssstyle": "1.1.1",
"devtools-timeline-model": "1.1.6",
"esprima": "^4.0.1",
"http-link-header": "^0.8.0",
Expand Down
23 changes: 23 additions & 0 deletions typings/cssstyle/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @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.
*/

declare module 'cssstyle/lib/parsers' {
interface TYPES {
INTEGER: 1;
NUMBER: 2;
LENGTH: 3;
PERCENT: 4;
URL: 5;
COLOR: 6;
STRING: 7;
ANGLE: 8;
KEYWORD: 9;
NULL_OR_EMPTY_STR: 10;
}

export var TYPES: TYPES;
export function valueType(val: any): TYPES[keyof TYPES] | undefined;
}
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,12 @@ cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0":
version "0.3.2"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.2.tgz#b8036170c79f07a90ff2f16e22284027a243848b"

cssstyle@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-1.1.1.tgz#18b038a9c44d65f7a8e428a653b9f6fe42faf5fb"
dependencies:
cssom "0.3.x"

"cssstyle@>= 0.2.37 < 0.3.0":
version "0.2.37"
resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-0.2.37.tgz#541097234cb2513c83ceed3acddc27ff27987d54"
Expand Down