{"record":{"id":"0641b20d7143f432","repo":"gildas-lormeau/SingleFile","slug":"first-argument-to-readability-constructor-should-b","errorCode":null,"errorMessage":"First argument to Readability constructor should be a document object.","messagePattern":"First argument to Readability constructor should be a document object\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/readability/Readability.js","lineNumber":33,"sourceCode":" */\r\n\r\n/*\r\n * This code is heavily based on Arc90's readability.js (1.7.1) script\r\n * available at: http://code.google.com/p/arc90labs-readability\r\n */\r\n\r\n/**\r\n * Public constructor.\r\n * @param {HTMLDocument} doc     The document to parse.\r\n * @param {Object}       options The options object.\r\n */\r\nfunction Readability(doc, options) {\r\n  // In some older versions, people passed a URI as the first argument. Cope:\r\n  if (options && options.documentElement) {\r\n    doc = options;\r\n    options = arguments[2];\r\n  } else if (!doc || !doc.documentElement) {\r\n    throw new Error(\r\n      \"First argument to Readability constructor should be a document object.\"\r\n    );\r\n  }\r\n  options = options || {};\r\n\r\n  this._doc = doc;\r\n  this._docJSDOMParser = this._doc.firstChild.__JSDOMParser__;\r\n  this._articleTitle = null;\r\n  this._articleByline = null;\r\n  this._articleDir = null;\r\n  this._articleSiteName = null;\r\n  this._attempts = [];\r\n  this._metadata = {};\r\n\r\n  // Configurable options\r\n  this._debug = !!options.debug;\r\n  this._maxElemsToParse =\r\n    options.maxElemsToParse || this.DEFAULT_MAX_ELEMS_TO_PARSE;\r","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/gildas-lormeau/SingleFile/blob/517fb7c5cf2096d89933b747e862d8ecf616a9f9/src/lib/readability/Readability.js#L15-L51","documentation":"The Readability constructor requires a parsed DOM Document object (something with a .documentElement property) as its first argument. It throws this error when given null/undefined or any non-document value — most commonly a raw HTML string, which older API versions and tutorials mistakenly showed as acceptable.","triggerScenarios":"new Readability(htmlString) instead of a DOM document; passing null/undefined because the page fetch or DOMParser.parseFromString returned an error document or nothing; passing a JSDOM instance instead of its window.document.","commonSituations":"Following outdated examples that passed an HTML string or URI; mixing up JSDOM object with its .window.document; DOMParser failing silently in non-browser environments; response body not parsed into a document before constructing.","solutions":["Parse the HTML string into a document first: new JSDOM(html).window.document or new DOMParser().parseFromString(html, \"text/html\")","If using JSDOM, pass jsdom.window.document, not the JSDOM instance itself","Check that the fetch/DOM parse step actually produced a document before constructing Readability","If you intentionally passed a URI in options, ensure options.documentElement handling matches the current library version"],"exampleFix":"// before\nconst article = new Readability(htmlString).parse();\n// after\nconst doc = new JSDOM(htmlString).window.document;\nconst article = new Readability(doc).parse();","handlingStrategy":"type-guard","validationCode":"if (!doc || typeof doc !== \"object\" || !doc.documentElement) {\n  throw new Error(\"Readability requires a Document object, not a string\");\n}","typeGuard":"function isDocument(obj) {\n  return obj != null && typeof obj === \"object\" && obj.nodeType === 9 && !!obj.documentElement;\n}\n// usage: if (!isDocument(input)) input = new JSDOM(html).window.document;","tryCatchPattern":"let doc = htmlString ? new JSDOM(htmlString).window.document : maybeDoc;\nif (!isDocument(doc)) throw new Error(\"Could not build a DOM document for Readability\");\ntry {\n  const article = new Readability(doc).parse();\n} catch (e) {\n  if (e.message.includes(\"First argument to Readability\")) throw new Error(\"Input HTML failed to parse into a document\");\n  throw e;\n}","preventionTips":["Always parse HTML strings via JSDOM/DOMParser before Readability","Pass jsdom.window.document, never the JSDOM wrapper","Check DOMParser results for parsererror elements","Pin library version and follow its current constructor docs"],"tags":["readability","type-error","dom"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"517fb7c5cf2096d89933b747e862d8ecf616a9f9","analyzedAt":"2026-09-01T10:05:25.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}