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

HTML5 Tools + Drupal 7 + HTML5 PHP breaks </address> tag #109

Closed
sylus opened this issue May 2, 2016 · 8 comments
Closed

HTML5 Tools + Drupal 7 + HTML5 PHP breaks </address> tag #109

sylus opened this issue May 2, 2016 · 8 comments
Labels

Comments

@sylus
Copy link
Contributor

sylus commented May 2, 2016

It seems when using HTML5 Tools + HTML5 PHP for Drupal 7 that address tags are broken.

We originally added the HTML5 PHP library to fix issues with video tags which worked. Except now it has been proven when saving a piece of content with the opening + closing address tags that the closing tags get removed on node render. CKEditor source view shows the close address tag as still present. On node view and inspect page source the closing address tag is removed.

Was wondering if had any ideas about this? ^_^

@mattfarina
Copy link
Member

@sylus Can you point me to the code using Masterminds\HTML5 that's causing this problem? Is it in a module?

If you look in Drupal\Component\Utility\Html you'll see it uses \DOMDocument instead of the html5 parser here. For example, see the load function.

I'm curious where this is happening.

@sylus
Copy link
Contributor Author

sylus commented May 5, 2016

Hey thanks for the response! :)

We are using a patch to get html5_tools to work with html5-php from this issue:

http://drupal.org/node/2415305
https://www.drupal.org/files/issues/replace_core_text-2415305-7.patch

When looking at the markup for the body field in devel I see the following:

value (String, 40 characters ) <address>Testing address tag</address>
...
safe_value (String, 39 characters ) <address>Testing address tag

So it is only the safe_value which has the issue and gets called by the following:

function html5_tools_filter_info() {
...
  $filters['filter_html5corrector'] = array(
    'title' =>  t('Correct faulty and chopped off HTML5'),
    'process callback' => '_html5_tools_filter_htmlcorrector',
    'weight' => 10,
  );
...
}
/**
 * Implements callback_filter_process().
 *
 * Scans the input and makes sure that HTML tags are properly closed.
 */
function _html5_tools_filter_htmlcorrector($text) {
  return html5_tools_filter_dom_serialize(html5_tools_filter_dom_load($text));
}
function html5_tools_filter_dom_serialize($dom_document) {
  $body_node = $dom_document->getElementsByTagName('body')->item(0);
  $body_content = '';
  foreach ($body_node->getElementsByTagName('script') as $node) {
    filter_dom_serialize_escape_cdata_element($dom_document, $node);
  }
  foreach ($body_node->getElementsByTagName('style') as $node) {
    filter_dom_serialize_escape_cdata_element($dom_document, $node, '/*', '*/');
  }
  $html5 = html5_tools_get_html5_instance();
  foreach ($body_node->childNodes as $child_node) {
    $body_content .= $html5->saveHTML($child_node);
  }
  return preg_replace('|<([^> ]*)/>|i', '<$1 />', $body_content);
}
function html5_tools_filter_dom_load($text) {
  $html5 = html5_tools_get_html5_instance();

  $text = <<<EOL
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>$text</body>
</html>
EOL;

  $dom_document = $html5->loadHTML($text);
  return $dom_document;
}

If i don't use that filter then the issues goes away. Hope this helps!

@sylus
Copy link
Contributor Author

sylus commented May 9, 2016

I believe I have reduced it down to this:

  $html5 = html5_tools_get_html5_instance();
  $text = <<<EOL
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<address>Example</address>
<video src="http://www.google.com"/>
</body>
</html>
EOL;

  $dom_document = $html5->loadHTML($text);

  $body_node = $dom_document->getElementsByTagName('body')->item(0);
  $html5 = html5_tools_get_html5_instance();
  foreach ($body_node->childNodes as $child_node) {
    $body_content .= $html5->saveHTML($child_node);
  }

Where $body_content is returned as:

<address>Example
<video src="http://www.google.com">&nbsp;</video>

Noticably the video gets fixed but address isn't.

@sylus
Copy link
Contributor Author

sylus commented May 9, 2016

When I changed the following:

        "address" => 89, // NORMAL | VOID_TAG | AUTOCLOSE_P | BLOCK_TAG

to

        "address" => 65, // NORMAL | BLOCK_TAG

I then got correct output.

<address>Example</address>
<video src="http://www.google.com">&nbsp;</video>

@mattfarina
Copy link
Member

Looking at the spec for address I see that neither the start or end tag can be omitted. I'm not sure why AUTOCLOSE_P is on this. I think that's what's causing this issue.

@sylus Do you want to update this on master and the 2.x branch or shall I? I'm always open to PRs.

    "address" => 65, // NORMAL | BLOCK_TAG

@sylus
Copy link
Contributor Author

sylus commented May 9, 2016

Thanks a bunch submitted a P.R. ^_^

@mattfarina
Copy link
Member

Version 2.2.1 has been released with this fix included. Thanks for the contribution.

@sylus
Copy link
Contributor Author

sylus commented May 10, 2016

Thank you for the awesomeness that is html5-php :)

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

No branches or pull requests

2 participants