This project is read-only.

Better handling of unknown/unhandled node types in SVG document

Sep 8, 2010 at 12:25 PM
Edited Sep 8, 2010 at 12:28 PM

Hi,

I was trying to load an SVG created using InkScape. The SVG had a node called "marker", which is not handled in the current implementation. When SvgElementFactory encounters this node, it returns a null. Here is the relevant portion of code from SvgDocument.cs (in function public static SvgDocument Open(Stream stream, Dictionary<string, string> entities).

 

if (elementStack.Count > 0)
{
element = SvgElementFactory.CreateElement(reader, svgDocument);
}
else
{
element = SvgElementFactory.CreateDocument(reader);
svgDocument = (SvgDocument)element;
}

if (element == null)
{
continue;
}

 

As you can see, the loop is skipped  and reading continues on the the next node (which could be a child of the unknown / unhandled node type). The better approach would be to skip the node and it's children and move to the next sibling as shown below:

 

if (elementStack.Count > 0)
{
element = SvgElementFactory.CreateElement(reader, svgDocument);
}
else
{
Debug.Assert(svgDocument == null);
element = SvgElementFactory.CreateDocument(reader);
svgDocument = (SvgDocument)element;
}

if (element == null)
{
// skip all child nodes of unknown element
if (!elementEmpty)
{
Trace.TraceWarning("Unknown/unhandled element type '" + reader.LocalName + "' found. Skip child nodes");

int depth = reader.Depth;
reader.ReadStartElement();
while (!reader.EOF && reader.Depth > depth)
{
reader.Read();
}
}

continue;
}

 

Regards,

- Vikram