Skip to content

Commit

Permalink
capricorn86#463@minor: Added partial support for XMLHttpRequest.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mas0nShi committed Jun 25, 2022
1 parent f9d55eb commit 18b0bd6
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/happy-dom/src/exception/DOMExceptionNameEnum.ts
Expand Up @@ -3,6 +3,8 @@ enum DOMExceptionNameEnum {
indexSizeError = 'IndexSizeError',
syntaxError = 'SyntaxError',
hierarchyRequestError = 'HierarchyRequestError',
invalidCharacterError = 'InvalidCharacterError'
invalidCharacterError = 'InvalidCharacterError',
securityError = 'SecurityError',
networkError = 'NetworkError'
}
export default DOMExceptionNameEnum;
20 changes: 18 additions & 2 deletions packages/happy-dom/src/location/RelativeURL.ts
Expand Up @@ -11,16 +11,32 @@ export default class RelativeURL {
* @param url URL.
*/
public static getAbsoluteURL(location: Location, url: string): string {
// If the URL starts with '//' then it is a Protocol relative URL.
// Reference: https://url.spec.whatwg.org/#protocol-relative-urls.
// E.g. '//example.com/' needs to be converted to 'http://example.com/'.
if (url.startsWith('//')) {
return location.protocol + url;
}
// If the URL starts with '/' then it is a Path relative URL.
// E.g. '/example.com/' needs to be converted to 'http://example.com/'.
if (url.startsWith('/')) {
return location.origin + url;
}

// If the URL starts with 'https://' or 'http://' then it is a Absolute URL.
// E.g. 'https://example.com' needs to be converted to 'https://example.com/'.
// E.g. 'http://example.com' needs to be converted to 'http://example.com/'.
if (!url.startsWith('https://') && !url.startsWith('http://')) {
let pathname = location.pathname;
if (pathname.endsWith('/')) {
pathname = pathname.slice(0, -1);
}
return location.origin + pathname + '/' + url;

return (
location.origin +
(/(.*)\/(.*)[^\/]$/.test(pathname) ? pathname.match(/(.*)\/(.*)[^\/]$/)[1] : pathname) +
'/' +
url
);
}

return url;
Expand Down
9 changes: 9 additions & 0 deletions packages/happy-dom/src/window/Window.ts
Expand Up @@ -93,6 +93,9 @@ import * as PerfHooks from 'perf_hooks';
import VM from 'vm';
import { Buffer } from 'buffer';
import { atob, btoa } from './WindowBase64';
import XMLHttpRequest from '../xml-http-request/XMLHttpRequest';
import XMLHttpRequestUpload from '../xml-http-request/XMLHttpRequestUpload';
import { XMLHttpRequestEventTarget } from '../xml-http-request/XMLHttpRequestEventTarget';

/**
* Browser window.
Expand Down Expand Up @@ -196,6 +199,11 @@ export default class Window extends EventTarget implements IWindow {
} = Response;
public readonly DOMRect: typeof DOMRect;

// XMLHttpRequest
public XMLHttpRequest = XMLHttpRequest;
public XMLHttpRequestUpload = XMLHttpRequestUpload;
public XMLHttpRequestEventTarget = XMLHttpRequestEventTarget;

// Events
public onload: (event: Event) => void = null;
public onerror: (event: ErrorEvent) => void = null;
Expand Down Expand Up @@ -305,6 +313,7 @@ export default class Window extends EventTarget implements IWindow {
this.dispatchEvent(new Event('load'));
});

XMLHttpRequest._defaultView = this;
DOMParser._ownerDocument = this.document;
FileReader._ownerDocument = this.document;
Image.ownerDocument = this.document;
Expand Down

0 comments on commit 18b0bd6

Please sign in to comment.