tor-browser

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

browser_rules_add-property_02.js (6372B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test adding a valid property to a CSS rule, and navigating through the fields
      7 // by pressing ENTER.
      8 
      9 const TEST_URI = `
     10  <style type="text/css">
     11    #testid {
     12      color: blue;
     13    }
     14  </style>
     15  <div id='testid'>Styled Node</div>
     16 `;
     17 
     18 add_task(async function () {
     19  await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
     20  const { inspector, view } = await openRuleView();
     21  await selectNode("#testid", inspector);
     22 
     23  info("Focus the new property name field");
     24  const ruleEditor = getRuleViewRuleEditor(view, 1);
     25  let editor = await focusNewRuleViewProperty(ruleEditor);
     26  const input = editor.input;
     27 
     28  is(
     29    inplaceEditor(ruleEditor.newPropSpan),
     30    editor,
     31    "Next focused editor should be the new property editor."
     32  );
     33  ok(
     34    input.selectionStart === 0 && input.selectionEnd === input.value.length,
     35    "Editor contents are selected."
     36  );
     37 
     38  // Try clicking on the editor's input again, shouldn't cause trouble
     39  // (see bug 761665).
     40  EventUtils.synthesizeMouse(input, 1, 1, {}, view.styleWindow);
     41  input.select();
     42 
     43  info("Entering the property name");
     44  editor.input.value = "background-color";
     45 
     46  info("Pressing RETURN and waiting for the value field focus");
     47  let onNameAdded = view.once("ruleview-changed");
     48  EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
     49 
     50  await onNameAdded;
     51 
     52  editor = inplaceEditor(view.styleDocument.activeElement);
     53 
     54  is(
     55    ruleEditor.rule.textProps.length,
     56    2,
     57    "Should have created a new text property."
     58  );
     59  is(
     60    ruleEditor.propertyList.children.length,
     61    2,
     62    "Should have created a property editor."
     63  );
     64  const textProp = ruleEditor.rule.textProps[1];
     65  is(
     66    editor,
     67    inplaceEditor(textProp.editor.valueSpan),
     68    "Should be editing the value span now."
     69  );
     70 
     71  info("Entering the property value");
     72  let onValueAdded = view.once("ruleview-changed");
     73  editor.input.value = "purple";
     74  EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
     75  await onValueAdded;
     76 
     77  is(textProp.value, "purple", "Text prop should have been changed.");
     78 
     79  info("Test creating a new empty CSS variable");
     80  editor = await focusNewRuleViewProperty(ruleEditor);
     81  editor.input.value = "--x";
     82 
     83  info("Pressing RETURN and waiting for the value field focus");
     84  onNameAdded = view.once("ruleview-changed");
     85  EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
     86  await onNameAdded;
     87 
     88  info("Entering the empty property value");
     89  onValueAdded = view.once("ruleview-changed");
     90  // the input value should already be empty, but let's make it explicit
     91  inplaceEditor(view.styleDocument.activeElement).input.value = "";
     92  EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
     93  await onValueAdded;
     94 
     95  is(
     96    ruleEditor.rule.textProps.length,
     97    3,
     98    "Should have created a new text property."
     99  );
    100  const emptyVarTextProp = ruleEditor.rule.textProps[2];
    101  is(emptyVarTextProp.value, "", "The empty variable was created.");
    102  ok(emptyVarTextProp.editor.isValid(), "The empty variable is valid.");
    103  is(
    104    emptyVarTextProp.editor.nameSpan.innerText,
    105    "--x",
    106    "Created expected declaration"
    107  );
    108 
    109  EventUtils.synthesizeKey("VK_ESCAPE", {}, view.styleWindow);
    110 
    111  info("Check that editing the variable name doesn't remove the declaration");
    112  await focusEditableField(view, emptyVarTextProp.editor.nameSpan);
    113  const onRuleViewChanged = view.once("ruleview-changed");
    114  const onTimeout = wait(500).then(() => "TIMEOUT");
    115  EventUtils.synthesizeKey("VK_ESCAPE", {}, view.styleWindow);
    116  const raceResult = await Promise.race([onRuleViewChanged, onTimeout]);
    117  is(raceResult, "TIMEOUT", "ruleview-changed wasn't called");
    118  is(
    119    ruleEditor.rule.textProps.length,
    120    3,
    121    "We still have the same number of text properties."
    122  );
    123 
    124  await checkRuleViewContent(view, [
    125    { selector: "element", selectorEditable: false, declarations: [] },
    126    {
    127      selector: "#testid",
    128      declarations: [
    129        { name: "color", value: "blue" },
    130        { name: "background-color", value: "purple", dirty: true },
    131        { name: "--x", value: "", dirty: true },
    132      ],
    133    },
    134  ]);
    135 });
    136 
    137 add_task(async function addDeclarationInRuleWithNestedRule() {
    138  await addTab(
    139    "data:text/html;charset=utf-8," +
    140      encodeURIComponent(`
    141    <style>
    142      h1 {
    143        position: absolute;
    144        &.myClass {
    145          outline-color: hotpink;
    146        }
    147      }
    148    </style>
    149    <h1 class=myClass>Hello</h1>
    150  `)
    151  );
    152  const { inspector, view } = await openRuleView();
    153  await selectNode("h1", inspector);
    154 
    155  info("Focus the new property name field in h1 rule");
    156  const ruleEditor = getRuleViewRuleEditor(view, 2);
    157  let editor = await focusNewRuleViewProperty(ruleEditor);
    158 
    159  is(
    160    inplaceEditor(ruleEditor.newPropSpan),
    161    editor,
    162    "Next focused editor should be the new property editor."
    163  );
    164 
    165  info("Entering the property name");
    166  editor.input.value = "color";
    167 
    168  info("Pressing RETURN and waiting for the value field focus");
    169  const onNameAdded = view.once("ruleview-changed");
    170  EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
    171 
    172  await onNameAdded;
    173 
    174  editor = inplaceEditor(view.styleDocument.activeElement);
    175 
    176  const textProp = ruleEditor.rule.textProps[1];
    177  is(
    178    editor,
    179    inplaceEditor(textProp.editor.valueSpan),
    180    "Should be editing the value span now."
    181  );
    182 
    183  info("Entering the property value");
    184  const onValueAdded = view.once("ruleview-changed");
    185  editor.input.value = "gold";
    186  EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
    187  await onValueAdded;
    188 
    189  is(textProp.value, "gold", "Text prop should have been changed.");
    190 
    191  const goldRgb = InspectorUtils.colorToRGBA("gold");
    192  is(
    193    await getComputedStyleProperty("h1", null, "color"),
    194    `rgb(${goldRgb.r}, ${goldRgb.g}, ${goldRgb.b})`,
    195    "color was properly applied on element"
    196  );
    197  is(
    198    await getComputedStyleProperty("h1", null, "position"),
    199    "absolute",
    200    `The "position" declaration from the top level rule was preserved`
    201  );
    202  const hotpinkRgb = InspectorUtils.colorToRGBA("hotpink");
    203  is(
    204    await getComputedStyleProperty("h1", null, "outline-color"),
    205    `rgb(${hotpinkRgb.r}, ${hotpinkRgb.g}, ${hotpinkRgb.b})`,
    206    `The "outline-color" declaration from the nested rule was preserved`
    207  );
    208 });