diff --git a/src/formats/newick.js b/src/formats/newick.js index 398dfb0..39082b2 100644 --- a/src/formats/newick.js +++ b/src/formats/newick.js @@ -252,8 +252,9 @@ export function getNewick(annotator, root) { if(n.data.name !== 'root') { const node_label = n.data.name.replaceAll("'", "''"); - // Escape the entire string if it contains any whitespace. - if (/\w/.test(node_label)) { + // Surround the entire string with single quotes if it contains any + // non-alphanumeric characters. + if (/\W/.test(node_label)) { element_array.push("'" + node_label + "'"); } else { element_array.push(node_label); diff --git a/test/formats-test.js b/test/formats-test.js index 56ccf09..85b30db 100644 --- a/test/formats-test.js +++ b/test/formats-test.js @@ -82,10 +82,10 @@ tape("Handle Newick strings with spaces", function(test) { * can read and write Newick strings correctly. */ - let nwk = "('Alpha beta', ('Alpha gamma', ('Delta''s epsilon', 'Epsilon zeta ''alphonso''', 'test''')))"; + let nwk = "('Alpha beta', ('Alpha gamma', ('Delta''s epsilon', 'Epsilon zeta ''alphonso''', 'test''s')))"; let phylo = new phylotree.phylotree(nwk); - let test_leaves = ['Alpha beta', 'Alpha gamma', "Delta's epsilon", "Epsilon zeta 'alphonso'", "test'"]; + let test_leaves = ['Alpha beta', 'Alpha gamma', "Delta's epsilon", "Epsilon zeta 'alphonso'", "test's"]; test_leaves.forEach(function(leaf) { let node = phylo.getNodeByName(leaf); test.equal(node.data.name, leaf); @@ -94,7 +94,7 @@ tape("Handle Newick strings with spaces", function(test) { // This would be identical to the original Newick string, but phylotree.js coerces // lengths to 1 (see https://github.com/veg/phylotree.js/issues/440). So we expect // all lengths to be set to 1 on export. - test.equal(phylo.getNewick(), "('Alpha beta':1,('Alpha gamma':1,('Delta''s epsilon':1,'Epsilon zeta ''alphonso''':1,'test''':1):1):1):1;"); + test.equal(phylo.getNewick(), "('Alpha beta':1,('Alpha gamma':1,('Delta''s epsilon':1,'Epsilon zeta ''alphonso''':1,'test''s':1):1):1):1;"); test.end(); });