Skip to content

Commit

Permalink
Allow escape quotes characters in actor name
Browse files Browse the repository at this point in the history
Make it possible to write almost anything as an actor name:

    "arrow-> and quotes\" in the actor name" -> bar : foo
  • Loading branch information
eloyesp committed Jul 18, 2017
1 parent 78e00d3 commit 56b1427
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/diagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ Diagram.Note.prototype.hasManyActors = function() {
};

Diagram.unescape = function(s) {
// Turn "\\n" into "\n"
return s.trim().replace(/^"(.*)"$/m, '$1').replace(/\\n/gm, '\n');
var unescaped = s.trim()
.replace(/^"(.*)"$/m, '$1') // remove quotes on quoted actors
.replace(/\\n/gm, '\n') // turn "\\n" into "\n"
.replace(/\\"/gm, '"'); // turn '\"' into '"'
return unescaped;
};

Diagram.LINETYPE = {
Expand Down
2 changes: 1 addition & 1 deletion src/grammar.jison
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"title" return 'title';
"," return ',';
[^\->:,\r\n"]+ return 'ACTOR';
\"[^"]+\" return 'ACTOR';
\"(([^\"]|\\\")*[^\\])?\" return 'ACTOR'; // quoted actors
"--" return 'DOTLINE';
"-" return 'LINE';
">>" return 'OPENARROW';
Expand Down
5 changes: 5 additions & 0 deletions test/grammar-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ test('Quoted names', function() {
assertSingleActor(Diagram.parse('Participant "->:"'), '->:');
});

test('Quoted names with escape characters', function() {
assertSingleArrow(Diagram.parse('"->\\":"->B: M'), ARROWTYPE.FILLED, LINETYPE.SOLID,
'->":', 'B', 'M');
});

test('API', function() {
// Public API
ok(typeof Diagram.parse == 'function');
Expand Down

0 comments on commit 56b1427

Please sign in to comment.