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 index out of range when <body> or <head> is missing #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lxml/html/__init__.py
Expand Up @@ -287,15 +287,19 @@ def body(self):
Return the <body> element. Can be called from a child element
to get the document's head.
"""
return self.xpath('//body|//x:body', namespaces={'x':XHTML_NAMESPACE})[0]
result = self.xpath('//body|//x:body', namespaces={'x':XHTML_NAMESPACE})
if len(result) > 1:
return result[0]

@property
def head(self):
"""
Returns the <head> element. Can be called from a child
element to get the document's head.
"""
return self.xpath('//head|//x:head', namespaces={'x':XHTML_NAMESPACE})[0]
result = self.xpath('//head|//x:head', namespaces={'x':XHTML_NAMESPACE})
if len(result) > 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why > 1 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"> 0", because I'm stupid :p

return result[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise … do what? Return None? Would be good to say so if that's what you want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've some difficulty to run lxml tests, it's a bit new for me, and that pull request is a kind of experience. It should return None indeed, I can correct that.


@property
def label(self):
Expand Down