1+ # frozen_string_literal: true
2+
13# <copyright file="print_options.rb" company="Selenium Committers">
24# Licensed to the Software Freedom Conservancy (SFC) under one
35# or more contributor license agreements. See the NOTICE file
@@ -22,11 +24,11 @@ module WebDriver
2224 # Represents options for printing a page.
2325 class PrintOptions
2426 DEFAULT_SCALE = 1.0
25- DEFAULT_ORIENTATION = 'portrait' . freeze
27+ DEFAULT_ORIENTATION = 'portrait'
2628 DEFAULT_PAGE_SIZE = { width : 21.0 , height : 29.7 } . freeze # A4 size in cm
2729 DEFAULT_MARGINS = { top : 1.0 , bottom : 1.0 , left : 1.0 , right : 1.0 } . freeze
2830
29- attr_accessor :orientation , :scale , :background , :page_ranges , :page_size , : margins
31+ attr_accessor :orientation , :scale , :background , :page_ranges , :margins
3032
3133 def initialize
3234 @orientation = DEFAULT_ORIENTATION
@@ -57,20 +59,65 @@ def to_h
5759 options . compact
5860 end
5961
60- # Sets the page size to a predefined size.
62+ # Gets the current page size.
6163 #
62- # @param [Symbol] size The predefined size (:letter, :legal, :a4, :tabloid).
63- def set_page_size ( size )
64+ # @return [Hash] The current page size hash with :width and :height.
65+ def page_size
66+ @page_size
67+ end
68+
69+ # Sets the page size. Can be a predefined symbol or custom size hash.
70+ #
71+ # @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
72+ def page_size = ( value )
6473 predefined_sizes = {
6574 letter : { width : 21.59 , height : 27.94 } ,
6675 legal : { width : 21.59 , height : 35.56 } ,
6776 a4 : { width : 21.0 , height : 29.7 } ,
6877 tabloid : { width : 27.94 , height : 43.18 }
6978 }
7079
71- raise ArgumentError , "Invalid page size: #{ size } " unless predefined_sizes . key? ( size )
80+ case value
81+ when Symbol
82+ raise ArgumentError , "Invalid page size: #{ value } " unless predefined_sizes . key? ( value )
83+
84+ @page_size = predefined_sizes [ value ]
85+ when Hash
86+ unless value . key? ( :width ) && value . key? ( :height )
87+ raise ArgumentError , 'Custom page size must include :width and :height'
88+ end
89+
90+ @page_size = value
91+ else
92+ raise ArgumentError , 'Page size must be a Symbol or a Hash'
93+ end
94+ end
95+
96+ # Sets the page size. Can be a predefined symbol or custom size hash.
97+ #
98+ # @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
99+ def page_size = ( value )
100+ predefined_sizes = {
101+ letter : { width : 21.59 , height : 27.94 } ,
102+ legal : { width : 21.59 , height : 35.56 } ,
103+ a4 : { width : 21.0 , height : 29.7 } ,
104+ tabloid : { width : 27.94 , height : 43.18 }
105+ }
106+
107+ case value
108+ when Symbol
109+ raise ArgumentError , "Invalid page size: #{ value } " unless predefined_sizes . key? ( value )
110+
111+ @page_size = predefined_sizes [ value ]
112+ when Hash
113+ unless value . key? ( :width ) && value . key? ( :height )
114+ raise ArgumentError , 'Custom page size must include :width and :height'
115+ end
72116
73- @page_size = predefined_sizes [ size ]
117+ @page_size = value
118+ else
119+ raise ArgumentError , 'Page size must be a Symbol or a Hash'
120+ end
74121 end
75122 end
76123 end
0 commit comments