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

Commit d6ad921

Browse files
authored
Merge pull request #44 from shinsenter/develop
[Bugfix] Fixed wrong encoding
2 parents 7dfad62 + f1a86ca commit d6ad921

File tree

6 files changed

+49
-31
lines changed

6 files changed

+49
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ $defer->defer_web_fonts = true;
121121
$defer->empty_gif = '';
122122
$defer->empty_src = '';
123123
$defer->use_color_placeholder = true;
124-
$defer->use_css_fadein_effects = true;
124+
$defer->use_css_fadein_effects = true; // true or 'grey'
125125

126126
// Blacklist
127127
$defer->do_not_optimize = [

src/DeferInterface.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}
3131

3232
if (!defined('DEFER_JS_IGNORE')) {
33+
define('DEFER_CSS_IGNORE', 'not(@data-ignore) and not(ancestor::noscript)');
3334
define('DEFER_JS_IGNORE', 'not(@data-ignore) and not(ancestor::noscript)');
3435

3536
define('DEFER_IMG_IGNORE', implode(' and ', [
@@ -39,6 +40,7 @@
3940
]));
4041

4142
define('DEFER_IMG_TAGS', implode(' or ', [
43+
'(self::input and @type="image")',
4244
'self::img',
4345
'self::picture',
4446
'self::source',
@@ -92,22 +94,23 @@ abstract class DeferInterface
9294
const SVG_PLACEHOLDER = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 %d %d'></svg>";
9395

9496
// Content tags
95-
const AUDIO_TAG = 'audio';
96-
const BR_TAG = 'br';
97-
const BODY_TAG = 'body';
98-
const EMBED_TAG = 'embed';
99-
const FRAME_TAG = 'frame';
100-
const HEAD_TAG = 'head';
101-
const IFRAME_TAG = 'iframe';
102-
const IMG_TAG = 'img';
103-
const LINK_TAG = 'link';
104-
const META_TAG = 'meta';
105-
const NOSCRIPT_TAG = 'noscript';
106-
const PICTURE_TAG = 'picture';
107-
const SCRIPT_TAG = 'script';
108-
const SOURCE_TAG = 'source';
109-
const STYLE_TAG = 'style';
110-
const VIDEO_TAG = 'video';
97+
const AUDIO_TAG = 'audio';
98+
const BR_TAG = 'br';
99+
const BODY_TAG = 'body';
100+
const EMBED_TAG = 'embed';
101+
const FRAME_TAG = 'frame';
102+
const HEAD_TAG = 'head';
103+
const IFRAME_TAG = 'iframe';
104+
const IMG_TAG = 'img';
105+
const INPUT_TAG = 'input';
106+
const LINK_TAG = 'link';
107+
const META_TAG = 'meta';
108+
const NOSCRIPT_TAG = 'noscript';
109+
const PICTURE_TAG = 'picture';
110+
const SCRIPT_TAG = 'script';
111+
const SOURCE_TAG = 'source';
112+
const STYLE_TAG = 'style';
113+
const VIDEO_TAG = 'video';
111114

112115
// LINK tag's rel attribute
113116
const REL_DNSPREFETCH = 'dns-prefetch';
@@ -138,6 +141,7 @@ abstract class DeferInterface
138141
const ATTR_CONTENT = 'content';
139142
const ATTR_CROSSORIGIN = 'crossorigin';
140143
const ATTR_DATA_IGNORE = 'data-ignore';
144+
const ATTR_DATA_NOLAZY = 'data-nolazy';
141145
const ATTR_DATA_SRC = 'data-src';
142146
const ATTR_DATA_SRCSET = 'data-srcset';
143147
const ATTR_DATA_STYLE = 'data-style';
@@ -180,7 +184,7 @@ abstract class DeferInterface
180184
const COMMENT_XPATH = '//comment()[not(contains(.,"[if ")) and not(contains(.,"[endif]"))]';
181185
const DNSCONN_XPATH = '//link[@rel="dns-prefetch" or @rel="preconnect"]';
182186
const PRELOAD_XPATH = '//link[@rel="preload"]';
183-
const STYLE_XPATH = '//style[' . DEFER_JS_IGNORE . ']|//link[' . DEFER_JS_IGNORE . ' and @rel="stylesheet"]';
187+
const STYLE_XPATH = '//style[' . DEFER_CSS_IGNORE . ']|//link[' . DEFER_CSS_IGNORE . ' and @rel="stylesheet"]';
184188
const SCRIPT_XPATH = '//script[' . DEFER_JS_IGNORE . ' and (not(@type) or contains(@type,"javascript"))]';
185189
const IMG_XPATH = '//*[(' . DEFER_IMG_TAGS . ') and ' . DEFER_IMG_IGNORE . ']';
186190
const IFRAME_XPATH = '//*[(' . DEFER_IFRAME_TAGS . ') and ' . DEFER_IFRAME_IGNORE . ']';

src/DeferOptimizer.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ protected function optimizeStyleTags()
486486
}
487487

488488
// Defer the style tag if there is background url
489-
if (preg_match('/url\s*\(/', $code)) {
489+
if (preg_match('/url\s*\(/', $code) && !$node->hasAttribute(static::ATTR_DATA_NOLAZY)) {
490490
$this->makeLazyStyle($node);
491491
}
492492
}
@@ -537,13 +537,14 @@ protected function optimizeImgTags()
537537
$replaced_src = $this->makeLazySrc($node);
538538
$replaced = $replaced_srcset || $replaced_src;
539539

540-
if ($node->nodeName !== static::IMG_TAG || !$replaced || $node->hasAttribute(static::ATTR_SRC)) {
540+
if (($node->nodeName !== static::IMG_TAG && $node->nodeName !== static::INPUT_TAG) ||
541+
!$replaced || $node->hasAttribute(static::ATTR_SRC)) {
541542
continue;
542543
}
543544

544545
if ($this->empty_gif) {
545546
$node->setAttribute(static::ATTR_SRC, $this->empty_gif);
546-
} elseif ($node->nodeName == static::IMG_TAG) {
547+
} else {
547548
$this->setPlaceholderSrc($node);
548549
}
549550

@@ -829,7 +830,14 @@ protected function getPreloadType($node)
829830
protected function addBackgroundColor($node)
830831
{
831832
if ($this->use_color_placeholder) {
832-
$placeholder = 'background-color:hsl(' . rand(1, 360) . ',100%,96%);';
833+
if ($this->use_color_placeholder == 'grey') {
834+
// Light grey placeholder
835+
$placeholder = 'background-color:hsl(0,0%,' . rand(95, 99) . '%);';
836+
} else {
837+
// Colorful placeholder
838+
$placeholder = 'background-color:hsl(' . rand(1, 360) . ',100%,96%);';
839+
}
840+
833841
$style = (string) $node->getAttribute(static::ATTR_STYLE);
834842
$node->setAttribute(static::ATTR_STYLE, $placeholder . $style);
835843
}

src/DeferOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ trait DeferOptions
5151
// Web-font optimizations
5252
'defer_web_fonts' => true,
5353
'web_fonts_patterns' => [
54-
'_debugbar.*stylesheets',
54+
'(font-?awesome).*?\.css',
5555
'fonts\.google(apis)?\.com',
5656
'(gadget|popup|modal|icons)[^\/]*\.css',
57-
'(font-awesome)([-_][\d\.]+)?(\.min)?\.css',
57+
'_debugbar.*stylesheets',
5858
],
5959

6060
// Custom loader scripts

src/DeferParser.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ protected function parseHtml($html)
121121

122122
// Detect charset charset
123123
if (empty($this->charset)) {
124-
$this->charset = mb_detect_encoding($html) ?: 'UTF-8';
124+
$encoding = mb_detect_encoding($html, 'auto');
125+
126+
if ($encoding === false || $encoding === 'ASCII') {
127+
$encoding = 'UTF-8';
128+
}
129+
130+
$this->charset = $encoding;
125131
}
126132

127133
// Convert HTML into HTML entities
@@ -406,7 +412,7 @@ protected function normalizeUrl($node, $attr = 'src', $preload_flag = true)
406412
}
407413

408414
// Remove ads
409-
if (preg_match('/ads|click|googletags|publisher/i', $src)) {
415+
if (preg_match('#ads|click|googletags|publisher|gpt\.js#i', $src)) {
410416
$preload_flag = false;
411417
}
412418

@@ -434,7 +440,7 @@ protected function isAmpHtml($html)
434440
}
435441

436442
/**
437-
* Return TRUE if it is an AMP page
443+
* Convert input string into html entities
438444
*
439445
* @since 1.0.7
440446
* @param string $html
@@ -447,7 +453,7 @@ protected function charset2entity($html, $charset)
447453
}
448454

449455
/**
450-
* Return TRUE if it is an AMP page
456+
* Detect and convert html entities into normal text
451457
*
452458
* @since 1.0.7
453459
* @param string $html
@@ -456,9 +462,9 @@ protected function charset2entity($html, $charset)
456462
*/
457463
protected function entity2charset($html, $charset)
458464
{
459-
$encoding = mb_detect_encoding($html);
465+
$encoding = mb_detect_encoding($html, 'auto');
460466

461-
if (empty($encoding) || $encoding == 'ASCII') {
467+
if ($encoding === false || $encoding === 'ASCII') {
462468
$encoding = 'HTML-ENTITIES';
463469

464470
if (is_null(static::$__html_mapping)) {

test/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
$defer->defer_web_fonts = true;
5454
$defer->use_css_fadein_effects = true;
55-
$defer->use_color_placeholder = false;
55+
$defer->use_color_placeholder = 'grey';
5656

5757
// Manually add deferjs library script tag
5858
// $defer->manually_add_deferjs = true;

0 commit comments

Comments
 (0)