Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve whitespace for space only text node: <text> </text> #1211

Merged
merged 3 commits into from Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/wsdl/index.ts
Expand Up @@ -356,7 +356,9 @@ export class WSDL {
const name = splitQName(nsName).name;

if (typeof cur.schema === 'string' && (cur.schema === 'string' || cur.schema.split(':')[1] === 'string')) {
if (typeof obj === 'object' && Object.keys(obj).length === 0) { obj = cur.object = ''; }
if (typeof obj === 'object' && Object.keys(obj).length === 0) {
obj = cur.object = (this.options.preserveWhitespace ? cur.text || '' : '');
}
}

if (cur.nil === true) {
Expand Down Expand Up @@ -423,13 +425,17 @@ export class WSDL {
};

p.ontext = (text) => {
const top = stack[stack.length - 1];

const originalText = text;
text = trim(text);
if (!text.length) {
if (this.options.preserveWhitespace) {
top.text = (top.text || '') + originalText;
}
return;
}

const top = stack[stack.length - 1];
const name = splitQName(top.schema).name;
let value;

Expand Down
62 changes: 62 additions & 0 deletions test/response-preserve-whitespace-test.js
@@ -0,0 +1,62 @@
'use strict';

var request = require('request');
var assert = require('assert');
var http = require('http');
var soap = require('../');
var server;
var port;

describe('Preverse whitespace', function() {
var wsdl = __dirname + '/wsdl/hello.wsdl';

before(function(done) {
server = http.createServer(function(req, res) {
res.statusCode = 200;
res.end('"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tns=\"http://www.examples.com/wsdl/HelloService.wsdl\"><soap:Body><tns:sayHelloResponse><tns:greeting> </tns:greeting></tns:sayHelloResponse></soap:Body></soap:Envelope>"');
}).listen(51515, done);
});

after(function() {
server.close();
});

it('preserves leading and trailing whitespace when preserveWhitespace option is true',
function(done) {
var url = 'http://' + server.address().address + ':' + server.address().port;

if (server.address().address === '0.0.0.0' || server.address().address === '::') {
url = 'http://127.0.0.1:' + server.address().port;
}

soap.createClient(
wsdl,
{
endpoint: url,
disableCache: true, // disable wsdl cache, otherwise 'mocha test/client-response-options-test.js test/response-preserve-whitespace-test.js' will fail.
preserveWhitespace: true
},
function(err, client) {
if (err) {
console.log(err);
throw err;
}

client.sayHello(
{
firstName: 'hello world'
},
function(err, result, rawResponse, soapHeader, rawRequest) {
if (err) {
console.log(err);
throw err;
}
assert.equal(' ', result.greeting);
done();
}
);
}
);
});

});