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

Added text_content() method to selectors. #34

Closed
wants to merge 1 commit into from
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
19 changes: 19 additions & 0 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ def extract_first(self, default=None):
else:
return default

def text_content(self):
"""
Call the ``.text_content()`` method for each element is this list and return
their results flattened, as a list of unicode strings.
"""
return [x.text_content() for x in self]

def text_content_first(self, default=None):
for x in self:
return x.text_content()
else:
return default

class Selector(object):
"""
Expand Down Expand Up @@ -222,6 +234,13 @@ def extract(self):
else:
return six.text_type(self.root)

def text_content(self):
"""
Returns the text content of the element, including the text content of
its children, with no markup.
"""
return six.text_type(self.root.xpath("normalize-space()"))

def register_namespace(self, prefix, uri):
"""
Register the given namespace to be used in this :class:`Selector`.
Expand Down
55 changes: 55 additions & 0 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,61 @@ def test_extract_first_default(self):

self.assertEqual(sel.xpath('//div/text()').extract_first(default='missing'), 'missing')

def test_text_content_first(self):
"""Test if text_first() returns first element"""
body = u'<ul><li id="1">1</li><li id="2">2</li></ul>'
sel = self.sscls(text=body)

self.assertEqual(sel.xpath('//ul/li').text_content_first(),
sel.xpath('//ul/li').text_content()[0])

self.assertEqual(sel.xpath('//ul/li[@id="1"]').text_content_first(),
sel.xpath('//ul/li[@id="1"]').text_content()[0])

self.assertEqual(sel.xpath('//ul/li[2]').text_content_first(),
sel.xpath('//ul/li').text_content()[1])

self.assertEqual(sel.xpath('//ul/li[@id="doesnt-exist"]').text_content_first(), None)

self.assertEqual(sel.xpath('//ul/li').text_content_first(), '1')

self.assertEqual(sel.xpath('//ul/li[2]').text_content_first(), '2'),

self.assertEqual(sel.xpath('//ul').text_content_first(), '12'),

def test_text_content_first_default(self):
"""Test if text_first() returns default value when no results found"""
body = u'<ul><li id="1">1</li><li id="2">2</li></ul>'
sel = self.sscls(text=body)

self.assertEqual(sel.xpath('//div').text_content_first(default='missing'), 'missing')

def test_text_content(self):
"""Test if text_first() returns default value when no results found"""
body = u'<ul><li id="1">1</li><li id="2">2</li></ul>'
sel = self.sscls(text=body)

self.assertEqual(sel.xpath('//ul').text_content(), [u'12'])
self.assertEqual(sel.xpath('//ul/li').text_content(), [u'1', u'2'])

def test_text_content_with_spaces(self):
"""Test if text_first() returns default value when no results found"""
body = u"""
<p>
Mary <b>had </b> a little <i> <br/>
lamb </i>
</p>
<div>meh meh</div>
<p>
It's
<txd>fleece</txd>
was w<em>hi</em>te as s<span>no</span>w.
</p>
"""
sel = self.sscls(text=body)

self.assertEqual(sel.xpath('//p').text_content(), [u'Mary had a little lamb', u'It\'s fleece was white as snow.'])

def test_re_first(self):
"""Test if re_first() returns first matched element"""
body = u'<ul><li id="1">1</li><li id="2">2</li></ul>'
Expand Down