Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from shinsenter/develop
Browse files Browse the repository at this point in the history
Fix bug
  • Loading branch information
shinsenter authored Mar 24, 2019
2 parents 2c66e0d + 63b6ecf commit 4ce6fb1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion public/helpers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Defer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public function toHtml()
$output = mb_convert_encoding($output, $this->charset, $encoding);
}

if (!empty($this->bug72288_body)) {
$output = str_replace('<body>', $this->bug72288_body, $output);
}

return $output;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DeferInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class DeferInterface

// Simple fade-in effect
const FADEIN_EFFECT = 'html.no-deferjs img[data-src],html.no-deferjs iframe[data-src]{display:none!important}' .
'[data-src],[data-srcset],[data-style]{min-width:1px;min-height:1px;display:inline-block;max-width:100%;visibility:visible}' .
'[data-src],[data-srcset],[data-style]{min-width:1px;min-height:1px;max-width:100%;visibility:visible}' .
'[data-lazied]{opacity:.3!important;transition:opacity .15s ease-in-out}' .
'[data-lazied].in,[data-style][data-lazied]{background-color:transparent!important;opacity:inherit!important}';

Expand Down
15 changes: 10 additions & 5 deletions src/DeferParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ trait DeferParser
protected $preconnect_map;
protected $preload_map;

// Fix PHP bugs
// https://bugs.php.net/bug.php?id=72288
protected $bug72288_body;

/**
* Cleanup internal variables
*
Expand Down Expand Up @@ -87,7 +91,7 @@ protected function parseHtml($html)
$this->cleanup();

// Check if DOMDocument module was loaded
if (!class_exists('DOMDocument')) {
if (!class_exists(\DOMDocument::class)) {
throw new DeferException('DOMDocument module is not loaded. Please install php-xml module for PHP.', 1);
}

Expand All @@ -97,12 +101,12 @@ protected function parseHtml($html)
}

// Force HTML5 doctype
$html = preg_replace('/<!DOCTYPE html[^>]*?>/i', '<!DOCTYPE html>', $html, 1);
$html = preg_replace('/<!DOCTYPE html[^>]*>/i', '<!DOCTYPE html>', $html, 1);

// Create DOM document
$this->dom = new \DOMDocument('1.0', $this->charset);
$this->dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', $this->charset));
$this->dom = new \DOMDocument();
$this->dom->preserveWhiteSpace = false;
$this->dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', $this->charset));

// Create xpath object for searching tags
$this->xpath = new \DOMXPath($this->dom);
Expand All @@ -115,7 +119,8 @@ protected function parseHtml($html)

// Check if the <body> tag exists
if ($attempt = $this->xpath->query('//body')) {
$this->body = $attempt->item(0);
$this->body = $attempt->item(0);
$this->bug72288_body = preg_match('/(<body[^>]*>)/mi', $html, $match) ? $match[1] : '';
}

// If none of above, throw an error
Expand Down
3 changes: 3 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
(function(window, document, console, name) {
var NOOP = Function();
var GET_ATTRIBUTE = 'getAttribute';
var REM_ATTRIBUTE = 'removeAttribute';
var IS_CHROME = typeof window.chrome == 'object' && window.navigator.userAgent.indexOf('Trident/') == -1;

var COMMON_EXCEPTIONS = ':not([data-lazied])';
Expand Down Expand Up @@ -128,6 +129,8 @@
function mediafilter(media) {
function onload() {
addClass(media, helper.f);
media[REM_ATTRIBUTE]('data-src');
media[REM_ATTRIBUTE]('data-srcset');
}

if (media.src == media[GET_ATTRIBUTE]('data-src') || media[GET_ATTRIBUTE]('data-style')) {
Expand Down

0 comments on commit 4ce6fb1

Please sign in to comment.