-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add style blocks to body rather than head v2 #396
Conversation
Issue356
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some review comments.
CHANGELOG.md
Outdated
@@ -9,7 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). | |||
### Added | |||
- Debug mode. Throw debug exceptions only if debug is active. | |||
([#392](https://github.com/jjriv/emogrifier/pull/392)) | |||
|
|||
- Added setting for putting the media query style block to <body> vs <head> | |||
([#396](https://github.com/jjriv/emogrifier/pull/396)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entries should be ordered in reverse chronological order, i.e., newest on top. Could you please move this entry to the top of the "Added" section?
@@ -1037,8 +1037,10 @@ protected function addStyleElementToDocument(\DOMDocument $document, $css) | |||
$styleAttribute->value = 'text/css'; | |||
$styleElement->appendChild($styleAttribute); | |||
|
|||
$head = $this->getOrCreateHeadElement($document); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method getOrCreateHeadElement then is not needed anymore and can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is removed
Classes/Emogrifier.php
Outdated
$body = $this->getBodyElement($document); | ||
if ($body) { | ||
$body->appendChild($styleElement); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that the style tag will not be added if there is no body element, while the body element actually is optional in HTML. So we have several options (orderd by how much I like them):
- If there is no BODY element present, we create one and move all children of the HTML into it (except for the HEAD element if it is present).
- If there is no BODY element present, we attach the style element directly to the HTML element.
- If there is no body element, we throw an exception.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This behavior also needs to be covered by unit tests.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only way there would not be a body tag is if there is no content in document. In all other cases DOMDocument will add the body tag automatically. But document without content is useless anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure?
As far as I know, the HEAD and BODY elements are optional, i.e., the child tags (TITLE etc. for HEAD, H1 etc. for BODY) can be direct children of the HTML element. Does DOMDocument then automatically sort the existing tags correctly in either HEAD or BODY, or will e.g., TITLE also go into BODY?
Classes/Emogrifier.php
Outdated
* | ||
* @param \DOMDocument $document | ||
* | ||
* @return \DOMNode the body element |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be \DOMNode|null because the return value will be null if there is no BODY element.
Classes/Emogrifier.php
Outdated
$head = $this->getOrCreateHeadElement($document); | ||
$head->appendChild($styleElement); | ||
$body = $this->getBodyElement($document); | ||
if ($body) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$body isn't a bool, but either a \DOMNode instance or null. So the check needs to be !== null
.
Tests/Unit/EmogrifierTest.php
Outdated
@@ -1111,7 +1111,7 @@ public function emogrifyKeepExistingHeadElementContent() | |||
*/ | |||
public function emogrifyKeepExistingHeadElementAddStyleElement() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this tests does not make sense anymore with the new behavior.
Tests/Unit/EmogrifierTest.php
Outdated
@@ -1121,6 +1121,23 @@ public function emogrifyKeepExistingHeadElementAddStyleElement() | |||
} | |||
|
|||
/** | |||
* @test | |||
*/ | |||
public function emogrifyMoveStyleElementFromHeadToBody() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Third person, please: Move -> Moves
Tests/Unit/EmogrifierTest.php
Outdated
$result = $this->subject->emogrify(); | ||
|
||
self::assertContains( | ||
'<body><style type="text/css">@media all { html { color: red; } }</style></body>', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let's move the STYLE node into a local variabel to make this shorter and also communicate the intent more clearly.
@Renkas Any news on this PR? (Please don't feel discouraged by the review comments. I aim to get your changes into the code base in their best form.) |
CHANGELOG.md
Outdated
@@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). | |||
## x.y.z (unreleased) | |||
|
|||
### Added | |||
- Added setting for putting the media query style block to <body> vs <head> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose we simplify/shorten this to: "Automatically the STYLE block into the BODY". What do you think=
We've moved the repository to https://github.com/MyIntervals/emogrifier . If you have an "upstream" branch in your fork, please make sure to update the URL:
|
So what is the status? |
I seems that you have re-pushed. Unfortunately. GitHub does not send any notifications of this. :-( Thanks for the reminder! 🍪 I'll have another look at the PR. |
This is a prerequisite to #396.
This is a prerequisite to #396.
I've just created #410 for making sure we always have a BODY element. |
This is a prerequisite to #396.
Merged. Thanks for contributing, @Renkas and @Gabe-SpotMe! ❤️ |
This is modification of existing pull request: #359
I removed the option to move style element to body and made it default/only way.
Added unit test and modified some existing ones.