Skip to content

Commit

Permalink
Convert regex matches to equals
Browse files Browse the repository at this point in the history
This changes how we check whether a content_id matches for
link/contacts/attachments. As the content_id is user input it should not be
treated as a regex, so the equality operator is used instead. This
shouldn't break any existing usages as nothing should have been reliant
on regexs.

This is to fix an error that was occurring of: "premature end of
char-class: /1-1992 G-BJRT[2/"
  • Loading branch information
kevindew committed May 3, 2017
1 parent 34c5a30 commit e130f49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Fix matching links/attachments/contacts by regex to use equality

## 5.0.2
* Loosen ActionView dependency to allow use with Rails
5 [#99](https://github.com/alphagov/govspeak/pull/99)
Expand Down
10 changes: 5 additions & 5 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
end

extension('attachment', /\[embed:attachments:(?!inline:|image:)\s*(.*?)\s*\]/) do |content_id, body|
attachment = attachments.detect { |a| a[:content_id].match(content_id) }
attachment = attachments.detect { |a| a[:content_id] == content_id }
next "" unless attachment
attachment = AttachmentPresenter.new(attachment)
content = File.read(__dir__ + '/templates/attachment.html.erb')
ERB.new(content).result(binding)
end

extension('attachment inline', /\[embed:attachments:inline:\s*(.*?)\s*\]/) do |content_id|
attachment = attachments.detect { |a| a[:content_id].match(content_id) }
attachment = attachments.detect { |a| a[:content_id] == content_id }
next "" unless attachment
attachment = AttachmentPresenter.new(attachment)
span_id = attachment.id ? %{ id="attachment_#{attachment.id}"} : ""
Expand All @@ -210,7 +210,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
end

extension('attachment image', /\[embed:attachments:image:\s*(.*?)\s*\]/) do |content_id|
attachment = attachments.detect { |a| a[:content_id].match(content_id) }
attachment = attachments.detect { |a| a[:content_id] == content_id }
next "" unless attachment
attachment = AttachmentPresenter.new(attachment)
title = (attachment.title || "").tr("\n", " ")
Expand Down Expand Up @@ -300,7 +300,7 @@ def self.devolved_options
end

extension('embed link', /\[embed:link:\s*(.*?)\s*\]/) do |content_id|
link = links.detect { |l| l[:content_id].match(content_id) }
link = links.detect { |l| l[:content_id] == content_id }
next "" unless link
if link[:url]
"[#{link[:title]}](#{link[:url]})"
Expand All @@ -315,7 +315,7 @@ def render_hcard_address(contact)
private :render_hcard_address

extension('Contact', /\[Contact:\s*(.*?)\s*\]/) do |content_id|
contact = contacts.detect { |c| c[:content_id].match(content_id) }
contact = contacts.detect { |c| c[:content_id] == content_id }
next "" unless contact
contact = ContactPresenter.new(contact)
@renderer ||= ERB.new(File.read(__dir__ + '/templates/contact.html.erb'))
Expand Down

0 comments on commit e130f49

Please sign in to comment.