defuddle
Version:
Extract article content and metadata from web pages.
1 lines • 160 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Defuddle=e():t.Defuddle=e()}(Object("undefined"!=typeof self?self:this),(()=>(()=>{"use strict";var t={640(t,e){Object.defineProperty(e,"__esModule",{value:!0}),e.ALLOWED_ATTRIBUTES_DEBUG=e.ALLOWED_ATTRIBUTES=e.ALLOWED_EMPTY_ELEMENTS=e.FOOTNOTE_LIST_SELECTORS=e.FOOTNOTE_INLINE_REFERENCES=e.PARTIAL_SELECTORS=e.TEST_ATTRIBUTES=e.EXACT_SELECTORS=e.INLINE_ELEMENTS=e.PRESERVE_ELEMENTS=e.BLOCK_ELEMENTS_SET=e.BLOCK_ELEMENTS_SELECTOR=e.BLOCK_ELEMENTS=e.MOBILE_WIDTH=e.ENTRY_POINT_ELEMENTS=void 0,e.ENTRY_POINT_ELEMENTS=["#post",".post-content",".article-content","#article-content",".article_post",".article-wrapper",".entry-content",".content-article",".instapaper_body",".post",".markdown-body","article",'[role="article"]',"main",'[role="main"]',"#content","body"],e.MOBILE_WIDTH=600,e.BLOCK_ELEMENTS=["div","section","article","main","aside","header","footer","nav","content"],e.BLOCK_ELEMENTS_SELECTOR=e.BLOCK_ELEMENTS.join(","),e.BLOCK_ELEMENTS_SET=new Set(e.BLOCK_ELEMENTS),e.PRESERVE_ELEMENTS=new Set(["pre","code","table","thead","tbody","tr","td","th","ul","ol","li","dl","dt","dd","figure","figcaption","picture","details","summary","blockquote","form","fieldset"]),e.INLINE_ELEMENTS=new Set(["a","span","strong","em","i","b","u","code","br","small","sub","sup","mark","date","del","ins","q","abbr","cite","relative-time","time","font"]),e.EXACT_SELECTORS=["noscript",'script:not([type^="math/"])',"style","meta","link",'.ad:not([class*="gradient"])','[class^="ad-" i]','[class$="-ad" i]','[id^="ad-" i]','[id$="-ad" i]','[role="banner" i]','[alt*="advert" i]',".promo",".Promo","#barrier-page",".alert",'[id="comments" i]','[id="comment" i]','div[class*="cover-"]','div[id*="cover-"]',"header",".header:not(.banner)","#header","#Header","#banner","#Banner","nav",".navigation","#navigation",'[role="navigation" i]','[role="dialog" i]','[role*="complementary" i]','[class*="pagination" i]',".menu","#siteSub",".previous",".author",".Author",'[class$="_bio"]',"#categories",".contributor",".date","#date","[data-date]",".entry-meta",".meta",".tags","#tags",".toc",".Toc","#toc",".headline","#headline","#title","#Title","#articleTag",'[href*="/tag/"]','[href*="/tags/"]','[href*="/author/"]','[href*="/author?"]','[href$="/author"]','a[href*="copyright.com"]','a[href*="google.com/preferences"]','[href*="#toc"]','[href="#top"]','[href="#Top"]','[href="#page-header"]','[href="#content"]','[href="#site-content"]','[href="#main-content"]','[href^="#main"]','[src*="author"]',"footer",".aside",'aside:not([class*="callout"])',"button","canvas","date","dialog","fieldset","form",'input:not([type="checkbox"])',"label","option","select",'[role="listbox"]','[role="option"]',"textarea","[hidden]",'[aria-hidden="true"]:not([class*="math"])','[style*="display: none"]:not([class*="math"])','[style*="display:none"]:not([class*="math"])','[style*="visibility: hidden"]','[style*="visibility:hidden"]',".hidden",".invisible","instaread-player",'iframe:not([src*="youtube"]):not([src*="youtu.be"]):not([src*="vimeo"]):not([src*="twitter"]):not([src*="x.com"]):not([src*="datawrapper"])','[class="logo" i]',"#logo","#Logo","#newsletter","#Newsletter",".subscribe",".noprint",'[data-print-layout="hide" i]','[data-block="donotprint" i]','[class*="clickable-icon" i]','li span[class*="ltx_tag" i][class*="ltx_tag_item" i]','a[href^="#"][class*="anchor" i]','a[href^="#"][class*="ref" i]:not(.ltx_ref)','[data-container*="most-viewed" i]',".sidebar",".Sidebar","#sidebar","#Sidebar","#side-bar","#sitesub",'[data-link-name*="skip" i]','[aria-label*="skip" i]',".copyright","#copyright",".licensebox","#page-info","#rss","#feed",".gutter","#primaryaudio","#NYT_ABOVE_MAIN_CONTENT_REGION",'[data-testid="photoviewer-children-figure"] > span',"table.infobox",'[data-optimizely="related-articles-section" i]','[data-orientation="vertical"]',".gh-header-sticky",'[data-testid="issue-metadata-sticky"]'],e.TEST_ATTRIBUTES=["class","id","data-test","data-testid","data-test-id","data-qa","data-cy"],e.PARTIAL_SELECTORS=["a-statement","access-wall","activitypub","actioncall","addcomment","addtoany","advert","adlayout","ad-tldr","ad-placement","ads-container","_ad_","AdBlock_","AdUnit","after_content","after_main_article","afterpost","allterms","-alert-","alert-box","_archive","around-the-web","aroundpages","article-author","article-badges","article-banner","article-bottom-section","article-bottom","article-category","article-card","article-citation","article__copy","article_date","article-date","article-end ","article_header","article-header","article__header","article__hero","article__info","article-info","article-meta","article_meta","article__meta","articlename","article-subject","article_subject","article-snippet","article-separator","article--share","article--topics","articletags","article-tags","article_tags","articletitle","article-title","article_title","articletopics","article-topics","article-actions","article--lede","articlewell","associated-people","audio-card","author-bio","author-box","author-info","author_info","authorm","author-mini-bio","author-name","author-publish-info","authored-by","avatar","back-to-top","backlink_container","backlinks-section","bio-block","biobox","blog-pager","bookmark-","-bookmark","bottominfo","bottomnav","bottom-of-article","bottom-wrapper","brand-bar","bcrumb","breadcrumb","brdcrumb","button-wrapper","buttons-container","btn-","-btn","byline","captcha","card-text","card-media","card-post","carouselcontainer","carousel-container","cat_header","catlinks","_categories","card-author","card-content","chapter-list","collections","comments","commentbox","comment-button","commentcomp","comment-content","comment-count","comment-form","comment-number","comment-respond","comment-thread","comment-wrap","complementary","consent","contact-","content-card","content-topics","contentpromo","context-bar","context-widget","core-collateral","cover-image","cover-photo","cover-wrap","created-date","creative-commons_","c-subscribe","_cta","-cta","cta-","cta_","current-issue","custom-list-number","dateline","dateheader","date-header","date-pub","disclaimer","disclosure","discussion","discuss_","disqus","donate","donation","dropdown","element-invisible","eletters","emailsignup","emoji-bar","engagement-widget","enhancement-","entry-author-info","entry-categories","entry-date","entry-title","entry-utility","-error","error-","eyebrow","expand-reduce","external-anchor","externallinkembedwrapper","extra-services","extra-title","facebook","fancy-box","favorite","featured-content","feature_feed","feedback","feed-links","field-site-sections","fixheader","floating-vid","follower","footer","footnote-back","footnoteback","form-group","for-you","frontmatter","further-reading","fullbleedheader","gallery-count","gated-","gh-feed","gist-meta","goog-","graph-view","hamburger","header_logo","header-logo","header-pattern","hero-list","hide-for-print","hide-print","hide-when-no-script","hidden-print","hidden-sidenote","hidden-accessibility","infoline","inline-topic","instacartIntegration","interlude","interaction","itemendrow","intro-date","invisible","jp-no-solution","jp-relatedposts","jswarning","js-warning","jumplink","jumpto","jump-to-","js-skip-to-content","keepreading","keep-reading","keep_reading","keyword_wrap","kicker","labstab","-labels","language-name","lastupdated","latest-content","-ledes-","-license","license-","lightbox-popup","like-button","link-box","links-grid","links-title","listing-dynamic-terms","list-tags","listinks","loading","loa-info","logo_container","ltx_role_refnum","ltx_tag_bibitem","ltx_error","masthead","marketing","media-inquiry","-menu","menu-","metadata","meta-date","meta-row","might-like","minibio","more-about","mod-paywall","_modal","-modal","more-","morenews","morestories","more_wrapper","most-read","move-helper","mw-editsection","mw-cite-backlink","mw-indicators","mw-jump-link","nav-","nav_","navigation-post","next-","newsgallery","news-story-title","newsletter_","newsletterbanner","newslettercontainer","newsletter-form","newsletter-signup","newslettersignup","newsletterwidget","newsletterwrapper","not-found","notessection","nomobile","noprint","open-slideshow","originally-published","other-blogs","outline-view","pagehead","page-header","page-title","paywall_message","-partners","permission-","plea","popular","popup_links","pop_stories","pop-up","post-author","post-bottom","post__category","postcomment","postdate","post-date","post_date","post-details","post-feeds","postinfo","post-info","post_info","post-inline-date","post-links","postlist","post_list","post_meta","post-meta","postmeta","post_more","postnavi","post-navigation","postpath","post-preview","postsnippet","post_snippet","post-snippet","post-subject","posttax","post-tax","post_tax","posttag","post_tag","post-tag","post_time","posttitle","post-title","post_title","post__title","post-ufi-button","prev-post","prevnext","prev_next","prev-next","previousnext","press-inquiries","print-none","print-header","print:hidden","privacy-notice","privacy-settings","profile","promo_article","promo-bar","promo-box","pubdate","pub_date","pub-date","publish_date","publish-date","publication-date","publicationName","qr-code","qr_code","quick_up","_rail","ratingssection","read_also","readmore","read-next","read_next","read_time","read-time","reading_time","reading-time","reading-list","recent-","recent-articles","recentpost","recent_post","recent-post","recommend","redirectedfrom","recirc","register","related","relevant","reversefootnote","robots-nocontent","_rss","rss-link","screen-reader-text","scroll_to","scroll-to","_search","-search","section-nav","series-banner","share-box","sharedaddy","share-icons","sharelinks","share-post","share-print","share-section","shariff-","show-for-print","sidebartitle","sidebar-content","sidebar-wrapper","sideitems","sidebar-author","sidebar-item","side-box","side-logo","sign-in-gate","similar-","similar_","similars-","site-index","site-header","siteheader","site-logo","site-name","site-wordpress","skip-content","skip-to-content","skip-link","c-skip-link","_skip-link","-slider","slug-wrap","social-author","social-shar","social-date","speechify-ignore","speedbump","sponsor","springercitation","sr-only","_stats","story-date","story-navigation","storyreadtime","storysmall","storypublishdate","subject-label","subhead","submenu","-subscribe-","subscriber-drive","subscription-","_tags","tags__item","tag_list","taxonomy","table-of-contents","tabs-","terminaltout","time-rubric","timestamp","time-read","time-to-read","tip_off","tiptout","-tout-","toc-container","toggle-caption","tooltip-content","topbar","topic-authors","topic-footer","topic-list","topic-subnav","top-wrapper","tree-item","trending","trust-feat","trust-badge","trust-project","twitter","u-hide","upsell","viewbottom","yarpp-related","visually-hidden","welcomebox","widget_pages"],e.FOOTNOTE_INLINE_REFERENCES=["sup.reference","cite.ltx_cite",'sup[id^="fnr"]','span[id^="fnr"]','span[class*="footnote_ref"]','span[class*="footnote-ref"]',"span.footnote-link","a.citation",'a[id^="ref-link"]','a[href^="#fn"]','a[href^="#cite"]','a[href^="#reference"]','a[href^="#footnote"]','a[href^="#r"]','a[href^="#b"]','a[href*="cite_note"]','a[href*="cite_ref"]',"a.footnote-anchor","span.footnote-hovercard-target a",'a[role="doc-biblioref"]','a[id^="fnref"]','a[id^="ref-link"]',"sup.footnoteref"].join(","),e.FOOTNOTE_LIST_SELECTORS=["div.footnote ol","div.footnotes ol",'div[role="doc-endnotes"]','div[role="doc-footnotes"]',"ol.footnotes-list","ol.footnotes","ol.references",'ol[class*="article-references"]',"section.footnotes ol",'section[role="doc-endnotes"]','section[role="doc-footnotes"]','section[role="doc-bibliography"]',"ul.footnotes-list","ul.ltx_biblist",'div.footnote[data-component-name="FootnoteToDOM"]',"div.footnotes-footer"].join(","),e.ALLOWED_EMPTY_ELEMENTS=new Set(["area","audio","base","br","circle","col","defs","ellipse","embed","figure","g","hr","iframe","img","input","line","link","mask","meta","object","param","path","pattern","picture","polygon","polyline","rect","source","stop","svg","td","th","track","use","video","wbr"]),e.ALLOWED_ATTRIBUTES=new Set(["alt","allow","allowfullscreen","aria-label","checked","colspan","controls","data-latex","data-src","data-srcset","data-callout","data-lang","dir","display","frameborder","headers","height","href","kind","label","lang","role","rowspan","src","srclang","srcset","title","type","width","accent","accentunder","align","columnalign","columnlines","columnspacing","columnspan","data-mjx-texclass","depth","displaystyle","fence","frame","framespacing","linethickness","lspace","mathsize","mathvariant","maxsize","minsize","movablelimits","notation","rowalign","rowlines","rowspacing","rowspan","rspace","scriptlevel","separator","stretchy","symmetric","voffset","xmlns"]),e.ALLOWED_ATTRIBUTES_DEBUG=new Set(["class","id"])},628(t,e,r){var n=this&&this.__awaiter||function(t,e,r,n){return new(r||(r=Promise))((function(o,i){function s(t){try{l(n.next(t))}catch(t){i(t)}}function a(t){try{l(n.throw(t))}catch(t){i(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,a)}l((n=n.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.Defuddle=void 0;const o=r(608),i=r(917),s=r(640),a=r(840),l=r(610),c=r(968),u=r(552),d=r(639),h=new Set(["title","author","published","site","description","image","language"]),m=/(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+\d{1,2}/i,p=/\d+\s*min(?:ute)?s?\s+read\b/i,f=[/^This (?:article|story|piece) (?:appeared|was published|originally appeared) in\b/i,/^A version of this (?:article|story) (?:appeared|was published) in\b/i,/^Originally (?:published|appeared) (?:in|on|at)\b/i],g=[/\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:t(?:ember)?)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\b/gi,/\b\d+(?:st|nd|rd|th)?\b/g,/\bmin(?:ute)?s?\b/gi,/\bread\b/gi,/[|\xb7\u2022\u2014\u2013\-,.\s]/g];e.Defuddle=class{constructor(t,e={}){this._schemaOrgData=void 0,this._schemaOrgExtracted=!1,this.doc=t,this.options=e,this.debug=e.debug||!1}getSchemaOrgData(){return this._schemaOrgExtracted||(this._schemaOrgData=this._extractSchemaOrgData(this.doc),this._schemaOrgExtracted=!0),this._schemaOrgData}parse(){let t=this.parseInternal();if(t.wordCount<200){this._log("Initial parse returned very little content, trying again");const e=this.parseInternal({removePartialSelectors:!1});e.wordCount>2*t.wordCount&&(this._log("Retry produced more content"),t=e)}if(t.wordCount<50){this._log("Still very little content, retrying without scoring/partial selectors (possible index page)");const e=this.parseInternal({removeLowScoring:!1,removePartialSelectors:!1,removeContentPatterns:!1});e.wordCount>t.wordCount&&(this._log("Index page retry produced more content"),t=e)}this._stripUnsafeElements();const e=this._getSchemaText(t.schemaOrgData);if(e&&this.countWords(e)>t.wordCount){const r=this._findContentBySchemaText(e);r?(this._log("Found DOM content matching schema.org text"),t.content=r,t.wordCount=this.countWords(r)):(this._log("Using schema.org text as content (DOM element not found)"),t.content=e,t.wordCount=this.countWords(e))}return t}_getSchemaText(t){if(!t)return"";const e=Array.isArray(t)?t:[t];for(const t of e){if((null==t?void 0:t.text)&&"string"==typeof t.text)return t.text;if((null==t?void 0:t.articleBody)&&"string"==typeof t.articleBody)return t.articleBody}return""}_stripUnsafeElements(){const t=this.doc.body;if(!t)return;const e=t.querySelectorAll('script:not([type^="math/"]), style, noscript, frame, frameset, object, embed, applet, base');for(const t of e)t.remove();const r=t.querySelectorAll("*");for(const t of r)for(const e of Array.from(t.attributes)){const r=e.name.toLowerCase();(r.startsWith("on")||"srcdoc"===r||["href","src","action","formaction","xlink:href"].includes(r)&&(0,d.isDangerousUrl)(e.value))&&t.removeAttribute(e.name)}}_findContentBySchemaText(t){var e;const r=this.doc.body;if(!r)return"";const n=((null===(e=t.split(/\n\s*\n/)[0])||void 0===e?void 0:e.trim())||"").substring(0,100).trim();if(!n)return"";const o=this.countWords(t);let i=null,s=1/0;const a=r.querySelectorAll("*");for(const t of a){const e=t.textContent||"";if(!e.includes(n))continue;const r=e.trim().split(/\s+/).length;r>=.8*o&&r<s&&(s=r,i=t)}if(!i)return"";let l="",c="";const u=i.parentElement;if(u&&u!==r){const t=u.querySelectorAll("img");let e=null,r=0;for(const n of t){if(i.contains(n))continue;const t=parseInt(n.getAttribute("width")||"0",10)*parseInt(n.getAttribute("height")||"0",10);t>r&&(r=t,e=n)}if(e){l=this._getLargestImageSrc(e),c=e.getAttribute("alt")||"";try{const t=this.options.url||this.doc.URL;t&&(l=new URL(l,t).href)}catch(t){}}}this.resolveRelativeUrls(i);let h=(0,d.serializeHTML)(i);if(l){const t=this.doc.createElement("img");t.setAttribute("src",l),t.setAttribute("alt",c),h+=t.outerHTML}return h}_getLargestImageSrc(t){const e=t.getAttribute("srcset")||"";if(!e)return t.getAttribute("src")||"";const r=/(.+?)\s+(\d+(?:\.\d+)?)w/g;let n,o="",i=0,s=0;for(;null!==(n=r.exec(e));){let t=n[1].trim();s>0&&(t=t.replace(/^,\s*/,"")),s=r.lastIndex;const e=parseFloat(n[2]);t&&e>i&&(i=e,o=t)}let a=o||t.getAttribute("src")||"";return a=a.replace(/,w_\d+/g,"").replace(/,c_\w+/g,""),a}parseAsync(){return n(this,void 0,void 0,(function*(){var t;if(!1!==this.options.useAsync){const t=yield this.tryAsyncExtractor(i.ExtractorRegistry.findPreferredAsyncExtractor.bind(i.ExtractorRegistry));if(t)return t}const e=this.parse();return e.wordCount>0||!1===this.options.useAsync?e:null!==(t=yield this.tryAsyncExtractor(i.ExtractorRegistry.findAsyncExtractor.bind(i.ExtractorRegistry)))&&void 0!==t?t:e}))}fetchAsyncVariables(){return n(this,void 0,void 0,(function*(){if(!1===this.options.useAsync)return null;try{const t=this.options.url||this.doc.URL,e=this.getSchemaOrgData(),r=i.ExtractorRegistry.findPreferredAsyncExtractor(this.doc,t,e);if(r){const t=yield r.extractAsync();return this.getExtractorVariables(t.variables)||null}}catch(t){console.error("Defuddle","Error fetching async variables:",t)}return null}))}tryAsyncExtractor(t){return n(this,void 0,void 0,(function*(){try{const e=this.options.url||this.doc.URL,r=this.getSchemaOrgData(),n=t(this.doc,e,r);if(n){const t=Date.now(),e=yield n.extractAsync(),i=this._collectMetaTags(),s=o.MetadataExtractor.extract(this.doc,r,i);return this.buildExtractorResponse(e,s,t,n,i)}}catch(t){console.error("Defuddle","Error in async extraction:",t)}return null}))}parseInternal(t={}){const e=Date.now(),r=Object.assign(Object.assign({removeExactSelectors:!0,removePartialSelectors:!0,removeHiddenElements:!0,removeLowScoring:!0,removeSmallImages:!0,removeContentPatterns:!0,standardize:!0},this.options),t),n=[],s=this.getSchemaOrgData();this._metaTags||(this._metaTags=this._collectMetaTags());const u=this._metaTags;this._metadata||(this._metadata=o.MetadataExtractor.extract(this.doc,s,u));const h=this._metadata;r.removeImages&&this.removeImages(this.doc);try{const t=r.url||this.doc.URL,o=i.ExtractorRegistry.findExtractor(this.doc,t,s);if(o&&o.canExtract()){const t=o.extract();return this.buildExtractorResponse(t,h,e,o,u)}this._mobileStyles||(this._mobileStyles=this._evaluateMediaQueries(this.doc));const m=this._mobileStyles;this._smallImages||(this._smallImages=this.findSmallImages(this.doc));const p=this._smallImages,f=this.doc.cloneNode(!0);this.flattenShadowRoots(this.doc,f),this.resolveStreamedContent(f),this.applyMobileStyles(f,m);let g=null;if(r.contentSelector&&(g=f.querySelector(r.contentSelector),this._log("Using contentSelector:",r.contentSelector,g?"found":"not found")),g||(g=this.findMainContent(f)),!g){const t=this.resolveContentUrls((0,d.serializeHTML)(this.doc.body)),r=Date.now();return Object.assign(Object.assign({content:t},h),{wordCount:this.countWords(t),parseTime:Math.round(r-e),metaTags:u})}r.standardize&&(0,l.standardizeFootnotes)(g),r.removeSmallImages&&this.removeSmallImages(f,p),r.removeHiddenElements&&this.removeHiddenElements(f,n),r.removeLowScoring&&c.ContentScorer.scoreAndRemove(f,this.debug,n,g),(r.removeExactSelectors||r.removePartialSelectors)&&this.removeBySelector(f,r.removeExactSelectors,r.removePartialSelectors,g,n),r.removeContentPatterns&&g&&this.removeByContentPattern(g,this.debug?n:void 0),r.standardize&&(0,a.standardizeContent)(g,h,this.doc,this.debug),this.resolveRelativeUrls(g);const v=g.outerHTML,y=Date.now(),b=Object.assign(Object.assign({content:v},h),{wordCount:this.countWords(v),parseTime:Math.round(y-e),metaTags:u});return this.debug&&(b.debug={contentSelector:this.getElementSelector(g),removals:n}),b}catch(t){console.error("Defuddle","Error processing document:",t);const r=this.resolveContentUrls((0,d.serializeHTML)(this.doc.body)),n=Date.now();return Object.assign(Object.assign({content:r},h),{wordCount:this.countWords(r),parseTime:Math.round(n-e),metaTags:u})}}countWords(t){const e=t.replace(/<[^>]*>/g," ").replace(/ /gi," ").replace(/&/gi,"&").replace(/</gi,"<").replace(/>/gi,">").replace(/"/gi,'"').replace(/&#\d+;/g," ").replace(/&\w+;/g," ").trim();if(!e)return 0;let r=0,n=!1;for(let t=0;t<e.length;t++){const o=e.charCodeAt(t)<=32;o||n?o&&(n=!1):(r++,n=!0)}return r}_log(...t){this.debug&&console.log("Defuddle:",...t)}_evaluateMediaQueries(t){const e=[],r=/max-width[^:]*:\s*(\d+)/;try{const n=Array.from(t.styleSheets).filter((t=>{try{return t.cssRules,!0}catch(t){return t instanceof DOMException&&t.name,!1}}));n.flatMap((t=>{try{return"undefined"==typeof CSSMediaRule?[]:Array.from(t.cssRules).filter((t=>t instanceof CSSMediaRule&&t.conditionText.includes("max-width")))}catch(t){return this.debug&&console.warn("Defuddle: Failed to process stylesheet:",t),[]}})).forEach((t=>{const n=t.conditionText.match(r);if(n){const r=parseInt(n[1]);if(s.MOBILE_WIDTH<=r){Array.from(t.cssRules).filter((t=>t instanceof CSSStyleRule)).forEach((t=>{try{e.push({selector:t.selectorText,styles:t.style.cssText})}catch(t){this.debug&&console.warn("Defuddle: Failed to process CSS rule:",t)}}))}}}))}catch(t){console.error("Defuddle: Error evaluating media queries:",t)}return e}applyMobileStyles(t,e){e.forEach((({selector:e,styles:r})=>{try{t.querySelectorAll(e).forEach((t=>{t.setAttribute("style",(t.getAttribute("style")||"")+r)}))}catch(t){console.error("Defuddle","Error applying styles for selector:",e,t)}}))}removeImages(t){const e=t.getElementsByTagName("img");Array.from(e).forEach((t=>{t.remove()}))}removeHiddenElements(t,e){let r=0;const n=new Map,o=/(?:^|;\s*)(?:display\s*:\s*none|visibility\s*:\s*hidden|opacity\s*:\s*0)(?:\s*;|\s*$)/i,i=t.defaultView,s="undefined"!=typeof window&&i===window,a=t.querySelectorAll("*");for(const t of a){if(t.querySelector("math, [data-mathml], .katex-mathml")||"math"===t.tagName.toLowerCase())continue;const e=t.getAttribute("style");if(e&&o.test(e)){const o=e.includes("display")?"display:none":e.includes("visibility")?"visibility:hidden":"opacity:0";n.set(t,o),r++;continue}if(s)try{const e=i.getComputedStyle(t);let o="";if("none"===e.display?o="display:none":"hidden"===e.visibility?o="visibility:hidden":"0"===e.opacity&&(o="opacity:0"),o){n.set(t,o),r++;continue}}catch(t){}const a=t.getAttribute("class")||"";if(a){const e=a.split(/\s+/);for(const o of e)if("hidden"===o||o.endsWith(":hidden")){n.set(t,`class:${o}`),r++;break}}}n.forEach(((t,r)=>{this.debug&&e&&e.push({step:"removeHiddenElements",reason:t,text:(0,u.textPreview)(r)}),r.remove()})),this._log("Removed hidden elements:",r)}removeBySelector(t,e=!0,r=!0,n,o){const i=Date.now();let a=0,l=0;const c=new Map;if(e){t.querySelectorAll(s.EXACT_SELECTORS.join(",")).forEach((t=>{if(null==t?void 0:t.parentNode){if(t.closest("pre, code"))return;c.set(t,{type:"exact"}),a++}}))}if(r){const e=s.PARTIAL_SELECTORS.join("|"),r=new RegExp(e,"i"),n=this.debug?s.PARTIAL_SELECTORS.map((t=>({pattern:t,regex:new RegExp(t,"i")}))):null,o=s.TEST_ATTRIBUTES.map((t=>`[${t}]`)).join(",");t.querySelectorAll(o).forEach((t=>{var e;if(c.has(t))return;const o=t.tagName;if("CODE"===o||"PRE"===o||t.querySelector("pre"))return;const i=s.TEST_ATTRIBUTES.map((e=>"class"===e?t.className&&"string"==typeof t.className?t.className:"":"id"===e?t.id||"":t.getAttribute(e)||"")).join(" ").toLowerCase();if(i.trim()&&r.test(i)){const r=n?null===(e=n.find((t=>t.regex.test(i))))||void 0===e?void 0:e.pattern:void 0;c.set(t,{type:"partial",selector:r}),l++}}))}c.forEach((({type:t,selector:e},r)=>{if(!(n&&r.contains(n)||"A"===r.tagName&&r.closest("h1, h2, h3, h4, h5, h6"))){try{if(r.matches(s.FOOTNOTE_LIST_SELECTORS)||r.querySelector(s.FOOTNOTE_LIST_SELECTORS))return;const t=r.parentElement;if(t&&t.matches(s.FOOTNOTE_LIST_SELECTORS))return}catch(t){}this.debug&&o&&o.push({step:"removeBySelector",selector:"exact"===t?"exact":e,reason:"exact"===t?"exact selector match":`partial match: ${e}`,text:(0,u.textPreview)(r)}),r.remove()}}));const d=Date.now();this._log("Removed clutter elements:",{exactSelectors:a,partialSelectors:l,total:c.size,processingTime:`${(d-i).toFixed(2)}ms`})}findSmallImages(t){var e,r;const n=new Set;let o=0;const i=t.querySelectorAll("img, svg"),s=t.defaultView,a="undefined"!=typeof window&&s===window;for(const t of i){const i=parseInt(t.getAttribute("width")||"0"),l=parseInt(t.getAttribute("height")||"0"),c=t.getAttribute("style")||"",u=parseInt((null===(e=c.match(/width\s*:\s*(\d+)/))||void 0===e?void 0:e[1])||"0"),d=parseInt((null===(r=c.match(/height\s*:\s*(\d+)/))||void 0===r?void 0:r[1])||"0");let h=0,m=0;if(a){try{const e=s.getComputedStyle(t);h=parseInt(e.width)||0,m=parseInt(e.height)||0}catch(t){}try{const e=t.getBoundingClientRect();e.width>0&&(h=h||e.width),e.height>0&&(m=m||e.height)}catch(t){}}const p=[i,u,h].filter((t=>t>0)),f=[l,d,m].filter((t=>t>0));if(p.length>0&&f.length>0){const e=Math.min(...p),r=Math.min(...f);if(e<33||r<33){const e=this.getElementIdentifier(t);e&&(n.add(e),o++)}}}return this._log("Found small elements:",o),n}removeSmallImages(t,e){let r=0;["img","svg"].forEach((n=>{const o=t.getElementsByTagName(n);Array.from(o).forEach((t=>{const n=this.getElementIdentifier(t);n&&e.has(n)&&(t.remove(),r++)}))})),this._log("Removed small elements:",r)}getElementIdentifier(t){if("img"===t.tagName.toLowerCase()){const e=t.getAttribute("data-src");if(e)return`src:${e}`;const r=t.getAttribute("src")||"",n=t.getAttribute("srcset")||"",o=t.getAttribute("data-srcset");if(r)return`src:${r}`;if(n)return`srcset:${n}`;if(o)return`srcset:${o}`}const e=t.id||"",r=t.className||"",n="svg"===t.tagName.toLowerCase()&&t.getAttribute("viewBox")||"";return e?`id:${e}`:n?`viewBox:${n}`:r?`class:${r}`:null}findMainContent(t){const e=[];if(s.ENTRY_POINT_ELEMENTS.forEach(((r,n)=>{t.querySelectorAll(r).forEach((t=>{let r=40*(s.ENTRY_POINT_ELEMENTS.length-n);r+=c.ContentScorer.scoreElement(t),e.push({element:t,score:r,selectorIndex:n})}))})),0===e.length)return this.findContentByScoring(t);if(e.sort(((t,e)=>e.score-t.score)),this.debug&&this._log("Content candidates:",e.map((t=>({element:t.element.tagName,selector:this.getElementSelector(t.element),score:t.score})))),1===e.length&&"body"===e[0].element.tagName.toLowerCase()){const e=this.findTableBasedContent(t);if(e)return e}const r=e[0];let n=r;for(let t=1;t<e.length;t++){const o=e[t],i=(o.element.textContent||"").split(/\s+/).length;if(o.selectorIndex<n.selectorIndex&&n.element.contains(o.element)&&i>50){let t=0;for(const n of e)if(n.selectorIndex===o.selectorIndex&&r.element.contains(n.element)&&++t>1)break;if(t>1)continue;n=o}}return n!==r?n.element:r.element}findTableBasedContent(t){if(!Array.from(t.getElementsByTagName("table")).some((t=>{var e;const r=parseInt(t.getAttribute("width")||"0"),n=this.getComputedStyle(t);return r>400||(null===(e=null==n?void 0:n.width)||void 0===e?void 0:e.includes("px"))&&parseInt(n.width)>400||"center"===t.getAttribute("align")||(t.className||"").toLowerCase().includes("content")||(t.className||"").toLowerCase().includes("article")})))return null;const e=Array.from(t.getElementsByTagName("td"));return c.ContentScorer.findBestElement(e)}findContentByScoring(t){const e=[];return t.querySelectorAll(s.BLOCK_ELEMENTS_SELECTOR).forEach((t=>{const r=c.ContentScorer.scoreElement(t);r>0&&e.push({score:r,element:t})})),e.length>0?e.sort(((t,e)=>e.score-t.score))[0].element:null}getElementSelector(t){const e=[];let r=t;for(;r&&r!==this.doc.documentElement;){let t=r.tagName.toLowerCase();r.id?t+="#"+r.id:r.className&&"string"==typeof r.className&&(t+="."+r.className.trim().split(/\s+/).join(".")),e.unshift(t),r=r.parentElement}return e.join(" > ")}getComputedStyle(t){return(0,u.getComputedStyle)(t)}resolveRelativeUrls(t){const e=this.options.url||this.doc.URL;if(!e)return;const r=t=>{try{return new URL(t,e).href}catch(e){return t}};t.querySelectorAll("[href]").forEach((t=>{const e=t.getAttribute("href");e&&t.setAttribute("href",r(e))})),t.querySelectorAll("[src]").forEach((t=>{const e=t.getAttribute("src");e&&t.setAttribute("src",r(e))})),t.querySelectorAll("[srcset]").forEach((t=>{const e=t.getAttribute("srcset");if(e){const n=/(.+?)\s+(\d+(?:\.\d+)?[wx])/g,o=[];let i,s=0;for(;null!==(i=n.exec(e));){let t=i[1].trim();s>0&&(t=t.replace(/^,\s*/,"")),s=n.lastIndex,o.push(`${r(t)} ${i[2]}`)}if(o.length>0)t.setAttribute("srcset",o.join(", "));else{const n=e.split(",").map((t=>{const e=t.trim().split(/\s+/);return e[0]&&(e[0]=r(e[0])),e.join(" ")})).join(", ");t.setAttribute("srcset",n)}}})),t.querySelectorAll("[poster]").forEach((t=>{const e=t.getAttribute("poster");e&&t.setAttribute("poster",r(e))}))}flattenShadowRoots(t,e){var r,n,o;const i=Array.from(t.body.querySelectorAll("*")),s=i.find((t=>t.shadowRoot));if(!s)return;const a=Array.from(e.body.querySelectorAll("*"));if((null!==(o=null===(n=null===(r=s.shadowRoot)||void 0===r?void 0:r.childNodes)||void 0===n?void 0:n.length)&&void 0!==o?o:0)>0)for(let t=i.length-1;t>=0;t--){const r=i[t];if(!r.shadowRoot)continue;const n=a[t];if(!n)continue;const o=r.shadowRoot.innerHTML;o.length>0&&this.replaceShadowHost(n,o,e)}else{const t=[];for(let e=0;e<i.length;e++){const r=i[e],n=r.getAttribute("data-defuddle-shadow");if(!n)continue;const o=a[e];o&&(t.push({cloneEl:o,html:n}),r.removeAttribute("data-defuddle-shadow"),o.removeAttribute("data-defuddle-shadow"))}for(const{cloneEl:r,html:n}of t)this.replaceShadowHost(r,n,e)}}resolveStreamedContent(t){const e=t.querySelectorAll("script"),r=[],n=/\$RC\("(B:\d+)","(S:\d+)"\)/g;for(const t of e){const e=t.textContent||"";if(!e.includes("$RC("))continue;let o;for(n.lastIndex=0;null!==(o=n.exec(e));)r.push({templateId:o[1],contentId:o[2]})}if(0===r.length)return;let o=0;for(const{templateId:e,contentId:n}of r){const r=t.getElementById(e),i=t.getElementById(n);if(!r||!i)continue;const s=r.parentNode;if(!s)continue;let a=r.nextSibling,l=!1;for(;a;){const t=a.nextSibling;if(8===a.nodeType&&"/$"===a.data){a.remove(),l=!0;break}a.remove(),a=t}if(l){for(;i.firstChild;)s.insertBefore(i.firstChild,r);r.remove(),i.remove(),o++}}o>0&&this._log("Resolved streamed content:",o,"suspense boundaries")}replaceShadowHost(t,e,r){var n;const o=(0,d.parseHTML)(r,e);if(t.tagName.includes("-")){const e=r.createElement("div");e.appendChild(o),null===(n=t.parentNode)||void 0===n||n.replaceChild(e,t)}else t.textContent="",t.appendChild(o)}resolveContentUrls(t){if(!(this.options.url||this.doc.URL))return t;const e=this.doc.createElement("div");return e.appendChild((0,d.parseHTML)(this.doc,t)),this.resolveRelativeUrls(e),(0,d.serializeHTML)(e)}_extractSchemaOrgData(t){const e=t.querySelectorAll('script[type="application/ld+json"]'),r=[];e.forEach((t=>{let e=t.textContent||"";try{e=e.replace(/\/\*[\s\S]*?\*\/|^\s*\/\/.*$/gm,"").replace(/^\s*<!\[CDATA\[([\s\S]*?)\]\]>\s*$/,"$1").replace(/^\s*(\*\/|\/\*)\s*|\s*(\*\/|\/\*)\s*$/g,"").trim();const t=JSON.parse(e);t["@graph"]&&Array.isArray(t["@graph"])?r.push(...t["@graph"]):r.push(t)}catch(t){console.error("Defuddle: Error parsing schema.org data:",t),this.debug&&console.error("Defuddle: Problematic JSON content:",e)}}));const n=t=>{if("string"==typeof t)return this._decodeHTMLEntities(t);if(Array.isArray(t))return t.map(n);if("object"==typeof t&&null!==t){const e={};for(const r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=n(t[r]));return e}return t};return r.map(n)}_collectMetaTags(){const t=[];return this.doc.querySelectorAll("meta").forEach((e=>{const r=e.getAttribute("name"),n=e.getAttribute("property");let o=e.getAttribute("content");o&&t.push({name:r,property:n,content:this._decodeHTMLEntities(o)})})),t}_decodeHTMLEntities(t){return(0,d.decodeHTMLEntities)(this.doc,t)}buildExtractorResponse(t,e,r,n,o){var i,s,a,l,c;const u=this.resolveContentUrls(t.contentHtml),d=this.getExtractorVariables(t.variables);return Object.assign({content:u,title:(null===(i=t.variables)||void 0===i?void 0:i.title)||e.title,description:e.description,domain:e.domain,favicon:e.favicon,image:e.image,language:(null===(s=t.variables)||void 0===s?void 0:s.language)||e.language,published:(null===(a=t.variables)||void 0===a?void 0:a.published)||e.published,author:(null===(l=t.variables)||void 0===l?void 0:l.author)||e.author,site:(null===(c=t.variables)||void 0===c?void 0:c.site)||e.site,schemaOrgData:e.schemaOrgData,wordCount:this.countWords(t.contentHtml),parseTime:Math.round(Date.now()-r),extractorType:n.constructor.name.replace("Extractor","").toLowerCase(),metaTags:o},d?{variables:d}:{})}getExtractorVariables(t){if(!t)return;const e={};let r=!1;for(const[n,o]of Object.entries(t))h.has(n)||(e[n]=o,r=!0);return r?e:void 0}removeByContentPattern(t,e){var r,n,o,i,s,a;const l=Array.from(t.querySelectorAll("p, span, div, time"));for(const t of l){if(!t.parentNode)continue;if(t.closest("pre")||t.closest("code"))continue;const n=(null===(r=t.textContent)||void 0===r?void 0:r.trim())||"";if(n.split(/\s+/).length<=15&&m.test(n)&&p.test(n)&&0===t.querySelectorAll("p, div, section, article").length){let r=n;for(const t of g)r=r.replace(t,"");if(r.trim().length>0)continue;this.debug&&e&&e.push({step:"removeByContentPattern",reason:"read time metadata",text:(0,u.textPreview)(t)}),t.remove()}}const c=Array.from(t.querySelectorAll("time")),d=t.textContent||"";for(const r of c){if(!r.parentNode)continue;let s=r,a=(null===(n=s.textContent)||void 0===n?void 0:n.trim())||"";for(;s.parentElement&&s.parentElement!==t;){const t=s.parentElement.tagName.toLowerCase(),e=(null===(o=s.parentElement.textContent)||void 0===o?void 0:o.trim())||"";if("p"===t&&e===a){s=s.parentElement;break}if(!["i","em","span","b","strong","small"].includes(t)||e!==a)break;s=s.parentElement,a=e}const l=(null===(i=s.textContent)||void 0===i?void 0:i.trim())||"";if(l.split(/\s+/).length>10)continue;const c=d.indexOf(l),h=d.length-(c+l.length);c>200&&h>200||(this.debug&&e&&e.push({step:"removeByContentPattern",reason:"boundary date element",text:(0,u.textPreview)(s)}),s.remove())}const h=this.options.url||this.doc.URL||"";let v="";try{v=new URL(h).pathname}catch(t){}if(v){const r=t.querySelectorAll("div, span, p");for(const t of r){if(!t.parentNode)continue;if(((null===(s=t.textContent)||void 0===s?void 0:s.trim())||"").split(/\s+/).length>10)continue;if(t.querySelectorAll("p, div, section, article").length>0)continue;const r=t.querySelector("a[href]");if(r)try{const n=new URL(r.getAttribute("href")||"",h).pathname;"/"!==n&&n!==v&&v.startsWith(n)&&(this.debug&&e&&e.push({step:"removeByContentPattern",reason:"section breadcrumb",text:(0,u.textPreview)(t)}),t.remove())}catch(t){}}}const y=t.textContent||"",b=t.querySelectorAll("p, div, span, section");for(const r of b){if(!r.parentNode)continue;const n=(null===(a=r.textContent)||void 0===a?void 0:a.trim())||"",o=n.split(/\s+/).length;if(!(o>50||o<3))for(const o of f)if(o.test(n)){let n=r;for(;n.parentElement&&n.parentElement!==t&&!n.nextElementSibling;)n=n.parentElement;const o=n.textContent||"";if(y.indexOf(o)<200)continue;const i=[];let s=n.parentElement;for(;s&&s!==t;)i.push(s),s=s.parentElement;this.removeTrailingSiblings(n,!0,e);for(const t of i)this.removeTrailingSiblings(t,!1,e);return}}}removeTrailingSiblings(t,e,r){let n=t.nextElementSibling;for(;n;){const t=n.nextElementSibling;this.debug&&r&&r.push({step:"removeByContentPattern",reason:"trailing non-content",text:(0,u.textPreview)(n)}),n.remove(),n=t}e&&(this.debug&&r&&r.push({step:"removeByContentPattern",reason:"boilerplate text",text:(0,u.textPreview)(t)}),t.remove())}}},754(t,e,r){Object.defineProperty(e,"__esModule",{value:!0}),e.codeBlockRules=void 0;const n=r(552),o=[/^language-(\w+)$/,/^lang-(\w+)$/,/^(\w+)-code$/,/^code-(\w+)$/,/^syntax-(\w+)$/,/^code-snippet__(\w+)$/,/^highlight-(\w+)$/,/^(\w+)-snippet$/,/(?:^|\s)(?:language|lang|brush|syntax)-(\w+)(?:\s|$)/i],i=new Set(["abap","actionscript","ada","adoc","agda","antlr4","applescript","arduino","armasm","asciidoc","aspnet","atom","bash","batch","c","clojure","cmake","cobol","coffeescript","cpp","c++","crystal","csharp","cs","dart","django","dockerfile","dotnet","elixir","elm","erlang","fortran","fsharp","gdscript","gitignore","glsl","golang","gradle","graphql","groovy","haskell","hs","haxe","hlsl","html","idris","java","javascript","js","jsx","jsdoc","json","jsonp","julia","kotlin","latex","lisp","elisp","livescript","lua","makefile","markdown","md","markup","masm","mathml","matlab","mongodb","mysql","nasm","nginx","nim","nix","objc","ocaml","pascal","perl","php","postgresql","powershell","prolog","puppet","python","regex","rss","ruby","rb","rust","scala","scheme","shell","sh","solidity","sparql","sql","ssml","svg","swift","tcl","terraform","tex","toml","typescript","ts","tsx","unrealscript","verilog","vhdl","webassembly","wasm","xml","yaml","yml","zig"]);e.codeBlockRules=[{selector:["pre",'div[class*="prismjs"]',".syntaxhighlighter",".highlight",".highlight-source",".wp-block-syntaxhighlighter-code",".wp-block-code",'div[class*="language-"]'].join(", "),element:"pre",transform:(t,e)=>{if(!(t=>"classList"in t&&"getAttribute"in t&&"querySelector"in t)(t))return t;const r=t=>{var e;const r=t.getAttribute("data-lang")||t.getAttribute("data-language")||t.getAttribute("language");if(r)return r.toLowerCase();const n=Array.from(t.classList||[]);if(null===(e=t.classList)||void 0===e?void 0:e.contains("syntaxhighlighter")){const t=n.find((t=>!["syntaxhighlighter","nogutter"].includes(t)));if(t&&i.has(t.toLowerCase()))return t.toLowerCase()}for(const t of n)for(const e of o){const r=t.toLowerCase().match(e);if(r&&r[1]&&i.has(r[1].toLowerCase()))return r[1].toLowerCase()}for(const t of n)if(i.has(t.toLowerCase()))return t.toLowerCase();return""};let s="",a=t;for(;a&&!s;){s=r(a);const t=a.querySelector("code");!s&&t&&(s=r(t)),a=a.parentElement}const l=t=>{var e;if((0,n.isTextNode)(t))return(null===(e=t.parentElement)||void 0===e?void 0:e.querySelector("[data-line], .line"))&&!(t.textContent||"").trim()?"":t.textContent||"";let r="";if((0,n.isElement)(t)){if("BR"===t.tagName)return"\n";if(t.matches('div[class*="line"], span[class*="line"], .ec-line, [data-line-number], [data-line]')){const e=t.querySelector('.code, .content, [class*="code-"], [class*="content-"]');if(e)return(e.textContent||"")+"\n";const r=t.querySelector('.line-number, .gutter, [class*="line-number"], [class*="gutter"]');if(r){return Array.from(t.childNodes).filter((t=>!r.contains(t))).map((t=>l(t))).join("")+"\n"}return t.textContent+"\n"}t.childNodes.forEach((t=>{r+=l(t)}))}return r};let c="";t.matches(".syntaxhighlighter, .wp-block-syntaxhighlighter-code")&&(c=(t=>{const e=t.querySelector(".syntaxhighlighter table .code .container");if(e)return Array.from(e.children).map((t=>{const e=Array.from(t.querySelectorAll("code")).map((t=>{var e;let r=t.textContent||"";return(null===(e=t.classList)||void 0===e?void 0:e.contains("spaces"))&&(r=" ".repeat(r.length)),r})).join("");return e||t.textContent||""})).join("\n");const r=t.querySelectorAll(".code .line");return r.length>0?Array.from(r).map((t=>{const e=Array.from(t.querySelectorAll("code")).map((t=>t.textContent||"")).join("");return e||t.textContent||""})).join("\n"):""})(t)),c||(c=l(t)),c=c.replace(/^\s+|\s+$/g,"").replace(/\t/g," ").replace(/\n{3,}/g,"\n\n").replace(/\u00a0/g," ").replace(/^\n+/,"").replace(/\n+$/,"");let u=t;for(let e=0;e<3&&u;e++){const e=u.parentElement;if(!e||"BODY"===e.tagName)break;const r=Array.from(e.children);for(const e of r){if(e.contains(t))continue;const r=e.tagName;if("DIV"!==r&&"SPAN"!==r)continue;(e.textContent||"").trim().split(/\s+/).length<=5&&!e.querySelector("pre, code, img, table, h1, h2, h3, h4, h5, h6, p, blockquote, ul, ol")&&e.remove()}u=e}const d=e.createElement("pre"),h=e.createElement("code");return s&&(h.setAttribute("data-lang",s),h.setAttribute("class",`language-${s}`)),h.textContent=c,d.appendChild(h),d}}]},610(t,e,r){Object.defineProperty(e,"__esModule",{value:!0}),e.standardizeFootnotes=function(t){const e=t.ownerDocument;if(!e)return void console.warn("standardizeFootnotes: No document available");new i(e).standardizeFootnotes(t)};const n=r(640),o=r(639);class i{constructor(t){this.genericContainer=null,this.doc=t}createFootnoteItem(t,e,r){const n="string"==typeof e?this.doc:e.ownerDocument,i=n.createElement("li");if(i.className="footnote",i.id=`fn:${t}`,"string"==typeof e){const t=n.createElement("p");t.appendChild((0,o.parseHTML)(n,e)),i.appendChild(t)}else{const t=Array.from(e.querySelectorAll("p"));if(0===t.length){const t=n.createElement("p");(0,o.transferContent)(e,t),this.removeBackrefs(t),i.appendChild(t)}else t.forEach((t=>{const e=n.createElement("p");(0,o.transferContent)(t,e),this.removeBackrefs(e),i.appendChild(e)}))}const s=i.querySelector("p:last-of-type")||i;return r.forEach(((t,e)=>{const o=n.createElement("a");o.href=`#${t}`,o.title="return to article",o.className="footnote-backref",o.textContent="\u21a9",e<r.length-1&&(o.textContent+=" "),s.appendChild(o)})),i}collectFootnotes(t){const e={};let r=1;const i=new Set;if(t.querySelectorAll(n.FOOTNOTE_LIST_SELECTORS).forEach((n=>{if(n.matches("div.footnotes-footer")){return void n.querySelectorAll("div.footnote-footer").forEach((n=>{const s=(n.id||"").match(/^footnote-(\d+)$/);if(s){const a=s[1];if(!i.has(a)){const s=n.cloneNode(!0),l=s.querySelector("a");l&&l.remove();let c=(0,o.serializeHTML)(s);c=c.replace(/^\s*\.\s*/,"");const u=t.ownerDocument.createElement("div");u.appendChild((0,o.parseHTML)(t.ownerDocument,c.trim())),e[r]={content:u,originalId:a,refs:[]},i.add(a),r++}}}))}if(n.matches('div.footnote[data-component-name="FootnoteToDOM"]')){const t=n.querySelector("a.footnote-number"),o=n.querySelector(".footnote-content");if(t&&o){const n=t.id.replace("footnote-","").toLowerCase();n&&!i.has(n)&&(e[r]={content:o,originalId:n,refs:[]},i.add(n),r++)}return}n.querySelectorAll('li, div[role="listitem"]').forEach((t=>{var n,o,s,a;let l="",c=null;const u=t.querySelector(".citations");if(null===(n=null==u?void 0:u.id)||void 0===n?void 0:n.toLowerCase().startsWith("r")){l=u.id.toLowerCase();const t=u.querySelector(".citation-content");t&&(c=t)}else{if(t.id.toLowerCase().startsWith("bib.bib"))l=t.id.replace("bib.bib","").toLowerCase();else if(t.id.toLowerCase().startsWith("fn:"))l=t.id.replace("fn:","").toLowerCase();else if(t.id.toLowerCase().startsWith("fn"))l=t.id.replace("fn","").toLowerCase();else if(t.hasAttribute("data-counter"))l=(null===(s=null===(o=t.getAttribute("data-counter"))||void 0===o?void 0:o.replace(/\.$/,""))||void 0===s?void 0:s.toLowerCase())||"";else{const e=null===(a=t.id.split("/").pop())||void 0===a?void 0:a.match(/cite_note-(.+)/);l=e?e[1].toLowerCase():t.id.toLowerCase()}c=t}l&&!i.has(l)&&(e[r]={content:c||t,originalId:l,refs:[]},i.add(l),r++)}))})),1===r){const n=new Map;if(t.querySelectorAll('a[href*="#"]').forEach((t=>{var e,r;const o=null===(e=(t.getAttribute("href")||"").split("#").pop())||void 0===e?void 0:e.toLowerCase();if(!o)return;const i=(null===(r=t.textContent)||void 0===r?void 0:r.trim())||"";if(!/^\[?\(?\d{1,4}\)?\]?$/.test(i))return;const s=t.parentElement;if(!s)return;const a=s.tagName.toLowerCase();"sup"!==a&&"span"!==a&&"a"!==t.tagName.toLowerCase()||(n.has(o)||n.set(o,[]),n.get(o).push(t))})),n.size>=2){const o=new Set(n.keys()),s=t.querySelectorAll("div, section, aside, footer");let a=null,l=0;if(s.forEach((e=>{if(e===t)return;const r=e.querySelectorAll("p[id], li[id], div[id]");let n=0;r.forEach((t=>{o.has(t.id.toLowerCase())&&n++})),n>=2&&n>=l&&(l=n,a=e)})),a){const n=a.querySelectorAll("p[id], li[id], div[id]"),s=[];n.forEach((t=>{o.has(t.id.toLowerCase())&&s.push(t)})),s.forEach((n=>{const o=n.id.toLowerCase();if(i.has(o))return;const s=t.ownerDocument.createElement("div"),a=n.cloneNode(!0),l=a.childNodes[0];l&&3===l.nodeType&&(l.textContent=l.textContent.replace(/^\d+\.\s*/,"")),s.appendChild(a);let c=n.nextElementSibling;for(;c&&!c.id;){const t=c.cloneNode(!0);s.appendChild(t),c=c.nextElementSibling}e[r]={content:s,originalId:o,refs:[]},i.add(o),r++})),this.genericContainer=a}}}return e}removeBackrefs(t){for(t.querySelectorAll("a").forEach((t=>{var e,r;const n=(null===(e=t.textContent)||void 0===e?void 0:e.trim().replace(/\uFE0E|\uFE0F/g,""))||"";(/^[\u21A9\u21A5\u2191\u21B5\u2934\u2935\u23CE]+$/.test(n)||(null===(r=t.classList)||void 0===r?void 0:r.contains("footnote-backref")))&&t.remove()}));t.lastChild&&3===t.lastChild.nodeType;){const e=t.lastChild.textContent;if(!/^[\s,.;]*$/.test(e))break;t.lastChild.remove()}}findOuterFootnoteContainer(t){let e=t,r=t.parentElement;for(;r&&("span"===r.tagName.toLowerCase()||"sup"===r.tagName.toLowerCase());)e=r,r=r.parentElement;return e}createFootnoteReference(t,e){const r=this.doc.createElement("sup");r.id=e;const n=this.doc.createElement("a");return n.href=`#fn:${t}`,n.textContent=t,r.appendChild(n),r}collectInlineSidenotes(t){const e={},r=t.querySelectorAll("span.footnote-container, span.sidenote-container");if(0===r.length)return e;let n=1;return r.forEach((t=>{const r=t.querySelector("span.footnote, span.sidenote");if(!r)return;const o=r.cloneNode(!0);e[n]={content:o,originalId:String(n),refs:[`fnref:${n}`]};const i=this.createFootnoteReference(String(n),`fnref:${n}`);t.replaceWith(i),n++})),e}standardizeFootnotes(t){const e=this.collectInlineSidenotes(t),r=this.collectFootnotes(t),o=t.querySelectorAll(n.FOOTNOTE_INLINE_REFERENCES),i=new Map;o.forEach((t=>{var e,n,o,s;if(!t||!t.parentNode)return;let a="",l="";if(t.matches("sup.footnoteref")){const e=t.querySelector('a[id^="footnoteref-"]');if(e){const t=(e.id||"").match(/^footnoteref-(\d+)$/);t&&(a=t[1])}}else if(t.matches('a[id^="ref-link"]'))a=(null===(e=t.textContent)||void 0===e?void 0:e.trim())||"";else if(t.matches('a[role="doc-biblioref"]')){const e=t.getAttribute("data-xml-rid");if(e)a=e;else{const e=t.getAttribute("href");(null==e?void 0:e.startsWith("#core-R"))&&(a=e.replace("#core-",""))}}else if(t.matches("a.footnote-anchor, span.footnote-hovercard-target a")){const e=(null===(n=t.id)||void 0===n?void 0:n.replace("footnote-anchor-",""))||"";e&&(a=e.toLowerCase())}else if(t.matches("cite.ltx_cite")){const e=Array.from(t.querySelectorAll("a"));if(e.length>0){const n=[];if(e.forEach((t=>{var e;const o=t.getAttribute("href");if(!o)return;const i=null===(e=o.split("/").pop())||void 0===e?void 0:e.match(/bib\.bib(\d+)/);if(!i)return;const s=i[1].toLowerCase(),a=Object.entries(r).find((([t,e])=>e.originalId===s));if(!a)return;const[l,c]=a,u=c.refs.length>0?`fnref:${l}-${c.refs.length+1}`:`fnref:${l}`;c.refs.push(u),n.push(this.createFootnoteReference(l,u))})),n.length>0){const e=this.findOuterFootnoteContainer(t),r=t.ownerDocument.createDocumentFragment();return n.forEach(((e,n)=>{n>0&&r.appendChild(t.ownerDocument.createTextNode(" ")),r.appendChild(e)})),void e.replaceWith(r)}}}else if(t.matches("sup.reference")){const e=t.querySelectorAll("a");Array.from(e).forEach((t=>{var e;const r=t.getAttribute("href");if(r){const t=null===(e=r.split("/").pop())||void 0===e?void 0:e.match(/(?:cite_note|cite_ref)-(.+)/);t&&(a=t[1].toLowerCase())}}))}else if(t.matches('sup[id^="fnref:"]'))a=t.id.replace("fnref:","").toLowerCase();else if(t.matches('sup[id^="fnr"]'))a=t.id.replace("fnr","").toLowerCase();else if(t.matches("span.footnote-reference"))a=t.getAttribute("data-footnote-id")||"",!a&&(null===(o=t.id)||void 0===o?void 0:o.startsWith("fnref"))&&(a=t.id.replace("fnref","").toLowerCase());else if(t.matches("span.footnote-link"))a=t.getAttribute("data-footnote-id")||"",l=t.getAttribute("data-footnote-content")||"";else if(t.matches("a.citation"))a=(null===(s=t.textContent)||void 0===s?void 0:s.trim())||"",l=t.getAttribute("href")||"";else if(t.matches('a[id^="fnref"]'))a=t.id.replace("fnref","").toLowerCase();else{const e=t.getAttribute("href");if(e){const t=e.replace(/^[#]/,"");a=t.toLowerCase()}}if(a){const e=Object.entries(r).find((([t,e])=>e.originalId===a.toLowerCase()));if(e){const[r,n]=e,o=n.refs.length>0?`fnref:${r}-${n.refs.length+1}`:`fnref:${r}`;n.refs.push(o);const s=this.findOuterFootnoteContainer(t);if("sup"===s.tagName.toLowerCase()){i.has(s)||i.set(s,[]);i.get(s).push(this.createFootnoteReference(r,o))}else s.replaceWith(this.createFootnoteReference(r,o))}}}));const s=Object.entries(r).filter((([t,e])=>0===e.refs.length));if(s.length>0){const e=new Map,n=new Map;s.forEach((([t,r])=>{e.set(r.originalId,[t,r]),n.set(t,[t,r])}));t.querySelectorAll('a[href*="#"]').forEach((t=>{var r,n;if(!t.parentNode)return;if(t.closest('[id^="fnref:"]'))return;if(t.closest("#footnotes"))return;if(this.genericContainer&&this.genericContainer.contains(t))return;const o=null===(r=(t.getAttribute("href")||"").split("#").pop())||void 0===r?void 0:r.toLowerCase();if(!o)return;const i=e.get(o);if(!i)return;const s=(null===(n=t.textContent)||void 0===n?void 0:n.trim())||"";if(!/^[\[\(]?\d{1,4}[\]\)]?$/.test(s))return;const[a,l]=i,c=l.refs.length>0?`fnref:${a}-${l.refs