Skip to content
Closed
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
21 changes: 17 additions & 4 deletions pyquery/pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,11 +1590,24 @@ def serialize_pairs(self):
if el[0].tag == 'form':
form_id = el.attr('id')
if form_id:
# Include inputs outside of their form owner
# Avoid #id selectors: HTML ids may contain CSS metacharacters.
root = self._copy(el.root.getroot())
controls.extend(root(
'#%s :not([form]):input, [form="%s"]:input'
% (form_id, form_id)))
form_elem = el[0]
matched = []
for tag in root(':input'):
owner = tag.get('form')
if owner is not None:
if owner == form_id:
matched.append(tag)
continue
parent = tag.getparent()
while parent is not None:
if parent is form_elem:
matched.append(tag)
break
parent = parent.getparent()
if matched:
controls.extend(self._copy(matched))
else:
controls.extend(el(':not([form]):input'))
elif el[0].tag == 'fieldset':
Expand Down
30 changes: 30 additions & 0 deletions tests/test_pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,36 @@ def test_serialize_pairs_form_id(self):
('spam', 'Spam'),
])

def test_serialize_pairs_form_id_css_metacharacters(self):
# HTML ids may contain CSS metacharacters; #id is not a literal match.
html = (
'<form id="a.b">'
'<input name="in" value="1">'
'</form>'
'<input form="a.b" name="out" value="2">'
)
self.assertEqual(pq(html)('form').serialize_pairs(), [
('in', '1'), ('out', '2'),
])
html_colon = (
'<form id="a:b">'
'<input name="in" value="1">'
'</form>'
'<input form="a:b" name="out" value="2">'
)
self.assertEqual(pq(html_colon)('form').serialize_pairs(), [
('in', '1'), ('out', '2'),
])
html_quote = (
'<form id="a&quot;b">'
'<input name="in" value="1">'
'</form>'
'<input form=\'a"b\' name="out" value="2">'
)
self.assertEqual(pq(html_quote)('form').serialize_pairs(), [
('in', '1'), ('out', '2'),
])

def test_serialize_pairs_form_controls(self):
d = pq(self.html2)
self.assertEqual(d('fieldset').serialize_pairs(), [
Expand Down
Loading