Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Quote all needed XML chars. See #263.
Browse files Browse the repository at this point in the history
Includes testcase.
  • Loading branch information
Sandro Santilli committed Mar 27, 2013
1 parent acf94e5 commit d6585d3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/carto/tree/quoted.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ tree.Quoted.prototype = {
toString: function(quotes) {
var xmlvalue = this.value.replace(/&/g, '&');
xmlvalue = xmlvalue.replace(/\'/g, ''');
xmlvalue = xmlvalue.replace(/\"/g, '"');
xmlvalue = xmlvalue.replace(/\</g, '&lt;');
xmlvalue = xmlvalue.replace(/\>/g, '&gt;');
return (quotes === true) ? "'" + xmlvalue + "'" : this.value;
},

Expand Down
10 changes: 5 additions & 5 deletions test/quoted.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ require('../lib/carto/tree/quoted');
describe('Quoted', function() {
describe('basic functionality', function() {
it('should be constructed', function() {
var f = new tree.Quoted("Tom's & quoted");
var f = new tree.Quoted("Tom's & \"<quoted>\"");
assert.ok(f);
assert.equal(f.is, 'string');
});
it('should produce normal output', function() {
var f = new tree.Quoted("Tom's & quoted");
var f = new tree.Quoted("Tom's & \"<quoted>\"");
assert.ok(f);
assert.equal(f.toString(), "Tom's & quoted");
assert.equal(f.toString(), "Tom's & \"<quoted>\"");
});
it('should produce xml-friendly output', function() {
var f = new tree.Quoted("Tom's & quoted");
var f = new tree.Quoted("Tom's & \"<quoted>\"");
assert.ok(f);
assert.equal(f.toString(true), "'Tom&apos;s &amp; quoted'");
assert.equal(f.toString(true), "'Tom&apos;s &amp; &quot;&lt;quoted&gt;&quot;'");
});
});
});

0 comments on commit d6585d3

Please sign in to comment.