summaryrefslogtreecommitdiffstats
path: root/devtools/client/fronts/page-style.js
blob: dfe9f593e452dbd5cf8c6fb753dde1ddb1104e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  FrontClassWithSpec,
  registerFront,
} = require("resource://devtools/shared/protocol.js");
const {
  pageStyleSpec,
} = require("resource://devtools/shared/specs/page-style.js");

/**
 * PageStyleFront, the front object for the PageStyleActor
 */
class PageStyleFront extends FrontClassWithSpec(pageStyleSpec) {
  _attributesCache = new Map();

  constructor(conn, targetFront, parentFront) {
    super(conn, targetFront, parentFront);
    this.inspector = this.getParent();

    this._clearAttributesCache = this._clearAttributesCache.bind(this);
    this.on("stylesheet-updated", this._clearAttributesCache);
    this.walker.on("new-mutations", this._clearAttributesCache);
  }

  form(form) {
    this._form = form;
  }

  get walker() {
    return this.inspector.walker;
  }

  get supportsFontStretchLevel4() {
    return this._form.traits && this._form.traits.fontStretchLevel4;
  }

  get supportsFontStyleLevel4() {
    return this._form.traits && this._form.traits.fontStyleLevel4;
  }

  get supportsFontVariations() {
    return this._form.traits && this._form.traits.fontVariations;
  }

  get supportsFontWeightLevel4() {
    return this._form.traits && this._form.traits.fontWeightLevel4;
  }

  getMatchedSelectors(node, property, options) {
    return super.getMatchedSelectors(node, property, options).then(ret => {
      return ret.matched;
    });
  }

  async getApplied(node, options = {}) {
    const ret = await super.getApplied(node, options);
    return ret.entries;
  }

  addNewRule(node, pseudoClasses) {
    return super.addNewRule(node, pseudoClasses).then(ret => {
      return ret.entries[0];
    });
  }

  /**
   * Get an array of existing attribute values in a node document, given an attribute type.
   *
   * @param {String} search: A string to filter attribute value on.
   * @param {String} attributeType: The type of attribute we want to retrieve the values.
   * @param {Element} node: The element we want to get possible attributes for. This will
   *        be used to get the document where the search is happening.
   * @returns {Array<String>} An array of strings
   */
  async getAttributesInOwnerDocument(search, attributeType, node) {
    if (!attributeType) {
      throw new Error("`type` should not be empty");
    }

    if (!search) {
      return [];
    }

    const lcFilter = search.toLowerCase();

    // If the new filter includes the string that was used on our last trip to the server,
    // we can filter the cached results instead of calling the server again.
    if (
      this._attributesCache &&
      this._attributesCache.has(attributeType) &&
      search.startsWith(this._attributesCache.get(attributeType).search)
    ) {
      const cachedResults = this._attributesCache
        .get(attributeType)
        .results.filter(item => item.toLowerCase().startsWith(lcFilter));
      this.emitForTests(
        "getAttributesInOwnerDocument-cache-hit",
        cachedResults
      );
      return cachedResults;
    }

    const results = await super.getAttributesInOwnerDocument(
      search,
      attributeType,
      node
    );
    this._attributesCache.set(attributeType, { search, results });
    return results;
  }

  _clearAttributesCache() {
    this._attributesCache.clear();
  }
}

exports.PageStyleFront = PageStyleFront;
registerFront(PageStyleFront);