Skip to content

Commit

Permalink
Fix: Remove IHTMLDocument and IHTMLElement
Browse files Browse the repository at this point in the history
  • Loading branch information
sarvaje committed Feb 25, 2019
1 parent 9cfb1d9 commit 234b72b
Show file tree
Hide file tree
Showing 38 changed files with 230 additions and 234 deletions.
14 changes: 8 additions & 6 deletions packages/connector-jsdom/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
HttpHeaders,
HTMLDocument,
IConnector,
IHTMLElement,
HTMLElement,
Event, FetchEnd, FetchError,
NetworkData
} from 'hint/dist/src/lib/types';
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class JSDOMConnector implements IConnector {
private _window!: Window;
private _document!: HTMLDocument;
private _timeout: number;
private _resourceLoader: ResourceLoader;
private _resourceLoader?: ResourceLoader;
private _subprocesses: Set<ChildProcess>;

public request: Requester;
Expand All @@ -81,7 +81,7 @@ export default class JSDOMConnector implements IConnector {
this.request = new Requester(this._options);
this.server = server;
this._timeout = server.timeout;
this._resourceLoader = new CustomResourceLoader(this);
// this._resourceLoader = new CustomResourceLoader(this);
this._subprocesses = new Set();
}

Expand Down Expand Up @@ -123,7 +123,7 @@ export default class JSDOMConnector implements IConnector {
const href = (element && element.getAttribute('href')) || '/favicon.ico';

try {
await this._resourceLoader.fetch(new URL(href, this.finalHref).href, { element });
await this._resourceLoader!.fetch(new URL(href, this.finalHref).href, { element });
} catch (e) {
/* istanbul ignore next */
debug('Error loading ${href}', e);
Expand Down Expand Up @@ -225,6 +225,8 @@ export default class JSDOMConnector implements IConnector {
debug(`Console: ${err}`);
});

this._resourceLoader = new CustomResourceLoader(this, fetchEnd.response. body.content);

const jsdom = new JSDOM(this._targetNetworkData.response.body.content, {
beforeParse: beforeParse(this.finalHref),
includeNodeLocations: true,
Expand All @@ -249,7 +251,7 @@ export default class JSDOMConnector implements IConnector {

debug(`${this.finalHref} loaded, traversing`);
try {
const html = await this._window.document.documentElement.outerHTML;
const html = this._window.document.documentElement.outerHTML;

const htmlDocument = createHTMLDocument(html);

Expand Down Expand Up @@ -385,7 +387,7 @@ export default class JSDOMConnector implements IConnector {
}

/* istanbul ignore next */
public querySelectorAll(selector: string): IHTMLElement[] {
public querySelectorAll(selector: string): HTMLElement[] {
return this._document.querySelectorAll(selector);
}

Expand Down
26 changes: 22 additions & 4 deletions packages/connector-jsdom/src/resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,39 @@ import { debug as d } from 'hint/dist/src/lib/utils/debug';
import { ResourceLoader } from 'jsdom';

import JSDOMConnector from './connector';
import { IHTMLElement } from 'hint/dist/src/lib/types';
import { HTMLDocument } from 'hint/dist/src/lib/types';
import createHTMLDocument from 'hint/dist/src/lib/utils/dom/create-html-document';
import { NetworkData, FetchEnd, FetchError } from 'hint/dist/src/lib/types';
import { getContentTypeData, getType } from 'hint/dist/src/lib/utils/content-type';

const debug: debug.IDebugger = d(__filename);

export default class CustomResourceLoader extends ResourceLoader {
private _connector: JSDOMConnector;
private _HTMLDocument: HTMLDocument;

public constructor(connector: JSDOMConnector) {
public constructor(connector: JSDOMConnector, html: string) {
super();

this._connector = connector;
this._HTMLDocument = createHTMLDocument(html);
}

public async fetch(url: string, options: { element: IHTMLElement }): Promise<Buffer | null> {
private createQuery(element: Element): string {
let query: string = element.tagName;

for (let i = 0; i < element.attributes.length; i++) {
const attribute = element.attributes.item(i);

if (attribute) {
query += `[${attribute.name}="${attribute.value}"]`;
}
}

return query;
}

public async fetch(url: string, options: any): Promise<Buffer | null> {
/* istanbul ignore if */
if (!url) {
const promise = Promise.resolve(null);
Expand All @@ -29,9 +46,10 @@ export default class CustomResourceLoader extends ResourceLoader {
return await promise;
}

const elements = options.element ? this._HTMLDocument.querySelectorAll(this.createQuery(options.element)) : null;
const element = (elements && elements.length > 0) ? elements[0] : null;
const urlAsUrl = new URL(url);
let resourceUrl: string = urlAsUrl.href;
const element = options.element;

/* istanbul ignore if */
if (!urlAsUrl.protocol) {
Expand Down
13 changes: 9 additions & 4 deletions packages/connector-local/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import * as globby from 'globby';
import { getAsUri } from 'hint/dist/src/lib/utils/network/as-uri';
import asPathString from 'hint/dist/src/lib/utils/network/as-path-string';
import { getContentTypeData, isTextMediaType, getType } from 'hint/dist/src/lib/utils/content-type';
import { IHTMLDocument, IHTMLElement } from 'hint/dist/src/lib/types';
import createHTMLDocument from 'hint/dist/src/lib/utils/dom/create-html-document';
import traverse from 'hint/dist/src/lib/utils/dom/traverse';

Expand All @@ -33,9 +32,15 @@ import readFileAsync from 'hint/dist/src/lib/utils/fs/read-file-async';
import * as logger from 'hint/dist/src/lib/utils/logging';

import {
CanEvaluateScript,
Event,
FetchEnd,
HTMLDocument,
HTMLElement,
IConnector,
IFetchOptions,
Event, FetchEnd, ScanEnd, NetworkData, CanEvaluateScript, HTMLDocument
NetworkData,
ScanEnd
} from 'hint/dist/src/lib/types';
import { Engine } from 'hint/dist/src/lib/engine';
import { HTMLParse, HTMLEvents } from '@hint/parser-html';
Expand Down Expand Up @@ -371,7 +376,7 @@ export default class LocalConnector implements IConnector {
}

/* istanbul ignore next */
public querySelectorAll(selector: string): IHTMLElement[] {
public querySelectorAll(selector: string): HTMLElement[] {
return this._document ? this._document.querySelectorAll(selector) : [];
}

Expand All @@ -381,7 +386,7 @@ export default class LocalConnector implements IConnector {
}

/* istanbul ignore next */
public get dom(): IHTMLDocument | undefined {
public get dom(): HTMLDocument | undefined {
return this._document && this._document;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/extension-browser/src/content-script/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
IConnector,
FetchEnd,
NetworkData,
IHTMLElement,
IHTMLDocument,
HTMLDocument,
HTMLElement
} from 'hint/dist/src/lib/types';

Expand Down Expand Up @@ -78,7 +77,7 @@ export default class WebExtensionConnector implements IConnector {
});

if (elements.length) {
event.element = new HTMLElement(elements[0] as HTMLElement, this._document);
event.element = new HTMLElement(elements[0], this._document);
}
}

Expand Down Expand Up @@ -157,7 +156,7 @@ export default class WebExtensionConnector implements IConnector {
return Promise.resolve(eval(source)); // eslint-disable-line no-eval
}

public querySelectorAll(selector: string): IHTMLElement[] {
public querySelectorAll(selector: string): HTMLElement[] {
return this._document.querySelectorAll(selector);
}

Expand All @@ -166,7 +165,7 @@ export default class WebExtensionConnector implements IConnector {
return Promise.resolve();
}

public get dom(): IHTMLDocument {
public get dom(): HTMLDocument {
return this._document;
}

Expand Down
16 changes: 8 additions & 8 deletions packages/hint-apple-touch-icons/src/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as getImageData from 'image-size';
import { debug as d } from 'hint/dist/src/lib/utils/debug';
import isRegularProtocol from 'hint/dist/src/lib/utils/network/is-regular-protocol';
import normalizeString from 'hint/dist/src/lib/utils/misc/normalize-string';
import { IHint, TraverseEnd, NetworkData, IHTMLElement, IHTMLDocument } from 'hint/dist/src/lib/types';
import { IHint, TraverseEnd, NetworkData, HTMLElement, HTMLDocument } from 'hint/dist/src/lib/types';
import { HintContext } from 'hint/dist/src/lib/hint-context';

import meta from './meta';
Expand All @@ -35,7 +35,7 @@ export default class AppleTouchIconsHint implements IHint {
* https://www.w3.org/TR/selectors4/#attribute-case
*/

const getAppleTouchIcons = (elements: IHTMLElement[]): IHTMLElement[] => {
const getAppleTouchIcons = (elements: HTMLElement[]): HTMLElement[] => {
return elements.filter((element) => {

/*
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class AppleTouchIconsHint implements IHint {
});
};

const checkImage = async (appleTouchIcon: IHTMLElement, resource: string) => {
const checkImage = async (appleTouchIcon: HTMLElement, resource: string) => {
const appleTouchIconHref = normalizeString(appleTouchIcon.getAttribute('href'));

/*
Expand Down Expand Up @@ -184,7 +184,7 @@ export default class AppleTouchIconsHint implements IHint {
// TODO: Check if the image has some kind of transparency.
};

const chooseBestIcon = (icons: IHTMLElement[]): IHTMLElement => {
const chooseBestIcon = (icons: HTMLElement[]): HTMLElement => {

/*
* Site will usually have something such as:
Expand Down Expand Up @@ -220,8 +220,8 @@ export default class AppleTouchIconsHint implements IHint {


const validate = async ({ resource }: TraverseEnd) => {
const pageDOM: IHTMLDocument = context.pageDOM as IHTMLDocument;
const appleTouchIcons: IHTMLElement[] = getAppleTouchIcons(pageDOM.querySelectorAll('link'));
const pageDOM: HTMLDocument = context.pageDOM as HTMLDocument;
const appleTouchIcons: HTMLElement[] = getAppleTouchIcons(pageDOM.querySelectorAll('link'));

if (appleTouchIcons.length === 0) {
context.report(resource, `'apple-touch-icon' link element was not specified.`);
Expand All @@ -234,7 +234,7 @@ export default class AppleTouchIconsHint implements IHint {
* pass most of the following tests.
*/

const appleTouchIcon: IHTMLElement = chooseBestIcon(appleTouchIcons);
const appleTouchIcon: HTMLElement = chooseBestIcon(appleTouchIcons);

/*
* Check if `rel='apple-touch-icon'`.
Expand Down Expand Up @@ -273,7 +273,7 @@ export default class AppleTouchIconsHint implements IHint {
* Check if the `apple-touch-icon` is included in the `<body>`.
*/

const bodyAppleTouchIcons: IHTMLElement[] = getAppleTouchIcons(pageDOM.querySelectorAll('body link'));
const bodyAppleTouchIcons: HTMLElement[] = getAppleTouchIcons(pageDOM.querySelectorAll('body link'));

for (const icon of bodyAppleTouchIcons) {
if (icon.isSame(appleTouchIcon)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/hint-axe/src/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { AxeResults, Result as AxeResult, NodeResult as AxeNodeResult } from 'axe-core';

import { debug as d } from 'hint/dist/src/lib/utils/debug';
import { IHTMLElement, IHint, Severity, CanEvaluateScript } from 'hint/dist/src/lib/types';
import { HTMLElement, IHint, Severity, CanEvaluateScript } from 'hint/dist/src/lib/types';
import readFileAsync from 'hint/dist/src/lib/utils/fs/read-file-async';
import { HintContext } from 'hint/dist/src/lib/hint-context';

Expand Down Expand Up @@ -56,9 +56,9 @@ export default class AxeHint implements IHint {
return script;
};

const getElement = (node: AxeNodeResult): IHTMLElement => {
const getElement = (node: AxeNodeResult): HTMLElement => {
const selector: string = node.target[0];
const elements: IHTMLElement[] = context.querySelectorAll(selector);
const elements: HTMLElement[] = context.querySelectorAll(selector);

return elements[0];
};
Expand Down
4 changes: 2 additions & 2 deletions packages/hint-compat-api/src/core/api-hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ export abstract class APIHint<T extends Events> implements IHint {
this.pendingReports = [];
}

private async consumeReports(): Promise<void> {
private consumeReports() {
while (this.reports.length > 0) {
const [feature, supportStatementResult] = this.reports.shift() as [FeatureInfo, SupportStatementResult];
const message = this.generateReportErrorMessage(feature, supportStatementResult);

await this.compatLibrary.reportError(feature, message);
this.compatLibrary.reportError(feature, message);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/hint-compat-api/src/helpers/compat-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class CompatBase<T extends Events, K extends Event> implements I
protected testFunction: TestFeatureFunction;
private cachedFeatures: CompatFeaturesCache;

public abstract async searchFeatures(parser?: K): Promise<void>
public abstract searchFeatures(parser?: K): void

public constructor(hintContext: HintContext<T>, MDNData: MDNTreeFilteredByBrowsers, testFunction: TestFeatureFunction) {
if (!testFunction) {
Expand All @@ -34,10 +34,10 @@ export abstract class CompatBase<T extends Events, K extends Event> implements I
this.hintResource = hintResource;
}

public async reportError(feature: FeatureInfo, message: string): Promise<void> {
public reportError(feature: FeatureInfo, message: string) {
const { location } = feature;

await this.hintContext.report(this.hintResource, message, { location });
this.hintContext.report(this.hintResource, message, { location });
}

private isFeatureAlreadyReported(feature: FeatureInfo): boolean {
Expand Down
10 changes: 5 additions & 5 deletions packages/hint-compat-api/src/helpers/compat-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ export class CompatCSS extends CompatBase<StyleEvents, StyleParse> {
hintContext.on('parse::end::css', this.onParse.bind(this));
}

public async searchFeatures(parse: StyleParse): Promise<void> {
await parse.ast.walk(async (node: ChildNode) => {
public searchFeatures(parse: StyleParse) {
parse.ast.walk((node: ChildNode) => {
const strategy = this.chooseStrategyToSearchCSSFeature(node);
const location = this.getProblemLocationFromNode(node);

await strategy.testFeature(node, location);
strategy.testFeature(node, location);
});
}

private async onParse(parse: StyleParse): Promise<void> {
private onParse(parse: StyleParse) {
const { resource } = parse;

this.setResource(resource);

await this.searchFeatures(parse);
this.searchFeatures(parse);
}

private testFeature(strategyName: string, featureNameWithPrefix: string, location?: ProblemLocation, subfeatureNameWithPrefix?: string): void {
Expand Down
Loading

0 comments on commit 234b72b

Please sign in to comment.