Wrong $page->field()->kirbytext() output

Hello,

I have the problem that kirbytext () automatically closes a .

blueprint
gallery:
label: Galerie
type: structure
style: table
fields:
gallery_img:
label: Bild
type: image
caption:
label: Bild-Untertitel
type: simplemde

panel input
test (email:text@text.ch text: test) test

raw output
caption: ‘test (email:text@text.ch text: test) test’

template

   foreach($image_collection as $single_image): ?>

    <?php 
    $counter++;
    ?>

    <?php
    $image = $single_image->gallery_img()->toFile();
       
    $thumbURL = $image? $image->resize(2000)->url(): '';
    $thumbURL_mobile = $image? $image->resize(800)->url(): '';

    ?>
      <li class="col-dont-break-inside" id="<?php echo $gallery_name.$counter ?>" >
                              
          <a class="gallery-image flex-column justify-content-spacebetween" href="<?php  echo '#'.$gallery_name.$counter ?>">
           
            
            <span class="caption"> 
            
              <?php
              
                echo $single_image->caption()->kirbytext();
            
              ?>

            </span>
            <div>
              <img src=<?php echo $thumbURL ?>>
            </div>

          </a>

html output

<li class="col-dont-break-inside" id="gallery-1-3">
                              
          <a class="gallery-image flex-column justify-content-spacebetween" href="#gallery-1-3">

            <span class="caption"> 
            <p>
              </p></span></a><p><a class="gallery-image flex-column justify-content-spacebetween" href="#gallery-1-3">test </a><a href="mailto:">test</a> test</p>              <p></p>
            
            <div>

The problem is in your markup, because you use an mailto anker link within an anker element.

Also, but not related to your issue, you shouldn’t use kirbytext within a span element, because using a block element inside an inline element like span is not valid HTML.

Thank you @texnixe for answering , I changed my template.
But what I not understand is why kirbytext() close the a tag (before I changed the template) .

<!--GALLERY-->

  <?php
  $counter = 0;//counter for image id;
  $counter1 = 0;//counter for gallery name
  $counter1++;//counter to jump to next img
  $gallery_name = $gallery_name_unique."-";
  $image_arr = $collection;
  $structur_field = $structur_field_name;

  $image_collection = $collection->{$structur_field}()->toStructure();
  $structurfield_count = $collection->{$structur_field}()->toStructure()->count();


    foreach($image_collection as $single_image): ?>

    <?php 
    $counter++;
    ?>

    <?php
    $image = $single_image->gallery_img()->toFile();
       
    $thumbURL = $image? $image->resize(2000)->url(): '';
    $thumbURL_mobile = $image? $image->resize(800)->url(): '';

    ?>
      <li class="col-dont-break-inside" id="<?php echo $gallery_name.$counter ?>" >

          <?php if($single_image->caption() != ""): ?>
            <div class="caption"> 
              <?php  
                echo $single_image->caption()->kirbytext();
              ?>
            </div>  
          <?php endif ?>          
          <a class="gallery-image flex-column justify-content-spacebetween" href="<?php  echo '#'.$gallery_name.$counter ?>">
           
             
           
            <div>
              <img src=<?php echo $thumbURL ?>>
            </div>

          </a>
          <img class="only-mobile" src=<?php echo $thumbURL_mobile ?>>
          <!-- Gallery-Close-Button -->
          <a title="close" href="#close" class="background-lightbox"></a>
          <a class="close" href="#close">close</a>
         <!-- Gallery-Navigation- -->
          <a style="margin-top:<?php echo $counter*25?>px;" class="gallery-nav-button" href="#<?php echo $gallery_name.$counter ?>">
          </a>


          <?php  if($counter ==  $structurfield_count ) { ?>
          <a class="gallery-nav-button-center" href="#<?php echo $gallery_name."1" ?>"></a>
          <?php } else { $counter1++;?>

          <a class="gallery-nav-button-center" href="#<?php echo $gallery_name.$counter1; ?>"></a>

            <?php } ?>
      </li>
    <?php endforeach ?>

You should get the same unwanted result if you just manually add some anker element without any kirbytext. The browser closes the first open a tag when it encounters a new a tag.