// 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
{
///
/// Represents a node in the browser's DOM.
///
public class DomNode : IDomNode
{
private readonly IDictionary _attributes;
public DomNode(string tagName, IDictionary attributes)
{
TagName = tagName;
_attributes = attributes;
}
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();
}
public string this[string name]
{
get
{
if (_attributes == null || _attributes.Count < 1 || !_attributes.ContainsKey(name))
{
return null;
}
return _attributes[name];
}
}
public string TagName { get; private set; }
public ReadOnlyCollection AttributeNames
{
get
{
if (_attributes == null)
{
return new ReadOnlyCollection(new List());
}
return Array.AsReadOnly(_attributes.Keys.ToArray());
}
}
public bool HasAttribute(string attributeName)
{
if (_attributes == null)
{
return false;
}
return _attributes.ContainsKey(attributeName);
}
public IEnumerator> GetEnumerator()
{
if (_attributes == null)
{
return new Dictionary().GetEnumerator();
}
return _attributes.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}