Skip to content

Commit

Permalink
DYN-1261 Escape StringInputNode value when converting NodeToCode (#11295
Browse files Browse the repository at this point in the history
)

* Escape StringInputNode value when converting NodeToCode

* Updated workflow comment

* Added NodeToCode StringInput escaping test

* Restructured NodeToCode StringInput escaping test

* Escape StringInput value when compiling NodeToCode
  • Loading branch information
StudioLE authored and QilongTang committed Dec 4, 2020
1 parent e919f17 commit 9845a7f
Show file tree
Hide file tree
Showing 3 changed files with 510 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Libraries/CoreNodeModels/Input/BaseTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ protected override void DeserializeCore(XmlElement nodeElement, SaveContext cont

internal override IEnumerable<AssociativeNode> BuildAst(List<AssociativeNode> inputAstNodes, CompilationContext context)
{
var rhs = AstFactory.BuildStringNode(Value);
string value = Value;
if (context == CompilationContext.NodeToCode)
{
value = value.Replace(@"\", @"\\")
.Replace("\"", "\\\"");
}
var rhs = AstFactory.BuildStringNode(value);
var assignment = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), rhs);

return new[] { assignment };
Expand Down
30 changes: 30 additions & 0 deletions test/DynamoCoreTests/NodeToCodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,36 @@ public void TestNodeToCodeUndoRecorder()
Assert.AreEqual(0, recorder.ActionCount());
}

[Test]
public void TestNodeToCodeStringInputEscaping()
{
// Arrange
OpenModel(@"core\node2code\stringNodesInNeedOfEscaping.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;
var engine = CurrentDynamoModel.EngineController;

// Act
var nodesToCode = engine.ConvertNodesToCode(nodes, nodes);
var results = nodesToCode.AstNodes.OfType<BinaryExpressionNode>()
.Where((x, i) => i < 8)
.Select(x => x.RightNode.ToString())
.ToList();

// Assert
var expect = new List<string>
{
"\"C:\\\\\"", // "C:\\"
"\"4\\\"\"", // "4\""
"\"\\\"Hello, world.\\\"\"", // "\"Hello, world.\""
"\"Hello\\\\r\\\\nWorld\"", // "Hello\\r\\nWorld"
"\"\\\\tHello World\"", // "\\tHello World"
"\"\\\\u33A1\"", // "\\u33A1"
"\"\\\\u00B2\"", // "\\u00B2"
"\"\\\\\\\\SERVER\\\\PATH\"" // "\\\\SERVER\\PATH"
};
Assert.AreEqual(expect, results);
}

[Test]
public void TestUINode_String()
{
Expand Down
Loading

0 comments on commit 9845a7f

Please sign in to comment.