This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
forked from marcelduran/yslow
-
Notifications
You must be signed in to change notification settings - Fork 8
/
yslow.js
148 lines (136 loc) · 4.28 KB
/
yslow.js
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
/*global YSLOW:true*/
/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */
/**
* @module YSLOW
* @class YSLOW
* @static
*/
if (typeof YSLOW === 'undefined') {
YSLOW = {};
}
/**
* Enable/disable debbuging messages
*/
YSLOW.DEBUG = true;
/**
*
* Adds a new rule to the pool of rules.
*
* Rule objects must implement the rule interface or an error will be thrown. The interface
* of a rule object is as follows:
* <ul>
* <li><code>id</code>, e.g. "numreq"</li>
* <li><code>name</code>, e.g. "Minimize HTTP requests"</li>
* <li><code>url</code>, more info about the rule</li>
* <li><code>config</code>, configuration object with defaults</li>
* <li><code>lint()</code> a method that accepts a document, array of components and a config object and returns a reuslt object</li>
* </ul>
*
* @param {YSLOW.Rule} rule A new rule object to add
*/
YSLOW.registerRule = function (rule) {
YSLOW.controller.addRule(rule);
};
/**
*
* Adds a new ruleset (new grading algorithm).
*
* Ruleset objects must implement the ruleset interface or an error will be thrown. The interface
* of a ruleset object is as follows:
* <ul>
* <li><code>id</code>, e.g. "ydefault"</li>
* <li><code>name</code>, e.g. "Yahoo! Default"</li>
* <li><code>rules</code> a hash of ruleID => ruleconfig </li>
* <li><code>weights</code> a hash of ruleID => ruleweight </li>
* </ul>
*
* @param {YSLOW.Ruleset} ruleset The new ruleset object to be registered
*/
YSLOW.registerRuleset = function (ruleset) {
YSLOW.controller.addRuleset(ruleset);
};
/**
* Register a renderer.
*
* Renderer objects must implement the renderer interface.
* The interface is as follows:
* <ul>
* <li><code>id</code></li>
* <li><code>supports</code> a hash of view_name => 1 or 0 to indicate what views are supported</li>
* <li>and the methods</li>
* </ul>
*
* For instance if you define a JSON renderer that only render grade. Your renderer object will look like this:
* { id: 'json',
* supports: { reportcard: 1, components: 0, stats: 0, cookies: 0},
* reportcardView: function(resultset) { ... }
* }
*
* Refer to YSLOW.HTMLRenderer for the function prototype.
*
*
* @param {YSLOW.renderer} renderer The new renderer object to be registered.
*/
YSLOW.registerRenderer = function (renderer) {
YSLOW.controller.addRenderer(renderer);
};
/**
* Adds a new tool.
*
* Tool objects must implement the tool interface or an error will be thrown.
* The interface of a tool object is as follows:
* <ul>
* <li><code>id</code>, e.g. 'mytool'</li>
* <li><code>name</code>, eg. 'Custom tool #3'</li>
* <li><code>print_output</code>, whether this tool will produce output.</li>
* <li><code>run</code>, function that takes doc and componentset object, return content to be output</li>
* </ul>
*
* @param {YSLOW.Tool} tool The new tool object to be registered
*/
YSLOW.registerTool = function (tool) {
YSLOW.Tools.addCustomTool(tool);
};
/**
* Register an event listener
*
* @param {String} event_name Name of the event
* @param {Function} callback A function to be called when the event fires
* @param {Object} that Object to be assigned to the "this" value of the callback function
*/
YSLOW.addEventListener = function (event_name, callback, that) {
YSLOW.util.event.addListener(event_name, callback, that);
};
/**
* Unregister an event listener.
*
* @param {String} event_name Name of the event
* @param {Function} callback The callback function that was added as a listener
* @return {Boolean} TRUE is the listener was removed successfully, FALSE otherwise (for example in cases when the listener doesn't exist)
*/
YSLOW.removeEventListener = function (event_name, callback) {
return YSLOW.util.event.removeListener(event_name, callback);
};
/**
* @namespace YSLOW
* @constructor
* @param {String} name Error type
* @param {String} message Error description
*/
YSLOW.Error = function (name, message) {
/**
* Type of error, e.g. "Interface error"
* @type String
*/
this.name = name;
/**
* Error description
* @type String
*/
this.message = message;
};
YSLOW.Error.prototype = {
toString: function () {
return this.name + "\n" + this.message;
}
};