From 1170a1be76350e799fa7f1e61f16c91ef6187547 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 25 Jan 2015 17:44:07 -0500 Subject: [PATCH] Displaying the hex code of escaped characters This is only done where appropriate (escapes like "word boundary" don't have a hex value, and displaying it for hex or unicode escapes would be a little redundant). --- spec/parser/javascript/charset_escape_spec.js | 18 ++--- spec/parser/javascript/escape_spec.js | 16 ++--- src/js/parser/javascript/charset_escape.js | 2 +- src/js/parser/javascript/escape.js | 72 +++++++++++-------- 4 files changed, 62 insertions(+), 46 deletions(-) diff --git a/spec/parser/javascript/charset_escape_spec.js b/spec/parser/javascript/charset_escape_spec.js index e739892..c1b1dd2 100644 --- a/spec/parser/javascript/charset_escape_spec.js +++ b/spec/parser/javascript/charset_escape_spec.js @@ -5,21 +5,21 @@ import Snap from 'snapsvg'; describe('parser/javascript/charset_escape.js', function() { _.forIn({ - '\\b': { label: 'backspace', ordinal: 0x08 }, + '\\b': { label: 'backspace (0x08)', ordinal: 0x08 }, '\\d': { label: 'digit', ordinal: -1 }, '\\D': { label: 'non-digit', ordinal: -1 }, - '\\f': { label: 'form feed', ordinal: 0x0c }, - '\\n': { label: 'line feed', ordinal: 0x0a }, - '\\r': { label: 'carriage return', ordinal: 0x0d }, + '\\f': { label: 'form feed (0x0C)', ordinal: 0x0c }, + '\\n': { label: 'line feed (0x0A)', ordinal: 0x0a }, + '\\r': { label: 'carriage return (0x0D)', ordinal: 0x0d }, '\\s': { label: 'white space', ordinal: -1 }, '\\S': { label: 'non-white space', ordinal: -1 }, - '\\t': { label: 'tab', ordinal: 0x09 }, - '\\v': { label: 'vertical tab', ordinal: 0x0b }, + '\\t': { label: 'tab (0x09)', ordinal: 0x09 }, + '\\v': { label: 'vertical tab (0x0B)', ordinal: 0x0b }, '\\w': { label: 'word', ordinal: -1 }, '\\W': { label: 'non-word', ordinal: -1 }, - '\\0': { label: 'null', ordinal: 0 }, - '\\012': { label: 'octal: 12', ordinal: 10 }, - '\\cx': { label: 'ctrl-X', ordinal: 24 }, + '\\0': { label: 'null (0x00)', ordinal: 0 }, + '\\012': { label: 'octal: 12 (0x0A)', ordinal: 10 }, + '\\cx': { label: 'ctrl-X (0x18)', ordinal: 24 }, '\\xab': { label: '0xAB', ordinal: 0xab }, '\\uabcd': { label: 'U+ABCD', ordinal: 0xabcd } }, (content, str) => { diff --git a/spec/parser/javascript/escape_spec.js b/spec/parser/javascript/escape_spec.js index 04e3b44..d47a90c 100644 --- a/spec/parser/javascript/escape_spec.js +++ b/spec/parser/javascript/escape_spec.js @@ -9,16 +9,16 @@ describe('parser/javascript/escape.js', function() { '\\B': { label: 'non-word boundary', ordinal: -1 }, '\\d': { label: 'digit', ordinal: -1 }, '\\D': { label: 'non-digit', ordinal: -1 }, - '\\f': { label: 'form feed', ordinal: 0x0c }, - '\\n': { label: 'line feed', ordinal: 0x0a }, - '\\r': { label: 'carriage return', ordinal: 0x0d }, + '\\f': { label: 'form feed (0x0C)', ordinal: 0x0c }, + '\\n': { label: 'line feed (0x0A)', ordinal: 0x0a }, + '\\r': { label: 'carriage return (0x0D)', ordinal: 0x0d }, '\\s': { label: 'white space', ordinal: -1 }, '\\S': { label: 'non-white space', ordinal: -1 }, - '\\t': { label: 'tab', ordinal: 0x09 }, - '\\v': { label: 'vertical tab', ordinal: 0x0b }, + '\\t': { label: 'tab (0x09)', ordinal: 0x09 }, + '\\v': { label: 'vertical tab (0x0B)', ordinal: 0x0b }, '\\w': { label: 'word', ordinal: -1 }, '\\W': { label: 'non-word', ordinal: -1 }, - '\\0': { label: 'null', ordinal: 0 }, + '\\0': { label: 'null (0x00)', ordinal: 0 }, '\\1': { label: 'Back reference (group = 1)', ordinal: -1 }, '\\2': { label: 'Back reference (group = 2)', ordinal: -1 }, '\\3': { label: 'Back reference (group = 3)', ordinal: -1 }, @@ -28,8 +28,8 @@ describe('parser/javascript/escape.js', function() { '\\7': { label: 'Back reference (group = 7)', ordinal: -1 }, '\\8': { label: 'Back reference (group = 8)', ordinal: -1 }, '\\9': { label: 'Back reference (group = 9)', ordinal: -1 }, - '\\012': { label: 'octal: 12', ordinal: 10 }, - '\\cx': { label: 'ctrl-X', ordinal: 24 }, + '\\012': { label: 'octal: 12 (0x0A)', ordinal: 10 }, + '\\cx': { label: 'ctrl-X (0x18)', ordinal: 24 }, '\\xab': { label: '0xAB', ordinal: 0xab }, '\\uabcd': { label: 'U+ABCD', ordinal: 0xabcd } }, (content, str) => { diff --git a/src/js/parser/javascript/charset_escape.js b/src/js/parser/javascript/charset_escape.js index 8690268..507a1d0 100644 --- a/src/js/parser/javascript/charset_escape.js +++ b/src/js/parser/javascript/charset_escape.js @@ -4,5 +4,5 @@ import Escape from './escape.js'; export default _.extend({}, Escape, { type: 'charset-escape', - b: ['backspace', 0x08] + b: ['backspace', 0x08, true] }); diff --git a/src/js/parser/javascript/escape.js b/src/js/parser/javascript/escape.js index 27eaf8e..0720fd5 100644 --- a/src/js/parser/javascript/escape.js +++ b/src/js/parser/javascript/escape.js @@ -1,5 +1,15 @@ import _ from 'lodash'; +function hex(value) { + var str = value.toString(16).toUpperCase(); + + if (str.length < 2) { + str = '0' + str; + } + + return `(0x${str})`; +} + export default { type: 'escape', @@ -14,48 +24,54 @@ export default { }, setup() { + var addHex; + this.code = this.properties.esc.properties.code.textValue; this.arg = this.properties.esc.properties.arg.textValue; - [this.label, this.ordinal] = _.result(this, this.code); + [this.label, this.ordinal, addHex] = _.result(this, this.code); + + if (addHex) { + this.label = `${this.label} ${hex(this.ordinal)}`; + } }, // Escape code mappings - b: ['word boundary', -1], - B: ['non-word boundary', -1], - d: ['digit', -1], - D: ['non-digit', -1], - f: ['form feed', 0x0c], - n: ['line feed', 0x0a], - r: ['carriage return', 0x0d], - s: ['white space', -1], - S: ['non-white space', -1], - t: ['tab', 0x09], - v: ['vertical tab', 0x0b], - w: ['word', -1], - W: ['non-word', -1], - 1: ['Back reference (group = 1)', -1], - 2: ['Back reference (group = 2)', -1], - 3: ['Back reference (group = 3)', -1], - 4: ['Back reference (group = 4)', -1], - 5: ['Back reference (group = 5)', -1], - 6: ['Back reference (group = 6)', -1], - 7: ['Back reference (group = 7)', -1], - 8: ['Back reference (group = 8)', -1], - 9: ['Back reference (group = 9)', -1], + b: ['word boundary', -1, false], + B: ['non-word boundary', -1, false], + d: ['digit', -1, false], + D: ['non-digit', -1, false], + f: ['form feed', 0x0c, true], + n: ['line feed', 0x0a, true], + r: ['carriage return', 0x0d, true], + s: ['white space', -1, false], + S: ['non-white space', -1, false], + t: ['tab', 0x09, true], + v: ['vertical tab', 0x0b, true], + w: ['word', -1, false], + W: ['non-word', -1, false], + 1: ['Back reference (group = 1)', -1, false], + 2: ['Back reference (group = 2)', -1, false], + 3: ['Back reference (group = 3)', -1, false], + 4: ['Back reference (group = 4)', -1, false], + 5: ['Back reference (group = 5)', -1, false], + 6: ['Back reference (group = 6)', -1, false], + 7: ['Back reference (group = 7)', -1, false], + 8: ['Back reference (group = 8)', -1, false], + 9: ['Back reference (group = 9)', -1, false], 0() { if (this.arg) { - return [`octal: ${this.arg}`, parseInt(this.arg, 8)]; + return [`octal: ${this.arg}`, parseInt(this.arg, 8), true]; } else { - return ['null', 0]; + return ['null', 0, true]; } }, c() { - return [`ctrl-${this.arg.toUpperCase()}`, this.arg.toUpperCase().charCodeAt(0) - 64]; + return [`ctrl-${this.arg.toUpperCase()}`, this.arg.toUpperCase().charCodeAt(0) - 64, true]; }, x() { - return [`0x${this.arg.toUpperCase()}`, parseInt(this.arg, 16)]; + return [`0x${this.arg.toUpperCase()}`, parseInt(this.arg, 16), false]; }, u() { - return [`U+${this.arg.toUpperCase()}`, parseInt(this.arg, 16)]; + return [`U+${this.arg.toUpperCase()}`, parseInt(this.arg, 16), false]; } };