Fixing a couple of bugs: support for unknown (i.e. not Java or XML) filetypes, searching for usages in such files properly, proper escaping right after load and when adding highlights.
authorJan Lahoda <jlahoda@netbeans.org>
Mon, 01 Jul 2013 18:45:06 +0200
changeset 97114aa56c001ef
parent 970 a2dcd04c7747
child 972 5c7fa3e46c42
Fixing a couple of bugs: support for unknown (i.e. not Java or XML) filetypes, searching for usages in such files properly, proper escaping right after load and when adding highlights.
remoting/server/web/web.ui.frontend/public_html/index/ui/script.js
remoting/server/web/web.ui/src/org/netbeans/modules/jackpot30/backend/ui/UI.java
     1.1 --- a/remoting/server/web/web.ui.frontend/public_html/index/ui/script.js	Sun Jun 30 21:21:02 2013 +0200
     1.2 +++ b/remoting/server/web/web.ui.frontend/public_html/index/ui/script.js	Mon Jul 01 18:45:06 2013 +0200
     1.3 @@ -319,9 +319,10 @@
     1.4      };
     1.5  
     1.6      $http.get("/index/source/cat?path=" + escape(path) + "&relative=" + escape(relative)).success(function(data) {
     1.7 -        $scope.sourceCode = data.replace(/\r\n/g, '\n');
     1.8 +        var sourceCode = data.replace(/\r\n/g, '\n');
     1.9 +        $scope.sourceCode = escapeHTML(sourceCode);
    1.10          $http.get("/index/ui/highlightData?path=" + escape(path) + "&relative=" + escape(relative)).success(function(parsedData) {
    1.11 -            doColoring(path, relative, parsedData.categories, parsedData.spans, $scope, $location, $routeParams, $http);
    1.12 +            doColoring(path, relative, parsedData.categories, parsedData.spans, $scope, $location, $routeParams, $http, sourceCode);
    1.13          });
    1.14      });
    1.15      
    1.16 @@ -347,9 +348,14 @@
    1.17      for (var i = 0; i < tokenColoring.length; i++ ) {
    1.18          var currentCode = code.slice(current, current+tokenSpans[i]);
    1.19          var byLines = currentCode.split("\n");
    1.20 +        var index = current;
    1.21          for (var j = 0; j < byLines.length; j++) {
    1.22 -            if (j > 0) coloredCode += '</td></tr><tr><td class="unselectable">' + (line++) + "</td><td>";
    1.23 -            coloredCode += '<span id="p' + current + '" class="' + tokenColoring[i] + '" jpt30pos="' + current + '">' + byLines[j].replace(/&/g, '&amp;').replace(/</g, '&lt;') + "</span>";
    1.24 +            if (j > 0) {
    1.25 +                coloredCode += '</td></tr><tr><td class="unselectable">' + (line++) + "</td><td>";
    1.26 +                index++;
    1.27 +            }
    1.28 +            coloredCode += '<span id="p' + index + '" class="' + tokenColoring[i] + '" jpt30pos="' + current + '">' + byLines[j].replace(/&/g, '&amp;').replace(/</g, '&lt;') + "</span>";
    1.29 +            index += byLines[j].length;
    1.30          }
    1.31          current += tokenSpans[i];
    1.32      }
    1.33 @@ -359,6 +365,10 @@
    1.34      return coloredCode;
    1.35  }
    1.36  
    1.37 +function escapeHTML(source) {
    1.38 +    return source.replace(/&/g, '&amp;').replace(/</g, '&lt;');
    1.39 +}
    1.40 +
    1.41  function addHighlights(highlights) {
    1.42      $(".highlight").removeClass("highlight");
    1.43  
    1.44 @@ -387,11 +397,11 @@
    1.45                  var result = "";
    1.46  
    1.47                  if (start < highlightStart) {
    1.48 -                    result += '<span id="p' + start + '" class="' + clazz + '" jpt30pos="' + jpt30pos + '">' + text.substring(0, Math.min(text.length, highlightStart - start)) + "</span>";
    1.49 +                    result += '<span id="p' + start + '" class="' + clazz + '" jpt30pos="' + jpt30pos + '">' + escapeHTML(text.substring(0, Math.min(text.length, highlightStart - start))) + "</span>";
    1.50                  }
    1.51 -                result += '<span id="p' + Math.max(start, highlightStart) + '" class="' + clazz + ' highlight" jpt30pos="' + Math.max(start, highlightStart) + '">' + text.substring(highlightStart - start, Math.min(text.length, highlightEnd - start)) + "</span>";
    1.52 +                result += '<span id="p' + Math.max(start, highlightStart) + '" class="' + clazz + ' highlight" jpt30pos="' + Math.max(start, highlightStart) + '">' + escapeHTML(text.substring(highlightStart - start, Math.min(text.length, highlightEnd - start))) + "</span>";
    1.53                  if (highlightEnd < end) {
    1.54 -                    result += '<span id="p' + (highlightStart + highlightLen) + '" class="' + clazz + '" jpt30pos="' + (highlightStart + highlightLen) + '">' + text.substring(highlightEnd - start, end - start) + "</span>";
    1.55 +                    result += '<span id="p' + (highlightStart + highlightLen) + '" class="' + clazz + '" jpt30pos="' + (highlightStart + highlightLen) + '">' + escapeHTML(text.substring(highlightEnd - start, end - start)) + "</span>";
    1.56                  }
    1.57  
    1.58                  $(this).replaceWith(result);
    1.59 @@ -400,8 +410,8 @@
    1.60      }
    1.61  }
    1.62  
    1.63 -function doColoring(path, relative, $highlights, $spans, $scope, $location, $routeParams, $http) {
    1.64 -    $scope.sourceCode = tokenColoring($scope.sourceCode, $highlights, $spans);
    1.65 +function doColoring(path, relative, $highlights, $spans, $scope, $location, $routeParams, $http, sourceCode) {
    1.66 +    $scope.sourceCode = tokenColoring(sourceCode, $highlights, $spans);
    1.67  
    1.68      $location.replace();
    1.69  
     2.1 --- a/remoting/server/web/web.ui/src/org/netbeans/modules/jackpot30/backend/ui/UI.java	Sun Jun 30 21:21:02 2013 +0200
     2.2 +++ b/remoting/server/web/web.ui/src/org/netbeans/modules/jackpot30/backend/ui/UI.java	Mon Jul 01 18:45:06 2013 +0200
     2.3 @@ -320,13 +320,15 @@
     2.4          if (relative.endsWith(".java")) {
     2.5              CompilationInfo info = ResolveService.parse(segment, relative);
     2.6              highlights = colorTokens(info, Collections.<Long>emptyList());
     2.7 -        } else if (relative.endsWith(".xml")) {
     2.8 +        } else {
     2.9              String urlBase = URL_BASE_OVERRIDE != null ? URL_BASE_OVERRIDE : uriInfo.getBaseUri().toString();
    2.10              URI u = new URI(urlBase + "index/source/cat?path=" + escapeForQuery(segment) + "&relative=" + escapeForQuery(relative));
    2.11              String content = WebUtilities.requestStringResponse(u).replace("\r\n", "\n");
    2.12 -            highlights = colorTokens(TokenHierarchy.create(content, XMLTokenId.language()).tokenSequence(XMLTokenId.language()), Collections.<Token, Coloring>emptyMap(), Collections.<Long>emptyList());
    2.13 -        } else {
    2.14 -            highlights = new HighlightData(Collections.<String>emptyList(), Collections.<Long>emptyList());
    2.15 +            if (relative.endsWith(".xml")) {
    2.16 +                highlights = colorTokens(TokenHierarchy.create(content, XMLTokenId.language()).tokenSequence(XMLTokenId.language()), Collections.<Token, Coloring>emptyMap(), Collections.<Long>emptyList());
    2.17 +            } else {
    2.18 +                highlights = new HighlightData(Collections.<String>singletonList(""), Collections.<Long>singletonList((long) content.length()));
    2.19 +            }
    2.20          }
    2.21  
    2.22