Tailwind Prefixer

3 years ago

thijsvdanker

Prefixes Tailwind (UI) component classes

Code

error_reporting(0);
$prefix = 'tw-';

$dom = new DomDocument();

$html = <<< 'HERE'
  <div class="fixed bottom-0 inset-x-0 px-4 pb-4 sm:inset-0 sm:flex sm:items-center sm:justify-center">
  <!--
    Background overlay, show/hide based on modal state.

    Entering: "ease-out duration-300"
      From: "opacity-0"
      To: "opacity-100"
    Leaving: "ease-in duration-200"
      From: "opacity-100"
      To: "opacity-0"
  -->
  <div class="fixed inset-0 transition-opacity">
    <div class="absolute inset-0 bg-gray-500 opacity-75"></div>
  </div>

  <!--
    Modal panel, show/hide based on modal state.

    Entering: "ease-out duration-300"
      From: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
      To: "opacity-100 translate-y-0 sm:scale-100"
    Leaving: "ease-in duration-200"
      From: "opacity-100 translate-y-0 sm:scale-100"
      To: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  -->
  <div class="bg-white rounded-lg overflow-hidden shadow-xl transform transition-all sm:max-w-lg sm:w-full" role="dialog" aria-modal="true" aria-labelledby="modal-headline">
    <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
      <div class="sm:flex sm:items-start">
        <div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10">
          <svg class="h-6 w-6 text-red-600" stroke="currentColor" fill="none" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
          </svg>
        </div>
        <div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
          <h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-headline">
            Deactivate account
          </h3>
          <div class="mt-2">
            <p class="text-sm leading-5 text-gray-500">
              Are you sure you want to deactivate your account? All of your data will be permanently removed. This action cannot be undone.
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
      <span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
        <button type="button" class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-red-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-red-500 focus:outline-none focus:border-red-700 focus:shadow-outline-red transition ease-in-out duration-150 sm:text-sm sm:leading-5">
          Deactivate
        </button>
      </span>
      <span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto">
        <button type="button" class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5">
          Cancel
        </button>
      </span>
    </div>
  </div>
</div>
HERE;


$dom->loadHTML($html);
$xpath = new DomXpath($dom);

$res = $xpath->query('//@class');
foreach($res as $attr)
{
    $value = explode(' ', $attr->value);
    foreach($value as &$set)
    {
        $set = $prefix.trim($set);
    }
    unset($set);
    $attr->value = implode(' ', $value);
}

$result = $dom->saveHTML();

Output

PHP Warning:  DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 30 in phar:///Applications/Tinkerwell.app/Contents/Resources/tinkerwell/tinker.phar/index.php(108) : eval()'d code on line 65
PHP Warning:  DOMDocument::loadHTML(): Tag path invalid in Entity, line: 31 in phar:///Applications/Tinkerwell.app/Contents/Resources/tinkerwell/tinker.phar/index.php(108) : eval()'d code on line 65
=> """
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">\n
   <html><body><div class="tw-fixed tw-bottom-0 tw-inset-x-0 tw-px-4 tw-pb-4 tw-sm:inset-0 tw-sm:flex tw-sm:items-center tw-sm:justify-center">\n
     <!--\n
       Background overlay, show/hide based on modal state.\n
   \n
       Entering: "ease-out duration-300"\n
         From: "opacity-0"\n
         To: "opacity-100"\n
       Leaving: "ease-in duration-200"\n
         From: "opacity-100"\n
         To: "opacity-0"\n
     -->\n
     <div class="tw-fixed tw-inset-0 tw-transition-opacity">\n
       <div class="tw-absolute tw-inset-0 tw-bg-gray-500 tw-opacity-75"></div>\n
     </div>\n
   \n
     <!--\n
       Modal panel, show/hide based on modal state.\n
   \n
       Entering: "ease-out duration-300"\n
         From: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"\n
         To: "opacity-100 translate-y-0 sm:scale-100"\n
       Leaving: "ease-in duration-200"\n
         From: "opacity-100 translate-y-0 sm:scale-100"\n
         To: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"\n
     -->\n
     <div class="tw-bg-white tw-rounded-lg tw-overflow-hidden tw-shadow-xl tw-transform tw-transition-all tw-sm:max-w-lg tw-sm:w-full" role="dialog" aria-modal="true" aria-labelledby="modal-headline">\n
       <div class="tw-bg-white tw-px-4 tw-pt-5 tw-pb-4 tw-sm:p-6 tw-sm:pb-4">\n
         <div class="tw-sm:flex tw-sm:items-start">\n
           <div class="tw-mx-auto tw-flex-shrink-0 tw-flex tw-items-center tw-justify-center tw-h-12 tw-w-12 tw-rounded-full tw-bg-red-100 tw-sm:mx-0 tw-sm:h-10 tw-sm:w-10">\n
             <svg class="tw-h-6 tw-w-6 tw-text-red-600" stroke="currentColor" fill="none" viewbox="0 0 24 24">\n
               <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>\n
             </svg>\n
           </div>\n
           <div class="tw-mt-3 tw-text-center tw-sm:mt-0 tw-sm:ml-4 tw-sm:text-left">\n
             <h3 class="tw-text-lg tw-leading-6 tw-font-medium tw-text-gray-900" id="modal-headline">\n
               Deactivate account\n
             </h3>\n
             <div class="tw-mt-2">\n
               <p class="tw-text-sm tw-leading-5 tw-text-gray-500">\n
                 Are you sure you want to deactivate your account? All of your data will be permanently removed. This action cannot be undone.\n
               </p>\n
             </div>\n
           </div>\n
         </div>\n
       </div>\n
       <div class="tw-bg-gray-50 tw-px-4 tw-py-3 tw-sm:px-6 tw-sm:flex tw-sm:flex-row-reverse">\n
         <span class="tw-flex tw-w-full tw-rounded-md tw-shadow-sm tw-sm:ml-3 tw-sm:w-auto">\n
           <button type="button" class="tw-inline-flex tw-justify-center tw-w-full tw-rounded-md tw-border tw-border-transparent tw-px-4 tw-py-2 tw-bg-red-600 tw-text-base tw-leading-6 tw-font-medium tw-text-white tw-shadow-sm tw-hover:bg-red-500 tw-focus:outline-none tw-focus:border-red-700 tw-focus:shadow-outline-red tw-transition tw-ease-in-out tw-duration-150 tw-sm:text-sm tw-sm:leading-5">\n
             Deactivate\n
           </button>\n
         </span>\n
         <span class="tw-mt-3 tw-flex tw-w-full tw-rounded-md tw-shadow-sm tw-sm:mt-0 tw-sm:w-auto">\n
           <button type="button" class="tw-inline-flex tw-justify-center tw-w-full tw-rounded-md tw-border tw-border-gray-300 tw-px-4 tw-py-2 tw-bg-white tw-text-base tw-leading-6 tw-font-medium tw-text-gray-700 tw-shadow-sm tw-hover:text-gray-500 tw-focus:outline-none tw-focus:border-blue-300 tw-focus:shadow-outline-blue tw-transition tw-ease-in-out tw-duration-150 tw-sm:text-sm tw-sm:leading-5">\n
             Cancel\n
           </button>\n
         </span>\n
       </div>\n
     </div>\n
   </div></body></html>\n
   """
New course: Desktop apps with Electron