Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(attributes): Align prop undefined handling with jQuery #2557

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/api/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ describe('$(...)', () => {
expect(checkbox.prop('nodeName')).toBe('INPUT');
});

it('(valid key) : should return on empty collection', () => {
expect($(undefined).prop('checked')).toBeUndefined();
expect($(undefined).prop('style')).toBeUndefined();
expect($(undefined).prop('tagName')).toBeUndefined();
expect($(undefined).prop('nodeName')).toBeUndefined();
});

it('(invalid key) : invalid prop should get undefined', () => {
expect(checkbox.prop('lol')).toBeUndefined();
expect(checkbox.prop(4 as any)).toBeUndefined();
Expand Down Expand Up @@ -246,6 +253,12 @@ describe('$(...)', () => {
expect(imgs.prop('data-foo')).toBeUndefined();
});

it('(key, value) : should ignore empty collection', () => {
expect($(undefined).prop('checked')).toBeUndefined();
$(undefined).prop('checked', true);
expect($(undefined).prop('checked')).toBeUndefined();
});

it('(map) : object map should set multiple props', () => {
checkbox.prop({
id: 'check',
Expand Down Expand Up @@ -300,6 +313,8 @@ describe('$(...)', () => {
expect($('#2').prop('href')).toBe('http://example.org/');
expect($('#3').prop('href')).toBe('http://example.com/example.org');
expect($('#4').prop('href')).toBe('http://example.com/page/example.org');

expect($(undefined).prop('href')).toBeUndefined();
});

it('("src") : should resolve links with `baseURI`', () => {
Expand All @@ -321,19 +336,25 @@ describe('$(...)', () => {
expect($('#4').prop('src')).toBe(
'http://example.com/page/example.org/image.png'
);

expect($(undefined).prop('src')).toBeUndefined();
});

it('("outerHTML") : should render properly', () => {
const outerHtml = '<div><a></a></div>';
const $a = $(outerHtml);

expect($a.prop('outerHTML')).toBe(outerHtml);

expect($(undefined).prop('outerHTML')).toBeUndefined();
});

it('("innerHTML") : should render properly', () => {
const $a = $('<div><a></a></div>');

expect($a.prop('innerHTML')).toBe('<a></a>');

expect($(undefined).prop('innerHTML')).toBeUndefined();
});

it('("textContent") : should render properly', () => {
Expand All @@ -342,6 +363,8 @@ describe('$(...)', () => {
);

expect($(script).prop('textContent')).toBe('A var foo = "bar";B');

expect($(undefined).prop('textContent')).toBeUndefined();
});

it('("textContent") : should include style and script tags', () => {
Expand All @@ -363,6 +386,8 @@ describe('$(...)', () => {
);

expect($(script).prop('innerText')).toBe('AB');

expect($(undefined).prop('innerText')).toBeUndefined();
});

it('("innerText") : should omit style and script tags', () => {
Expand Down
33 changes: 15 additions & 18 deletions src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,10 @@ export function attr<T extends AnyNode>(
* @returns The prop's value.
*/
function getProp(
el: AnyNode | undefined,
el: Element,
name: string,
xmlMode?: boolean
): string | undefined | Element[keyof Element] {
if (!el || !isTag(el)) return;

return name in el
? // @ts-expect-error TS doesn't like us accessing the value directly here.
el[name]
Expand Down Expand Up @@ -308,15 +306,15 @@ export function prop<T extends AnyNode>(
export function prop<T extends AnyNode>(
this: Cheerio<T>,
name: 'style'
): StyleProp;
): StyleProp | undefined;
/**
* Resolve `href` or `src` of supported elements. Requires the `baseURI` option
* to be set, and a global `URL` object to be part of the environment.
*
* @example With `baseURI` set to `'https://example.com'`:
*
* ```js
* $('<img src="image.png">).prop('src');
* $('<img src="image.png">').prop('src');
* //=> 'https://example.com/image.png'
* ```
*/
Expand Down Expand Up @@ -363,6 +361,10 @@ export function prop<T extends AnyNode>(
| unknown
): Cheerio<T> | string | undefined | null | Element[keyof Element] | StyleProp {
if (typeof name === 'string' && value === undefined) {
const el = this[0];

if (!el || !isTag(el)) return undefined;

switch (name) {
case 'style': {
const property = this.css() as StyleProp;
Expand All @@ -377,18 +379,11 @@ export function prop<T extends AnyNode>(
}
case 'tagName':
case 'nodeName': {
const el = this[0];
return isTag(el) ? el.name.toUpperCase() : undefined;
return el.name.toUpperCase();
}

case 'href':
case 'src': {
const el = this[0];

if (!isTag(el)) {
return undefined;
}

const prop = el.attribs?.[name];

/* eslint-disable node/no-unsupported-features/node-builtins */
Expand All @@ -411,11 +406,13 @@ export function prop<T extends AnyNode>(
return prop;
}

case 'innerText':
return innerText(this[0]);
case 'innerText': {
return innerText(el);
}

case 'textContent':
return textContent(this[0]);
case 'textContent': {
return textContent(el);
}

case 'outerHTML':
return this.clone().wrap('<container />').parent().html();
Expand All @@ -424,7 +421,7 @@ export function prop<T extends AnyNode>(
return this.html();

default:
return getProp(this[0], name, this.options.xmlMode);
return getProp(el, name, this.options.xmlMode);
}
}

Expand Down