Skip to content

Commit ecb2412

Browse files
authored
fixing prefix always being added (#1577)
1 parent 4c0a6be commit ecb2412

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

Src/CSharpier.Core/Xml/XNodePrinters/Element.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Doc PrintLineAfterChildren()
6161
return Doc.Group(
6262
Doc.GroupWithId(attrGroupId, Tag.PrintOpeningTag(node, context)),
6363
elementContent,
64-
Tag.PrintClosingTag(node)
64+
Tag.PrintClosingTag(node, context)
6565
);
6666
}
6767

Src/CSharpier.Core/Xml/XNodePrinters/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal static Doc Print(XNode xNode, XmlPrintingContext context)
4343
[
4444
Tag.PrintOpeningTagPrefix(xText),
4545
GetEncodedTextValue(xText),
46-
Tag.PrintClosingTagSuffix(xText),
46+
Tag.PrintClosingTagSuffix(xText, context),
4747
];
4848

4949
if (doc.All(o => o is StringDoc))

Src/CSharpier.Core/Xml/XNodePrinters/Tag.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ internal static class Tag
88
public static Doc PrintOpeningTag(XElement element, XmlPrintingContext context)
99
{
1010
return Doc.Concat(
11-
PrintOpeningTagStart(element),
11+
PrintOpeningTagStart(element, context),
1212
Attributes.Print(element, context),
1313
element.IsEmpty ? Doc.Null : PrintOpeningTagEnd(element)
1414
);
1515
}
1616

17-
private static Doc PrintOpeningTagStart(XElement element)
17+
private static Doc PrintOpeningTagStart(XElement element, XmlPrintingContext context)
1818
{
1919
return
2020
element.PreviousNode is not null
2121
&& NeedsToBorrowNextOpeningTagStartMarker(element.PreviousNode)
2222
? Doc.Null
23-
: Doc.Concat(PrintOpeningTagPrefix(element), PrintOpeningTagStartMarker(element));
23+
: Doc.Concat(
24+
PrintOpeningTagPrefix(element),
25+
PrintOpeningTagStartMarker(element, context)
26+
);
2427
}
2528

2629
private static Doc PrintOpeningTagEnd(XElement node)
@@ -60,37 +63,40 @@ private static bool NeedsToBorrowPrevClosingTagEndMarker(XNode node)
6063
;
6164
}
6265

63-
public static Doc PrintClosingTag(XElement node)
66+
public static Doc PrintClosingTag(XElement node, XmlPrintingContext context)
6467
{
6568
return Doc.Concat(
66-
node.IsEmpty ? Doc.Null : PrintClosingTagStart(node),
67-
PrintClosingTagEnd(node)
69+
node.IsEmpty ? Doc.Null : PrintClosingTagStart(node, context),
70+
PrintClosingTagEnd(node, context)
6871
);
6972
}
7073

71-
public static Doc PrintClosingTagStart(XElement element)
74+
public static Doc PrintClosingTagStart(XElement element, XmlPrintingContext context)
7275
{
7376
var lastChild = element.Nodes().LastOrDefault();
7477

7578
return lastChild is not null && NeedsToBorrowParentClosingTagStartMarker(lastChild)
7679
? Doc.Null
77-
: Doc.Concat(PrintClosingTagPrefix(element), PrintClosingTagStartMarker(element));
80+
: Doc.Concat(
81+
PrintClosingTagPrefix(element),
82+
PrintClosingTagStartMarker(element, context)
83+
);
7884
}
7985

80-
public static Doc PrintClosingTagStartMarker(XElement element)
86+
public static Doc PrintClosingTagStartMarker(XElement element, XmlPrintingContext context)
8187
{
82-
return $"</{GetPrefixedElementName(element)}";
88+
return $"</{GetPrefixedElementName(element, context)}";
8389
}
8490

85-
public static Doc PrintClosingTagEnd(XElement node)
91+
public static Doc PrintClosingTagEnd(XElement node, XmlPrintingContext context)
8692
{
8793
return (
8894
node.NextNode is not null
8995
? NeedsToBorrowPrevClosingTagEndMarker(node.NextNode)
9096
: NeedsToBorrowLastChildClosingTagEndMarker(node.Parent!)
9197
)
9298
? Doc.Null
93-
: Doc.Concat(PrintClosingTagEndMarker(node), PrintClosingTagSuffix(node));
99+
: Doc.Concat(PrintClosingTagEndMarker(node), PrintClosingTagSuffix(node, context));
94100
}
95101

96102
public static bool NeedsToBorrowLastChildClosingTagEndMarker(XElement node)
@@ -116,29 +122,32 @@ public static Doc PrintClosingTagEndMarker(XElement node)
116122
return node.IsEmpty ? "/>" : ">";
117123
}
118124

119-
public static Doc PrintClosingTagSuffix(XNode node)
125+
public static Doc PrintClosingTagSuffix(XNode node, XmlPrintingContext context)
120126
{
121127
return NeedsToBorrowParentClosingTagStartMarker(node)
122-
? PrintClosingTagStartMarker(node.Parent!)
128+
? PrintClosingTagStartMarker(node.Parent!, context)
123129
: NeedsToBorrowNextOpeningTagStartMarker(node)
124-
? PrintOpeningTagStartMarker(node.NextNode!)
130+
? PrintOpeningTagStartMarker(node.NextNode!, context)
125131
: Doc.Null;
126132
}
127133

128-
private static Doc PrintOpeningTagStartMarker(XNode node)
134+
private static Doc PrintOpeningTagStartMarker(XNode node, XmlPrintingContext context)
129135
{
130136
if (node is not XElement element)
131137
{
132138
return "<" + node;
133139
}
134140

135-
return $"<{GetPrefixedElementName(element)}";
141+
return $"<{GetPrefixedElementName(element, context)}";
136142
}
137143

138-
private static string GetPrefixedElementName(XElement element)
144+
private static string GetPrefixedElementName(XElement element, XmlPrintingContext context)
139145
{
140146
var prefix = element.GetPrefixOfNamespace(element.Name.Namespace);
141-
if (string.IsNullOrEmpty(prefix))
147+
if (
148+
string.IsNullOrEmpty(prefix)
149+
|| !context.Mapping[element].Name.StartsWith(prefix, StringComparison.Ordinal)
150+
)
142151
{
143152
return element.Name.LocalName;
144153
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Metadata
2+
xmlns="http://schemas.xmlsoap.org/ws/2004/09/mex"
3+
xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"
4+
>
5+
<wsx:MetadataSection
6+
Dialect="http://schemas.xmlsoap.org/wsdl/"
7+
Identifier="http://myns/echo"
8+
xmlns=""
9+
></wsx:MetadataSection>
10+
</Metadata>

0 commit comments

Comments
 (0)