Beispiel #1
0
 /// <summary>
 /// HtmlElementクラスのインスタンスを初期化
 /// </summary>
 /// <param name="name"></param>
 public HtmlElement(string name)
 {
     //
     // TODO: コンストラクタ ロジックをここに追加してください。
     //
     this.nodes             = new HtmlNodeCollection(this);
     this.attributes        = new HtmlAttributeCollection(this);
     this.isTerminated      = false;
     this.isEmptyElementTag = false;
     this.name = name;
 }
        /// <summary>
        /// 属性を解析しattributes変数に格納
        /// </summary>
        /// <param name="html"></param>
        /// <param name="index"></param>
        /// <param name="attributes"></param>
        private void ParseAttributes(string html, ref int index,
                                     HtmlAttributeCollection attributes)
        {
            while (index < html.Length)
            {
                // 空白読み飛ばし
                SkipWhiteSpace(html, ref index);

                // タグの終わりなら終了
                if (is_match(html, index, "/>") || is_match(html, index, ">"))
                {
                    break;
                }

                int attrSt = index;

                // 属性名を検索
                if (SkipChars(html, ref index, "=".ToCharArray()))
                {
                    string attrName, attrVal = String.Empty;

                    attrName = html.Substring(attrSt, index - attrSt).ToLower();
                    index++;

                    // 空白読み飛ばし
                    SkipWhiteSpace(html, ref index);

                    // クオーテーションから始まる場合は、同じクオーテーションが出るまでを値とする
                    if (index < html.Length && IsQuote(html[index]))
                    {
                        char quote = html[index];
                        int  st    = index++;

                        SkipChars(html, ref index, new char[] { quote, '>' });

                        attrVal = html.Substring(st, index - st).Trim(QuoteChars);

                        if (is_match(html, index, quote.ToString()))
                        {
                            index++;
                        }
                    }
                    // すぐに値が始まっている場合
                    else
                    {
                        // 空白かタグの終わりが来るまでindexを進める
                        for (int i = index; i < html.Length; i++)
                        {
                            if (is_match(html, i, " ") ||
                                is_match(html, i, ">") ||
                                is_match(html, i, "/>"))
                            {
                                attrVal = html.Substring(index, i - index);
                                index   = i;
                                break;
                            }
                        }
                    }

                    // 値を検索
                    HtmlAttribute attr = new HtmlAttribute(attrName, attrVal);
                    attributes.Add(attr);
                }
                else
                {
                    break;
                }
            }
        }