css-properties.js (2812B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 cssPropertiesSpec, 10 } = require("resource://devtools/shared/specs/css-properties.js"); 11 12 loader.lazyRequireGetter( 13 this, 14 "CSS_TYPES", 15 "resource://devtools/shared/css/constants.js", 16 true 17 ); 18 19 class CssPropertiesActor extends Actor { 20 constructor(conn, targetActor) { 21 super(conn, cssPropertiesSpec); 22 this.targetActor = targetActor; 23 } 24 25 getCSSDatabase() { 26 const properties = generateCssProperties(this.targetActor.window.document); 27 28 return { properties }; 29 } 30 } 31 exports.CssPropertiesActor = CssPropertiesActor; 32 33 /** 34 * Generate the CSS properties object. Every key is the property name, while 35 * the values are objects that contain information about that property. 36 * 37 * @param {Document} doc 38 * @return {object} 39 */ 40 function generateCssProperties(doc) { 41 const properties = {}; 42 const propertyNames = InspectorUtils.getCSSPropertyNames({ 43 includeAliases: true, 44 }); 45 46 for (const name of propertyNames) { 47 // Get the list of CSS types this property supports. 48 const supports = []; 49 for (const type in CSS_TYPES) { 50 if (safeCssPropertySupportsType(name, type)) { 51 supports.push(type); 52 } 53 } 54 55 const values = InspectorUtils.getCSSValuesForProperty(name); 56 const subproperties = InspectorUtils.getSubpropertiesForCSSProperty(name); 57 58 properties[name] = { 59 isInherited: InspectorUtils.isInheritedProperty(doc, name), 60 values, 61 supports, 62 subproperties, 63 }; 64 } 65 66 return properties; 67 } 68 exports.generateCssProperties = generateCssProperties; 69 70 /** 71 * Test if a CSS is property is known using server-code. 72 * 73 * @param {string} name 74 * @return {boolean} 75 */ 76 function isCssPropertyKnown(name) { 77 try { 78 // If the property name is unknown, the cssPropertyIsShorthand 79 // will throw an exception. But if it is known, no exception will 80 // be thrown; so we just ignore the return value. 81 InspectorUtils.cssPropertyIsShorthand(name); 82 return true; 83 } catch (e) { 84 return false; 85 } 86 } 87 88 exports.isCssPropertyKnown = isCssPropertyKnown; 89 90 /** 91 * A wrapper for InspectorUtils.cssPropertySupportsType that ignores invalid 92 * properties. 93 * 94 * @param {string} name The property name. 95 * @param {number} type The type tested for support. 96 * @return {boolean} Whether the property supports the type. 97 * If the property is unknown, false is returned. 98 */ 99 function safeCssPropertySupportsType(name, type) { 100 try { 101 return InspectorUtils.cssPropertySupportsType(name, type); 102 } catch (e) { 103 return false; 104 } 105 }