html-minifier 0.6.9 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ /*!
2
+ * HTMLLint (to be used in conjunction with HTMLMinifier)
3
+ *
4
+ * Copyright (c) 2010-2013 Juriy "kangax" Zaytsev
5
+ * Licensed under the MIT license.
6
+ *
7
+ */
8
+
9
+ (function(global) {
10
+ 'use strict';
11
+
12
+ function isPresentationalElement(tag) {
13
+ return (/^(?:big|small|hr|blink|marquee)$/).test(tag);
14
+ }
15
+ function isDeprecatedElement(tag) {
16
+ return (/^(?:applet|basefont|center|dir|font|isindex|strike)$/).test(tag);
17
+ }
18
+ function isEventAttribute(attrName) {
19
+ return (/^on[a-z]+/).test(attrName);
20
+ }
21
+ function isStyleAttribute(attrName) {
22
+ return (attrName.toLowerCase() === 'style');
23
+ }
24
+ function isDeprecatedAttribute(tag, attrName) {
25
+ return (
26
+ (attrName === 'align' &&
27
+ (/^(?:caption|applet|iframe|img|imput|object|legend|table|hr|div|h[1-6]|p)$/).test(tag)) ||
28
+ (attrName === 'alink' && tag === 'body') ||
29
+ (attrName === 'alt' && tag === 'applet') ||
30
+ (attrName === 'archive' && tag === 'applet') ||
31
+ (attrName === 'background' && tag === 'body') ||
32
+ (attrName === 'bgcolor' && (/^(?:table|t[rdh]|body)$/).test(tag)) ||
33
+ (attrName === 'border' && (/^(?:img|object)$/).test(tag)) ||
34
+ (attrName === 'clear' && tag === 'br') ||
35
+ (attrName === 'code' && tag === 'applet') ||
36
+ (attrName === 'codebase' && tag === 'applet') ||
37
+ (attrName === 'color' && (/^(?:base(?:font)?)$/).test(tag)) ||
38
+ (attrName === 'compact' && (/^(?:dir|[dou]l|menu)$/).test(tag)) ||
39
+ (attrName === 'face' && (/^base(?:font)?$/).test(tag)) ||
40
+ (attrName === 'height' && (/^(?:t[dh]|applet)$/).test(tag)) ||
41
+ (attrName === 'hspace' && (/^(?:applet|img|object)$/).test(tag)) ||
42
+ (attrName === 'language' && tag === 'script') ||
43
+ (attrName === 'link' && tag === 'body') ||
44
+ (attrName === 'name' && tag === 'applet') ||
45
+ (attrName === 'noshade' && tag === 'hr') ||
46
+ (attrName === 'nowrap' && (/^t[dh]$/).test(tag)) ||
47
+ (attrName === 'object' && tag === 'applet') ||
48
+ (attrName === 'prompt' && tag === 'isindex') ||
49
+ (attrName === 'size' && (/^(?:hr|font|basefont)$/).test(tag)) ||
50
+ (attrName === 'start' && tag === 'ol') ||
51
+ (attrName === 'text' && tag === 'body') ||
52
+ (attrName === 'type' && (/^(?:li|ol|ul)$/).test(tag)) ||
53
+ (attrName === 'value' && tag === 'li') ||
54
+ (attrName === 'version' && tag === 'html') ||
55
+ (attrName === 'vlink' && tag === 'body') ||
56
+ (attrName === 'vspace' && (/^(?:applet|img|object)$/).test(tag)) ||
57
+ (attrName === 'width' && (/^(?:hr|td|th|applet|pre)$/).test(tag))
58
+ );
59
+ }
60
+ function isInaccessibleAttribute(attrName, attrValue) {
61
+ return (
62
+ attrName === 'href' &&
63
+ (/^\s*javascript\s*:\s*void\s*(\s+0|\(\s*0\s*\))\s*$/i).test(attrValue)
64
+ );
65
+ }
66
+
67
+ function Lint() {
68
+ this.log = [ ];
69
+ this._lastElement = null;
70
+ this._isElementRepeated = false;
71
+ }
72
+
73
+ Lint.prototype.testElement = function(tag) {
74
+ if (isDeprecatedElement(tag)) {
75
+ this.log.push(
76
+ 'Found <span class="deprecated-element">deprecated</span> <strong><code>&lt;' +
77
+ tag + '&gt;</code></strong> element'
78
+ );
79
+ }
80
+ else if (isPresentationalElement(tag)) {
81
+ this.log.push(
82
+ 'Found <span class="presentational-element">presentational</span> <strong><code>&lt;' +
83
+ tag + '&gt;</code></strong> element'
84
+ );
85
+ }
86
+ else {
87
+ this.checkRepeatingElement(tag);
88
+ }
89
+ };
90
+
91
+ Lint.prototype.checkRepeatingElement = function(tag) {
92
+ if (tag === 'br' && this._lastElement === 'br') {
93
+ this._isElementRepeated = true;
94
+ }
95
+ else if (this._isElementRepeated) {
96
+ this._reportRepeatingElement();
97
+ this._isElementRepeated = false;
98
+ }
99
+ this._lastElement = tag;
100
+ };
101
+
102
+ Lint.prototype._reportRepeatingElement = function() {
103
+ this.log.push('Found <code>&lt;br></code> sequence. Try replacing it with styling.');
104
+ };
105
+
106
+ Lint.prototype.testAttribute = function(tag, attrName, attrValue) {
107
+ if (isEventAttribute(attrName)) {
108
+ this.log.push(
109
+ 'Found <span class="event-attribute">event attribute</span> (<strong>' +
110
+ attrName + '</strong>) on <strong><code>&lt;' + tag + '&gt;</code></strong> element.'
111
+ );
112
+ }
113
+ else if (isDeprecatedAttribute(tag, attrName)) {
114
+ this.log.push(
115
+ 'Found <span class="deprecated-attribute">deprecated</span> <strong>' +
116
+ attrName + '</strong> attribute on <strong><code>&lt;' + tag + '&gt;</code></strong> element.'
117
+ );
118
+ }
119
+ else if (isStyleAttribute(attrName)) {
120
+ this.log.push(
121
+ 'Found <span class="style-attribute">style attribute</span> on <strong><code>&lt;' +
122
+ tag + '&gt;</code></strong> element.'
123
+ );
124
+ }
125
+ else if (isInaccessibleAttribute(attrName, attrValue)) {
126
+ this.log.push(
127
+ 'Found <span class="inaccessible-attribute">inaccessible attribute</span> ' +
128
+ '(on <strong><code>&lt;' + tag + '&gt;</code></strong> element).'
129
+ );
130
+ }
131
+ };
132
+
133
+ Lint.prototype.testChars = function(chars) {
134
+ this._lastElement = '';
135
+ if (/(&nbsp;\s*){2,}/.test(chars)) {
136
+ this.log.push('Found repeating <strong><code>&amp;nbsp;</code></strong> sequence. Try replacing it with styling.');
137
+ }
138
+ };
139
+
140
+ Lint.prototype.test = function(tag, attrName, attrValue) {
141
+ this.testElement(tag);
142
+ this.testAttribute(tag, attrName, attrValue);
143
+ };
144
+
145
+ Lint.prototype.populate = function(writeToElement) {
146
+ if (this._isElementRepeated) {
147
+ this._reportRepeatingElement();
148
+ }
149
+
150
+ if (this.log.length) {
151
+ if (writeToElement) {
152
+ writeToElement.innerHTML = '<ol><li>' + this.log.join('<li>') + '</ol>';
153
+ }
154
+ else {
155
+ var output = ' - ' +
156
+ this.log.join('\n - ')
157
+ .replace(/(<([^>]+)>)/ig, '')
158
+ .replace(/&lt;/g, '<')
159
+ .replace(/&gt;/g, '>');
160
+
161
+ console.log(output);
162
+ }
163
+ }
164
+ };
165
+
166
+ global.HTMLLint = Lint;
167
+
168
+ })(typeof exports === 'undefined' ? this : exports);