in src/XmlNotepad/Commands.cs [846:926]
public ChangeNode(XmlTreeView view, XmlTreeNode node, XmlNodeType nt)
{
this._doc = view.Model.Document;
this._nt = nt;
this._view = view;
this._node = node;
XmlNode n = node.Node;
if (n == null) return;
init:
this._oldnt = n.NodeType;
string innerXml = (_oldnt == XmlNodeType.Element) ? n.InnerXml : SpecialUnescape(_oldnt, n.Value);
string outerXml = n.OuterXml;
string qname = n.Name;
string localName = n.LocalName;
string ns = n.NamespaceURI;
bool noName = false;
if (qname.StartsWith("#"))
{
qname = localName = "";
}
noName = string.IsNullOrEmpty(qname);
if (noName && IsNamedNodeType(nt))
{
// Try parsing the content of the node as markup! (but first check for special unescaping
// that we do for nested comment/cdata blocks)
PasteCommand paste = new PasteCommand(_doc, view, InsertPosition.Before, new TreeData(innerXml));
XmlTreeNode nte = paste.NewNode;
if (nte != null && IsNamedNodeType(nte.NodeType))
{
// then it worked - we extracted a node with a name, so start over.
n = _newNode = nte.Node;
goto init;
}
}
if (_newNode == null || _newNode.NodeType != nt)
{
switch (nt)
{
case XmlNodeType.Element:
if (noName)
{
qname = "element";
}
_newNode = _doc.CreateElement(qname, ns);
_newNode.InnerXml = innerXml;
break;
case XmlNodeType.Attribute:
if (noName)
{
qname = "attribute";
}
_newNode = _doc.CreateAttribute(qname, ns);
_newNode.Value = innerXml;
break;
case XmlNodeType.Comment:
_newNode = _doc.CreateComment(SpecialEscape(nt, noName ? innerXml : outerXml));
break;
case XmlNodeType.ProcessingInstruction:
if (noName)
{
localName = "pi";
}
_newNode = _doc.CreateProcessingInstruction(localName, innerXml);
break;
case XmlNodeType.Text:
_newNode = _doc.CreateTextNode(noName ? innerXml : outerXml);
break;
case XmlNodeType.CDATA:
_newNode = _doc.CreateCDataSection(SpecialEscape(nt, noName ? innerXml : outerXml));
break;
}
}
InsertNode icmd = new InsertNode(node, InsertPosition.Before, _newNode, true, true);
_newTreeNode = icmd.NewNode;
DeleteNode del = new DeleteNode(_doc, node);
_group = new CompoundCommand(this.Name);
_group.Add(icmd);
_group.Add(del);
}