Location
A location object with information about a Url.
using AngleSharp.Text;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace AngleSharp.Dom
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class Location : ILocation, IUrlUtilities
{
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class ChangedEventArgs : EventArgs
{
public bool IsReloaded => PreviousLocation.Is(CurrentLocation);
public bool IsHashChanged { get; }
public string PreviousLocation { get; }
public string CurrentLocation { get; }
public ChangedEventArgs(bool hashChanged, string previousLocation, string currentLocation)
{
IsHashChanged = hashChanged;
PreviousLocation = previousLocation;
CurrentLocation = currentLocation;
}
}
private readonly Url _url;
public Url Original => _url;
[System.Runtime.CompilerServices.Nullable(2)]
public string Origin {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _url.Origin;
}
}
public bool IsRelative => _url.IsRelative;
[System.Runtime.CompilerServices.Nullable(2)]
public string UserName {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _url.UserName;
}
[System.Runtime.CompilerServices.NullableContext(2)]
set {
_url.UserName = value;
}
}
[System.Runtime.CompilerServices.Nullable(2)]
public string Password {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _url.Password;
}
[System.Runtime.CompilerServices.NullableContext(2)]
set {
_url.Password = value;
}
}
public string Hash {
get {
return NonEmptyPrefix(_url.Fragment, "#");
}
[param: System.Diagnostics.CodeAnalysis.AllowNull]
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.Is(_url.Fragment)) {
_url.Fragment = value;
RaiseHashChanged(href);
}
}
}
public string Host {
get {
return _url.Host;
}
set {
string href = _url.Href;
if (!value.Isi(_url.Host)) {
_url.Host = value;
RaiseLocationChanged(href);
}
}
}
public string HostName {
get {
return _url.HostName;
}
set {
string href = _url.Href;
if (!value.Isi(_url.HostName)) {
_url.HostName = value;
RaiseLocationChanged(href);
}
}
}
public string Href {
get {
return _url.Href;
}
set {
string href = _url.Href;
if (!value.Is(_url.Href)) {
_url.Href = value;
RaiseLocationChanged(href);
}
}
}
public string PathName {
get {
string data = _url.Data;
if (!string.IsNullOrEmpty(data))
return data;
return "/" + _url.Path;
}
set {
string href = _url.Href;
if (!value.Is(_url.Path)) {
_url.Path = value;
RaiseLocationChanged(href);
}
}
}
public string Port {
get {
return _url.Port;
}
set {
string href = _url.Href;
if (!value.Isi(_url.Port)) {
_url.Port = value;
RaiseLocationChanged(href);
}
}
}
public string Protocol {
get {
return NonEmptyPostfix(_url.Scheme, ":");
}
set {
string href = _url.Href;
if (!value.Isi(_url.Scheme)) {
_url.Scheme = value;
RaiseLocationChanged(href);
}
}
}
public string Search {
get {
return NonEmptyPrefix(_url.Query, "?");
}
set {
string href = _url.Href;
if (!value.Is(_url.Query)) {
_url.Query = value;
RaiseLocationChanged(href);
}
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
[method: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
[field: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
public event EventHandler<ChangedEventArgs> Changed;
internal Location(string url)
: this(new Url(url))
{
}
internal Location(Url url)
{
_url = (url ?? new Url(string.Empty));
}
public void Assign(string url)
{
string href = _url.Href;
if (!href.Is(url)) {
_url.Href = url;
RaiseLocationChanged(href);
}
}
public void Replace(string url)
{
string href = _url.Href;
if (!href.Is(url)) {
_url.Href = url;
RaiseLocationChanged(href);
}
}
public void Reload()
{
this.Changed?.Invoke(this, new ChangedEventArgs(false, _url.Href, _url.Href));
}
public override string ToString()
{
return _url.Href;
}
private void RaiseHashChanged(string oldAddress)
{
this.Changed?.Invoke(this, new ChangedEventArgs(true, oldAddress, _url.Href));
}
private void RaiseLocationChanged(string oldAddress)
{
this.Changed?.Invoke(this, new ChangedEventArgs(false, oldAddress, _url.Href));
}
private static string NonEmptyPrefix([System.Runtime.CompilerServices.Nullable(2)] string check, string prefix)
{
if (!string.IsNullOrEmpty(check))
return prefix + check;
return string.Empty;
}
private static string NonEmptyPostfix([System.Runtime.CompilerServices.Nullable(2)] string check, string postfix)
{
if (!string.IsNullOrEmpty(check))
return check + postfix;
return string.Empty;
}
}
}