Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,23 @@ private function inject_form_after_element( $content, $tag, $index, $form ) {
return $content . $form;
}

// Create new element for the Form.
$form_node = new DOMDocument();
$form_node->loadHTML( $form, LIBXML_HTML_NODEFDTD );
// Load the form into the parser.
$form_parser = new ConvertKit_HTML_Parser( $form, LIBXML_HTML_NODEFDTD );
$form_body = $form_parser->html->getElementsByTagName( 'body' )->item( 0 );

// Collect nodes first to avoid live NodeList mutation issues.
$nodes_to_insert = array();
foreach ( $form_body->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$nodes_to_insert[] = $parser->html->importNode( $child, true );
}

// Append the form to the specific element.
$element_node->parentNode->insertBefore( $parser->html->importNode( $form_node->documentElement, true ), $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
// Inject the form node(s) after the element node e.g. after the paragraph, heading etc.
$next_sibling = $element_node->nextSibling; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach ( $nodes_to_insert as $node ) {
$element_node->parentNode->insertBefore( $node, $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
}

// Fetch HTML string.
// Return modified HTML string.
return $parser->get_body_html();

}
Expand Down
1 change: 0 additions & 1 deletion tests/EndToEnd.suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ modules:
capabilities:
"goog:chromeOptions":
args:
- "--headless"
- "--disable-gpu"
- "--disable-dev-shm-usage"
- "--disable-software-rasterizer"
Expand Down
15 changes: 15 additions & 0 deletions tests/EndToEnd/forms/post-types/CPTFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ public function testAddNewCPTUsingDefaultFormAfterParagraphElement(EndToEndTeste

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -403,6 +406,9 @@ public function testAddNewCPTUsingDefaultNonInlineFormAfterParagraphElement(EndT

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -455,6 +461,9 @@ public function testAddNewCPTUsingDefaultFormAfterHeadingElement(EndToEndTester

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -507,6 +516,9 @@ public function testAddNewCPTUsingDefaultFormAfterImageElement(EndToEndTester $I

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -558,6 +570,9 @@ public function testAddNewCPTUsingDefaultFormAfterOutOfBoundsElement(EndToEndTes

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down
16 changes: 14 additions & 2 deletions tests/EndToEnd/forms/post-types/PageFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ public function testAddNewPageUsingDefaultFormAfterParagraphElement(EndToEndTest

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -324,6 +327,9 @@ public function testAddNewPageUsingDefaultNonInlineFormAfterParagraphElement(End

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -375,6 +381,9 @@ public function testAddNewPageUsingDefaultFormAfterHeadingElement(EndToEndTester

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -427,8 +436,8 @@ public function testAddNewPageUsingDefaultFormAfterImageElement(EndToEndTester $
// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');
// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -479,6 +488,9 @@ public function testAddNewPageUsingDefaultFormAfterOutOfBoundsElement(EndToEndTe

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/EndToEnd/forms/post-types/PostFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ public function testAddNewPostUsingDefaultFormAfterParagraphElement(EndToEndTest

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -329,6 +332,9 @@ public function testAddNewPostUsingDefaultNonInlineFormAfterParagraphElement(End

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -381,6 +387,9 @@ public function testAddNewPostUsingDefaultFormAfterHeadingElement(EndToEndTester

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -433,6 +442,9 @@ public function testAddNewPostUsingDefaultFormAfterImageElement(EndToEndTester $

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down Expand Up @@ -484,6 +496,9 @@ public function testAddNewPostUsingDefaultFormAfterOutOfBoundsElement(EndToEndTe

// Confirm no meta tag exists within the content.
$I->dontSeeInSource('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');

// Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
$I->seeNoExtraHtmlHeadBodyTagsOutput($I);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/Helper/KitForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,18 @@ public function seeLandingPageOutput($I, $langTag = false)
$I->seeInSource('</body>');
$I->seeInSource('</html>');
}

/**
* Confirm no extra <html>, <head> or <body> tags are output i.e. injecting the form doesn't result in DOMDocument adding tags.
*
* @since 3.2.1
*
* @param EndToEndTester $I Tester.
*/
public function seeNoExtraHtmlHeadBodyTagsOutput($I)
{
$I->seeNumberOfElementsInDOM('html', 1);
$I->seeNumberOfElementsInDOM('head', 1);
$I->seeNumberOfElementsInDOM('body', 1);
}
}