Commit 1a5baee0 authored by Gertjan van Heertum's avatar Gertjan van Heertum
Browse files

Partial solution for threading issues

parent 537fc79e
......@@ -9,9 +9,9 @@ namespace Svg.Css
{
internal static class CssQuery
{
public static IEnumerable<SvgElement> QuerySelectorAll(this SvgElement elem, string selector)
public static IEnumerable<SvgElement> QuerySelectorAll(this SvgElement elem, string selector, SvgElementFactory elementFactory)
{
var generator = new SelectorGenerator<SvgElement>(new SvgElementOps());
var generator = new SelectorGenerator<SvgElement>(new SvgElementOps(elementFactory));
Fizzler.Parser.Parse(selector, generator);
return generator.Selector(Enumerable.Repeat(elem, 1));
}
......
......@@ -8,10 +8,17 @@ namespace Svg.Css
{
internal class SvgElementOps : IElementOps<SvgElement>
{
private readonly SvgElementFactory _elementFactory;
public SvgElementOps(SvgElementFactory elementFactory)
{
_elementFactory = elementFactory;
}
public Selector<SvgElement> Type(NamespacePrefix prefix, string name)
{
SvgElementFactory.ElementInfo type = null;
if (SvgElementFactory.AvailableElements.TryGetValue(name, out type))
if (_elementFactory.AvailableElements.TryGetValue(name, out type))
{
return nodes => nodes.Where(n => n.GetType() == type.ElementType);
}
......
......@@ -243,6 +243,7 @@ namespace Svg
SvgElement element = null;
SvgElement parent;
T svgDocument = null;
var elementFactory = new SvgElementFactory();
var styles = new List<ISvgNode>();
......@@ -259,11 +260,11 @@ namespace Svg
// Create element
if (elementStack.Count > 0)
{
element = SvgElementFactory.CreateElement(reader, svgDocument);
element = elementFactory.CreateElement(reader, svgDocument);
}
else
{
svgDocument = SvgElementFactory.CreateDocument<T>(reader);
svgDocument = elementFactory.CreateDocument<T>(reader);
element = svgDocument;
}
......@@ -349,7 +350,7 @@ namespace Svg
foreach (var selector in selectors)
{
elemsToStyle = svgDocument.QuerySelectorAll(rule.Selector.ToString());
elemsToStyle = svgDocument.QuerySelectorAll(rule.Selector.ToString(), elementFactory);
foreach (var elem in elemsToStyle)
{
foreach (var decl in rule.Declarations)
......
......@@ -14,13 +14,13 @@ namespace Svg
/// </summary>
internal class SvgElementFactory
{
private static Dictionary<string, ElementInfo> availableElements;
private static Parser cssParser = new Parser();
private Dictionary<string, ElementInfo> availableElements;
private Parser cssParser = new Parser();
/// <summary>
/// Gets a list of available types that can be used when creating an <see cref="SvgElement"/>.
/// </summary>
public static Dictionary<string, ElementInfo> AvailableElements
public Dictionary<string, ElementInfo> AvailableElements
{
get
{
......@@ -47,7 +47,7 @@ namespace Svg
/// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into an <see cref="SvgDocument"/>.</param>
/// <exception cref="ArgumentNullException">The <paramref name="reader"/> parameter cannot be <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">The CreateDocument method can only be used to parse root &lt;svg&gt; elements.</exception>
public static T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new()
public T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new()
{
if (reader == null)
{
......@@ -68,7 +68,7 @@ namespace Svg
/// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into a subclass of <see cref="SvgElement"/>.</param>
/// <param name="document">The <see cref="SvgDocument"/> that the created element belongs to.</param>
/// <exception cref="ArgumentNullException">The <paramref name="reader"/> and <paramref name="document"/> parameters cannot be <c>null</c>.</exception>
public static SvgElement CreateElement(XmlReader reader, SvgDocument document)
public SvgElement CreateElement(XmlReader reader, SvgDocument document)
{
if (reader == null)
{
......@@ -78,7 +78,7 @@ namespace Svg
return CreateElement<SvgDocument>(reader, false, document);
}
private static SvgElement CreateElement<T>(XmlReader reader, bool fragmentIsDocument, SvgDocument document) where T : SvgDocument, new()
private SvgElement CreateElement<T>(XmlReader reader, bool fragmentIsDocument, SvgDocument document) where T : SvgDocument, new()
{
SvgElement createdElement = null;
string elementName = reader.LocalName;
......@@ -122,7 +122,7 @@ namespace Svg
return createdElement;
}
private static void SetAttributes(SvgElement element, XmlReader reader, SvgDocument document)
private void SetAttributes(SvgElement element, XmlReader reader, SvgDocument document)
{
//Trace.TraceInformation("Begin SetAttributes");
......
......@@ -68,8 +68,8 @@ namespace Svg
return new SizeF(rect.Width, Ascent(renderer));
}
private static Graphics _graphics;
private static Graphics GetGraphics(object renderer)
private Graphics _graphics;
private Graphics GetGraphics(object renderer)
{
var provider = renderer as IGraphicsProvider;
if (provider == null)
......
......@@ -34,17 +34,9 @@ namespace Svg.UnitTests
public void TestMultiThread()
{
bool valid = true;
Parallel.For(0, 10, (x) =>
{
try
Parallel.For(0, 3, (x) =>
{
LoadFile();
}
catch (Exception e)
{
Trace.WriteLine("Run error in parallel: " + e.Message);
valid = false;
}
});
Assert.IsTrue(valid, "One or more of the runs was invalid");
Trace.WriteLine("Done");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment