@@ -604,7 +604,7 @@ function wikify_link($node) {
604604 function add_captions_to_all_images ($ element ) {
605605 /** @var \DOMWrap\NodeList $img */
606606 // process images that reside within "main"
607- foreach ($ element ->findXPath ('//*[@id="main"]//img[@title] | //*[@id="main"]//img[ @data-attribution] ' ) as $ img ) {
607+ foreach ($ element ->findXPath ('//*[@id="main"]//img[@title or @data-attribution] ' ) as $ img ) {
608608 // do not process images that reside within the wymeditor
609609 if ($ img ->parents ('.wymeditor ' )->count () === 0 ) {
610610 add_caption_to_image ($ img );
@@ -613,19 +613,24 @@ function add_captions_to_all_images($element) {
613613 }
614614
615615 function add_caption_to_image ($ img ) {
616- $ figure = new DOMWrap \Element ('figure ' );
617- $ img ->parentNode ->insertBefore ($ figure , $ img );
618- $ figure ->append ($ img );
616+ $ doc = $ img ->document ();
617+
618+ $ img ->wrap ('<figure> ' );
619+ $ caption = $ doc ->createElement ('figcaption ' );
620+ $ img ->after ($ caption );
619621
620622 $ title = $ img ->getAttribute ('title ' );
621- $ text = $ title ? $ title : '' ;
622- $ caption = new DOMWrap \Element ('figcaption ' , $ text );
623- $ figure ->append ($ caption );
623+ if ($ title ) {
624+ $ span = $ doc ->createElement ('span ' , $ title );
625+ $ span ->addClass ('title ' );
626+ $ caption ->append ($ span );
627+ }
624628
625629 $ attribution = $ img ->getAttribute ('data-attribution ' );
626630 if ($ attribution ) {
627- $ small = new DOMWrap \Element ('small ' , $ attribution );
628- $ figure ->append ($ small );
631+ $ small = $ doc ->createElement ('small ' , $ attribution );
632+ $ small ->addClass ('attribution ' );
633+ $ caption ->append ($ small );
629634 }
630635 }
631636
0 commit comments