Skip to content

Commit

Permalink
Static nth index fix
Browse files Browse the repository at this point in the history
Don't get caught in an infinite loop trying to adjust the bounds of a static index.
  • Loading branch information
facelessuser committed Dec 5, 2018
1 parent 2978354 commit 0e0dbfd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
68 changes: 35 additions & 33 deletions pyspelling/util/css_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,41 +794,43 @@ def match_nth(self, el, nth):
count_incr = 1
factor = -1 if last else 1
index = len(parent.contents) - 1 if last else 0

# Abort if our nth index is out of bounds and only getting further out of bounds as we increment.
# Otherwise, increment to try to get in bounds.
idx = last_idx = a * count + b if var else a
while idx < 1 or idx > last_index:
diff_low = 0 - idx
if idx < 0:
count += count_incr
idx = last_idx = a * count + b if var else a
diff = 0 - idx
if diff > diff_low:
break
diff_low = diff
diff_high = idx - last_index
if idx > last_index:
count += count_incr
idx = last_idx = a * count + b if var else a
diff = idx - last_index
if diff > diff_high:
break
diff_high = diff

# If a < 0, our count is working backwards, so floor the index by increasing the count.
# Find the count that yields the lowest, in bound value and use that.
# Lastly reverse count increment so that we'll increase our index.
lowest = count
if a < 0:
while idx >= 1:
lowest = count
count += count_incr
idx = last_idx = a * count + b if var else a
count_incr = -1
count = lowest
idx = last_idx = a * count + b if var else a

# We can only adjust bounds within a variable index
if var:
# Abort if our nth index is out of bounds and only getting further out of bounds as we increment.
# Otherwise, increment to try to get in bounds.
while idx < 1 or idx > last_index:
diff_low = 0 - idx
if idx < 0:
count += count_incr
idx = last_idx = a * count + b if var else a
diff = 0 - idx
if diff >= diff_low:
break
diff_low = diff
diff_high = idx - last_index
if idx > last_index:
count += count_incr
idx = last_idx = a * count + b if var else a
diff = idx - last_index
if diff >= diff_high:
break
diff_high = diff

# If a < 0, our count is working backwards, so floor the index by increasing the count.
# Find the count that yields the lowest, in bound value and use that.
# Lastly reverse count increment so that we'll increase our index.
lowest = count
if a < 0:
while idx >= 1:
lowest = count
count += count_incr
idx = last_idx = a * count + b if var else a
count_incr = -1
count = lowest
idx = last_idx = a * count + b if var else a

# Evaluate elements while our calculated nth index is still in range
while 1 <= idx <= last_index:
child = None
Expand Down
27 changes: 27 additions & 0 deletions tests/filters/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,33 @@ def test_css_nth_even(self):
]
)

def test_css_nth_child_bad(self):
"""Test HTML."""

config = self.dedent(
"""
matrix:
- name: html_css
sources:
- '{}/**/*.txt'
aspell:
lang: en
hunspell:
d: en_US
pipeline:
- pyspelling.filters.html:
mode: html5
ignores:
- 'p:nth-child(-2)'
"""
).format(self.tempdir)
self.mktemp('.html5.yml', config, 'utf-8')
self.assert_spellcheck(
'.html5.yml', [
'aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff', 'gggg', 'hhhh', 'iiii', 'jjjj', 'kkkk', 'llll'
]
)

def test_css_nth_child1(self):
"""Test HTML."""

Expand Down

0 comments on commit 0e0dbfd

Please sign in to comment.