Conditions | 21 |
Paths | 11520 |
Total Lines | 158 |
Code Lines | 125 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like plugin.js ➔ dataToHtml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | (function () { |
||
133 | var dataToHtml = function (editor, data, head) { |
||
134 | var headerFragment, headElement, html, elm, value; |
||
135 | var dom = editor.dom; |
||
136 | function setAttr(elm, name, value) { |
||
137 | elm.attr(name, value ? value : undefined); |
||
138 | } |
||
139 | function addHeadNode(node) { |
||
140 | if (headElement.firstChild) { |
||
141 | headElement.insert(node, headElement.firstChild); |
||
142 | } else { |
||
143 | headElement.append(node); |
||
144 | } |
||
145 | } |
||
146 | headerFragment = parseHeader(head); |
||
147 | headElement = headerFragment.getAll('head')[0]; |
||
148 | if (!headElement) { |
||
149 | elm = headerFragment.getAll('html')[0]; |
||
150 | headElement = new global$3('head', 1); |
||
151 | if (elm.firstChild) { |
||
152 | elm.insert(headElement, elm.firstChild, true); |
||
153 | } else { |
||
154 | elm.append(headElement); |
||
155 | } |
||
156 | } |
||
157 | elm = headerFragment.firstChild; |
||
158 | if (data.xml_pi) { |
||
159 | value = 'version="1.0"'; |
||
160 | if (data.docencoding) { |
||
161 | value += ' encoding="' + data.docencoding + '"'; |
||
162 | } |
||
163 | if (elm.type !== 7) { |
||
164 | elm = new global$3('xml', 7); |
||
165 | headerFragment.insert(elm, headerFragment.firstChild, true); |
||
166 | } |
||
167 | elm.value = value; |
||
168 | } else if (elm && elm.type === 7) { |
||
169 | elm.remove(); |
||
170 | } |
||
171 | elm = headerFragment.getAll('#doctype')[0]; |
||
172 | if (data.doctype) { |
||
173 | if (!elm) { |
||
174 | elm = new global$3('#doctype', 10); |
||
175 | if (data.xml_pi) { |
||
176 | headerFragment.insert(elm, headerFragment.firstChild); |
||
177 | } else { |
||
178 | addHeadNode(elm); |
||
179 | } |
||
180 | } |
||
181 | elm.value = data.doctype.substring(9, data.doctype.length - 1); |
||
182 | } else if (elm) { |
||
183 | elm.remove(); |
||
184 | } |
||
185 | elm = null; |
||
186 | global$1.each(headerFragment.getAll('meta'), function (meta) { |
||
187 | if (meta.attr('http-equiv') === 'Content-Type') { |
||
188 | elm = meta; |
||
189 | } |
||
190 | }); |
||
191 | if (data.docencoding) { |
||
192 | if (!elm) { |
||
193 | elm = new global$3('meta', 1); |
||
194 | elm.attr('http-equiv', 'Content-Type'); |
||
195 | elm.shortEnded = true; |
||
196 | addHeadNode(elm); |
||
197 | } |
||
198 | elm.attr('content', 'text/html; charset=' + data.docencoding); |
||
199 | } else if (elm) { |
||
200 | elm.remove(); |
||
201 | } |
||
202 | elm = headerFragment.getAll('title')[0]; |
||
203 | if (data.title) { |
||
204 | if (!elm) { |
||
205 | elm = new global$3('title', 1); |
||
206 | addHeadNode(elm); |
||
207 | } else { |
||
208 | elm.empty(); |
||
209 | } |
||
210 | elm.append(new global$3('#text', 3)).value = data.title; |
||
211 | } else if (elm) { |
||
212 | elm.remove(); |
||
213 | } |
||
214 | global$1.each('keywords,description,author,copyright,robots'.split(','), function (name) { |
||
215 | var nodes = headerFragment.getAll('meta'); |
||
216 | var i, meta; |
||
217 | var value = data[name]; |
||
218 | for (i = 0; i < nodes.length; i++) { |
||
219 | meta = nodes[i]; |
||
220 | if (meta.attr('name') === name) { |
||
221 | if (value) { |
||
222 | meta.attr('content', value); |
||
223 | } else { |
||
224 | meta.remove(); |
||
225 | } |
||
226 | return; |
||
227 | } |
||
228 | } |
||
229 | if (value) { |
||
230 | elm = new global$3('meta', 1); |
||
231 | elm.attr('name', name); |
||
232 | elm.attr('content', value); |
||
233 | elm.shortEnded = true; |
||
234 | addHeadNode(elm); |
||
235 | } |
||
236 | }); |
||
237 | var currentStyleSheetsMap = {}; |
||
238 | global$1.each(headerFragment.getAll('link'), function (stylesheet) { |
||
239 | if (stylesheet.attr('rel') === 'stylesheet') { |
||
240 | currentStyleSheetsMap[stylesheet.attr('href')] = stylesheet; |
||
241 | } |
||
242 | }); |
||
243 | global$1.each(data.stylesheets, function (stylesheet) { |
||
244 | if (!currentStyleSheetsMap[stylesheet]) { |
||
245 | elm = new global$3('link', 1); |
||
246 | elm.attr({ |
||
247 | rel: 'stylesheet', |
||
248 | text: 'text/css', |
||
249 | href: stylesheet |
||
250 | }); |
||
251 | elm.shortEnded = true; |
||
252 | addHeadNode(elm); |
||
253 | } |
||
254 | delete currentStyleSheetsMap[stylesheet]; |
||
255 | }); |
||
256 | global$1.each(currentStyleSheetsMap, function (stylesheet) { |
||
257 | stylesheet.remove(); |
||
258 | }); |
||
259 | elm = headerFragment.getAll('body')[0]; |
||
260 | if (elm) { |
||
261 | setAttr(elm, 'dir', data.langdir); |
||
262 | setAttr(elm, 'style', data.style); |
||
263 | setAttr(elm, 'vlink', data.visited_color); |
||
264 | setAttr(elm, 'link', data.link_color); |
||
265 | setAttr(elm, 'alink', data.active_color); |
||
266 | dom.setAttribs(editor.getBody(), { |
||
267 | style: data.style, |
||
268 | dir: data.dir, |
||
269 | vLink: data.visited_color, |
||
270 | link: data.link_color, |
||
271 | aLink: data.active_color |
||
272 | }); |
||
273 | } |
||
274 | elm = headerFragment.getAll('html')[0]; |
||
275 | if (elm) { |
||
276 | setAttr(elm, 'lang', data.langcode); |
||
277 | setAttr(elm, 'xml:lang', data.langcode); |
||
278 | } |
||
279 | if (!headElement.firstChild) { |
||
280 | headElement.remove(); |
||
281 | } |
||
282 | html = global$4({ |
||
283 | validate: false, |
||
284 | indent: true, |
||
285 | apply_source_formatting: true, |
||
286 | indent_before: 'head,html,body,meta,title,script,link,style', |
||
287 | indent_after: 'head,html,body,meta,title,script,link,style' |
||
288 | }).serialize(headerFragment); |
||
289 | return html.substring(0, html.indexOf('</body>')); |
||
290 | }; |
||
291 | var Parser = { |
||
520 |