tor-browser

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

browser_rules_highlight-used-fonts.js (4729B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests that a used font-family is highlighted in the rule-view.
      7 
      8 const TEST_URI = `
      9  <style type="text/css">
     10    @font-face {
     11      font-family: ostrich;
     12      font-weight: normal;
     13      src: url(${URL_ROOT_COM_SSL}ostrich-regular.ttf);
     14    }
     15 
     16    #id1 {
     17      font-family: foo, bar, sans-serif;
     18    }
     19    #id2 {
     20      font-family: serif;
     21    }
     22    #id3 {
     23      font-family: foo, monospace, monospace, serif;
     24    }
     25    #id4 {
     26      font-family: foo, bar;
     27    }
     28    #id5 {
     29      font-family: "monospace";
     30    }
     31    #id6 {
     32      font-family: georgia, arial;
     33    }
     34    #id7 {
     35      font-family: foo, serif !important;
     36    }
     37    #id8 {
     38      font-family: important;
     39    }
     40    #id9::before {
     41      content: 'before';
     42      font-family: foo, monospace;
     43    }
     44    #id10::before {
     45      content: ' ';
     46      font-family: monospace;
     47    }
     48    #id11 {
     49      font-family: ostrich, system-ui, monospace;
     50    }
     51  </style>
     52  <div id="id1">Text</div>
     53  <div id="id2">Text</div>
     54  <div id="id3">Text</div>
     55  <div id="id4">Text</div>
     56  <div id="id5">Text</div>
     57  <div id="id6">A &#586;</div>
     58  <div id="id7">Text</div>
     59  <div id="id8">Text</div>
     60  <div id="id9">Text</div>
     61  <div id="id10">Text</div>
     62  <div id="id11">Hello Wörld</div>
     63 `;
     64 
     65 // Tests that font-family properties in the rule-view correctly
     66 // indicates which font is in use.
     67 // Each entry in the test array should contain:
     68 // {
     69 //   baseSelector: the rule-view selector to look for font-family in
     70 //   nb: the number of fonts this property should have
     71 //   used: the indexes of all the fonts that should be highlighted, or null if none should
     72 //         be highlighted
     73 //   selectBeforePseudoElement: Whether the before pseudo element should be selectd or not
     74 // }
     75 const TESTS = [
     76  { baseSelector: "#id1", nb: 3, used: [2] }, // sans-serif
     77  { baseSelector: "#id2", nb: 1, used: [0] }, // serif
     78  { baseSelector: "#id3", nb: 4, used: [1] }, // monospace
     79  { baseSelector: "#id4", nb: 2, used: null },
     80  { baseSelector: "#id5", nb: 1, used: null }, // "monospace" (quotted) isn't a known font
     81  { baseSelector: "#id7", nb: 2, used: [1] }, // serif
     82  { baseSelector: "#id8", nb: 1, used: null },
     83  { baseSelector: "#id9", nb: 2, used: [1], selectBeforePseudoElement: true }, // monospace
     84  // content only has spaces and `white-space` is not set, so no character are being rendered
     85  { baseSelector: "#id10", nb: 1, used: null, selectBeforePseudoElement: true },
     86  { baseSelector: "#id11", nb: 3, used: [0, 1] }, // ostrich, system-ui
     87 ];
     88 
     89 if (Services.appinfo.OS !== "Linux") {
     90  // Both georgia and arial are used because the second character can't be rendered with
     91  // georgia, so the browser falls back. Also, skip this on Linux which has neither of
     92  // these fonts.
     93  TESTS.push({ baseSelector: "#id6", nb: 2, used: [0, 1] });
     94 }
     95 
     96 add_task(async function () {
     97  await addTab(
     98    "https://example.com/document-builder.sjs?html=" +
     99      encodeURIComponent(TEST_URI)
    100  );
    101  const { inspector, view } = await openRuleView();
    102 
    103  for (const { baseSelector, nb, used, selectBeforePseudoElement } of TESTS) {
    104    const onFontHighlighted = view.once("font-highlighted");
    105 
    106    if (selectBeforePseudoElement) {
    107      // Query the first children node to get the before pseudo element:
    108      const baseNode = await getNodeFront(baseSelector, inspector);
    109      const pseudoElement = (await inspector.walker.children(baseNode))
    110        .nodes[0];
    111      await selectNode(pseudoElement, inspector);
    112    } else {
    113      await selectNode(baseSelector, inspector);
    114    }
    115    await onFontHighlighted;
    116 
    117    const selector = !selectBeforePseudoElement
    118      ? baseSelector
    119      : `${baseSelector}::before`;
    120    info(`Looking for fonts in font-family property for: <${selector}>`);
    121 
    122    const prop = getRuleViewProperty(view, selector, "font-family").valueSpan;
    123    const fonts = prop.querySelectorAll(".ruleview-font-family");
    124 
    125    ok(fonts.length, "Fonts found in the property");
    126    is(fonts.length, nb, "Correct number of fonts found in the property");
    127 
    128    const highlighted = [...fonts].filter(span =>
    129      span.classList.contains("used-font")
    130    );
    131    const expectedHighlightedNb = used === null ? 0 : used.length;
    132    is(
    133      highlighted.length,
    134      expectedHighlightedNb,
    135      `Correct number of used fonts found for <${selector}>`
    136    );
    137 
    138    let highlightedIndex = 0;
    139    [...fonts].forEach((font, index) => {
    140      if (font === highlighted[highlightedIndex]) {
    141        is(
    142          index,
    143          used[highlightedIndex],
    144          `"${font.innerText}" is marked as used for <${selector}>`
    145        );
    146        highlightedIndex++;
    147      }
    148    });
    149  }
    150 });