tor-browser

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

addRange.js (10815B)


      1 "use strict";
      2 
      3 function testAddRange(exception, range, endpoints, qualifier, testName) {
      4    if (!isSelectableNode(endpoints[0]) || !isSelectableNode(endpoints[2])) {
      5        testAddRangeDoesNothing(exception, range, endpoints, qualifier, testName);
      6        return;
      7    }
      8 
      9    test(function() {
     10        assert_equals(exception, null, "Test setup must not throw exceptions");
     11 
     12        selection.addRange(range);
     13 
     14        assert_equals(range.startContainer, endpoints[0],
     15            "addRange() must not modify the startContainer of the Range it's given");
     16        assert_equals(range.startOffset, endpoints[1],
     17            "addRange() must not modify the startOffset of the Range it's given");
     18        assert_equals(range.endContainer, endpoints[2],
     19            "addRange() must not modify the endContainer of the Range it's given");
     20        assert_equals(range.endOffset, endpoints[3],
     21            "addRange() must not modify the endOffset of the Range it's given");
     22    }, testName + ": " + qualifier + " addRange() must not throw exceptions or modify the range it's given");
     23 
     24    test(function() {
     25        assert_equals(exception, null, "Test setup must not throw exceptions");
     26 
     27        assert_equals(selection.rangeCount, 1, "rangeCount must be 1");
     28    }, testName + ": " + qualifier + " addRange() must result in rangeCount being 1");
     29 
     30    // From here on out we check selection.getRangeAt(selection.rangeCount - 1)
     31    // so as not to double-fail Gecko.
     32 
     33    test(function() {
     34        assert_equals(exception, null, "Test setup must not throw exceptions");
     35        assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
     36 
     37        var newRange = selection.getRangeAt(selection.rangeCount - 1);
     38 
     39        assert_not_equals(newRange, null,
     40            "getRangeAt(rangeCount - 1) must not return null");
     41        assert_equals(typeof newRange, "object",
     42            "getRangeAt(rangeCount - 1) must return an object");
     43 
     44        assert_equals(newRange.startContainer, range.startContainer,
     45            "startContainer of the Selection's last Range must match the added Range");
     46        assert_equals(newRange.startOffset, range.startOffset,
     47            "startOffset of the Selection's last Range must match the added Range");
     48        assert_equals(newRange.endContainer, range.endContainer,
     49            "endContainer of the Selection's last Range must match the added Range");
     50        assert_equals(newRange.endOffset, range.endOffset,
     51            "endOffset of the Selection's last Range must match the added Range");
     52    }, testName + ": " + qualifier + " addRange() must result in the selection's last range having the specified endpoints");
     53 
     54    test(function() {
     55        assert_equals(exception, null, "Test setup must not throw exceptions");
     56        assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
     57 
     58        assert_equals(selection.getRangeAt(selection.rangeCount - 1), range,
     59            "getRangeAt(rangeCount - 1) must return the same object we added");
     60    }, testName + ": " + qualifier + " addRange() must result in the selection's last range being the same object we added");
     61 
     62    // Let's not test many different modifications -- one should be enough.
     63    test(function() {
     64        assert_equals(exception, null, "Test setup must not throw exceptions");
     65        assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
     66 
     67        if (range.startContainer == paras[0].firstChild
     68        && range.startOffset == 0
     69        && range.endContainer == paras[0].firstChild
     70        && range.endOffset == 2) {
     71            // Just in case . . .
     72            range.setStart(paras[0].firstChild, 1);
     73        } else {
     74            range.setStart(paras[0].firstChild, 0);
     75            range.setEnd(paras[0].firstChild, 2);
     76        }
     77 
     78        var newRange = selection.getRangeAt(selection.rangeCount - 1);
     79 
     80        assert_equals(newRange.startContainer, range.startContainer,
     81            "After mutating the " + qualifier + " added Range, startContainer of the Selection's last Range must match the added Range");
     82        assert_equals(newRange.startOffset, range.startOffset,
     83            "After mutating the " + qualifier + " added Range, startOffset of the Selection's last Range must match the added Range");
     84        assert_equals(newRange.endContainer, range.endContainer,
     85            "After mutating the " + qualifier + " added Range, endContainer of the Selection's last Range must match the added Range");
     86        assert_equals(newRange.endOffset, range.endOffset,
     87            "After mutating the " + qualifier + " added Range, endOffset of the Selection's last Range must match the added Range");
     88    }, testName + ": modifying the " + qualifier + " added range must modify the Selection's last Range");
     89 
     90    // Now test the other way too.
     91    test(function() {
     92        assert_equals(exception, null, "Test setup must not throw exceptions");
     93        assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
     94 
     95        var newRange = selection.getRangeAt(selection.rangeCount - 1);
     96 
     97        if (newRange.startContainer == paras[0].firstChild
     98        && newRange.startOffset == 4
     99        && newRange.endContainer == paras[0].firstChild
    100        && newRange.endOffset == 6) {
    101            newRange.setStart(paras[0].firstChild, 5);
    102        } else {
    103            newRange.setStart(paras[0].firstChild, 4);
    104            newRange.setStart(paras[0].firstChild, 6);
    105        }
    106 
    107        assert_equals(newRange.startContainer, range.startContainer,
    108            "After " + qualifier + " addRange(), after mutating the Selection's last Range, startContainer of the Selection's last Range must match the added Range");
    109        assert_equals(newRange.startOffset, range.startOffset,
    110            "After " + qualifier + " addRange(), after mutating the Selection's last Range, startOffset of the Selection's last Range must match the added Range");
    111        assert_equals(newRange.endContainer, range.endContainer,
    112            "After " + qualifier + " addRange(), after mutating the Selection's last Range, endContainer of the Selection's last Range must match the added Range");
    113        assert_equals(newRange.endOffset, range.endOffset,
    114            "After " + qualifier + " addRange(), after mutating the Selection's last Range, endOffset of the Selection's last Range must match the added Range");
    115    }, testName + ": modifying the Selection's last Range must modify the " + qualifier + " added Range");
    116 }
    117 
    118 function testAddRangeDoesNothing(exception, range, endpoints, qualifier, testName) {
    119    test(function() {
    120        assert_equals(exception, null, "Test setup must not throw exceptions");
    121 
    122        assertSelectionNoChange(function() { selection.addRange(range); });
    123        assert_equals(range.startContainer, endpoints[0],
    124            "addRange() must not modify the startContainer of the Range it's given");
    125        assert_equals(range.startOffset, endpoints[1],
    126            "addRange() must not modify the startOffset of the Range it's given");
    127        assert_equals(range.endContainer, endpoints[2],
    128            "addRange() must not modify the endContainer of the Range it's given");
    129        assert_equals(range.endOffset, endpoints[3],
    130            "addRange() must not modify the endOffset of the Range it's given");
    131    }, testName + ": " + qualifier + " addRange() must do nothing");
    132 }
    133 
    134 // Do only n evals, not n^2
    135 var testRangesEvaled = testRanges.map(eval);
    136 
    137 // Run a subset of all of addRange tests.
    138 // Huge number of tests in a single file causes problems. Each of
    139 // addRange-NN.html runs a part of them.
    140 //
    141 // startIndex - Start index in testRanges array
    142 // optionalEndIndex - End index in testRanges array + 1. If this argument is
    143 //     omitted, testRanges.length is applied.
    144 function testAddRangeSubSet(startIndex, optionalEndIndex) {
    145    var endIndex = optionalEndIndex === undefined ? testRanges.length : optionalEndIndex;
    146    if (startIndex < 0 || startIndex >= testRanges.length)
    147        throw "Sanity check: Specified index is invalid.";
    148    if (endIndex < 0 || endIndex > testRanges.length)
    149        throw "Sanity check: Specified index is invalid.";
    150 
    151    for (var i = startIndex; i < endIndex; i++) {
    152        for (var j = 0; j < testRanges.length; j++) {
    153            var testName = "Range " + i + " " + testRanges[i]
    154                + " followed by Range " + j + " " + testRanges[j];
    155 
    156            var exception = null;
    157            try {
    158                selection.removeAllRanges();
    159 
    160                var endpoints1 = testRangesEvaled[i];
    161                var range1 = ownerDocument(endpoints1[0]).createRange();
    162                range1.setStart(endpoints1[0], endpoints1[1]);
    163                range1.setEnd(endpoints1[2], endpoints1[3]);
    164 
    165                if (range1.startContainer !== endpoints1[0]) {
    166                    throw "Sanity check: the first Range we created must have the desired startContainer";
    167                }
    168                if (range1.startOffset !== endpoints1[1]) {
    169                    throw "Sanity check: the first Range we created must have the desired startOffset";
    170                }
    171                if (range1.endContainer !== endpoints1[2]) {
    172                    throw "Sanity check: the first Range we created must have the desired endContainer";
    173                }
    174                if (range1.endOffset !== endpoints1[3]) {
    175                    throw "Sanity check: the first Range we created must have the desired endOffset";
    176                }
    177 
    178                var endpoints2 = testRangesEvaled[j];
    179                var range2 = ownerDocument(endpoints2[0]).createRange();
    180                range2.setStart(endpoints2[0], endpoints2[1]);
    181                range2.setEnd(endpoints2[2], endpoints2[3]);
    182 
    183                if (range2.startContainer !== endpoints2[0]) {
    184                    throw "Sanity check: the second Range we created must have the desired startContainer";
    185                }
    186                if (range2.startOffset !== endpoints2[1]) {
    187                    throw "Sanity check: the second Range we created must have the desired startOffset";
    188                }
    189                if (range2.endContainer !== endpoints2[2]) {
    190                    throw "Sanity check: the second Range we created must have the desired endContainer";
    191                }
    192                if (range2.endOffset !== endpoints2[3]) {
    193                    throw "Sanity check: the second Range we created must have the desired endOffset";
    194                }
    195            } catch (e) {
    196                exception = e;
    197            }
    198 
    199            testAddRange(exception, range1, endpoints1, "first", testName);
    200            if (selection.rangeCount > 0)
    201                testAddRangeDoesNothing(exception, range2, endpoints2, "second", testName);
    202        }
    203    }
    204 }