Skip to content

Commit

Permalink
Merge branch 'release/1.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sommerregen committed Feb 11, 2015
2 parents 0ecdf60 + 2d1ca98 commit 094dd35
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v1.1.3
## 02/10/2015

3. [](#bugfix)
* Fixed self-closing tags in HTML5 and ensured to return contents compliant to HTML(5)

# v1.1.2
## 02/10/2015

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: External Links
version: 1.1.2
version: 1.1.3
description: "This plugin adds small icons to external and mailto links, informing users the link will take them to a new site or open their email client."
icon: external-link
author:
Expand Down
41 changes: 35 additions & 6 deletions external_links.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
/**
* External Links v1.1.2
* External Links v1.1.3
*
* This plugin adds small icons to external and mailto links, informing
* users the link will take them to a new site or open their email client.
*
* Licensed under MIT, see LICENSE.
*
* @package External Links
* @version 1.1.2
* @version 1.1.3
* @link <https://github.com/sommerregen/grav-plugin-archive-plus>
* @author Benjamin Regler <[email protected]>
* @copyright 2015, Benjamin Regler
Expand Down Expand Up @@ -87,13 +87,29 @@ public function onPageContentProcessed(Event $event) {
if ( $config->get('process', TRUE) ) {
$content = $page->getRawContent();

/**
* Two Really good resources to handle DOMDocument with HTML(5)
* correctly.
*
* @see http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php
* @see http://stackoverflow.com/questions/7997936/how-do-you-format-dom-structures-in-php
*/

// Clear previous errors
if ( libxml_use_internal_errors(TRUE) === TRUE ) {
libxml_clear_errors();
}

// Create a DOM parser object
$dom = new \DOMDocument('1.0', 'UTF-8');

// Pretty print output
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;

// Normalize newlines
$content = preg_replace('~\R~u', "\n", $content);

// Parse the HTML using UTF-8
// The @ before the method call suppresses any warnings that
// loadHTML might throw because of invalid HTML in the page.
Expand Down Expand Up @@ -183,15 +199,28 @@ public function onPageContentProcessed(Event $event) {
}

$content = '';
// Process HTML from DOM document
// Transform DOM document to valid HTML(5)
$body = $dom->getElementsByTagName('body')->item(0);
foreach ( $body->childNodes as $node ) {
$content .= $dom->saveXML($node);
// Expand empty tags (e.g. <br/> to <br></br>)
if ( ($html = $dom->saveXML($node, LIBXML_NOEMPTYTAG)) !== FALSE ) {
$content .= $html;
}
}

// Fix formatting for self-closing tags in HTML5
$content = preg_replace('~<(area|base(?:font)?|br|col|command|embed|frame|hr|img|input|keygen|link|meta|param|source|track|wbr)(.*?)\s*/>~i', '<$1$2 />', $content);
// Fix formatting for self-closing tags in HTML5 and removing
// encapsulated (uncommented) CDATA blocks in <script> and
// <style> tags
$regex = array(
'~' . preg_quote('<![CDATA[', '~') . '~' => '',
'~' . preg_quote(']]>', '~') . '~' => '',
'~></(?:area|base(?:font)?|br|col|command|embed|frame|hr|img|input|keygen|link|meta|param|source|track|wbr)>~' => ' />',
);

// Make XML HTML5 compliant
$content = preg_replace(array_keys($regex), $regex, $content);

// Write content back to page
$page->setRawcontent($content);
}
}
Expand Down

0 comments on commit 094dd35

Please sign in to comment.