Skip to content

Commit

Permalink
#243 Escape xpath names when they contain tabs or quotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Jun 3, 2021
1 parent 29b88df commit 372d3c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 13 additions & 2 deletions uSync8.Core/Tracking/SyncBaseTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,21 @@ private string MakeKeyPath(string key, string keyValue, bool isAttribute)
{
if (isAttribute)
{
return $"[@{key} = '{keyValue}']";
return $"[@{key} = {EscapeXPathString(keyValue)}]";
}

return $"[{key} = '{keyValue}']";
return $"[{key} = {EscapeXPathString(keyValue)}]";
}

private string EscapeXPathString(string value)
{
if (!value.Contains("'"))
return '\'' + value + '\'';

if (!value.Contains("\""))
return '"' + value + '"';

return "concat('" + value.Replace("'", "',\"'\",'") + "')";
}

private XElement GetTarget(IEnumerable<XElement> items, string key, string value, bool isAttribute)
Expand Down
14 changes: 13 additions & 1 deletion uSync8.Core/Tracking/SyncXmlTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,26 @@ private string MakeSelectionPath(XElement node, string keys)
var value = GetKeyValue(node, key);
if (!string.IsNullOrWhiteSpace(value))
{
selectionPath += $"[{key} = '{value}']";
selectionPath += $"[{key} = {EscapeXPathString(value)}]";
}

}

return selectionPath.Replace("][", " and ");
}

private string EscapeXPathString(string value)
{
if (!value.Contains("'"))
return '\'' + value + '\'';

if (!value.Contains("\""))
return '"' + value + '"';

return "concat('" + value.Replace("'", "',\"'\",'") + "')";
}


private string MakeSelectionName(XElement node, string keys)
{
var names = new List<string>();
Expand Down

0 comments on commit 372d3c3

Please sign in to comment.