Skip to content

Commit

Permalink
Merge pull request #37 from FoxMoss/main
Browse files Browse the repository at this point in the history
Fix Hash Setter
  • Loading branch information
Avad3 authored Aug 17, 2023
2 parents bc85e34 + e5ed461 commit 903386c
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions lib/global/client/methods/core/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function Location(self: any, doc: Boolean = true) {

const ancestor: DOMStringList | Array<string> = self.location.ancestorOrigins || [];

const descriptors: Array<Window & { prototype: object | any } | Location & { prototype: object | any } | Document & { prototype: object | any } > = [
const descriptors: Array<Window & { prototype: object | any } | Location & { prototype: object | any } | Document & { prototype: object | any }> = [
self.Window,
self.Location,
self.WorkerLocation,
Expand All @@ -36,12 +36,12 @@ export default function Location(self: any, doc: Boolean = true) {

const descriptor: PropertyDescriptor = {
get() {
return self.__dynamic.location;
return self.__dynamic.location;
},
set(value: Location | string) {
if (value instanceof self.Location) return self.__dynamic.location = value;
if (value instanceof self.Location) return self.__dynamic.location = value;

self.__dynamic.location.href = value;
self.__dynamic.location.href = value;
},
configurable: true,
};
Expand All @@ -66,48 +66,54 @@ export default function Location(self: any, doc: Boolean = true) {
];

try {
var property: URL = new URL(self.__dynamic$url || self.__dynamic.url.decode(self.location.pathname+self.location.search+self.location.hash));
var property: URL = new URL(self.__dynamic$url || self.__dynamic.url.decode(self.location.pathname + self.location.search + self.location.hash));
} catch {
self.__dynamic$url = 'about:blank'
var property: URL = new URL('about:blank');
self.__dynamic$url = 'about:blank'
var property: URL = new URL('about:blank');
}

self.__dynamic.property = property;
self.__dynamic.meta.load(property as URL);
self.__dynamic.location = self.__dynamic.util.clone(self.location) as Location

props.forEach(prop => {
self.__dynamic.define(self.__dynamic.location, prop, {
get: () =>
(prop == 'search' && (self.location[prop] + (self.location.search ? property.search.replace('?', '&') : property.search))) || (prop == 'hash' ? location[prop] : (property as any)[prop] as string),
set: (e:any) =>
(self.location[prop] = self.__dynamic.url.encode(self.__dynamic.meta.href.replace((property as any)[prop], e), property)) as string
});
self.__dynamic.define(self.__dynamic.location, prop, {
get: () =>
(prop == 'search' && (self.location[prop] + (self.location.search ? property.search.replace('?', '&') : property.search))) || (prop == 'hash' ? location[prop] : (property as any)[prop] as string),
set: (e: any) => {
if (prop === "href") {
(self.location[prop] = self.__dynamic.url.encode(self.__dynamic.meta.href.replace((property as any)[prop], e), property)) as string
}
else {
self.location[prop] = e.toString();
}
}
});
});

self.__dynamic.define(self.Object.prototype, '__dynamic$location', {
get() {
if (this === self || this === self.__dynamic$window || this === self.document || this === self.__dynamic$document) return this.__dynamic?.location;
if (this === self || this === self.__dynamic$window || this === self.document || this === self.__dynamic$document) return this.__dynamic?.location;

return this.location;
return this.location;
},
set(value: string) {
if (this === self || this === self.__dynamic$window || this === self.document || this === self.__dynamic$document) return this.__dynamic.location.href = value;
if (this === self || this === self.__dynamic$window || this === self.document || this === self.__dynamic$document) return this.__dynamic.location.href = value;

return this.location = value;
return this.location = value;
},
configurable: true
})
})

funcs.forEach(func => {
self.__dynamic.define(self.__dynamic.location, func, {
get: () => {
if (func == 'toString') return () => property['href'] as string;
self.__dynamic.define(self.__dynamic.location, func, {
get: () => {
if (func == 'toString') return () => property['href'] as string;

return new self.__dynamic.Function("arg", `return window.location.${func}(arg?${"reload"!==func&&"toString"!==func?"(self.__dynamic).url.encode(arg, new URL('"+property.href+"'))":"arg"}:null)`) as Function;
},
set: () => null
});
return new self.__dynamic.Function("arg", `return window.location.${func}(arg?${"reload" !== func && "toString" !== func ? "(self.__dynamic).url.encode(arg, new URL('" + property.href + "'))" : "arg"}:null)`) as Function;
},
set: () => null
});
});

if (ancestor.length) {
Expand All @@ -121,11 +127,11 @@ export default function Location(self: any, doc: Boolean = true) {
self.__dynamic.define(object.prototype, '__dynamic$location', descriptor);
});

if (!self.__dynamic.hashchange) self.__dynamic.hashchange = (self.addEventListener("hashchange", ( event: HashChangeEvent ) => {
if (!self.__dynamic.hashchange) self.__dynamic.hashchange = (self.addEventListener("hashchange", (event: HashChangeEvent) => {
//property["hash"] = "#" + (event.newURL.split("#")[1] || "");

//self.history.pushState(null, null, self.__dynamic.location.href);
}), true);

return self.__dynamic.location;
};
};

0 comments on commit 903386c

Please sign in to comment.