﻿// Decompiled with JetBrains decompiler
// Type: MS.Internal.Xml.XPath.IDQuery
// Assembly: System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 50ABAB51-7DC3-4F0A-A797-0D0C2D124D60
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Xml.dll

using System.Runtime;
using System.Xml;
using System.Xml.XPath;

namespace MS.Internal.Xml.XPath
{
  internal sealed class IDQuery : CacheOutputQuery
  {
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public IDQuery(Query arg)
      : base(arg)
    {
    }

    private IDQuery(IDQuery other)
      : base((CacheOutputQuery) other)
    {
    }

    public override object Evaluate(XPathNodeIterator context)
    {
      object obj = base.Evaluate(context);
      XPathNavigator contextNode = context.Current.Clone();
      switch (this.GetXPathType(obj))
      {
        case XPathResultType.Number:
          this.ProcessIds(contextNode, StringFunctions.toString((double) obj));
          break;
        case XPathResultType.String:
          this.ProcessIds(contextNode, (string) obj);
          break;
        case XPathResultType.Boolean:
          this.ProcessIds(contextNode, StringFunctions.toString((bool) obj));
          break;
        case XPathResultType.NodeSet:
          XPathNavigator xpathNavigator;
          while ((xpathNavigator = this.input.Advance()) != null)
            this.ProcessIds(contextNode, xpathNavigator.Value);
          break;
        case (XPathResultType) 4:
          this.ProcessIds(contextNode, ((XPathItem) obj).Value);
          break;
      }
      return (object) this;
    }

    public override XPathNavigator MatchNode(XPathNavigator context)
    {
      this.Evaluate((XPathNodeIterator) new XPathSingletonIterator(context, true));
      XPathNavigator xpathNavigator;
      while ((xpathNavigator = this.Advance()) != null)
      {
        if (xpathNavigator.IsSamePosition(context))
          return context;
      }
      return (XPathNavigator) null;
    }

    public override XPathNodeIterator Clone()
    {
      return (XPathNodeIterator) new IDQuery(this);
    }

    private void ProcessIds(XPathNavigator contextNode, string val)
    {
      foreach (string id in XmlConvert.SplitString(val))
      {
        if (contextNode.MoveToId(id))
          this.Insert(this.outputBuffer, contextNode);
      }
    }
  }
}
