Skip to content

Commit dda946b

Browse files
committed
Strip whitespace from css selectors.
1 parent 4f78e0c commit dda946b

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

app/models/page.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def document
3737
end
3838

3939
def match_text
40-
@match = document.css(self.css_selector)
40+
@match = document.css(self.css_selector.strip)
4141

4242
if self.exclude_selector.present?
4343
# Set the content of the exclude selector to the empty string
44-
@match.css(self.exclude_selector).each do |node|
44+
@match.css(self.exclude_selector.strip).each do |node|
4545
node.content = ""
4646
end
4747
end
@@ -50,7 +50,7 @@ def match_text
5050
end
5151

5252
def match_html
53-
document.css(self.css_selector).to_html
53+
document.css(self.css_selector.strip).to_html
5454
end
5555

5656
def sha2_hash
@@ -59,6 +59,8 @@ def sha2_hash
5959

6060
def sanitize
6161
self.url = url.strip
62+
self.css_selector = css_selector.strip unless self.css_selector.nil?
63+
self.exclude_selector = exclude_selector.strip unless self.exclude_selector.nil?
6264
end
6365

6466
def update_subscriptions

app/models/page_snapshot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def document
1212
end
1313

1414
def match_text
15-
@match = document.css(self.page.css_selector)
15+
@match = document.css(self.page.css_selector.strip)
1616

1717
if self.page.exclude_selector.present?
1818
# Set the content of the exclude selector to the empty string
19-
@match.css(self.page.exclude_selector).each do |node|
19+
@match.css(self.page.exclude_selector.strip).each do |node|
2020
node.content = ""
2121
end
2222
end

spec/models/page_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
expect(page.url).to eq url
7171
end
7272

73+
it "strips whitespace from the selectors" do
74+
selector = ".test"
75+
page = create(:page, css_selector: " #{selector} ", exclude_selector: " #{selector} ")
76+
end
77+
7378
it "gracefully handles parsing an invalid uri" do
7479
weird_url = " bad:///site.com"
7580
page = build(:page, url: weird_url) # our sanitizer is on before_save, which does not run here

0 commit comments

Comments
 (0)