/// <summary>
 /// Searches the entire sorted <see cref="HtmlScriptCollection"/> for an 
 /// <see cref="HtmlScript"/> element using the default comparer 
 /// and returns the zero-based index of the element.
 /// </summary>
 /// <param name="value">The <see cref="HtmlScript"/> object
 /// to locate in the <see cref="HtmlScriptCollection"/>.
 /// This argument can be a null reference.
 /// </param>    
 /// <returns>The zero-based index of <paramref name="value"/> in the sorted 
 /// <see cref="HtmlScriptCollection"/>, if <paramref name="value"/> is found; 
 /// otherwise, a negative number, which is the bitwise complement of the index 
 /// of the next element that is larger than <paramref name="value"/> or, if there 
 /// is no larger element, the bitwise complement of <see cref="Count"/>.</returns>
 /// <exception cref="InvalidOperationException">
 /// Neither <paramref name="value"/> nor the elements of the <see cref="HtmlScriptCollection"/> 
 /// implement the <see cref="IComparable"/> interface.</exception>
 /// <remarks>Please refer to <see cref="ArrayList.BinarySearch"/> for details.</remarks>
 public virtual int BinarySearch(HtmlScript value)
 {
     return Array.BinarySearch(this._array, 0, this._count, value);
 }
        /// <summary>
        /// Adds a <see cref="HtmlScript"/> to the end of the <see cref="HtmlScriptCollection"/>.
        /// </summary>
        /// <param name="value">The <see cref="HtmlScript"/> object 
        /// to be added to the end of the <see cref="HtmlScriptCollection"/>.
        /// This argument can be a null reference.
        /// </param>    
        /// <returns>The <see cref="HtmlScriptCollection"/> index at which the 
        /// <paramref name="value"/> has been added.</returns>
        /// <exception cref="NotSupportedException">
        /// <para>The <see cref="HtmlScriptCollection"/> is read-only.</para>
        /// <para>-or-</para>
        /// <para>The <b>HtmlScriptCollection</b> has a fixed size.</para></exception>    
        /// <remarks>Please refer to <see cref="ArrayList.Add"/> for details.</remarks>
        public virtual int Add(HtmlScript value)
        {
            if (this._count == this._array.Length)
                EnsureCapacity(this._count + 1);

            ++this._version;
            this._array[this._count] = value;
            return this._count++;
        }
        /// <summary>
        /// Adds the elements of a <see cref="HtmlScript"/> array 
        /// to the end of the <see cref="HtmlScriptCollection"/>.
        /// </summary>
        /// <param name="array">An <see cref="Array"/> of <see cref="HtmlScript"/> elements
        /// that should be added to the end of the <see cref="HtmlScriptCollection"/>.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="array"/> is a null reference.</exception>    
        /// <exception cref="NotSupportedException">
        /// <para>The <see cref="HtmlScriptCollection"/> is read-only.</para>
        /// <para>-or-</para>
        /// <para>The <b>HtmlScriptCollection</b> has a fixed size.</para></exception>    
        /// <remarks>Please refer to <see cref="ArrayList.AddRange"/> for details.</remarks>
        public virtual void AddRange(HtmlScript[] array)
        {
            if (array == null)
                throw new ArgumentNullException("array");

            if (array.Length == 0) return;
            if (this._count + array.Length > this._array.Length)
                EnsureCapacity(this._count + array.Length);

            ++this._version;
            Array.Copy(array, 0, this._array, this._count, array.Length);
            this._count += array.Length;
        }
 /// <summary>
 /// Copies the elements of the <see cref="HtmlScriptCollection"/> to a new
 /// <see cref="Array"/> of <see cref="HtmlScript"/> elements.
 /// </summary>
 /// <returns>A one-dimensional <see cref="Array"/> of <see cref="HtmlScript"/> 
 /// elements containing copies of the elements of the <see cref="HtmlScriptCollection"/>.</returns>
 /// <remarks>Please refer to <see cref="ArrayList.ToArray"/> for details.</remarks>
 public virtual HtmlScript[] ToArray()
 {
     HtmlScript[] array = new HtmlScript[this._count];
     Array.Copy(this._array, array, this._count);
     return array;
 }
 public override void AddRange(HtmlScript[] array)
 {
     throw new NotSupportedException("Read-only collections cannot be modified.");
 }
 /// <summary>
 /// Copies the entire <see cref="HtmlScriptCollection"/> to a one-dimensional <see cref="Array"/>
 /// of <see cref="HtmlScript"/> elements, starting at the specified index of the target array.
 /// </summary>
 /// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the
 /// <see cref="HtmlScript"/> elements copied from the <see cref="HtmlScriptCollection"/>.
 /// The <b>Array</b> must have zero-based indexing.</param>
 /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> 
 /// at which copying begins.</param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="array"/> is a null reference.</exception>    
 /// <exception cref="ArgumentOutOfRangeException">
 /// <paramref name="arrayIndex"/> is less than zero.</exception>    
 /// <exception cref="ArgumentException"><para>
 /// <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.
 /// </para><para>-or-</para><para>
 /// The number of elements in the source <see cref="HtmlScriptCollection"/> is greater than the
 /// available space from <paramref name="arrayIndex"/> to the end of the destination 
 /// <paramref name="array"/>.</para></exception>
 /// <remarks>Please refer to <see cref="ArrayList.CopyTo"/> for details.</remarks>
 public virtual void CopyTo(HtmlScript[] array, int arrayIndex)
 {
     CheckTargetArray(array, arrayIndex);
     Array.Copy(this._array, 0, array, arrayIndex, this._count);
 }
        /// <summary>
        /// Inserts a <see cref="HtmlScript"/> element into the 
        /// <see cref="HtmlScriptCollection"/> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="value"/> 
        /// should be inserted.</param>
        /// <param name="value">The <see cref="HtmlScript"/> object
        /// to insert into the <see cref="HtmlScriptCollection"/>.
        /// This argument can be a null reference.
        /// </param>    
        /// <exception cref="ArgumentOutOfRangeException">
        /// <para><paramref name="index"/> is less than zero.</para>
        /// <para>-or-</para>
        /// <para><paramref name="index"/> is greater than <see cref="Count"/>.</para>
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// <para>The <see cref="HtmlScriptCollection"/> is read-only.</para>
        /// <para>-or-</para>
        /// <para>The <b>HtmlScriptCollection</b> has a fixed size.</para></exception>    
        /// <remarks>Please refer to <see cref="ArrayList.Insert"/> for details.</remarks>
        public virtual void Insert(int index, HtmlScript value)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("index", index, "Argument cannot be negative.");
            if (index > this._count)
                throw new ArgumentOutOfRangeException("index", index, "Argument cannot exceed Count.");

            if (this._count == this._array.Length)
                EnsureCapacity(this._count + 1);

            ++this._version;
            if (index < this._count)
                Array.Copy(this._array, index, this._array, index + 1, this._count - index);

            this._array[index] = value;
            ++this._count;
        }
 public override bool Contains(HtmlScript value)
 {
     lock (this._root) return this._collection.Contains(value);
 }
 public override void CopyTo(HtmlScript[] array)
 {
     lock (this._root) this._collection.CopyTo(array);
 }
 public override void AddRange(HtmlScript[] array)
 {
     lock (this._root) this._collection.AddRange(array);
 }
 public override int BinarySearch(HtmlScript value)
 {
     lock (this._root) return this._collection.BinarySearch(value);
 }
 public override int Add(HtmlScript value)
 {
     lock (this._root) return this._collection.Add(value);
 }
 public override void Remove(HtmlScript value)
 {
     throw new NotSupportedException("Read-only collections cannot be modified.");
 }
 public override void CopyTo(HtmlScript[] array)
 {
     this._collection.CopyTo(array);
 }
 /// <summary>
 /// Determines whether the <see cref="HtmlScriptCollection"/>
 /// contains the specified <see cref="HtmlScript"/> element.
 /// </summary>
 /// <param name="value">The <see cref="HtmlScript"/> object
 /// to locate in the <see cref="HtmlScriptCollection"/>.
 /// This argument can be a null reference.
 /// </param>    
 /// <returns><c>true</c> if <paramref name="value"/> is found in the 
 /// <see cref="HtmlScriptCollection"/>; otherwise, <c>false</c>.</returns>
 /// <remarks>Please refer to <see cref="ArrayList.Contains"/> for details.</remarks>
 public virtual bool Contains(HtmlScript value)
 {
     return (IndexOf(value) >= 0);
 }
 public override void CopyTo(HtmlScript[] array, int arrayIndex)
 {
     lock (this._root) this._collection.CopyTo(array, arrayIndex);
 }
 /// <overloads>
 /// Copies the <see cref="HtmlScriptCollection"/> or a portion of it to a one-dimensional array.
 /// </overloads>
 /// <summary>
 /// Copies the entire <see cref="HtmlScriptCollection"/> to a one-dimensional <see cref="Array"/>
 /// of <see cref="HtmlScript"/> elements, starting at the beginning of the target array.
 /// </summary>
 /// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the
 /// <see cref="HtmlScript"/> elements copied from the <see cref="HtmlScriptCollection"/>.
 /// The <b>Array</b> must have zero-based indexing.</param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="array"/> is a null reference.</exception>    
 /// <exception cref="ArgumentException">
 /// The number of elements in the source <see cref="HtmlScriptCollection"/> is greater 
 /// than the available space in the destination <paramref name="array"/>.</exception>
 /// <remarks>Please refer to <see cref="ArrayList.CopyTo"/> for details.</remarks>
 public virtual void CopyTo(HtmlScript[] array)
 {
     CheckTargetArray(array, 0);
     Array.Copy(this._array, array, this._count);
 }
 public override int IndexOf(HtmlScript value)
 {
     lock (this._root) return this._collection.IndexOf(value);
 }
 /// <summary>
 /// Returns the zero-based index of the first occurrence of the specified 
 /// <see cref="HtmlScript"/> in the <see cref="HtmlScriptCollection"/>.
 /// </summary>
 /// <param name="value">The <see cref="HtmlScript"/> object 
 /// to locate in the <see cref="HtmlScriptCollection"/>.
 /// This argument can be a null reference.
 /// </param>    
 /// <returns>
 /// The zero-based index of the first occurrence of <paramref name="value"/> 
 /// in the <see cref="HtmlScriptCollection"/>, if found; otherwise, -1.
 /// </returns>
 /// <remarks>Please refer to <see cref="ArrayList.IndexOf"/> for details.</remarks>
 public virtual int IndexOf(HtmlScript value)
 {
     return Array.IndexOf(this._array, value, 0, this._count);
 }
 public override void Insert(int index, HtmlScript value)
 {
     lock (this._root) this._collection.Insert(index, value);
 }
 /// <summary>
 /// Removes the first occurrence of the specified <see cref="HtmlScript"/>
 /// from the <see cref="HtmlScriptCollection"/>.
 /// </summary>
 /// <param name="value">The <see cref="HtmlScript"/> object
 /// to remove from the <see cref="HtmlScriptCollection"/>.
 /// This argument can be a null reference.
 /// </param>    
 /// <exception cref="NotSupportedException">
 /// <para>The <see cref="HtmlScriptCollection"/> is read-only.</para>
 /// <para>-or-</para>
 /// <para>The <b>HtmlScriptCollection</b> has a fixed size.</para></exception>    
 /// <remarks>Please refer to <see cref="ArrayList.Remove"/> for details.</remarks>
 public virtual void Remove(HtmlScript value)
 {
     int index = IndexOf(value);
     if (index >= 0) RemoveAt(index);
 }
 public override void Remove(HtmlScript value)
 {
     lock (this._root) this._collection.Remove(value);
 }
        /// <summary>
        /// Parses the scripts tags found in buffer.
        /// </summary>
        /// <param name="buffer"> The ResponseBuffer.</param>
        /// <returns> An updated ResponseBuffer.</returns>
        internal static ResponseBuffer ParseScriptTags(ResponseBuffer buffer)
        {
            Regex getScripts = null;
            Regex getAttributes = null;

            if ( RegExpList == null )
            {
                // Remove Scripts regex
                RegexOptions options = RegexOptions.None;
                getScripts = new Regex(@"(?<header><(?i:script)[^>]*?)(/>|>(?<source>[\w|\t|\r|\W]*?)</(?i:script)>)",options);
                getAttributes = new Regex(@"(?<name>(\w+))=(""|')(?<value>.*?)(""|')",options);

                // add to list
                RegExpList = new Hashtable();
                RegExpList.Add("ScriptQuery",getScripts);
                RegExpList.Add("AttributesQuery",getAttributes);
            }
            else
            {
                getScripts = (Regex)RegExpList["ScriptQuery"];
                getAttributes = (Regex)RegExpList["AttributesQuery"];
            }

            // Get matches
            MatchCollection matches = getScripts.Matches(buffer.HttpBody);

            for(int i=0;i<matches.Count;i++)
            {
                HtmlScript scriptTag = new HtmlScript();
                string scriptHeader = matches[i].Groups["header"].Value;
                string scriptSource = matches[i].Groups["source"].Value;

                scriptTag.Text = scriptSource;

                // get attributes
                MatchCollection attributes = getAttributes.Matches(scriptHeader);
                foreach (Match m in attributes)
                {
                    string name = m.Groups["name"].Value;
                    string result = m.Groups["value"].Value;

                    if ( name.ToLower() == "language" )
                    {
                        scriptTag.Language=result.Trim();
                    }
                    if ( name.ToLower() == "src" )
                    {
                        scriptTag.Source = result.Trim();
                    }
                }
                buffer.Scripts.Add(scriptTag);
            }

            return buffer;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlScriptCollection"/> class
        /// that contains elements copied from the specified <see cref="HtmlScript"/>
        /// array and that has the same initial capacity as the number of elements copied.
        /// </summary>
        /// <param name="array">An <see cref="Array"/> of <see cref="HtmlScript"/> 
        /// elements that are copied to the new collection.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="array"/> is a null reference.</exception>        
        /// <remarks>Please refer to <see cref="ArrayList(ICollection)"/> for details.</remarks>    
        public HtmlScriptCollection(HtmlScript[] array)
        {
            if (array == null)
                throw new ArgumentNullException("array");

            this._array = new HtmlScript[array.Length];
            AddRange(array);
        }