tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

page-style.js (3581B)


      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 {
      8  FrontClassWithSpec,
      9  registerFront,
     10 } = require("resource://devtools/shared/protocol.js");
     11 const {
     12  pageStyleSpec,
     13 } = require("resource://devtools/shared/specs/page-style.js");
     14 
     15 /**
     16 * PageStyleFront, the front object for the PageStyleActor
     17 */
     18 class PageStyleFront extends FrontClassWithSpec(pageStyleSpec) {
     19  _attributesCache = new Map();
     20 
     21  constructor(conn, targetFront, parentFront) {
     22    super(conn, targetFront, parentFront);
     23    this.inspector = this.getParent();
     24 
     25    this._clearAttributesCache = this._clearAttributesCache.bind(this);
     26    this.on("stylesheet-updated", this._clearAttributesCache);
     27    this.walker.on("new-mutations", this._clearAttributesCache);
     28  }
     29 
     30  form(form) {
     31    this._form = form;
     32  }
     33 
     34  get walker() {
     35    return this.inspector.walker;
     36  }
     37 
     38  get traits() {
     39    return this._form.traits;
     40  }
     41 
     42  get supportsFontStretchLevel4() {
     43    return this._form.traits && this._form.traits.fontStretchLevel4;
     44  }
     45 
     46  get supportsFontStyleLevel4() {
     47    return this._form.traits && this._form.traits.fontStyleLevel4;
     48  }
     49 
     50  get supportsFontVariations() {
     51    return this._form.traits && this._form.traits.fontVariations;
     52  }
     53 
     54  get supportsFontWeightLevel4() {
     55    return this._form.traits && this._form.traits.fontWeightLevel4;
     56  }
     57 
     58  getMatchedSelectors(node, property, options) {
     59    return super.getMatchedSelectors(node, property, options).then(ret => {
     60      return ret.matched;
     61    });
     62  }
     63 
     64  async getApplied(node, options = {}) {
     65    const ret = await super.getApplied(node, options);
     66    return ret.entries;
     67  }
     68 
     69  addNewRule(node, pseudoClasses) {
     70    return super.addNewRule(node, pseudoClasses).then(ret => {
     71      return ret.entries[0];
     72    });
     73  }
     74 
     75  /**
     76   * Get an array of existing attribute values in a node document, given an attribute type.
     77   *
     78   * @param {string} search: A string to filter attribute value on.
     79   * @param {string} attributeType: The type of attribute we want to retrieve the values.
     80   * @param {Element} node: The element we want to get possible attributes for. This will
     81   *        be used to get the document where the search is happening.
     82   * @returns {Array<string>} An array of strings
     83   */
     84  async getAttributesInOwnerDocument(search, attributeType, node) {
     85    if (!attributeType) {
     86      throw new Error("`type` should not be empty");
     87    }
     88 
     89    if (!search) {
     90      return [];
     91    }
     92 
     93    const lcFilter = search.toLowerCase();
     94 
     95    // If the new filter includes the string that was used on our last trip to the server,
     96    // we can filter the cached results instead of calling the server again.
     97    if (
     98      this._attributesCache &&
     99      this._attributesCache.has(attributeType) &&
    100      search.startsWith(this._attributesCache.get(attributeType).search)
    101    ) {
    102      const cachedResults = this._attributesCache
    103        .get(attributeType)
    104        .results.filter(item => item.toLowerCase().startsWith(lcFilter));
    105      this.emitForTests(
    106        "getAttributesInOwnerDocument-cache-hit",
    107        cachedResults
    108      );
    109      return cachedResults;
    110    }
    111 
    112    const results = await super.getAttributesInOwnerDocument(
    113      search,
    114      attributeType,
    115      node
    116    );
    117    this._attributesCache.set(attributeType, { search, results });
    118    return results;
    119  }
    120 
    121  _clearAttributesCache() {
    122    this._attributesCache.clear();
    123  }
    124 }
    125 
    126 registerFront(PageStyleFront);