Location
A location object with information about a Url.
using AngleSharp.Extensions;
using System;
using System.Diagnostics;
namespace AngleSharp.Dom
{
[DebuggerStepThrough]
internal sealed class Location : ILocation, IUrlUtilities
{
public sealed class LocationChangedEventArgs : EventArgs
{
public bool IsHashChanged { get; set; }
public string PreviousLocation { get; set; }
public string CurrentLocation { get; set; }
public LocationChangedEventArgs(bool hashChanged, string previousLocation, string currentLocation)
{
IsHashChanged = hashChanged;
PreviousLocation = previousLocation;
CurrentLocation = currentLocation;
}
}
private readonly Url _url;
internal Url Original => _url;
public string Origin => _url.Origin;
public bool IsRelative => _url.IsRelative;
public string UserName {
get {
return _url.UserName;
}
set {
_url.UserName = value;
}
}
public string Password {
get {
return _url.Password;
}
set {
_url.Password = value;
}
}
public string Hash {
get {
return NonEmptyPrefix(_url.Fragment, "#");
}
set {
string href = _url.Href;
if (value != null) {
if (value.Has('#', 0))
value = value.Substring(1);
else if (value.Length == 0) {
value = null;
}
}
if (value != _url.Fragment) {
_url.Fragment = value;
RaiseChanged(href, true);
}
}
}
public string Host {
get {
return _url.Host;
}
set {
string href = _url.Href;
if (value != _url.Host) {
_url.Host = value;
RaiseChanged(href, false);
}
}
}
public string HostName {
get {
return _url.HostName;
}
set {
string href = _url.Href;
if (value != _url.HostName) {
_url.HostName = value;
RaiseChanged(href, false);
}
}
}
public string Href {
get {
return _url.Href;
}
set {
string href = _url.Href;
if (value != _url.Href) {
_url.Href = value;
RaiseChanged(href, false);
}
}
}
public string PathName {
get {
string data = _url.Data;
if (!string.IsNullOrEmpty(data))
return data;
return "/" + _url.Path;
}
set {
string href = _url.Href;
if (value != _url.Path) {
_url.Path = value;
RaiseChanged(href, false);
}
}
}
public string Port {
get {
return _url.Port;
}
set {
string href = _url.Href;
if (value != _url.Port) {
_url.Port = value;
RaiseChanged(href, false);
}
}
}
public string Protocol {
get {
return NonEmptyPostfix(_url.Scheme, ":");
}
set {
string href = _url.Href;
if (value != _url.Scheme) {
_url.Scheme = value;
RaiseChanged(href, false);
}
}
}
public string Search {
get {
return NonEmptyPrefix(_url.Query, "?");
}
set {
string href = _url.Href;
if (value != _url.Query) {
_url.Query = value;
RaiseChanged(href, false);
}
}
}
public event EventHandler<LocationChangedEventArgs> Changed;
internal Location()
: this(string.Empty)
{
}
internal Location(string url)
: this(new Url(url))
{
}
internal Location(Url url)
{
_url = (url ?? new Url(string.Empty));
}
public void Assign(string url)
{
_url.Href = url;
}
public void Replace(string url)
{
_url.Href = url;
}
public void Reload()
{
_url.Href = Href;
}
public override string ToString()
{
return _url.Href;
}
private void RaiseChanged(string oldAddress, bool hashChanged)
{
if (this.Changed != null)
this.Changed(this, new LocationChangedEventArgs(hashChanged, oldAddress, _url.Href));
}
private static string NonEmptyPrefix(string check, string prefix)
{
if (string.IsNullOrEmpty(check))
return string.Empty;
return prefix + check;
}
private static string NonEmptyPostfix(string check, string postfix)
{
if (string.IsNullOrEmpty(check))
return string.Empty;
return check + postfix;
}
}
}