Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Quote ampersend chars in XML text. Closes #263. #265

Merged
merged 2 commits into from Mar 29, 2013
Jump to file or symbol
Failed to load files and symbols.
+10 −6
Split
View
@@ -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;');
@tmcw

tmcw Mar 27, 2013

Contributor

These should be chained:

var xmlvalue = this.value.replace(/&/g, '&amp;')
    .replace(/\"/g, '&quot;');

etc instead of creating 4 new temp strings.

@strk

strk Mar 27, 2013

Contributor

I belive Temporary strings would be created anyway, just unnamed.
If you want to avoid that I guess you can list all characters
in the pattern and provide a function that does a lookup to determine
the replacement. This should be done after the first pass escaping
the ampersend itself.

return (quotes === true) ? "'" + xmlvalue + "'" : this.value;
},
View
@@ -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;'");
});
});
});