Permalink
Browse files

Merge remote branch 'huerlisi/master'

  • Loading branch information...
2 parents 16de846 + 7fd74e3 commit d54daa7fb13cb1e79a694bca413709efb555b964 @mdeering mdeering committed May 9, 2011
Showing with 41 additions and 14 deletions.
  1. +1 −0 lib/pdfkit/middleware.rb
  2. +10 −14 lib/pdfkit/pdfkit.rb
  3. +30 −0 spec/pdfkit_spec.rb
View
@@ -10,6 +10,7 @@ def initialize(app, options = {}, conditions = {})
def call(env)
@request = Rack::Request.new(env)
+ @render_pdf = false
set_request_to_render_as_pdf(env) if render_as_pdf?
status, headers, response = @app.call(env)
View
@@ -79,23 +79,19 @@ def to_file(path)
protected
- def find_options_in_meta(body)
- pdfkit_meta_tags(body).inject({}) do |found, tag|
- name = tag.attributes["name"].sub(/^#{PDFKit.configuration.meta_tag_prefix}/, '').to_sym
- found.merge(name => tag.attributes["content"])
+ def find_options_in_meta(content)
+ # Read file if content is a File
+ content = content.read if content.is_a?(File)
+
+ found = {}
+ content.scan(/<meta [^>]*>/) do |meta|
+ if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
+ name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0]
+ found[name] = meta.scan(/content=["']([^"']*)/)[0][0]
+ end
end
- end
- def pdfkit_meta_tags(body)
- require 'rexml/document'
- xml_body = REXML::Document.new(body)
- found = []
- xml_body.elements.each("html/head/meta") do |tag|
- found << tag if tag.attributes['name'].to_s =~ /^#{PDFKit.configuration.meta_tag_prefix}/
- end
found
- rescue # rexml random crash on invalid xml
- []
end
def style_tag_for(stylesheet)
View
@@ -105,6 +105,36 @@
pdfkit.command[pdfkit.command.index('"--page-size"') + 1].should == '"Legal"'
pdfkit.command[pdfkit.command.index('"--orientation"') + 1].should == '"Landscape"'
end
+
+ it "should detect special pdfkit meta tags despite bad markup" do
+ body = %{
+ <html>
+ <head>
+ <meta name="pdfkit-page_size" content="Legal"/>
+ <meta name="pdfkit-orientation" content="Landscape"/>
+ </head>
+ <br>
+ </html>
+ }
+ pdfkit = PDFKit.new(body)
+ pdfkit.command[pdfkit.command.index('"--page-size"') + 1].should == '"Legal"'
+ pdfkit.command[pdfkit.command.index('"--orientation"') + 1].should == '"Landscape"'
+ end
+
+ it "should skip non-pdfkit meta tags" do
+ body = %{
+ <html>
+ <head>
+ <meta name="test-page_size" content="Legal"/>
+ <meta name="pdfkit-orientation" content="Landscape"/>
+ </head>
+ <br>
+ </html>
+ }
+ pdfkit = PDFKit.new(body)
+ pdfkit.command[pdfkit.command.index('"--orientation"') + 1].should == '"Landscape"'
+ end
+
end
context "#to_pdf" do

0 comments on commit d54daa7

Please sign in to comment.