#4032 Fix linking to specific lines in a pull-request
Merged 6 years ago by pingou. Opened 6 years ago by pingou.

@@ -1237,10 +1237,10 @@ 

          $('[href="#comments"]').addClass('active');

        }

      }

-     var file = parseInt(location.hash.substr(2).split(',')[0], 10);

-     var lines = location.hash.split(',')[1].split('-').map(function (x) { return parseInt(x, 10) });

+     var file = parseInt(location.hash.substr(2).split('__')[0], 10);

+     var lines = location.hash.split('__')[1].split('-').map(function (x) { return parseInt(x, 10) });

      for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {

-       $('#' + file + '_' + i).closest('tr').addClass(cls);

+       $('#' + '_' + file + '__' + i).closest('tr').addClass(cls);

      }

    }

  }
@@ -1428,16 +1428,16 @@ 

  var selected = [];

  $("[data-line-number]").click(function (ev) {

    var line = $(this).attr('data-line-number');

-   var file = $(this).attr('id').split('_')[0];

+   var file = $(this).attr('data-file-number');

    if (ev.shiftKey) {

      selected = selected.slice(-1).concat(line);

    } else {

      selected = [line];

    }

  

-   var hash = '_' + file + ',' + selected[0];

+   var hash = '_' + file + '__' + selected[0];

    if (selected.length === 2) {

-     hash = '_' + file + ',' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);

+     hash = '_' + file + '__' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);

    }

    window.location.hash = hash;

    return false;

file modified
+4 -2
@@ -147,7 +147,8 @@ 

              output.append(

                  '<td class="cell1">'

                  '<a id="%(cnt)s" href="#%(cnt)s" data-line-number='

-                 '"%(cnt_lbl)s"></a></td>'

+                 '"%(cnt_lbl)s" data-file-number='

+                 '"%(line)s"></a></td>'

                  '<td class="prc border-right" data-row="%(cnt_lbl)s"'

                  ' data-filename="%(filename)s" data-commit="%(commit)s"'

                  ' data-tree="%(tree_id)s">'
@@ -158,8 +159,9 @@ 

                  "</td>"

                  % (

                      {

-                         "cnt": "%s_%s" % (index, cnt),

+                         "cnt": "_%s__%s" % (index, cnt),

                          "cnt_lbl": cnt,

+                         "line": index,

                          "filename": filename,

                          "commit": commit,

                          "tree_id": tree_id,

Turns out HTML doesn't like commas in its identifiers, so replace the
commas with double underscore ('__') that seems to fix the behavior to
what once worked and now does again.

Fixes https://pagure.io/pagure/issue/3827

Thanks for the review :)

Pull-Request has been merged by pingou

6 years ago