Skip to content

Commit

Permalink
Merge pull request #149 from bsquizz/table_caption
Browse files Browse the repository at this point in the history
Add caption property and support for ignoring tags when building tree
  • Loading branch information
RonnyPfannschmidt authored Apr 18, 2019
2 parents 3ffe023 + 987b50a commit 421bae7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/widgetastic/widget/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ class MyCustomTable(Table):
COLUMN_AT_POSITION = './td[{0}]'
ROW_AT_INDEX = './tbody/tr[{0}]|./tr[not(./th)][{0}]'

# These tags are not added as Node objects to the table tree when
# _process_table is called.
IGNORE_TAGS = ['caption']

ROW_TAG = 'tr'
COLUMN_TAG = 'td'
HEADER_IN_ROWS = './tbody/tr[1]/th'
Expand Down Expand Up @@ -419,6 +423,13 @@ def table_tree(self):
def resolver(self):
return TableResolver()

@cached_property
def caption(self):
try:
return self.browser.elements('./caption')[0].text
except IndexError:
return None

def __repr__(self):
return (
'{}({!r}, column_widgets={!r}, assoc_column={!r}, rows_ignore_top={!r}, '
Expand Down Expand Up @@ -978,6 +989,9 @@ def has_rowcolspan(self):
"""Checks whether table has rowspan/colspan attributes"""
return bool(self.browser.elements('./tbody//td[@rowspan or @colspan]', parent=self))

def _filter_child(self, child):
return child.tag_name not in self.IGNORE_TAGS

def _process_table(self):
queue = deque()
tree = Node(name=self.browser.tag(self), obj=self, position=0)
Expand All @@ -987,8 +1001,10 @@ def _process_table(self):
node = queue.popleft()
# visit node
children = self.browser.elements('./*[descendant-or-self::node()]', parent=node.obj)
for position, child in enumerate(children):
cur_tag = self.browser.tag(child)

for position, child in enumerate(filter(self._filter_child, children)):
cur_tag = child.tag_name

if cur_tag == self.ROW_TAG:
# todo: add logger
cur_obj = self._create_row(
Expand All @@ -997,6 +1013,7 @@ def _process_table(self):
)
cur_node = Node(name=cur_tag, parent=node, obj=cur_obj, position=position)
queue.append(cur_node)

elif cur_tag == self.COLUMN_TAG:
cur_position = self._get_position_respecting_spans(node)
cur_obj = self._create_column(
Expand Down
2 changes: 2 additions & 0 deletions testing/test_basic_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class TestForm(View):
view = TestForm(browser)
assert view.table.headers == (None, 'Column 1', 'Column 2', 'Column 3', 'Column 4')
assert view.table1.headers == ('#', 'First Name', 'Last Name', 'Username', 'Widget')
assert view.table1.caption == "Some caption"
dir(view.table[0])
assert len(list(view.table.rows())) == 3
assert len(list(view.table1.rows())) == 8
Expand Down Expand Up @@ -391,6 +392,7 @@ class TestForm(View):
view = TestForm(browser)

assert view.table1.headers == ('#', 'First Name', 'Last Name', 'Username', 'Widget')
assert view.table1.caption == "Some caption"
assert len(list(view.table1.rows())) == 3

assert len(list(view.table1.rows(first_name='Mark'))) == 1
Expand Down
4 changes: 3 additions & 1 deletion testing/testing_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ <h3 id="switchabletesting-2">bartest</h3>
</script>

<table class="table table-striped table-bordered" border="1" id="rowcolspan_table">
<caption>Some caption</caption>
<thead>
<tr>
<th>#</th>
Expand Down Expand Up @@ -239,6 +240,7 @@ <h3 id="switchabletesting-2">bartest</h3>

<br><br>
<table class="table table-striped table-bordered" border="1" id="multiple_tbody_table">
<caption>Some caption</caption>
<thead>
<tr>
<th>#</th>
Expand Down Expand Up @@ -286,4 +288,4 @@ <h3 id="switchabletesting-2">bartest</h3>
</table>

</body>
</html>
</html>

0 comments on commit 421bae7

Please sign in to comment.