tor-browser

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

counter-style-testcommon.js (2624B)


      1 function test_counter_style_descriptor(descriptor, value, expected) {
      2  let descriptors = [];
      3  descriptors.push(`${descriptor}: ${value}`);
      4 
      5  // Fill out the remaining necessary descriptors
      6  if (descriptor === 'system') {
      7    if (value === 'additive')
      8      descriptors.push('additive-symbols: 1 "I"');
      9    else if (!value.startsWith('extends'))
     10      descriptors.push('symbols: "X" "Y"');
     11  } else if (descriptor === 'symbols') {
     12    descriptors.push('system: symbolic');
     13  } else if (descriptor === 'additive-symbols') {
     14    descriptors.push('system: additive');
     15  } else {
     16    descriptors.push('system: symbolic');
     17    descriptors.push('symbols: "X" "Y"');
     18  }
     19 
     20  let style = document.createElement('style');
     21  style.textContent = `@counter-style foo { ${descriptors.join(';')} }`;
     22  document.head.appendChild(style);
     23 
     24  test(() => {
     25    let rule = style.sheet.cssRules[0];
     26    // TODO: The spec is inconsistent on when the entire rule is invalid
     27    // (and hence absent from OM), and when only the descriptor is invalid.
     28    // Revise when spec issue is resolved.
     29    // See https://github.com/w3c/csswg-drafts/issues/5717
     30    if (!rule) {
     31      assert_equals(expected, undefined);
     32      return;
     33    }
     34 
     35    assert_equals(rule.constructor.name, 'CSSCounterStyleRule');
     36 
     37    let text = rule.cssText;
     38    if (expected)
     39      assert_not_equals(text.indexOf(`${descriptor}: ${expected}`), -1);
     40    else
     41      assert_equals(text.indexOf(`${descriptor}:`), -1);
     42  }, `@counter-style '${descriptor}: ${value}' is ${expected ? 'valid' : 'invalid'}`);
     43 
     44  style.remove();
     45 }
     46 
     47 function test_valid_counter_style_descriptor(descriptor, value, expected) {
     48  expected = expected || value;
     49  test_counter_style_descriptor(descriptor, value, expected);
     50 }
     51 
     52 function test_invalid_counter_style_descriptor(descriptor, value) {
     53  test_counter_style_descriptor(descriptor, value, undefined);
     54 }
     55 
     56 function test_counter_style_name(name, isValid) {
     57  let style = document.createElement('style');
     58  style.textContent = `@counter-style ${name} { system: symbolic; symbols: 'X' 'Y'; }`;
     59  document.head.appendChild(style);
     60 
     61  test(() => {
     62    let rule = style.sheet.cssRules[0];
     63    if (!isValid) {
     64      assert_equals(rule, undefined);
     65      return;
     66    }
     67 
     68    assert_not_equals(rule, undefined);
     69    assert_equals(rule.constructor.name, 'CSSCounterStyleRule');
     70    assert_equals(rule.name, name);
     71  }, `@counter-style name ${name} is ${isValid ? 'valid' : 'invalid'}`);
     72 
     73  style.remove();
     74 }
     75 
     76 function test_valid_name(name) {
     77  test_counter_style_name(name, true);
     78 }
     79 
     80 function test_invalid_name(name) {
     81  test_counter_style_name(name, false);
     82 }