From 7bd73e0c094891e5a2d7634c916df471bbc92eef Mon Sep 17 00:00:00 2001 From: elpolilla Date: Wed, 31 Dec 2008 13:23:25 +0000 Subject: [PATCH] Modified XPathSelectorLists: adding them should return a new XPathSelectorList --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40591 --- scrapy/trunk/scrapy/tests/test_xpath.py | 11 ++++++++++- scrapy/trunk/scrapy/xpath/selector.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/scrapy/trunk/scrapy/tests/test_xpath.py b/scrapy/trunk/scrapy/tests/test_xpath.py index 99c06119b..4a937a8ec 100644 --- a/scrapy/trunk/scrapy/tests/test_xpath.py +++ b/scrapy/trunk/scrapy/tests/test_xpath.py @@ -4,7 +4,7 @@ import unittest import libxml2 from scrapy.http import Response -from scrapy.xpath.selector import XmlXPathSelector, HtmlXPathSelector +from scrapy.xpath.selector import XmlXPathSelector, HtmlXPathSelector, XPathSelectorList class XPathTestCase(unittest.TestCase): @@ -220,6 +220,15 @@ class XPathTestCase(unittest.TestCase): u'\n ', u'\n pff\n']) + def test_selectorlist_add(self): + '''Adding XPathSelectorLists should return another XPathSelectorList''' + hxs = HtmlXPathSelector(text='') + xlist1 = XPathSelectorList([hxs.x('/'), hxs.x('//tag1')]) + xlist2 = XPathSelectorList([hxs.x('/html')]) + xlist3 = [1, 2, 3] + self.assertTrue(isinstance(xlist1 + xlist2, XPathSelectorList)) + self.assertTrue(isinstance(xlist2 + xlist3, list)) + if __name__ == "__main__": unittest.main() diff --git a/scrapy/trunk/scrapy/xpath/selector.py b/scrapy/trunk/scrapy/xpath/selector.py index f614f9f5f..f83db70b0 100644 --- a/scrapy/trunk/scrapy/xpath/selector.py +++ b/scrapy/trunk/scrapy/xpath/selector.py @@ -102,6 +102,12 @@ class XPathSelectorList(list): def __getslice__(self, i, j): return XPathSelectorList(list.__getslice__(self, i, j)) + def __add__(self, other): + if isinstance(other, XPathSelectorList): + return XPathSelectorList(super(XPathSelectorList, self).__add__(other)) + else: + return super(XPathSelectorList, self).__add__(other) + def x(self, xpath): """Perform the given XPath query on each XPathSelector of the list and return a new (flattened) XPathSelectorList of the results"""