Skip to content
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

Merged
merged 20 commits into from
Oct 16, 2017

Conversation

Renkas
Copy link
Contributor

@Renkas Renkas commented Jul 29, 2017

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.

@oliverklee oliverklee self-requested a review July 29, 2017 13:41
@oliverklee oliverklee added this to the 1.3.0 milestone Jul 29, 2017
Copy link
Contributor

@oliverklee oliverklee left a 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))
Copy link
Contributor

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);
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is removed

$body = $this->getBodyElement($document);
if ($body) {
$body->appendChild($styleElement);
}
Copy link
Contributor

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):

  1. 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).
  2. If there is no BODY element present, we attach the style element directly to the HTML element.
  3. If there is no body element, we throw an exception.

What do you think?

Copy link
Contributor

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.)

Copy link
Contributor Author

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.

Copy link
Contributor

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?

*
* @param \DOMDocument $document
*
* @return \DOMNode the body element
Copy link
Contributor

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.

$head = $this->getOrCreateHeadElement($document);
$head->appendChild($styleElement);
$body = $this->getBodyElement($document);
if ($body) {
Copy link
Contributor

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.

@@ -1111,7 +1111,7 @@ public function emogrifyKeepExistingHeadElementContent()
*/
public function emogrifyKeepExistingHeadElementAddStyleElement()
Copy link
Contributor

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.

@@ -1121,6 +1121,23 @@ public function emogrifyKeepExistingHeadElementAddStyleElement()
}

/**
* @test
*/
public function emogrifyMoveStyleElementFromHeadToBody()
Copy link
Contributor

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

$result = $this->subject->emogrify();

self::assertContains(
'<body><style type="text/css">@media all { html { color: red; } }</style></body>',
Copy link
Contributor

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.

@oliverklee
Copy link
Contributor

@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>
Copy link
Contributor

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=

@oliverklee
Copy link
Contributor

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:

git remote set-url upstream git@github.com:MyIntervals/emogrifier.git

@Renkas
Copy link
Contributor Author

Renkas commented Sep 20, 2017

So what is the status?

@oliverklee
Copy link
Contributor

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.

oliverklee added a commit that referenced this pull request Oct 13, 2017
oliverklee added a commit that referenced this pull request Oct 13, 2017
@oliverklee
Copy link
Contributor

I've just created #410 for making sure we always have a BODY element.

jjriv pushed a commit that referenced this pull request Oct 13, 2017
@oliverklee
Copy link
Contributor

@Renkas Now that #410 is merged (which makes sure we always have a BODY element), could you please rebase and make use of the BODY element always being there? Thanks!

@oliverklee oliverklee merged commit 351efe2 into MyIntervals:master Oct 16, 2017
@oliverklee
Copy link
Contributor

Merged. Thanks for contributing, @Renkas and @Gabe-SpotMe! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants