HtmlUrlBaseElement
Represents the base for a / area elements.
using AngleSharp.Dom.Collections;
using AngleSharp.Extensions;
using AngleSharp.Html;
using System;
namespace AngleSharp.Dom.Html
{
internal abstract class HtmlUrlBaseElement : HtmlElement, IUrlUtilities
{
private TokenList _relList;
private SettableTokenList _ping;
public string Download {
get {
return this.GetOwnAttribute(AttributeNames.Download);
}
set {
this.SetOwnAttribute(AttributeNames.Download, value, false);
}
}
public string Href {
get {
return this.GetUrlAttribute(AttributeNames.Href);
}
set {
SetAttribute(AttributeNames.Href, value);
}
}
public string Hash {
get {
return GetLocationPart((ILocation m) => m.Hash);
}
set {
SetLocationPart(delegate(ILocation m) {
m.Hash = value;
});
}
}
public string Host {
get {
return GetLocationPart((ILocation m) => m.Host);
}
set {
SetLocationPart(delegate(ILocation m) {
m.Host = value;
});
}
}
public string HostName {
get {
return GetLocationPart((ILocation m) => m.HostName);
}
set {
SetLocationPart(delegate(ILocation m) {
m.HostName = value;
});
}
}
public string PathName {
get {
return GetLocationPart((ILocation m) => m.PathName);
}
set {
SetLocationPart(delegate(ILocation m) {
m.PathName = value;
});
}
}
public string Port {
get {
return GetLocationPart((ILocation m) => m.Port);
}
set {
SetLocationPart(delegate(ILocation m) {
m.Port = value;
});
}
}
public string Protocol {
get {
return GetLocationPart((ILocation m) => m.Protocol);
}
set {
SetLocationPart(delegate(ILocation m) {
m.Protocol = value;
});
}
}
public string UserName {
get {
return GetLocationPart((ILocation m) => m.UserName);
}
set {
SetLocationPart(delegate(ILocation m) {
m.UserName = value;
});
}
}
public string Password {
get {
return GetLocationPart((ILocation m) => m.Password);
}
set {
SetLocationPart(delegate(ILocation m) {
m.Password = value;
});
}
}
public string Search {
get {
return GetLocationPart((ILocation m) => m.Search);
}
set {
SetLocationPart(delegate(ILocation m) {
m.Search = value;
});
}
}
public string Origin => GetLocationPart((ILocation m) => m.Origin);
public string TargetLanguage {
get {
return this.GetOwnAttribute(AttributeNames.HrefLang);
}
set {
this.SetOwnAttribute(AttributeNames.HrefLang, value, false);
}
}
public string Media {
get {
return this.GetOwnAttribute(AttributeNames.Media);
}
set {
this.SetOwnAttribute(AttributeNames.Media, value, false);
}
}
public string Relation {
get {
return this.GetOwnAttribute(AttributeNames.Rel);
}
set {
this.SetOwnAttribute(AttributeNames.Rel, value, false);
}
}
public ITokenList RelationList {
get {
if (_relList == null) {
_relList = new TokenList(this.GetOwnAttribute(AttributeNames.Rel));
_relList.Changed += delegate(string value) {
UpdateAttribute(AttributeNames.Rel, value);
};
}
return _relList;
}
}
public ISettableTokenList Ping {
get {
if (_ping == null) {
_ping = new SettableTokenList(this.GetOwnAttribute(AttributeNames.Ping));
_ping.Changed += delegate(string value) {
UpdateAttribute(AttributeNames.Ping, value);
};
}
return _ping;
}
}
public string Target {
get {
return this.GetOwnAttribute(AttributeNames.Target);
}
set {
this.SetOwnAttribute(AttributeNames.Target, value, false);
}
}
public string Type {
get {
return this.GetOwnAttribute(AttributeNames.Type);
}
set {
this.SetOwnAttribute(AttributeNames.Type, value, false);
}
}
internal bool IsVisited { get; set; }
internal bool IsActive { get; set; }
static HtmlUrlBaseElement()
{
Element.RegisterCallback(AttributeNames.Rel, delegate(HtmlUrlBaseElement element, string value) {
element.TryUpdate(element._relList, value);
});
Element.RegisterCallback(AttributeNames.Ping, delegate(HtmlUrlBaseElement element, string value) {
element.TryUpdate(element._ping, value);
});
}
public HtmlUrlBaseElement(Document owner, string name, string prefix, NodeFlags flags)
: base(owner, name, prefix, flags)
{
}
private string GetLocationPart(Func<ILocation, string> getter)
{
string ownAttribute = this.GetOwnAttribute(AttributeNames.Href);
Url url = (ownAttribute != null) ? new Url(base.BaseUrl, ownAttribute) : null;
if (url == null || url.IsInvalid)
return string.Empty;
Location arg = new Location(url);
return getter(arg);
}
private void SetLocationPart(Action<ILocation> setter)
{
string ownAttribute = this.GetOwnAttribute(AttributeNames.Href);
Url url = (ownAttribute != null) ? new Url(base.BaseUrl, ownAttribute) : null;
if (url == null || url.IsInvalid)
url = new Url(base.BaseUrl);
Location location = new Location(url);
setter(location);
this.SetOwnAttribute(AttributeNames.Href, location.Href, false);
}
}
}