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

feat: lowercased lookup with attr/data #3434

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/api/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ describe('$(...)', () => {
expect(attr).toBe('autofocus');
});

it('(valid key) : valid lowercased attr should get value', () => {
const cls = $('.apple').attr('Class');
expect(cls).toBe('apple');
});

it('(valid key) : valid lowercased attr should get lowercased name when boolean', () => {
const attr = $('<input name=email autofocus>').attr('autoFocus');
expect(attr).toBe('autofocus');
});

it('(key, value) : should set one attr', () => {
const $pear = $('.pear').attr('id', 'pear');
expect($('#pear')).toHaveLength(1);
Expand Down Expand Up @@ -486,6 +496,12 @@ describe('$(...)', () => {
expect(origin).toBe('swiss');
});

it('(valid key) : valid lowercased data attribute should get value', () => {
const highlight = $('.linth').data('highLight');

expect(highlight).toBe('Lindor');
});

it('(key) : should translate camel-cased key values to hyphen-separated versions', () => {
const $el = cheerio(
'<div data--three-word-attribute="a" data-foo-Bar_BAZ-="b">'
Expand Down
16 changes: 16 additions & 0 deletions src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ function getAttr(
return !xmlMode && rboolean.test(name) ? name : elem.attribs[name];
}

// Try again for lowercase
const lowerName = name.toLowerCase();
if (hasOwn.call(elem.attribs, lowerName)) {
// Get the (decoded) attribute
return !xmlMode && rboolean.test(lowerName)
? lowerName
: elem.attribs[lowerName];
}

// Mimic the DOM and return text content as value for `option's`
if (elem.name === 'option' && name === 'value') {
return text(elem.children);
Expand Down Expand Up @@ -580,10 +589,17 @@ function readData(el: DataElement, name: string): unknown {
return data[name];
}

// E.g. .data('helloWorld') will lookup cssCased name: attribs['data-hello-world']
if (hasOwn.call(el.attribs, domName)) {
return (data[name] = parseDataValue(el.attribs[domName]));
}

/** Try again for lowercase. E.g. .data('aBc') will lookup attribs['data-abc']. */
const lowerName = dataAttrPrefix + name.toLowerCase();
if (hasOwn.call(el.attribs, lowerName)) {
return (data[lowerName] = parseDataValue(el.attribs[lowerName]));
}

return undefined;
}

Expand Down