Skip to content

Commit

Permalink
Strip whitespace from css selectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
GabeIsman committed Jan 16, 2021
1 parent 4f78e0c commit dda946b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def document
end

def match_text
@match = document.css(self.css_selector)
@match = document.css(self.css_selector.strip)

if self.exclude_selector.present?
# Set the content of the exclude selector to the empty string
@match.css(self.exclude_selector).each do |node|
@match.css(self.exclude_selector.strip).each do |node|
node.content = ""
end
end
Expand All @@ -50,7 +50,7 @@ def match_text
end

def match_html
document.css(self.css_selector).to_html
document.css(self.css_selector.strip).to_html
end

def sha2_hash
Expand All @@ -59,6 +59,8 @@ def sha2_hash

def sanitize
self.url = url.strip
self.css_selector = css_selector.strip unless self.css_selector.nil?
self.exclude_selector = exclude_selector.strip unless self.exclude_selector.nil?
end

def update_subscriptions
Expand Down
4 changes: 2 additions & 2 deletions app/models/page_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def document
end

def match_text
@match = document.css(self.page.css_selector)
@match = document.css(self.page.css_selector.strip)

if self.page.exclude_selector.present?
# Set the content of the exclude selector to the empty string
@match.css(self.page.exclude_selector).each do |node|
@match.css(self.page.exclude_selector.strip).each do |node|
node.content = ""
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
expect(page.url).to eq url
end

it "strips whitespace from the selectors" do
selector = ".test"
page = create(:page, css_selector: " #{selector} ", exclude_selector: " #{selector} ")
end

it "gracefully handles parsing an invalid uri" do
weird_url = " bad:///site.com"
page = build(:page, url: weird_url) # our sanitizer is on before_save, which does not run here
Expand Down

0 comments on commit dda946b

Please sign in to comment.