Fossil

Diff
Login

Differences From Artifact [9ec63e0e9d]:

To Artifact [f59f66f569]:


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

107
108
109

110
111
112
113
114
115
116
91
92
93
94
95
96
97









98



99
100
101
102
103
104
105
106







-
-
-
-
-
-
-
-
-
+
-
-
-
+







     line-numer column in the given tr. isSplit must be true if tr
     represents a split diff, else false. Expects its tr to be valid:
     GIGO applies.  Returns the starting line number if getStart, else
     the ending line number. Returns the line number from the LHS file
     if getLHS is true, else the RHS.
  */
  const extractLineNo = function f(getLHS, getStart, tr, isSplit){
    if(!f.rx){
      f.rx = {
        start: /^\s*(\d+)/,
        end: /(\d+)\n?$/
      }
    }
    const td = tr.querySelector('td:nth-child('+(
      /* TD element with the line numbers */
      getLHS ? 1 : (isSplit ? 4 : 2)
    var n = getLHS ? 0 : isSplit ? 3 : 1;
    )+')');
    const m = f.rx[getStart ? 'start' : 'end'].exec(td.innerText);
    return m ? +m[1] : undefined/*"shouldn't happen"*/;
    return parseInt(tr.childNodes[n].innerText);
  };

  /**
     Installs chunk-loading controls into TR.diffskip element tr.
     Each instance corresponds to a single TR.diffskip element.

     The goal is to base these controls roughly on github's, a good
134
135
136
137
138
139
140
141

142
143
144
145
146
147
148
124
125
126
127
128
129
130

131
132
133
134
135
136
137
138







-
+







         neighboring TR blocks */
      startLhs: +tr.dataset.startln,
      endLhs: +tr.dataset.endln
    };
    D.clearElement(tr);
    this.e.td = D.addClass(
      /* Holder for our UI controls */
      D.attr(D.td(tr), 'colspan', this.isSplit ? 5 : 4),
      D.attr(D.td(tr), 'colspan', this.isSplit ? 6 : 4),
      'chunkctrl'
    );
    this.e.msgWidget = D.addClass(D.span(), 'hidden');
    this.e.btnWrapper = D.div();
    D.append(this.e.td, this.e.btnWrapper);
    /**
       Depending on various factors, we need one or more of:
299
300
301
302
303
304
305
306
307


308
309
310
311
312
313






314
315
316

317
318
319
320
321



322
323
324

325
326
327


328
329
330
331
332
333
334
335
336
337


338
339




340
341
342
343




344
345

346
347
348
349
350

351
352
353



354
355
356


357
358

359
360
361
362
363
364





365
366

367
368
369


370
371
372
373
374
375





376
377
378
379
380
381
382
383
384
385
386







387
388
389
390
391
392
393
394



395
396

397
398
399
400
401


402
403
404


405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
289
290
291
292
293
294
295


296
297






298
299
300
301
302
303



304





305
306
307



308



309
310










311
312


313
314
315
316




317
318
319
320


321





322



323
324
325



326
327


328






329
330
331
332
333


334



335
336






337
338
339
340
341











342
343
344
345
346
347
348





349


350
351
352


353





354
355
356


357
358
359
360
361







362
363
364








365
366
367
368
369
370
371
372
373










374
375
376





377
378
379
380
381
382
383







-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
-
-
-
-
-
+
+
+
-
-
-
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
-
-
-
-
-
+
-
-
-
+
+
+
-
-
-
+
+
-
-
+
-
-
-
-
-
-
+
+
+
+
+
-
-
+
-
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-

-
-
+
+
+
-
-
+
-
-
-
-
-
+
+

-
-
+
+



-
-
-
-
-
-
-



-
-
-
-
-
-
-
-









-
-
-
-
-
-
-
-
-
-



-
-
-
-
-







                               lines/*response lines*/){
      if(!lines.length){
        /* No more data to load */
        this.destroy();
        return this;
      }
      this.msg(false);
      //console.debug("Loaded line range ",
      //urlParam.from,"-",urlParam.to, "fetchType ",fetchType);
      var startLnR = -2147483648; /* ⚠ */
      switch( fetchType ){
      const lineno = [],
            trPrev = this.e.tr.previousElementSibling,
            trNext = this.e.tr.nextElementSibling,
            doAppend = (
              !!trPrev && fetchType>=this.FetchType.FillGap
            ) /* true to append to previous TR, else prepend to NEXT TR */;
        case this.FetchType.PrevDown:
          startLnR = this.pos.prev.endRhs + 1;
          break;
        case this.FetchType.NextUp:
          startLnR = this.pos.next.startRhs - lines.length;
          break;
      const tr = doAppend ? trPrev : trNext;
      const joinTr = (
        this.FetchType.FillGap===fetchType && trPrev && trNext
        case this.FetchType.FillGap:
      ) ? trNext : false
      /* Truthy if we want to combine trPrev, the new content, and
         trNext into trPrev and then remove trNext. */;
      let i, td;
      if(!f.convertLines){
          startLnR = this.pos.prev ? 
                       this.pos.prev.endRhs + 1 :
                       this.pos.next.startRhs - lines.length;
        /* Reminder: string.replaceAll() is a relatively new
           JS feature, not available in some still-widely-used
           browser versions. */
          break;
        f.rx = [[/&/g, '&amp;'], [/</g, '&lt;']];
        f.convertLines = function(li){
          var s = li.join('\n');
      }
      const lnTo = Math.min(urlParam.to,
          f.rx.forEach((a)=>s=s.replace(a[0],a[1]));
          return s + '\n';
        };
      }
      if(1){ // LHS line numbers...
        const selector = '.difflnl > pre';
        td = tr.querySelector(selector);
        const lnTo = Math.min(urlParam.to,
                              urlParam.from +
                              lines.length - 1/*b/c request range is inclusive*/);
                            urlParam.from +
                            lines.length - 1/*b/c request range is inclusive*/);
        for( i = urlParam.from; i <= lnTo; ++i ){
          lineno.push(i);
      function createDiffCommonLine(isSplit,lnl,lnr,txt){
        var tr, td;
        tr = document.createElement('tr');
        tr.className = 'diffline';
        }
        const lineNoTxt = lineno.join('\n')+'\n';
        const content = [td.innerHTML];
        if(doAppend) content.push(lineNoTxt);
        td = document.createElement('td');
        td.className = 'diffln difflnl';
        td.appendChild(document.createTextNode(lnl));
        tr.appendChild(td);
        else content.unshift(lineNoTxt);
        if(joinTr){
        if( isSplit ){
          content.push(trNext.querySelector(selector).innerHTML);
        }
        td.innerHTML = content.join('');
      }

          td = document.createElement('td');
      if(1){// code block(s)...
        const selector = '.difftxt > pre';
        td = tr.querySelectorAll(selector);
          td.className = 'diffsep';
          tr.appendChild(td);
          td = document.createElement('td');
        const code = f.convertLines(lines);
        let joinNdx = 0/*selector[X] index to join together*/;
        td.forEach(function(e){
          td.className = 'difftxt difftxtl';
          td.appendChild(document.createTextNode(txt));
          const content = [e.innerHTML];
          if(doAppend) content.push(code);
          tr.appendChild(td);
          else content.unshift(code);
          if(joinTr){
            content.push(trNext.querySelectorAll(selector)[joinNdx++].innerHTML)
          }
          e.innerHTML = content.join('');
        });
        }
        td = document.createElement('td');
        td.className = 'diffln difflnlr';
        td.appendChild(document.createTextNode(lnr));
        tr.appendChild(td);
      }

        td = document.createElement('td');
      if(1){// Add blank lines in (.diffsep>pre)
        const selector = '.diffsep > pre';
        td = tr.querySelector(selector);
        td.className = 'diffsep';
        tr.appendChild(td);
        for(i = 0; i < lineno.length; ++i) lineno[i] = '';
        const blanks = lineno.join('\n')+'\n';
        const content = [td.innerHTML];
        if(doAppend) content.push(blanks);
        else content.unshift(blanks);
        if(joinTr){
        td = document.createElement('td');
        td.className = 'difftxt difftxt' + isSplit ? 'r' : 'u';
        td.appendChild(document.createTextNode(txt));
        tr.appendChild(td);
        return tr;
          content.push(trNext.querySelector(selector).innerHTML);
        }
        td.innerHTML = content.join('');
      }

      if(this.FetchType.FillGap===fetchType){
        /* Closing the whole gap between two chunks or a whole gap
           at the start or end of a diff. */
        // RHS line numbers...
        let startLnR = this.pos.prev
            ? this.pos.prev.endRhs+1 /* Closing the whole gap between two chunks
      }
      if( fetchType==this.FetchType.NextUp ){
        for( i=lnTo; i>=urlParam.from; i-- ){
          var tr =
            createDiffCommonLine(
              this.isSplit,i,startLnR+i-urlParam.from,lines[i-urlParam.from]);
          this.e.tr.parentElement.insertBefore(tr,this.e.tr.nextElementSibling);
                                        or end-of-file gap. */
            : this.pos.next.startRhs - lines.length /* start-of-file gap */;
        lineno.length = lines.length;
        for( i = startLnR; i < startLnR + lines.length; ++i ){
          lineno[i-startLnR] = i;
        }
        const selector = '.difflnr > pre';
        td = tr.querySelector(selector);
      }else{
        for( i=urlParam.from; i<=lnTo; i++ ){
          var tr =
        const lineNoTxt = lineno.join('\n')+'\n';
        lineno.length = 0;
            createDiffCommonLine(
        const content = [td.innerHTML];
        if(doAppend) content.push(lineNoTxt);
        else content.unshift(lineNoTxt);
        if(joinTr){
          content.push(trNext.querySelector(selector).innerHTML);
              this.isSplit,i,startLnR+i-urlParam.from,lines[i-urlParam.from]);
          this.e.tr.parentElement.insertBefore(tr,this.e.tr);
        }
        td.innerHTML = content.join('');
        if(joinTr) D.remove(joinTr);
      }
      if(this.FetchType.FillGap===fetchType){
        this.destroy();
        return this;
      }else if(this.FetchType.PrevDown===fetchType){
        /* Append context to previous TR. */
        // RHS line numbers...
        let startLnR = this.pos.prev.endRhs+1;
        lineno.length = lines.length;
        for( i = startLnR; i < startLnR + lines.length; ++i ){
          lineno[i-startLnR] = i;
        }
        this.pos.startLhs += lines.length;
        this.pos.prev.endRhs += lines.length;
        this.pos.prev.endLhs += lines.length;
        const selector = '.difflnr > pre';
        td = tr.querySelector(selector);
        const lineNoTxt = lineno.join('\n')+'\n';
        lineno.length = 0;
        const content = [td.innerHTML];
        if(doAppend) content.push(lineNoTxt);
        else content.unshift(lineNoTxt);
        td.innerHTML = content.join('');
        if(lines.length < (urlParam.to - urlParam.from)){
          /* No more data. */
          this.destroy();
        }else{
          this.maybeReplaceButtons();
          this.updatePosDebug();
        }
        return this;
      }else if(this.FetchType.NextUp===fetchType){
        /* Prepend content to next TR. */
        // RHS line numbers...
        if(doAppend){
          throw new Error("Internal precondition violation: doAppend is true.");
        }
        let startLnR = this.pos.next.startRhs - lines.length;
        lineno.length = lines.length;
        for( i = startLnR; i < startLnR + lines.length; ++i ){
          lineno[i-startLnR] = i;
        }
        this.pos.endLhs -= lines.length;
        this.pos.next.startRhs -= lines.length;
        this.pos.next.startLhs -= lines.length;
        const selector = '.difflnr > pre';
        td = tr.querySelector(selector);
        const lineNoTxt = lineno.join('\n')+'\n';
        lineno.length = 0;
        td.innerHTML = lineNoTxt + td.innerHTML;
        if(this.pos.endLhs<1
           || lines.length < (urlParam.to - urlParam.from)){
          /* No more data. */
          this.destroy();
        }else{
          this.maybeReplaceButtons();
          this.updatePosDebug();