Merge pull request #265 from strk/master-quote-amp

Quote ampersend chars in XML text. Closes #263.
This commit is contained in:
Tom MacWright 2013-03-29 08:44:59 -07:00
commit 435452ba50
2 changed files with 10 additions and 6 deletions

View File

@ -8,7 +8,11 @@ tree.Quoted.prototype = {
is: 'string',
toString: function(quotes) {
var xmlvalue = this.value.replace(/\'/g, ''');
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;
},

View File

@ -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 quoted'");
assert.equal(f.toString(true), "'Tom&apos;s &amp; &quot;&lt;quoted&gt;&quot;'");
});
});
});