Fix escaping of single quote. Closes #307, updates tests.

This commit is contained in:
Sandro Santilli 2013-10-03 13:40:50 +02:00
parent 761b05b599
commit fa994557e8
3 changed files with 30 additions and 3 deletions

View File

@ -7,8 +7,12 @@ tree.Quoted = function Quoted(content) {
tree.Quoted.prototype = {
toString: function(quotes) {
var xmlvalue = this.value.replace(/&/g, '&');
xmlvalue = xmlvalue.replace(/\'/g, ''');
var xmlvalue = this.value
.replace(/&/g, '&')
.replace(/\'/g, '\\\'')
.replace(/\"/g, '"')
.replace(/</g, '&lt;')
.replace(/\>/g, '&gt;');
return (quotes === true) ? "'" + xmlvalue + "'" : this.value;
},

23
test/quoted.test.js Normal file
View File

@ -0,0 +1,23 @@
var assert = require('assert');
var tree = require('../lib/carto/tree.js');
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>\"");
assert.ok(f);
assert.equal(f.is, 'string');
});
it('should produce normal output', function() {
var f = new tree.Quoted("Tom's & \"<quoted>\"");
assert.ok(f);
assert.equal(f.toString(), "Tom's & \"<quoted>\"");
});
it('should produce xml-friendly output', function() {
var f = new tree.Quoted("Tom's & \"<quoted>\"");
assert.ok(f);
assert.equal(f.toString(true), "'Tom\\'s &amp; &quot;&lt;quoted&gt;&quot;'");
});
});
});

View File

@ -5,7 +5,7 @@
<Style name="world" filter-mode="first">
<Rule>
<Filter>([name2] = ' Sa&apos;ad')</Filter>
<Filter>([name2] = ' Sa\'ad')</Filter>
<PolygonSymbolizer fill="#ffffff" />
</Rule>
</Style>