@@ -356,37 +356,33 @@ function decorateTemplateAndTheme() {
356
356
}
357
357
358
358
/**
359
- * Decorates paragraphs containing a single link as buttons.
359
+ * Decorates single links in paragraphs within a container element as buttons.
360
360
* @param {Element } element container element
361
361
*/
362
362
function decorateButtons ( element ) {
363
- element . querySelectorAll ( 'a' ) . forEach ( ( a ) => {
363
+ element . querySelectorAll ( 'a[href] ' ) . forEach ( ( a ) => {
364
364
a . title = a . title || a . textContent ;
365
365
if ( a . href !== a . textContent ) {
366
- const up = a . parentElement ;
367
- const twoup = a . parentElement . parentElement ;
368
- if ( ! a . querySelector ( 'img' ) ) {
369
- if ( up . childNodes . length === 1 && ( up . tagName === 'P' || up . tagName === 'DIV' ) ) {
370
- a . className = 'button' ; // default
371
- up . classList . add ( 'button-container' ) ;
372
- }
373
- if (
374
- up . childNodes . length === 1
375
- && up . tagName === 'STRONG'
376
- && twoup . childNodes . length === 1
377
- && twoup . tagName === 'P'
378
- ) {
379
- a . className = 'button primary' ;
380
- twoup . classList . add ( 'button-container' ) ;
366
+ const linkedImg = a . querySelector ( 'img' ) ;
367
+ if ( ! linkedImg ) {
368
+ let wrapper = a . closest ( 'p' ) ;
369
+ if ( ! wrapper ) {
370
+ const inList = a . closest ( 'li' ) ;
371
+ const inBlock = a . closest ( '[class]' ) ;
372
+ if ( ! inList && inBlock ) {
373
+ // wrap unwrapped block links in paragraph tag
374
+ const cell = a . closest ( 'div' ) ;
375
+ const p = document . createElement ( 'p' ) ;
376
+ while ( cell . firstChild ) p . append ( cell . firstChild ) ;
377
+ cell . replaceChildren ( p ) ;
378
+ wrapper = p ;
379
+ }
381
380
}
382
- if (
383
- up . childNodes . length === 1
384
- && up . tagName === 'EM'
385
- && twoup . childNodes . length === 1
386
- && twoup . tagName === 'P'
387
- ) {
388
- a . className = 'button secondary' ;
389
- twoup . classList . add ( 'button-container' ) ;
381
+ if ( wrapper && wrapper . childNodes . length === 1 ) {
382
+ wrapper . className = 'button-container' ;
383
+ a . className = 'button' ;
384
+ if ( wrapper . querySelector ( 'strong' ) ) a . classList . add ( 'primary' ) ;
385
+ if ( wrapper . querySelector ( 'em' ) ) a . classList . add ( 'secondary' ) ;
390
386
}
391
387
}
392
388
}
0 commit comments