Skip to content

Commit

Permalink
Fix: Error with jsdom and attribute names with .
Browse files Browse the repository at this point in the history
Close #482
  • Loading branch information
Anton Molleda authored and alrra committed Sep 1, 2017
1 parent b125186 commit 19f95d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/lib/connectors/jsdom/jsdom-async-html.ts
Expand Up @@ -13,12 +13,17 @@ export class JSDOMAsyncHTMLDocument implements IAsyncHTMLDocument {
// ------------------------------------------------------------------------------

public querySelectorAll(selector: string): Promise<Array<JSDOMAsyncHTMLElement>> {
const elements = Array.prototype.slice.call(this._document.querySelectorAll(selector))
.map((element) => {
return new JSDOMAsyncHTMLElement(element); // eslint-disable-line no-use-before-define, typescript/no-use-before-define
});

return Promise.resolve(elements);
// jsdom's `querySelectorAll` can be a bit fragile (e.g.: fails if attribute name has `.` on it)
try {
const elements = Array.prototype.slice.call(this._document.querySelectorAll(selector))
.map((element) => {
return new JSDOMAsyncHTMLElement(element); // eslint-disable-line no-use-before-define, typescript/no-use-before-define
});

return Promise.resolve(elements);
} catch (e) {
return Promise.resolve([]);
}
}

public pageHTML(): Promise<string> {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/utils/location-helpers.ts
Expand Up @@ -23,7 +23,11 @@ const selectorFromElement = (element: IAsyncHTMLElement): string => {
for (let i = 0; i < attributes.length; i++) {
const attribute: IAsyncHTMLAttribute | NamedNodeMap = attributes[i];

selector += `[${attribute.name}="${escapeQuotes(attribute.value)}"]`;
/* jsdom breaks when attribute names have a `.` (invalid) but it is widely used,
so we ignore that selector. */
if (!attribute.name.includes('.')) {
selector += `[${attribute.name}="${escapeQuotes(attribute.value)}"]`;
}
}

debug(`Selector created: ${selector}`);
Expand Down

0 comments on commit 19f95d1

Please sign in to comment.