-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatdHTMLMinifier.class.php
More file actions
166 lines (149 loc) · 6.86 KB
/
atdHTMLMinifier.class.php
File metadata and controls
166 lines (149 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/*
* HTML OPTIMIERUNG PROOF OF CONCEPT nach
* http://perfectionkills.com/optimizing-html/
* http://code.google.com/intl/de-DE/speed/articles/optimizing-html.html
*
*/
class atdHTMLMinifier
{
static protected function domNodeDiscardable(DOMNode $node)
{
return ($node->tagName == 'meta' && strtolower($node->getAttribute('http-equiv')) == 'content-type');
}
static protected function getNextSiblingOfTypeDOMElement(DOMNode $node)
{
do
{
$node = $node->nextSibling;
}
while (!($node === null || $node instanceof DOMElement));
return $node;
}
static protected function domAttrDiscardable(DOMAttr $attr)
{
#(!in_array($attr->ownerElement->tagName, array('input', 'select', 'button', 'textarea')) && $attr->name == 'name' && $attr->ownerElement->getAttribute('id') == $attr->value) # elements with pairing name/id’s
return ($attr->ownerElement->tagName == 'form' && $attr->name == 'method' && strtolower($attr->value) == 'get') # <form method="get"> is default
|| ($attr->ownerElement->tagName == 'style' && $attr->name == 'media' && strtolower($attr->value) == 'all') # <style media="all"> is implicit default
|| ($attr->ownerElement->tagName == 'input' && $attr->name == 'type' && strtolower($attr->value) == 'text') # <input type="text"> is default
;
}
#http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#void-elements
private static $void_elements = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');
private static $optional_end_tags = array('html', 'head', 'body');
static protected function domNodeClosingTagOmittable(DOMNode $node)
{
# TODO: Exakt die Spezifikation implementieren, indem nachfolgende Elemente
# mitbetrachtet werden
# http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags
$tag_name = $node->tagName;
#$nextSibling = self::getNextSiblingOfTypeDOMElement($node);
$nextSibling = $node->nextSibling;
return in_array($tag_name, self::$void_elements)
|| in_array($tag_name, self::$optional_end_tags)
|| ($tag_name == 'li' && ($nextSibling === null || ($nextSibling instanceof DOMElement && $nextSibling->tagName == $tag_name)))
|| ($tag_name == 'p' && (($nextSibling === null && ($node->parentNode !== null && $node->parentNode->tagName != 'a'))
|| ($nextSibling instanceof DOMElement && in_array($nextSibling->tagName, array('address', 'article', 'aside', 'blockquote', 'dir',
'div', 'dl', 'fieldset', 'footer', 'form', 'h1', 'h2',
'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'menu',
'nav', 'ol', 'p', 'pre', 'section', 'table', 'ul')))
)
);
}
static protected function domAttrIsBoolean(DOMAttr $attr)
{
# INKOMPLETT !!
# gibt anscheinend keine Liste
# http://www.google.de/#hl=de&source=hp&q=%22boolean+attribute%22+site%3Ahttp%3A%2F%2Fwww.whatwg.org%2Fspecs%2Fweb-apps%2Fcurrent-work%2Fmultipage%2F&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=e48ec3a97faa7ccb
#
$tag_name = $attr->ownerElement->tagName;
return $attr->name == 'hidden'
|| ($tag_name == 'fieldset' && in_array($attr->name, array('disabled', 'readonly')))
|| ($tag_name == 'option' && in_array($attr->name, array('disabled', 'readonly', 'selected')))
|| ($tag_name == 'input' && in_array($attr->name, array('disabled', 'readonly', 'checked', 'required')))
|| ($tag_name == 'select' && in_array($attr->name, array('disabled', 'readonly', 'multiple', 'required')))
;
}
static protected function domNodeAttributesToString(DOMNode $node)
{
#Remove quotes around attribute values, when allowed (<p class="foo"> → <p class=foo>)
#Remove optional values from boolean attributes (<option selected="selected"> → <option selected>)
$attrstr = '';
if ($node->attributes != null)
{
foreach ($node->attributes as $attribute)
{
if (self::domAttrDiscardable($attribute))
continue;
$attrstr .= $attribute->name;
if (!self::domAttrIsBoolean($attribute))
{
$attrstr .= '=';
# http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#attributes-0
$omitquotes = $attribute->value != '' && 0 == preg_match('/["\'=<>` \t\r\n\f]+/', $attribute->value) ;
# DOM behält ENTITES nicht bei
# http://www.w3.org/TR/2004/REC-xml-20040204/#sec-predefined-ent
$attr_val = strtr($attribute->value, array('"' => '"', '&' => '&', '<' => '<', '>' => '>'));
$attrstr .= ($omitquotes ? '' : '"') . $attr_val . ($omitquotes ? '' : '"');
}
$attrstr .= ' ';
}
}
return trim($attrstr);
}
static protected function domNodeToString(DOMNode $node)
{
$htmlstr = '';
foreach ($node->childNodes as $child)
{
if ($child instanceof DOMDocumentType)
{
$htmlstr .= '<!doctype html>';
}
else if ($child instanceof DOMElement)
{
if (!self::domNodeDiscardable($child))
{
$htmlstr .= trim('<' . $child->tagName . ' ' . self::domNodeAttributesToString($child));
$htmlstr .= '>' . self::domNodeToString($child);
if (!self::domNodeClosingTagOmittable($child))
{
$htmlstr .= '</' . $child->tagName . '>';
}
}
}
else if ($child instanceof DOMText)
{
if ($child->isWhitespaceInElementContent())
{
if ($child->previousSibling !== null && $child->nextSibling !== null)
{
$htmlstr .= ' ';
}
} else
{
# DOM behält ENTITES nicht bei
# http://www.w3.org/TR/2004/REC-xml-20040204/#sec-predefined-ent
$htmlstr .= strtr($child->wholeText, array('&' => '&', '<' => '<', '>' => '>'));
}
}
else if ($child instanceof DOMComment)
{
// KOMMENTARE SCHÖN IGNOREN
// TODO KEEP IE CONDITIONAL COMMENTS
}
else
{
echo 'Unhandled:' . get_class($child) . "\n";
}
}
return $htmlstr;
}
static function minify($html, $consider_inline = 'li')
{
$dom = new DOMDocument();
$dom->substituteEntities = false;
$dom->loadHTML(str_replace('<head>', '<head><Meta http-equiv="content-type" content="text/html; charset=utf-8">', $html));
return self::domNodeToString($dom);
}
}