@@ -411,6 +411,34 @@ static void emit_one_attr(buffer_t *out, GumboElement *el,
411411 }
412412}
413413
414+ static bool is_checkbox_list (GumboElement * el ) {
415+ const char * val = get_attr (el , "data-type" );
416+ if (val && (strcmp (val , "checkbox" ) == 0 || strcmp (val , "checkboxList" ) == 0 )) {
417+ return true;
418+ }
419+
420+ // In Google Docs and MS Word the <li> elements define if it is a checkbox
421+ // list. We only need to check the first <li>.
422+ GumboVector * children = & el -> children ;
423+ for (unsigned int i = 0 ; i < children -> length ; i ++ ) {
424+ GumboNode * child = children -> data [i ];
425+ if (is_element (child )) {
426+ char child_tag [64 ];
427+ if (get_tag_name (child , child_tag , sizeof (child_tag )) && strcmp (child_tag , "li" ) == 0 ) {
428+ GumboElement * child_el = & child -> v .element ;
429+ const char * role = get_attr (child_el , "role" );
430+ const char * cls = get_attr (child_el , "class" );
431+
432+ // Matches Google Docs (role="checkbox") OR MS Word (class includes "checklist")
433+ return (role && strcmp (role , "checkbox" ) == 0 ) ||
434+ (cls && strstr (cls , "checklist" ) != NULL );
435+ }
436+ }
437+ }
438+
439+ return false;
440+ }
441+
414442static void emit_attributes (GumboElement * el , const char * tag_name ,
415443 buffer_t * out ) {
416444 if (strcmp (tag_name , "a" ) == 0 ) {
@@ -421,33 +449,7 @@ static void emit_attributes(GumboElement *el, const char *tag_name,
421449 emit_one_attr (out , el , "width" );
422450 emit_one_attr (out , el , "height" );
423451 } else if (strcmp (tag_name , "ul" ) == 0 ) {
424-
425- const char * val = get_attr (el , "data-type" );
426- bool is_checkbox = (val && (strcmp (val , "checkbox" ) == 0 || strcmp (val , "checkboxList" ) == 0 ));
427-
428- // In Google Docs and MS Word the <li> elements define if it is a checkbox list
429- if (!is_checkbox ) {
430- GumboVector * children = & el -> children ;
431- for (unsigned int i = 0 ; i < children -> length ; i ++ ) {
432- GumboNode * child = children -> data [i ];
433- if (is_element (child )) {
434- char child_tag [64 ];
435- if (get_tag_name (child , child_tag , sizeof (child_tag )) && strcmp (child_tag , "li" ) == 0 ) {
436- GumboElement * child_el = & child -> v .element ;
437- const char * role = get_attr (child_el , "role" );
438- const char * cls = get_attr (child_el , "class" );
439-
440- if ((role && strcmp (role , "checkbox" ) == 0 ) ||
441- (cls && strstr (cls , "checklist" ) != NULL )) {
442- is_checkbox = true;
443- }
444- break ; // We only need to check the first <li>
445- }
446- }
447- }
448- }
449-
450- if (is_checkbox ) {
452+ if (is_checkbox_list (el )) {
451453 buffer_append_str (out , " data-type=\"checkbox\"" );
452454 }
453455 } else if (strcmp (tag_name , "li" ) == 0 ) {
0 commit comments