UNPKG

zombie-globbies

Version:

A very quick fix for [**Zombie**](https://github.com/assaf/zombie) to permit to crawl correctly webpages with attributes on the html tag (eg: html lang="en").

199 lines (186 loc) 5.95 kB
var File, HTML, Mime, Path, UploadedFile; HTML = require("jsdom").defaultLevel; Path = require("path"); File = require("fs"); Mime = require("mime"); UploadedFile = function(filename) { var file; file = new String(Path.basename(filename)); file.filename = filename; file.mime = Mime.lookup(filename); file.read = function() { return File.readFileSync(filename); }; return file; }; HTML.HTMLFormElement.prototype.submit = function(button) { var document, params, process; document = this.ownerDocument; params = {}; process = (function(_this) { return function(index) { var field, name, option, selected, value, _i, _len, _name, _ref, _ref1; if (field = _this.elements.item(index)) { value = null; if (!field.getAttribute("disabled") && (name = field.getAttribute("name"))) { if (field.nodeName === "SELECT") { selected = []; _ref = field.options; for (_i = 0, _len = _ref.length; _i < _len; _i++) { option = _ref[_i]; if (option.selected) { selected.push(option.value); } } if (field.multiple) { params[name] = (params[name] || []).concat(selected); } else { if (selected.length > 0) { value = selected[0]; } else { value = (_ref1 = field.options[0]) != null ? _ref1.value : void 0; } params[name] || (params[name] = []); params[name].push(value); } } else if (field.nodeName === "INPUT" && (field.type === "checkbox" || field.type === "radio")) { if (field.checked) { params[name] || (params[name] = []); params[name].push(field.value || "1"); } } else if (field.nodeName === "INPUT" && field.type === "file") { if (field.value) { params[name] || (params[name] = []); params[name].push(UploadedFile(field.value)); } } else if (field.nodeName === "TEXTAREA" || field.nodeName === "INPUT") { if (field.type !== "submit" && field.type !== "image") { params[name] || (params[name] = []); params[name].push(field.value || ""); } } } return process(index + 1); } else { if (button && button.name) { params[_name = button.name] || (params[_name] = []); params[button.name].push(button.value); } return document.window._submit({ url: _this.getAttribute("action") || document.location.href, method: _this.getAttribute("method") || "GET", encoding: _this.getAttribute("enctype"), params: params, target: _this.getAttribute("target") }); } }; })(this); return process(0); }; HTML.HTMLFormElement.prototype._dispatchSubmitEvent = function(button) { var event; event = this.ownerDocument.createEvent("HTMLEvents"); event.initEvent("submit", true, true); event._button = button; return this.dispatchEvent(event); }; HTML.HTMLFormElement.prototype._eventDefaults["submit"] = function(event) { return event.target.submit(event._button); }; HTML.HTMLInputElement.prototype._eventDefaults = { click: function(event) { var change, form, input; input = event.target; change = function() { event = input.ownerDocument.createEvent("HTMLEvents"); event.initEvent("change", true, true); return input.dispatchEvent(event); }; switch (input.type) { case "reset": if (form = input.form) { return form.reset(); } break; case "submit": case "image": if (form = input.form) { return form._dispatchSubmitEvent(input); } break; case "checkbox": return change(); case "radio": if (!input.getAttribute("readonly")) { input.checked = true; return change(); } } } }; HTML.HTMLInputElement.prototype.click = function() { var checked, click, original, radio, radios, _i, _j, _len, _len1; this.focus(); click = (function(_this) { return function() { var event; event = _this.ownerDocument.createEvent("HTMLEvents"); event.initEvent("click", true, true); return _this.dispatchEvent(event); }; })(this); switch (this.type) { case "checkbox": if (!this.getAttribute("readonly")) { original = this.checked; this.checked = !this.checked; if (!click()) { this.checked = original; } } break; case "radio": if (!this.getAttribute("readonly")) { if (this.checked) { click(); } else { radios = this.ownerDocument.querySelectorAll("input[type=radio][name='" + (this.getAttribute("name")) + "']"); checked = null; for (_i = 0, _len = radios.length; _i < _len; _i++) { radio = radios[_i]; if (radio.checked && radio.form === this.form) { checked = radio; radio.checked = false; } } this.checked = true; if (!click()) { this.checked = false; for (_j = 0, _len1 = radios.length; _j < _len1; _j++) { radio = radios[_j]; if (radio.form === this.form) { radio.checked = radio === checked; } } } } } break; default: click(); } }; HTML.HTMLButtonElement.prototype._eventDefaults = { click: function(event) { var button, form; button = event.target; if (button.getAttribute("disabled")) { } else { form = button.form; if (form) { return form._dispatchSubmitEvent(button); } } } };