@@ -8,7 +8,6 @@ def __init__(self, fileName):
88 'input' : 'input' ,
99 'label' : 'label' ,
1010 'textarea' : 'textarea' ,
11- 'select' : 'select' ,
1211 'button' : 'button' ,
1312 'checkbox' : 'checkbox' ,
1413 'radio' : 'radio' ,
@@ -51,9 +50,6 @@ def get_table_styles(self):
5150 'td' : 'td'
5251 }
5352
54- def get_list_styles (self ):
55- return {} # no list styles
56-
5753 def add_form_classes (self , tag_dict : dict ):
5854 """
5955 The only difference between this and parent implementation is the addition of adding
@@ -62,33 +58,70 @@ def add_form_classes(self, tag_dict: dict):
6258 :return:
6359 """
6460 for form in self .soup .find_all ('form' ):
65- spotted_label = None
61+ label , input_ = None , None
6662 for elem in form .children :
6763 if elem .name == 'label' :
68- spotted_label = elem
64+ label = elem
6965 if 'label' in tag_dict :
7066 self .add_class_to_element (elem , tag_dict ['label' ])
67+
68+ # See if there is a input within the label (sometimes done for radio/cb)
69+ input_within_label = elem .find_all ('input' , recursive = False )
70+ if input_within_label :
71+ input_type = input_within_label .get ('type' )
72+ if input_type == 'checkbox' :
73+ self .add_class_to_element (label , 'checkbox' )
74+ elif input_type == 'radio' :
75+ self .add_class_to_element (label , 'radio' )
76+ label = None
77+ input = None
78+
7179 elif elem .name == 'input' :
72- self .add_class_to_element (elem , tag_dict ['input' ])
73- if elem .get ('type' ) == 'radio' :
74- if 'radio' in tag_dict :
75- self .add_class_to_element (elem , tag_dict ['radio' ])
76- elif elem .get ('type' ) == 'checkbox' :
77- if 'checkbox' in tag_dict :
78- self .add_class_to_element (elem , tag_dict ['checkbox' ])
80+ if input_ :
81+ raise Exception ("Found input without adjacent label" )
82+ else :
83+ input_ = elem
84+
85+ if elem .get ('type' ) != 'radio' and elem .get ('type' ) != 'checkbox' :
86+ # Radio/CB don't take a class but their label does
87+ self .add_class_to_element (elem , tag_dict ['input' ])
88+
89+ elif elem .name == 'select' :
90+ # Add select div
91+ select_div = self .soup .new_tag ('div' , ** {'class' : 'select' })
92+ elem .wrap (select_div )
93+
94+ # Add control div
95+ control_div = self .soup .new_tag ('div' , ** {'class' : 'control' })
96+ select_div .wrap (control_div )
7997
8098 # Add overall wrapper
81- if not spotted_label :
82- # add wrapper
83- field_div = self .soup .new_tag ('div' , ** {'class' : 'field' })
84- elem .wrap (field_div )
99+ field_div = self .soup .new_tag ('div' , ** {'class' : 'field' })
100+ control_div .wrap (field_div )
101+
102+ if input_ and label :
103+ if input_ .get ('type' ) == 'radio' or input_ .get ('type' ) == 'checkbox' :
104+ if input_ .get ('type' ) == 'radio' :
105+ self .add_class_to_element (label , 'radio' )
106+ else :
107+ self .add_class_to_element (label , 'checkbox' )
108+
109+ # Add input then label
110+ # todo wrap all consecutive inputs with wrapper
111+ # control_div = self.soup.new_tag('div', **{'class': 'control'})
112+ # label.wrap(control_div)
113+ label .insert (0 , input_ )
85114 else :
86- # the input has a preceding label
115+ # Add overall wrapper
87116 field_div = self .soup .new_tag ('div' , ** {'class' : 'field' })
88- spotted_label .wrap (field_div )
89- field_div .append (elem )
90- spotted_label = None
117+ label .wrap (field_div )
118+ field_div .append (input_ )
91119
92- # Add input wrapper
93- control_div = self .soup .new_tag ('div' , ** {'class' : 'control' })
94- elem .wrap (control_div )
120+ # Add input wrapper
121+ control_div = self .soup .new_tag ('div' , ** {'class' : 'control' })
122+ input_ .wrap (control_div )
123+
124+ label , input_ = None , None
125+
126+ def get_list_styles (self ):
127+ return {} # no list styles
0 commit comments