Skip to content
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
5 changes: 3 additions & 2 deletions pyquery/pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ def attr(self, *args, **kwargs):
length = len(args)
if length == 1:
attr = args[0]
attr = mapping.get(attr, attr)
if not isinstance(attr, dict):
attr = mapping.get(attr, attr)
elif length == 2:
attr, value = args
attr = mapping.get(attr, attr)
Expand All @@ -784,7 +785,7 @@ def attr(self, *args, **kwargs):
elif isinstance(attr, dict):
for tag in self:
for key, value in attr.items():
tag.set(key, value)
tag.set(mapping.get(key, key), value)
elif value is no_default:
return self[0].get(attr)
elif value is None:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ def test_attr_empty_string(self):
self.assertEqual(d.outer_html(), '<div value=""></div>')
self.assertEqual(d.outer_html(method="xml"), '<div value=""/>')

def test_attr_dict_mapping(self):
d = pq('<div>')
d.attr({'id': 'x', 'class_': 'y', 'data-a': '1'})
self.assertEqual(d.attr('id'), 'x')
self.assertEqual(d.attr('class'), 'y')
self.assertEqual(d.attr('data-a'), '1')
self.assertEqual(d.attr(id='z', class_='w').attr('id'), 'z')
self.assertEqual(d.attr('class'), 'w')

def test_remove(self):
d = pq(self.html)
d('img').remove()
Expand Down
Loading