From 2977a1f7e9108e80ddc3885e826b21e9bb5d2034 Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Fri, 18 Oct 2024 15:29:37 +0100 Subject: [PATCH 1/5] Fix: style issue on WC item meta when using `dd` --- wcpdf-mpdf.php | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/wcpdf-mpdf.php b/wcpdf-mpdf.php index f944294..ea00f77 100644 --- a/wcpdf-mpdf.php +++ b/wcpdf-mpdf.php @@ -183,14 +183,54 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { foreach ( $crawler as $ul ) { $div = $ul->ownerDocument->createElement( 'div' ); $div->setAttribute( 'class', 'wc-item-meta' ); - - foreach ( $ul->getElementsByTagName( 'li' ) as $li ) { + + // Collect
  • elements into an array + $liElements = array(); + foreach ( $ul->childNodes as $li ) { + if ( $li->nodeType === XML_ELEMENT_NODE && 'li' === strtolower( $li->nodeName ) ) { + $liElements[] = $li; + } + } + + foreach ( $liElements as $li ) { $p = $ul->ownerDocument->createElement( 'p' ); - while ( $li->firstChild ) { - $p->appendChild( $li->firstChild ); + + // Collect child nodes of
  • into an array + $childNodes = array(); + foreach ( $li->childNodes as $child ) { + $childNodes[] = $child; + } + + foreach ( $childNodes as $child ) { + // Replace with + if ( $child->nodeType === XML_ELEMENT_NODE && 'strong' === strtolower( $child->nodeName ) ) { + $span = $ul->ownerDocument->createElement( 'span', $child->textContent ); + $span->setAttribute( 'class', 'label' ); + $p->appendChild( $span ); + } + // Append content of
    or

    to the

    element + elseif ( $child->nodeType === XML_ELEMENT_NODE && in_array( strtolower( $child->nodeName ), array( 'dd', 'p' ) ) ) { + foreach ( $child->childNodes as $grandchild ) { + $importedNode = $ul->ownerDocument->importNode( $grandchild, true ); + $p->appendChild( $importedNode ); + } + } + // Append text nodes (e.g., whitespace or text) + elseif ( $child->nodeType === XML_TEXT_NODE ) { + $p->appendChild( $ul->ownerDocument->createTextNode( $child->nodeValue ) ); + } + // Handle other nodes if necessary + else { + $importedNode = $ul->ownerDocument->importNode( $child, true ); + $p->appendChild( $importedNode ); + } } + + // Append the

    to the

    $div->appendChild( $p ); } + + // Replace the
      with the new
      $ul->parentNode->replaceChild( $div, $ul ); } } ); From 2912d68ba2e55fcee4df03b7faafed9f288eccac Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Fri, 18 Oct 2024 16:46:44 +0100 Subject: [PATCH 2/5] Update wcpdf-mpdf.php --- wcpdf-mpdf.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/wcpdf-mpdf.php b/wcpdf-mpdf.php index ea00f77..e91a642 100644 --- a/wcpdf-mpdf.php +++ b/wcpdf-mpdf.php @@ -196,12 +196,7 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { $p = $ul->ownerDocument->createElement( 'p' ); // Collect child nodes of
    • into an array - $childNodes = array(); foreach ( $li->childNodes as $child ) { - $childNodes[] = $child; - } - - foreach ( $childNodes as $child ) { // Replace with if ( $child->nodeType === XML_ELEMENT_NODE && 'strong' === strtolower( $child->nodeName ) ) { $span = $ul->ownerDocument->createElement( 'span', $child->textContent ); @@ -215,11 +210,7 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { $p->appendChild( $importedNode ); } } - // Append text nodes (e.g., whitespace or text) - elseif ( $child->nodeType === XML_TEXT_NODE ) { - $p->appendChild( $ul->ownerDocument->createTextNode( $child->nodeValue ) ); - } - // Handle other nodes if necessary + // Handle all other nodes (including text nodes) else { $importedNode = $ul->ownerDocument->importNode( $child, true ); $p->appendChild( $importedNode ); From 94f025598634b91133b8088b4ad0ddc89aa93dd3 Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Fri, 18 Oct 2024 16:48:38 +0100 Subject: [PATCH 3/5] Update wcpdf-mpdf.php --- wcpdf-mpdf.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wcpdf-mpdf.php b/wcpdf-mpdf.php index e91a642..0b6d683 100644 --- a/wcpdf-mpdf.php +++ b/wcpdf-mpdf.php @@ -194,8 +194,7 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { foreach ( $liElements as $li ) { $p = $ul->ownerDocument->createElement( 'p' ); - - // Collect child nodes of
    • into an array + foreach ( $li->childNodes as $child ) { // Replace with if ( $child->nodeType === XML_ELEMENT_NODE && 'strong' === strtolower( $child->nodeName ) ) { From 1e07719b5e964f15a59c171e6d28ceaef7da206a Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Fri, 18 Oct 2024 16:51:38 +0100 Subject: [PATCH 4/5] Update wcpdf-mpdf.php --- wcpdf-mpdf.php | 50 ++++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/wcpdf-mpdf.php b/wcpdf-mpdf.php index 0b6d683..29fd9f0 100644 --- a/wcpdf-mpdf.php +++ b/wcpdf-mpdf.php @@ -183,41 +183,35 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { foreach ( $crawler as $ul ) { $div = $ul->ownerDocument->createElement( 'div' ); $div->setAttribute( 'class', 'wc-item-meta' ); - - // Collect
    • elements into an array - $liElements = array(); + foreach ( $ul->childNodes as $li ) { if ( $li->nodeType === XML_ELEMENT_NODE && 'li' === strtolower( $li->nodeName ) ) { - $liElements[] = $li; - } - } - - foreach ( $liElements as $li ) { - $p = $ul->ownerDocument->createElement( 'p' ); + $p = $ul->ownerDocument->createElement( 'p' ); - foreach ( $li->childNodes as $child ) { - // Replace with - if ( $child->nodeType === XML_ELEMENT_NODE && 'strong' === strtolower( $child->nodeName ) ) { - $span = $ul->ownerDocument->createElement( 'span', $child->textContent ); - $span->setAttribute( 'class', 'label' ); - $p->appendChild( $span ); - } - // Append content of
      or

      to the

      element - elseif ( $child->nodeType === XML_ELEMENT_NODE && in_array( strtolower( $child->nodeName ), array( 'dd', 'p' ) ) ) { - foreach ( $child->childNodes as $grandchild ) { - $importedNode = $ul->ownerDocument->importNode( $grandchild, true ); + foreach ( $li->childNodes as $child ) { + // Replace with + if ( $child->nodeType === XML_ELEMENT_NODE && 'strong' === strtolower( $child->nodeName ) ) { + $span = $ul->ownerDocument->createElement( 'span', $child->textContent ); + $span->setAttribute( 'class', 'label' ); + $p->appendChild( $span ); + } + // Append content of

      or

      to the

      element + elseif ( $child->nodeType === XML_ELEMENT_NODE && in_array( strtolower( $child->nodeName ), array( 'dd', 'p' ) ) ) { + foreach ( $child->childNodes as $grandchild ) { + $importedNode = $ul->ownerDocument->importNode( $grandchild, true ); + $p->appendChild( $importedNode ); + } + } + // Handle all other nodes (including text nodes) + else { + $importedNode = $ul->ownerDocument->importNode( $child, true ); $p->appendChild( $importedNode ); } } - // Handle all other nodes (including text nodes) - else { - $importedNode = $ul->ownerDocument->importNode( $child, true ); - $p->appendChild( $importedNode ); - } + + // Append the

      to the

      + $div->appendChild( $p ); } - - // Append the

      to the

      - $div->appendChild( $p ); } // Replace the
        with the new
        From 007d3d7aa3816cd11eb47a857f9c1be17b54ff9a Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Mon, 4 Nov 2024 12:29:01 +0000 Subject: [PATCH 5/5] Update wcpdf-mpdf.php --- wcpdf-mpdf.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/wcpdf-mpdf.php b/wcpdf-mpdf.php index 29fd9f0..8b11f4d 100644 --- a/wcpdf-mpdf.php +++ b/wcpdf-mpdf.php @@ -187,7 +187,7 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { foreach ( $ul->childNodes as $li ) { if ( $li->nodeType === XML_ELEMENT_NODE && 'li' === strtolower( $li->nodeName ) ) { $p = $ul->ownerDocument->createElement( 'p' ); - + foreach ( $li->childNodes as $child ) { // Replace with if ( $child->nodeType === XML_ELEMENT_NODE && 'strong' === strtolower( $child->nodeName ) ) { @@ -195,25 +195,27 @@ function wpo_wcpdf_mpdf_modify_html( $html, $document ) { $span->setAttribute( 'class', 'label' ); $p->appendChild( $span ); } + // Append content of
        or

        to the

        element elseif ( $child->nodeType === XML_ELEMENT_NODE && in_array( strtolower( $child->nodeName ), array( 'dd', 'p' ) ) ) { foreach ( $child->childNodes as $grandchild ) { - $importedNode = $ul->ownerDocument->importNode( $grandchild, true ); - $p->appendChild( $importedNode ); + $clonedNode = $grandchild->cloneNode( true ); + $p->appendChild( $clonedNode ); } } + // Handle all other nodes (including text nodes) else { - $importedNode = $ul->ownerDocument->importNode( $child, true ); - $p->appendChild( $importedNode ); + $clonedNode = $child->cloneNode( true ); + $p->appendChild( $clonedNode ); } } - + // Append the

        to the

        $div->appendChild( $p ); } } - + // Replace the
          with the new
          $ul->parentNode->replaceChild( $div, $ul ); }