Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / devtools / client / fronts / page-style.js
blobdfe9f593e452dbd5cf8c6fb753dde1ddb1104e59
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/. */
5 "use strict";
7 const {
8 FrontClassWithSpec,
9 registerFront,
10 } = require("resource://devtools/shared/protocol.js");
11 const {
12 pageStyleSpec,
13 } = require("resource://devtools/shared/specs/page-style.js");
15 /**
16 * PageStyleFront, the front object for the PageStyleActor
18 class PageStyleFront extends FrontClassWithSpec(pageStyleSpec) {
19 _attributesCache = new Map();
21 constructor(conn, targetFront, parentFront) {
22 super(conn, targetFront, parentFront);
23 this.inspector = this.getParent();
25 this._clearAttributesCache = this._clearAttributesCache.bind(this);
26 this.on("stylesheet-updated", this._clearAttributesCache);
27 this.walker.on("new-mutations", this._clearAttributesCache);
30 form(form) {
31 this._form = form;
34 get walker() {
35 return this.inspector.walker;
38 get supportsFontStretchLevel4() {
39 return this._form.traits && this._form.traits.fontStretchLevel4;
42 get supportsFontStyleLevel4() {
43 return this._form.traits && this._form.traits.fontStyleLevel4;
46 get supportsFontVariations() {
47 return this._form.traits && this._form.traits.fontVariations;
50 get supportsFontWeightLevel4() {
51 return this._form.traits && this._form.traits.fontWeightLevel4;
54 getMatchedSelectors(node, property, options) {
55 return super.getMatchedSelectors(node, property, options).then(ret => {
56 return ret.matched;
57 });
60 async getApplied(node, options = {}) {
61 const ret = await super.getApplied(node, options);
62 return ret.entries;
65 addNewRule(node, pseudoClasses) {
66 return super.addNewRule(node, pseudoClasses).then(ret => {
67 return ret.entries[0];
68 });
71 /**
72 * Get an array of existing attribute values in a node document, given an attribute type.
74 * @param {String} search: A string to filter attribute value on.
75 * @param {String} attributeType: The type of attribute we want to retrieve the values.
76 * @param {Element} node: The element we want to get possible attributes for. This will
77 * be used to get the document where the search is happening.
78 * @returns {Array<String>} An array of strings
80 async getAttributesInOwnerDocument(search, attributeType, node) {
81 if (!attributeType) {
82 throw new Error("`type` should not be empty");
85 if (!search) {
86 return [];
89 const lcFilter = search.toLowerCase();
91 // If the new filter includes the string that was used on our last trip to the server,
92 // we can filter the cached results instead of calling the server again.
93 if (
94 this._attributesCache &&
95 this._attributesCache.has(attributeType) &&
96 search.startsWith(this._attributesCache.get(attributeType).search)
97 ) {
98 const cachedResults = this._attributesCache
99 .get(attributeType)
100 .results.filter(item => item.toLowerCase().startsWith(lcFilter));
101 this.emitForTests(
102 "getAttributesInOwnerDocument-cache-hit",
103 cachedResults
105 return cachedResults;
108 const results = await super.getAttributesInOwnerDocument(
109 search,
110 attributeType,
111 node
113 this._attributesCache.set(attributeType, { search, results });
114 return results;
117 _clearAttributesCache() {
118 this._attributesCache.clear();
122 exports.PageStyleFront = PageStyleFront;
123 registerFront(PageStyleFront);