tor-browser

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

tokenization-noopener-noreferrer.js (5429B)


      1 function booleanTests(feature) {
      2  const windowURL = 'resources/close-self.html';
      3  // Tests for how windows features are tokenized into 'name', 'value'
      4  // window features separators are ASCII whitespace, '=' and  ','
      5 
      6  const featureUpper = feature.toUpperCase(),
      7        featureSplitBegin = feature.slice(0, 2),
      8        featureSplitEnd = feature.slice(2),
      9        featureMixedCase = featureSplitBegin.toUpperCase() + featureSplitEnd;
     10        featureMixedCase2 = featureSplitBegin + featureSplitEnd.toUpperCase();
     11 
     12  test (t => {
     13    // Tokenizing `name`: initial window features separators are ignored
     14    // Each of these variants should tokenize to (`${feature}`, '')
     15    [
     16      ` ${feature}`,
     17      `=${feature}`,
     18      `,,${feature}`,
     19      `,=, ${feature}`,
     20      `\n=${feature}=`,
     21      `\t${feature}`,
     22      `\r,,,=${feature}`,
     23      `\u000C${feature}`
     24    ].forEach(variant => {
     25      const win = window.open(windowURL, "", variant);
     26      assert_equals(win, null, `"${variant}" should activate feature "${feature}"`);
     27    });
     28  }, `Tokenization of "${feature}" should skip window features separators before feature`);
     29 
     30  test (t => {
     31    // Tokenizing `name`: lowercase conversion
     32    // Each of these variants should tokenize as feature (`${feature}`, '')
     33    // except where indicated
     34    // Note also that `value` is lowercased during tokenization
     35    [
     36      `${featureUpper}`,
     37      `${featureMixedCase}`,
     38      `  ${featureMixedCase2}`,
     39      `=${featureUpper}`,
     40      `${featureUpper}=1`,
     41      `${featureUpper}=1`,
     42      `${featureUpper}=yes`,
     43      `${feature}=YES`,
     44    ].forEach(variant => {
     45      const win = window.open(windowURL, '', variant);
     46      assert_equals(win, null, `"${variant}" should activate feature "${feature}"`);
     47    });
     48  }, `Feature "${feature}" should be converted to ASCII lowercase`);
     49 
     50  test (t => {
     51    // After `name` has been collected, ignore any window features separators until '='
     52    // except ',' OR a non-window-features-separator — break in those cases
     53    // i.e. ignore whitespace until '=' unless a ',' is encountered first
     54    // Each of these variants should tokenize as feature ('noopener', '')
     55    [
     56      `${feature}`,
     57      ` ${feature}\r`,
     58      `${feature}\n =`,
     59      `${feature},`,
     60      `${feature}  =,`,
     61      `, ${feature}   =`,
     62      `${feature},=`,
     63      `${feature} foo`,
     64      `foo ${feature}=1`,
     65      `foo=\u000Cbar\u000C${feature}`
     66    ].forEach(variant => {
     67      const win = window.open(windowURL, '', variant);
     68      assert_equals(win, null, `"${variant}" should activate feature "${feature}"`);
     69    });
     70  }, `After "${feature}", tokenization should skip window features separators that are not "=" or ","`);
     71 
     72  test (t => {
     73    // After initial '=', tokenizing should ignore all separators except ','
     74    // before collecting `value`
     75    // Each of these variants should tokenize as feature ('noopener', '')
     76    // Except where indicated
     77    [
     78      `${feature}=  yes`,
     79      `${feature}==,`,
     80      `${feature}=\n ,`,
     81      `${feature} = \t ,`,
     82      `${feature}\n=\r 1,`,
     83      `${feature}=,yes`,
     84      `${feature}= yes=,`,
     85      `${feature} = \u000Cyes`
     86    ].forEach(variant => {
     87      const win = window.open(windowURL, '', variant);
     88      assert_equals(win, null, `"${variant}" should activate feature "${feature}"`);
     89    });
     90  }, `Tokenizing "${feature}" should ignore window feature separators except "," after initial "=" and before value`);
     91 
     92  test (t => {
     93    // Tokenizing `value` should collect any non-separator code points until first separator
     94    [
     95      `${feature}=1`,
     96      `${feature}=yes`,
     97      `${feature} = yes ,`,
     98      `${feature}=\nyes  ,`,
     99      `${feature}=yes yes`,
    100      `${feature}=yes\ts`,
    101      `${feature}==`,
    102      `${feature}=1\n,`,
    103      `==${feature}===`,
    104      `${feature}==\u000C`
    105    ].forEach(variant => {
    106      const win = window.open(windowURL, '', variant);
    107      assert_equals(win, null, `"${variant}" should set "${feature}"`);
    108    });
    109  }, `Tokenizing "${feature}" should read characters until first window feature separator as \`value\``);
    110 
    111  test (t => {
    112    [
    113      `${feature}=1`,
    114      `${feature}=2`,
    115      `${feature}=12345`,
    116      `${feature}=1.5`,
    117      `${feature}=-1`,
    118    ].forEach(variant => {
    119      const win = window.open(windowURL, '', variant);
    120      assert_equals(win, null, `"${variant}" should activate feature "${feature}"`);
    121    });
    122  }, 'Integer values other than 0 should activate the feature');
    123 
    124  test (t => {
    125    [
    126      `${feature}=0`,
    127      `${feature}=0.5`,
    128      `${feature}=error`,
    129    ].forEach(variant => {
    130      const win = window.open(windowURL, '', variant);
    131      assert_not_equals(win, null, `"${variant}" should NOT activate feature "${feature}"`);
    132    });
    133  }, `Integer value of 0 should not activate "${feature}"`);
    134 
    135  test (t => {
    136    [
    137      `-${feature}`,
    138      `${featureUpper}RRR`,
    139      `${featureMixedCase}R`,
    140      `${featureSplitBegin}_${featureSplitEnd}`,
    141      ` ${featureSplitBegin} ${featureSplitEnd}`,
    142      `${featureSplitBegin}\n${featureSplitEnd}`,
    143      `${featureSplitBegin},${featureSplitEnd}`,
    144      `\0${feature}`,
    145      `${feature}\u0000=yes`,
    146      `foo=\u000C${feature}`
    147    ].forEach(variant => {
    148      const win = window.open(windowURL, '', variant);
    149      assert_not_equals(win, null, `"${variant}" should NOT activate feature "${feature}"`);
    150    });
    151  }, `Invalid feature names should not tokenize as "${feature}"`);
    152 }