|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The SFC licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | + |
| 21 | +module Selenium |
| 22 | + module WebDriver |
| 23 | + # Represents options for printing a page. |
| 24 | + class PrintOptions |
| 25 | + DEFAULT_SCALE = 1.0 |
| 26 | + DEFAULT_ORIENTATION = 'portrait' |
| 27 | + DEFAULT_PAGE_SIZE = {width: 21.0, height: 29.7}.freeze # A4 size in cm |
| 28 | + DEFAULT_MARGINS = {top: 1.0, bottom: 1.0, left: 1.0, right: 1.0}.freeze |
| 29 | + |
| 30 | + attr_accessor :orientation, :scale, :background, :page_ranges, :margins |
| 31 | + |
| 32 | + def initialize |
| 33 | + @orientation = DEFAULT_ORIENTATION |
| 34 | + @scale = DEFAULT_SCALE |
| 35 | + @background = false |
| 36 | + @page_ranges = nil |
| 37 | + @page_size = DEFAULT_PAGE_SIZE |
| 38 | + @margins = DEFAULT_MARGINS |
| 39 | + end |
| 40 | + |
| 41 | + # Converts the options to a hash format to be used by WebDriver. |
| 42 | + # |
| 43 | + # @return [Hash] |
| 44 | + def to_h |
| 45 | + options = { |
| 46 | + orientation: @orientation, |
| 47 | + scale: @scale, |
| 48 | + background: @background, |
| 49 | + pageRanges: @page_ranges, |
| 50 | + paperWidth: @page_size[:width], |
| 51 | + paperHeight: @page_size[:height], |
| 52 | + marginTop: @margins[:top], |
| 53 | + marginBottom: @margins[:bottom], |
| 54 | + marginLeft: @margins[:left], |
| 55 | + marginRight: @margins[:right] |
| 56 | + } |
| 57 | + |
| 58 | + options.compact |
| 59 | + end |
| 60 | + |
| 61 | + # Gets the current page size. |
| 62 | + # |
| 63 | + # @return [Hash] The current page size hash with :width and :height. |
| 64 | + attr_reader :page_size |
| 65 | + |
| 66 | + # Sets the page size. Can be a predefined symbol or custom size hash. |
| 67 | + # |
| 68 | + # @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash. |
| 69 | + def page_size=(value) |
| 70 | + predefined_sizes = { |
| 71 | + letter: {width: 21.59, height: 27.94}, |
| 72 | + legal: {width: 21.59, height: 35.56}, |
| 73 | + a4: {width: 21.0, height: 29.7}, |
| 74 | + tabloid: {width: 27.94, height: 43.18} |
| 75 | + } |
| 76 | + |
| 77 | + case value |
| 78 | + when Symbol |
| 79 | + raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value) |
| 80 | + |
| 81 | + @page_size = predefined_sizes[value] |
| 82 | + when Hash |
| 83 | + unless value.key?(:width) && value.key?(:height) |
| 84 | + raise ArgumentError, 'Custom page size must include :width and :height' |
| 85 | + end |
| 86 | + |
| 87 | + @page_size = value |
| 88 | + else |
| 89 | + raise ArgumentError, 'Page size must be a Symbol or a Hash' |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments