admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright © 2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
 
namespace CefSharp
{
    /// <summary>
    /// Represents a node in the browser's DOM.
    /// </summary>
    public class DomNode : IDomNode
    {
        private readonly IDictionary<string, string> attributes;
 
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="tagName">Name of the tag.</param>
        /// <param name="attributes">The attributes.</param>
        public DomNode(string tagName, IDictionary<string, string> attributes)
        {
            TagName = tagName;
            this.attributes = attributes;
        }
 
        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        /// <returns>
        /// A string that represents the current object.
        /// </returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            if (attributes != null)
            {
                foreach (var pair in attributes)
                {
                    sb.AppendFormat("{0}{1}:'{2}'", 0 < sb.Length ? ", " : String.Empty, pair.Key, pair.Value);
                }
            }
 
            if (!String.IsNullOrWhiteSpace(TagName))
            {
                sb.Insert(0, String.Format("{0} ", TagName));
            }
 
            if (sb.Length < 1)
            {
                return base.ToString();
            }
 
            return sb.ToString();
        }
 
        /// <summary>
        /// Get the value of an attribute.
        /// </summary>
        /// <param name="name">The name of the attribute value to get.</param>
        /// <returns>
        /// The attribute value if the name exists in the DomNode's attributes. Null if the name does not exist.
        /// </returns>
        public string this[string name]
        {
            get
            {
                if (attributes == null || attributes.Count < 1 || !attributes.ContainsKey(name))
                {
                    return null;
                }
 
                return attributes[name];
            }
        }
 
        /// <summary>
        /// The name of the HTML element.
        /// </summary>
        /// <value>
        /// The name of the tag.
        /// </value>
        public string TagName { get; private set; }
 
        /// <summary>
        /// Get a read only list of the attribute names.
        /// </summary>
        /// <value>
        /// A list of names of the attributes.
        /// </value>
        public ReadOnlyCollection<string> AttributeNames
        {
            get
            {
                if (attributes == null)
                {
                    return new ReadOnlyCollection<string>(new List<string>());
                }
 
                return Array.AsReadOnly<string>(attributes.Keys.ToArray());
            }
        }
 
        /// <summary>
        /// Determine if the DomNode has the requested attribute.
        /// </summary>
        /// <param name="attributeName">The name of the attribute value.</param>
        /// <returns>
        /// True if the attribute exists in the DomNode, false if it does not.
        /// </returns>
        public bool HasAttribute(string attributeName)
        {
            if (attributes == null)
            {
                return false;
            }
 
            return attributes.ContainsKey(attributeName);
        }
 
        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns>
        /// The enumerator.
        /// </returns>
        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
            if (attributes == null)
            {
                return new Dictionary<string, string>().GetEnumerator();
            }
 
            return attributes.GetEnumerator();
        }
 
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}