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,qBAAK,CAAC,OAAO,GAAI,KAAK,KAAK,OAAO,AAAC,CAAC;eACrC,CAAC,CAAC;aACN;;SACF;AACD,cAAM;OACP;;AAAA,AAED;AAAS;AACP,aAAK,EAAE,CAAC;AACR,cAAM;OACP;AAAA,GACF;CACF,CAAC;;;AAIF,GAAG,CAAC,iBAAiB,CAAC,SAAS,CAAC,cAAc,CAAC,KAAK,GAAG,UAAS,KAAK,EAAE;AACrE,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;AAC5B,MAAI,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,EACjC,OAAO,KAAK,CAAC;;AAEf,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;AACzB,MAAI,IAAI,EACN,OAAO,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;CAC5C,CAAC","file":"forms.js","sourcesContent":["// Patches to JSDOM for properly handling forms.\nconst DOM   = require('./index');\nconst File  = require('fs');\nconst Mime  = require('mime');\nconst Path  = require('path');\n\n\n// The Form\n// --------\n\n// Forms convert INPUT fields of type file into this object and pass it as\n// parameter to resource request.\n//\n// The base class is a String, so the value (e.g. when passed in a GET request)\n// is the base filename.  Additional properties include the MIME type (`mime`),\n// the full filename (`filename`) and the `read` method that returns the file\n// contents.\nfunction uploadedFile(filename) {\n  const file = {\n    valueOf() {\n      return Path.basename(filename);\n    }\n  };\n  file.filename = filename;\n  file.mime     = Mime.lookup(filename);\n  file.read     = function() {\n    return File.readFileSync(filename);\n  };\n  return file;\n}\n\n\n// Implement form.submit such that it actually submits a request to the server.\n// This method takes the submitting button so we can send the button name/value.\nDOM.HTMLFormElement.prototype.submit = function(button) {\n  const form      = this;\n  const document  = form.ownerDocument;\n  const params    = new Map();\n\n  function addFieldValues(fieldName, values) {\n    const current = (params.get(fieldName) || []);\n    const next    = current.concat(values);\n    params.set(fieldName, next);\n  }\n\n  function addFieldToParams(field) {\n    if (field.getAttribute('disabled'))\n      return;\n\n    const name = field.getAttribute('name');\n    if (!name)\n      return;\n\n    if (field.nodeName === 'SELECT') {\n      const selected = Array.from(field.options)\n        .filter(option  => option.selected)\n        .map(options    => options.value);\n\n      if (field.multiple)\n        addFieldValues(name, selected);\n      else {\n        const value = (selected.length > 0) ?\n          selected[0] :\n          (field.options.length && field.options[0].value);\n        addFieldValues(name, [ value ]);\n      }\n      return;\n    }\n\n    if (field.nodeName === 'INPUT' && (field.type === 'checkbox' || field.type === 'radio')) {\n      if (field.checked) {\n        const value   = field.value || '1';\n        addFieldValues(name, [ value ]);\n      }\n      return;\n    }\n\n    if (field.nodeName === 'INPUT' && field.type === 'file') {\n      if (field.value) {\n        const value   = uploadedFile(field.value);\n        addFieldValues(name, [ value ]);\n      }\n      return;\n    }\n\n    if (field.nodeName === 'TEXTAREA' || field.nodeName === 'INPUT') {\n      if (field.type !== 'submit' && field.type !== 'image')\n        addFieldValues(name, [ field.value ]);\n      return;\n    }\n  }\n\n  function addButtonToParams() {\n    if (button.nodeName === 'INPUT' && button.type === 'image') {\n      addFieldValues(button.name + '.x', [ '0' ]);\n      addFieldValues(button.name + '.y', [ '0' ]);\n\n      if (button.value)\n        addFieldValues(button.name, [ button.value ]);\n    } else\n      addFieldValues(button.name, [ button.value ]);\n  }\n\n  function submit() {\n    if (button && button.name)\n      addButtonToParams();\n\n    // Ask window to submit form, let it figure out how to handle this based on\n    // the target attribute.\n    document.defaultView._submit({\n      url:      form.getAttribute('action') || document.location.href,\n      method:   form.getAttribute('method') || 'GET',\n      encoding: form.getAttribute('enctype'),\n      params:   params,\n      target:   form.getAttribute('target')\n    });\n  }\n\n  function process(index) {\n    const field = form.elements.item(index);\n    if (!field) {\n      submit();\n      return;\n    }\n    addFieldToParams(field);\n    process(index + 1);\n  }\n\n  process(0);\n};\n\n\n\n// Replace dispatchEvent so we can send the button along the event.\nDOM.HTMLFormElement.prototype._dispatchSubmitEvent = function(button) {\n  const event = this.ownerDocument.createEvent('HTMLEvents');\n  event.initEvent('submit', true, true);\n  event._button = button;\n  return this.dispatchEvent(event);\n};\n\n\n// Default behavior for submit events is to call the form's submit method, but we\n// also pass the submitting button.\nDOM.HTMLFormElement.prototype._eventDefaults.submit = function(event) {\n  event.target.submit(event._button);\n};\n\n\n// Buttons\n// -------\n\n// Default behavior for clicking on inputs.\nDOM.HTMLInputElement.prototype._eventDefaults =\n  Object.assign({}, DOM.HTMLElement.prototype._eventDefaults);\n\nDOM.HTMLInputElement.prototype._eventDefaults.click = function(event) {\n  const input = event.target;\n\n  function change() {\n    const changeEvent = input.ownerDocument.createEvent('HTMLEvents');\n    changeEvent.initEvent('change', true, true);\n    input.dispatchEvent(changeEvent);\n  }\n\n  switch (input.type) {\n    case 'reset': {\n      if (input.form)\n        input.form.reset();\n      break;\n    }\n    case 'submit': {\n      if (input.form)\n        input.form._dispatchSubmitEvent(input);\n      break;\n    }\n    case 'image': {\n      if (input.form)\n        input.form._dispatchSubmitEvent(input);\n      break;\n    }\n    case 'checkbox': {\n      change();\n      break;\n    }\n    case 'radio': {\n      if (!input.getAttribute('readonly')) {\n        input.checked = true;\n        change();\n      }\n    }\n  }\n};\n\n\n\n// Current INPUT behavior on click is to capture sumbit and handle it, but\n// ignore all other clicks. We need those other clicks to occur, so we're going\n// to dispatch them all.\nDOM.HTMLInputElement.prototype.click = function() {\n  const input = this;\n  input.focus();\n\n  // First event we fire is click event\n  function click() {\n    const clickEvent = input.ownerDocument.createEvent('HTMLEvents');\n    clickEvent.initEvent('click', true, true);\n    return input.dispatchEvent(clickEvent);\n  }\n\n  switch (input.type) {\n    case 'checkbox': {\n      if (input.getAttribute('readonly'))\n        break;\n\n      const original    = input.checked;\n      input.checked     = !original;\n      const checkResult = click();\n      if (checkResult === false)\n        input.checked = original;\n      break;\n    }\n\n    case 'radio': {\n      if (input.getAttribute('readonly'))\n        break;\n\n      if (input.checked)\n        click();\n      else {\n        const radios = input.ownerDocument.querySelectorAll(`input[type=radio][name='${this.getAttribute('name')}']`);\n        const checked = Array.from(radios)\n          .filter(radio   => radio.checked && radio.form === this.form )\n          .map(radio  => {\n            radio.checked = false;\n          })[0];\n\n        input.checked = true;\n        const radioResult = click();\n        if (radioResult === false) {\n          input.checked = false;\n          Array.from(radios)\n            .filter(radio   => radio.form === input.form )\n            .forEach(radio  => {\n              radio.checked = (radio === checked);\n            });\n        }\n      }\n      break;\n    }\n\n    default: {\n      click();\n      break;\n    }\n  }\n};\n\n\n// Default behavior for form BUTTON: submit form.\nDOM.HTMLButtonElement.prototype._eventDefaults.click = function(event) {\n  const button = event.target;\n  if (button.getAttribute('disabled'))\n    return false;\n\n  const form = button.form;\n  if (form)\n    return form._dispatchSubmitEvent(button);\n};\n\n"]}