Bug 1310801 - Remove non-printable characters. r=nchevobbe
authorJan Odvarko <odvarko@gmail.com>
Thu, 27 Oct 2016 13:31:54 +0200
changeset 319844 68f683a2034bb280a323242b4de87c958ed7c6f4
parent 319843 111c1227f51eb6e94d7213f92b2a32e55bfdf3e8
child 319845 3a035c4acf604e6591157b664f67c73fcb7162fb
push id20749
push userryanvm@gmail.com
push dateSat, 29 Oct 2016 13:21:21 +0000
treeherderfx-team@1b170b39ed6b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnchevobbe
bugs1310801
milestone52.0a1
Bug 1310801 - Remove non-printable characters. r=nchevobbe
devtools/client/shared/components/reps/rep-utils.js
devtools/client/shared/components/test/mochitest/test_reps_string.html
--- a/devtools/client/shared/components/reps/rep-utils.js
+++ b/devtools/client/shared/components/reps/rep-utils.js
@@ -43,16 +43,23 @@ define(function (require, exports, modul
   function cropString(text, limit, alternativeText) {
     if (!alternativeText) {
       alternativeText = "\u2026";
     }
 
     // Make sure it's a string.
     text = text + "";
 
+    // Replace all non-printable characters, except of
+    // (horizontal) tab (HT: \x09) and newline (LF: \x0A, CR: \x0D),
+    // with unicode replacement character (u+fffd).
+    // eslint-disable-next-line no-control-regex
+    let re = new RegExp("[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]", "g");
+    text = text.replace(re, "\ufffd");
+
     // Crop the string only if a limit is actually specified.
     if (!limit || limit <= 0) {
       return text;
     }
 
     // Set the limit at least to the length of the alternative text
     // plus one character of the original text.
     if (limit <= alternativeText.length) {
--- a/devtools/client/shared/components/test/mochitest/test_reps_string.html
+++ b/devtools/client/shared/components/test/mochitest/test_reps_string.html
@@ -23,16 +23,17 @@ window.onload = Task.async(function* () 
     const renderedRep = shallowRenderComponent(Rep, { object: getGripStub("testMultiline") });
     is(renderedRep.type, StringRep.rep, `Rep correctly selects ${StringRep.rep.displayName}`);
 
     // Test rendering
     yield testMultiline();
     yield testMultilineOpen();
     yield testMultilineLimit();
     yield testUseQuotes();
+    yield testNonPritableCharacters();
   } catch(e) {
     ok(false, "Got an error: " + DevToolsUtils.safeErrorString(e));
   } finally {
     SimpleTest.finish();
   }
 
   function testMultiline() {
     const renderedComponent = renderComponent(StringRep.rep, { object: getGripStub("testMultiline") });
@@ -46,25 +47,31 @@ window.onload = Task.async(function* () 
 
   function testMultilineOpen() {
     const renderedComponent = renderComponent(StringRep.rep, { object: getGripStub("testMultiline"), member: {open: true} });
     is(renderedComponent.textContent, "\"aaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbb\ncccccccccccccccc\n\"", "String rep has expected text content for multiline string when open");
   }
 
   function testUseQuotes(){
      const renderedComponent = renderComponent(StringRep.rep, { object: getGripStub("testUseQuotes"), useQuotes: false });
-     is(renderedComponent.textContent, "abc","String rep was expected to omit quotes");
+     is(renderedComponent.textContent, "abc", "String rep was expected to omit quotes");
+  }
+
+  function testNonPritableCharacters(){
+     const renderedComponent = renderComponent(StringRep.rep, { object: getGripStub("testNonPritableCharacters"), useQuotes: false });
+     is(renderedComponent.textContent, "a\ufffdb", "String rep was expected to omit non printable characters");
   }
 
   function getGripStub(name) {
     switch (name) {
       case "testMultiline":
-      	 return "aaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbb\ncccccccccccccccc\n";
-	 break;
+        return "aaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbb\ncccccccccccccccc\n";
       case "testUseQuotes":
-	 return "abc";
+        return "abc";
+      case "testNonPritableCharacters":
+        return "a\x01b";
     }
   }
 });
 </script>
 </pre>
 </body>
 </html>