@@ -216,6 +216,7 @@ private function render_part(DOMNode $part, question_attempt $qa, ?question_disp
216
216
$ this ->hide_unwanted_feedback ($ xpath , $ options );
217
217
$ this ->set_input_values_and_readonly ($ xpath , $ qa , $ options );
218
218
$ this ->shuffle_contents ($ xpath );
219
+ $ this ->add_bootstrap_classes ($ xpath );
219
220
$ this ->mangle_ids_and_names ($ xpath , $ qa );
220
221
$ this ->clean_up ($ xpath );
221
222
$ this ->resolve_placeholders ($ xpath );
@@ -449,4 +450,37 @@ private function resolve_placeholders(DOMXPath $xpath): void {
449
450
}
450
451
}
451
452
}
453
+
454
+ private function add_bootstrap_classes (DOMXPath $ xpath ): void {
455
+ /** @var DOMElement $element */
456
+ foreach ($ xpath ->query ("
457
+ //xhtml:input[@type != 'checkbox' and @type != 'radio' and @type != 'button' and @type != 'submit' and @type != 'reset']
458
+ | //xhtml:select | //xhtml:textarea
459
+ " ) as $ element ) {
460
+ $ this ->add_class_names ($ element , "form-control " , "qpy-input " );
461
+ }
462
+
463
+ foreach ($ xpath ->query ("//xhtml:input[@type = 'button' or @type = 'submit' or @type = 'reset'] | //xhtml:button " ) as $ element ) {
464
+ $ this ->add_class_names ($ element , "btn " , "btn-primary " , "qpy-input " );
465
+ }
466
+
467
+ foreach ($ xpath ->query ("//xhtml:input[@type = 'checkbox' or @type = 'radio'] " ) as $ element ) {
468
+ $ this ->add_class_names ($ element , "qpy-input " );
469
+ }
470
+ }
471
+
472
+ private function add_class_names (DOMElement $ element , string ...$ newclasses ): void {
473
+ $ classarray = [];
474
+ for ($ class = strtok ($ element ->getAttribute ("class " ), " \t\n" ); $ class ; $ class = strtok (" \t\n" )) {
475
+ $ classarray [] = $ class ;
476
+ }
477
+
478
+ foreach ($ newclasses as $ newclass ) {
479
+ if (!in_array ($ newclass , $ classarray )) {
480
+ $ classarray [] = $ newclass ;
481
+ }
482
+ }
483
+
484
+ $ element ->setAttribute ("class " , implode (" " , $ classarray ));
485
+ }
452
486
}
0 commit comments