/ chmod777_includes / html_css / css_bg_size.lua
css_bg_size.lua
 1  -- File: css_bg_size.lua
 2  
 3  --[[
 4  Copyright (C) 2024 chmod777
 5  
 6  This program is free software: you can redistribute it and/or modify it under
 7  the terms of the GNU Affero General Public License version 3 as published by the
 8  Free Software Foundation.
 9  
10  This program is distributed in the hope that it will be useful, but WITHOUT ANY
11  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12  PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
13  
14  You should have received a copy of the GNU Affero General Public License along
15  with this program. If not, see <https://www.gnu.org/licenses/>. 
16  ]]
17  
18  -- background-size =
19  --   <bg-size>#
20  
21  -- <bg-size> = 
22  --   [ <length-percentage [0,∞]> | auto ]{1,2}  |
23  --   cover                                      |
24  --   contain
25  
26  -- TODO multiple backgrounds seperated by ,
27  BackgroundSize = {
28  	['auto'] = 0,
29  	['cover'] = 1,
30  	['contain'] = 2,
31  	parse = function (name, parser)
32  		local source = parser.source
33  		local position = source.position
34  	
35  		local value = source:match_ident_from_table(BackgroundSize)
36  		if value ~= nil and value ~= 'auto' then
37  			return {
38  				valueType = 'background-size',
39  				value = value
40  			}
41  		end
42  	
43  		local width
44  		if value == 'auto' then
45  			width = value
46  		else
47  			width = Length.parse(parser)
48  		end
49  		if width == nil then
50  			return nil
51  		end
52  	
53  		source:match_whitespaces()
54  	
55  		local height = Length.parse(parser)
56  		if height == nil then
57  			-- TODO match auto
58  			height = 'auto'
59  		end
60  	
61  		return {
62  			valueType = 'background-size',
63  			value = BackgroundSize:new(width, height)
64  		}
65  	end
66  }
67  function BackgroundSize:new(width, height)
68  	if width == nil then width = 'auto' end
69  	if height == nil then height = 'auto' end
70  
71  	local this = {
72  		width = width,
73  		height = height,
74  	}
75  
76  	return this
77  end
78  
79  return BackgroundSize