tor-browser

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

browser_rules_add-rule-then-property-edit-selector.js (3193B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests the behaviour of adding a new rule to the rule view, adding a new
      7 // property and editing the selector.
      8 
      9 const TEST_URI = `
     10  <style type="text/css">
     11    #testid {
     12      text-align: center;
     13    }
     14  </style>
     15  <div id="testid">Styled Node</div>
     16  <span>This is a span</span>
     17 `;
     18 
     19 add_task(async function () {
     20  await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
     21  const { inspector, view } = await openRuleView();
     22  await selectNode("#testid", inspector);
     23 
     24  const rule = await addNewRuleAndDismissEditor(inspector, view, "#testid", 1);
     25 
     26  // Wait for the new rule to be connected to its parent stylesheet, because
     27  // adding a new property will require to guess the indentation from the
     28  // stylesheet.
     29  // The parent stylesheet is coming from a resource, which might be available
     30  // later than the actual rule front. See Bug 1899341.
     31  await waitFor(
     32    () => rule.domRule.parentStyleSheet,
     33    "Wait until the stylesheet resource owning the style rule was received"
     34  );
     35  ok(
     36    rule.domRule.parentStyleSheet,
     37    "The rule front is connected to its parent stylesheet"
     38  );
     39 
     40  info("Adding a new property to the new rule");
     41  await testAddingProperty(view, 1);
     42 
     43  info("Editing existing selector field");
     44  await testEditSelector(view, "span");
     45 
     46  info("Selecting the modified element");
     47  await selectNode("span", inspector);
     48 
     49  info("Check new rule and property exist in the modified element");
     50  await checkModifiedElement(view, "span", 1);
     51 });
     52 
     53 function testAddingProperty(view, index) {
     54  const ruleEditor = getRuleViewRuleEditor(view, index);
     55  ruleEditor.addProperty("font-weight", "bold", "", true);
     56  const textProps = ruleEditor.rule.textProps;
     57  const lastRule = textProps[textProps.length - 1];
     58  is(lastRule.name, "font-weight", "Last rule name is font-weight");
     59  is(lastRule.value, "bold", "Last rule value is bold");
     60 }
     61 
     62 async function testEditSelector(view, name) {
     63  const idRuleEditor = getRuleViewRuleEditor(view, 1);
     64 
     65  info("Focusing an existing selector name in the rule-view");
     66  const editor = await focusEditableField(view, idRuleEditor.selectorText);
     67 
     68  is(
     69    inplaceEditor(idRuleEditor.selectorText),
     70    editor,
     71    "The selector editor got focused"
     72  );
     73 
     74  info("Entering a new selector name: " + name);
     75  editor.input.value = name;
     76 
     77  info("Waiting for rule view to update");
     78  const onRuleViewChanged = once(view, "ruleview-changed");
     79 
     80  info("Entering the commit key");
     81  EventUtils.synthesizeKey("KEY_Enter");
     82  await onRuleViewChanged;
     83 
     84  is(view._elementStyle.rules.length, 3, "Should have 3 rules.");
     85 }
     86 
     87 function checkModifiedElement(view, name, index) {
     88  is(view._elementStyle.rules.length, 2, "Should have 2 rules.");
     89  ok(getRuleViewRule(view, name), "Rule with " + name + " selector exists.");
     90 
     91  const idRuleEditor = getRuleViewRuleEditor(view, index);
     92  const textProps = idRuleEditor.rule.textProps;
     93  const lastRule = textProps[textProps.length - 1];
     94  is(lastRule.name, "font-weight", "Last rule name is font-weight");
     95  is(lastRule.value, "bold", "Last rule value is bold");
     96 }