Files
documentation/node_modules/react-helmet-async/lib/index.modern.js
2024-03-22 03:47:51 +05:30

3 lines
12 KiB
JavaScript

import e,{Component as t}from"react";import r from"prop-types";import n from"react-fast-compare";import s from"invariant";import i from"shallowequal";function o(){return o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},o.apply(this,arguments)}function a(e,t){if(null==e)return{};var r,n,s={},i=Object.keys(e);for(n=0;n<i.length;n++)t.indexOf(r=i[n])>=0||(s[r]=e[r]);return s}const l={BASE:"base",BODY:"body",HEAD:"head",HTML:"html",LINK:"link",META:"meta",NOSCRIPT:"noscript",SCRIPT:"script",STYLE:"style",TITLE:"title",FRAGMENT:"Symbol(react.fragment)"},c={rel:["amphtml","canonical","alternate"]},p={type:["application/ld+json"]},d={charset:"",name:["robots","description"],property:["og:type","og:title","og:url","og:image","og:image:alt","og:description","twitter:url","twitter:title","twitter:description","twitter:image","twitter:image:alt","twitter:card","twitter:site"]},h=Object.keys(l).map(e=>l[e]),u={accesskey:"accessKey",charset:"charSet",class:"className",contenteditable:"contentEditable",contextmenu:"contextMenu","http-equiv":"httpEquiv",itemprop:"itemProp",tabindex:"tabIndex"},m=Object.keys(u).reduce((e,t)=>(e[u[t]]=t,e),{}),T=(e,t)=>{for(let r=e.length-1;r>=0;r-=1){const n=e[r];if(Object.prototype.hasOwnProperty.call(n,t))return n[t]}return null},y=e=>{let t=T(e,l.TITLE);const r=T(e,"titleTemplate");if(Array.isArray(t)&&(t=t.join("")),r&&t)return r.replace(/%s/g,()=>t);const n=T(e,"defaultTitle");return t||n||void 0},f=e=>T(e,"onChangeClientState")||(()=>{}),g=(e,t)=>t.filter(t=>void 0!==t[e]).map(t=>t[e]).reduce((e,t)=>o({},e,t),{}),b=(e,t)=>t.filter(e=>void 0!==e[l.BASE]).map(e=>e[l.BASE]).reverse().reduce((t,r)=>{if(!t.length){const n=Object.keys(r);for(let s=0;s<n.length;s+=1){const i=n[s].toLowerCase();if(-1!==e.indexOf(i)&&r[i])return t.concat(r)}}return t},[]),A=(e,t,r)=>{const n={};return r.filter(t=>{return!!Array.isArray(t[e])||(void 0!==t[e]&&(r=`Helmet: ${e} should be of type "Array". Instead found type "${typeof t[e]}"`,console&&"function"==typeof console.warn&&console.warn(r)),!1);var r}).map(t=>t[e]).reverse().reduce((e,r)=>{const s={};r.filter(e=>{let r;const i=Object.keys(e);for(let n=0;n<i.length;n+=1){const s=i[n],o=s.toLowerCase();-1===t.indexOf(o)||"rel"===r&&"canonical"===e[r].toLowerCase()||"rel"===o&&"stylesheet"===e[o].toLowerCase()||(r=o),-1===t.indexOf(s)||"innerHTML"!==s&&"cssText"!==s&&"itemprop"!==s||(r=s)}if(!r||!e[r])return!1;const o=e[r].toLowerCase();return n[r]||(n[r]={}),s[r]||(s[r]={}),!n[r][o]&&(s[r][o]=!0,!0)}).reverse().forEach(t=>e.push(t));const i=Object.keys(s);for(let e=0;e<i.length;e+=1){const t=i[e],r=o({},n[t],s[t]);n[t]=r}return e},[]).reverse()},C=(e,t)=>{if(Array.isArray(e)&&e.length)for(let r=0;r<e.length;r+=1)if(e[r][t])return!0;return!1},O=e=>Array.isArray(e)?e.join(""):e,S=(e,t)=>Array.isArray(e)?e.reduce((e,r)=>(((e,t)=>{const r=Object.keys(e);for(let n=0;n<r.length;n+=1)if(t[r[n]]&&t[r[n]].includes(e[r[n]]))return!0;return!1})(r,t)?e.priority.push(r):e.default.push(r),e),{priority:[],default:[]}):{default:e},E=(e,t)=>o({},e,{[t]:void 0}),x=[l.NOSCRIPT,l.SCRIPT,l.STYLE],I=(e,t=!0)=>!1===t?String(e):String(e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;"),P=e=>Object.keys(e).reduce((t,r)=>{const n=void 0!==e[r]?`${r}="${e[r]}"`:`${r}`;return t?`${t} ${n}`:n},""),w=(e,t={})=>Object.keys(e).reduce((t,r)=>(t[u[r]||r]=e[r],t),t),L=(t,r)=>r.map((r,n)=>{const s={key:n,"data-rh":!0};return Object.keys(r).forEach(e=>{const t=u[e]||e;"innerHTML"===t||"cssText"===t?s.dangerouslySetInnerHTML={__html:r.innerHTML||r.cssText}:s[t]=r[e]}),e.createElement(t,s)}),v=(t,r,n)=>{switch(t){case l.TITLE:return{toComponent:()=>((t,r,n)=>{const s=w(n,{key:r,"data-rh":!0});return[e.createElement(l.TITLE,s,r)]})(0,r.title,r.titleAttributes),toString:()=>((e,t,r,n)=>{const s=P(r),i=O(t);return s?`<${e} data-rh="true" ${s}>${I(i,n)}</${e}>`:`<${e} data-rh="true">${I(i,n)}</${e}>`})(t,r.title,r.titleAttributes,n)};case"bodyAttributes":case"htmlAttributes":return{toComponent:()=>w(r),toString:()=>P(r)};default:return{toComponent:()=>L(t,r),toString:()=>((e,t,r)=>t.reduce((t,n)=>{const s=Object.keys(n).filter(e=>!("innerHTML"===e||"cssText"===e)).reduce((e,t)=>{const s=void 0===n[t]?t:`${t}="${I(n[t],r)}"`;return e?`${e} ${s}`:s},""),i=n.innerHTML||n.cssText||"",o=-1===x.indexOf(e);return`${t}<${e} data-rh="true" ${s}${o?"/>":`>${i}</${e}>`}`},""))(t,r,n)}}},j=e=>{const{baseTag:t,bodyAttributes:r,encode:n,htmlAttributes:s,noscriptTags:i,styleTags:o,title:a="",titleAttributes:h,prioritizeSeoTags:u}=e;let{linkTags:m,metaTags:T,scriptTags:y}=e,f={toComponent:()=>{},toString:()=>""};return u&&({priorityMethods:f,linkTags:m,metaTags:T,scriptTags:y}=(({metaTags:e,linkTags:t,scriptTags:r,encode:n})=>{const s=S(e,d),i=S(t,c),o=S(r,p);return{priorityMethods:{toComponent:()=>[...L(l.META,s.priority),...L(l.LINK,i.priority),...L(l.SCRIPT,o.priority)],toString:()=>`${v(l.META,s.priority,n)} ${v(l.LINK,i.priority,n)} ${v(l.SCRIPT,o.priority,n)}`},metaTags:s.default,linkTags:i.default,scriptTags:o.default}})(e)),{priority:f,base:v(l.BASE,t,n),bodyAttributes:v("bodyAttributes",r,n),htmlAttributes:v("htmlAttributes",s,n),link:v(l.LINK,m,n),meta:v(l.META,T,n),noscript:v(l.NOSCRIPT,i,n),script:v(l.SCRIPT,y,n),style:v(l.STYLE,o,n),title:v(l.TITLE,{title:a,titleAttributes:h},n)}},M=[];class ${constructor(e,t="undefined"!=typeof document){this.instances=[],this.value={setHelmet:e=>{this.context.helmet=e},helmetInstances:{get:()=>this.canUseDOM?M:this.instances,add:e=>{(this.canUseDOM?M:this.instances).push(e)},remove:e=>{const t=(this.canUseDOM?M:this.instances).indexOf(e);(this.canUseDOM?M:this.instances).splice(t,1)}}},this.context=e,this.canUseDOM=t,t||(e.helmet=j({baseTag:[],bodyAttributes:{},encodeSpecialCharacters:!0,htmlAttributes:{},linkTags:[],metaTags:[],noscriptTags:[],scriptTags:[],styleTags:[],title:"",titleAttributes:{}}))}}const k=e.createContext({}),H=r.shape({setHelmet:r.func,helmetInstances:r.shape({get:r.func,add:r.func,remove:r.func})}),N="undefined"!=typeof document;class R extends t{constructor(e){super(e),this.helmetData=new $(this.props.context,R.canUseDOM)}render(){/*#__PURE__*/return e.createElement(k.Provider,{value:this.helmetData.value},this.props.children)}}R.canUseDOM=N,R.propTypes={context:r.shape({helmet:r.shape()}),children:r.node.isRequired},R.defaultProps={context:{}},R.displayName="HelmetProvider";const D=(e,t)=>{const r=document.head||document.querySelector(l.HEAD),n=r.querySelectorAll(`${e}[data-rh]`),s=[].slice.call(n),i=[];let o;return t&&t.length&&t.forEach(t=>{const r=document.createElement(e);for(const e in t)Object.prototype.hasOwnProperty.call(t,e)&&("innerHTML"===e?r.innerHTML=t.innerHTML:"cssText"===e?r.styleSheet?r.styleSheet.cssText=t.cssText:r.appendChild(document.createTextNode(t.cssText)):r.setAttribute(e,void 0===t[e]?"":t[e]));r.setAttribute("data-rh","true"),s.some((e,t)=>(o=t,r.isEqualNode(e)))?s.splice(o,1):i.push(r)}),s.forEach(e=>e.parentNode.removeChild(e)),i.forEach(e=>r.appendChild(e)),{oldTags:s,newTags:i}},U=(e,t)=>{const r=document.getElementsByTagName(e)[0];if(!r)return;const n=r.getAttribute("data-rh"),s=n?n.split(","):[],i=[].concat(s),o=Object.keys(t);for(let e=0;e<o.length;e+=1){const n=o[e],a=t[n]||"";r.getAttribute(n)!==a&&r.setAttribute(n,a),-1===s.indexOf(n)&&s.push(n);const l=i.indexOf(n);-1!==l&&i.splice(l,1)}for(let e=i.length-1;e>=0;e-=1)r.removeAttribute(i[e]);s.length===i.length?r.removeAttribute("data-rh"):r.getAttribute("data-rh")!==o.join(",")&&r.setAttribute("data-rh",o.join(","))},q=(e,t)=>{const{baseTag:r,bodyAttributes:n,htmlAttributes:s,linkTags:i,metaTags:o,noscriptTags:a,onChangeClientState:c,scriptTags:p,styleTags:d,title:h,titleAttributes:u}=e;U(l.BODY,n),U(l.HTML,s),((e,t)=>{void 0!==e&&document.title!==e&&(document.title=O(e)),U(l.TITLE,t)})(h,u);const m={baseTag:D(l.BASE,r),linkTags:D(l.LINK,i),metaTags:D(l.META,o),noscriptTags:D(l.NOSCRIPT,a),scriptTags:D(l.SCRIPT,p),styleTags:D(l.STYLE,d)},T={},y={};Object.keys(m).forEach(e=>{const{newTags:t,oldTags:r}=m[e];t.length&&(T[e]=t),r.length&&(y[e]=m[e].oldTags)}),t&&t(),c(e,T,y)};let Y=null;class B extends t{constructor(...e){super(...e),this.rendered=!1}shouldComponentUpdate(e){return!i(e,this.props)}componentDidUpdate(){this.emitChange()}componentWillUnmount(){const{helmetInstances:e}=this.props.context;e.remove(this),this.emitChange()}emitChange(){const{helmetInstances:e,setHelmet:t}=this.props.context;let r=null;const n=(s=e.get().map(e=>{const t=o({},e.props);return delete t.context,t}),{baseTag:b(["href"],s),bodyAttributes:g("bodyAttributes",s),defer:T(s,"defer"),encode:T(s,"encodeSpecialCharacters"),htmlAttributes:g("htmlAttributes",s),linkTags:A(l.LINK,["rel","href"],s),metaTags:A(l.META,["name","charset","http-equiv","property","itemprop"],s),noscriptTags:A(l.NOSCRIPT,["innerHTML"],s),onChangeClientState:f(s),scriptTags:A(l.SCRIPT,["src","innerHTML"],s),styleTags:A(l.STYLE,["cssText"],s),title:y(s),titleAttributes:g("titleAttributes",s),prioritizeSeoTags:C(s,"prioritizeSeoTags")});var s,i;R.canUseDOM?(i=n,Y&&cancelAnimationFrame(Y),i.defer?Y=requestAnimationFrame(()=>{q(i,()=>{Y=null})}):(q(i),Y=null)):j&&(r=j(n)),t(r)}init(){if(this.rendered)return;this.rendered=!0;const{helmetInstances:e}=this.props.context;e.add(this),this.emitChange()}render(){return this.init(),null}}B.propTypes={context:H.isRequired},B.displayName="HelmetDispatcher";const K=["children"],z=["children"];class F extends t{shouldComponentUpdate(e){return!n(E(this.props,"helmetData"),E(e,"helmetData"))}mapNestedChildrenToProps(e,t){if(!t)return null;switch(e.type){case l.SCRIPT:case l.NOSCRIPT:return{innerHTML:t};case l.STYLE:return{cssText:t};default:throw new Error(`<${e.type} /> elements are self-closing and can not contain children. Refer to our API for more information.`)}}flattenArrayTypeChildren({child:e,arrayTypeChildren:t,newChildProps:r,nestedChildren:n}){return o({},t,{[e.type]:[...t[e.type]||[],o({},r,this.mapNestedChildrenToProps(e,n))]})}mapObjectTypeChildren({child:e,newProps:t,newChildProps:r,nestedChildren:n}){switch(e.type){case l.TITLE:return o({},t,{[e.type]:n,titleAttributes:o({},r)});case l.BODY:return o({},t,{bodyAttributes:o({},r)});case l.HTML:return o({},t,{htmlAttributes:o({},r)});default:return o({},t,{[e.type]:o({},r)})}}mapArrayTypeChildrenToProps(e,t){let r=o({},t);return Object.keys(e).forEach(t=>{r=o({},r,{[t]:e[t]})}),r}warnOnInvalidChildren(e,t){return s(h.some(t=>e.type===t),"function"==typeof e.type?"You may be attempting to nest <Helmet> components within each other, which is not allowed. Refer to our API for more information.":`Only elements types ${h.join(", ")} are allowed. Helmet does not support rendering <${e.type}> elements. Refer to our API for more information.`),s(!t||"string"==typeof t||Array.isArray(t)&&!t.some(e=>"string"!=typeof e),`Helmet expects a string as a child of <${e.type}>. Did you forget to wrap your children in braces? ( <${e.type}>{\`\`}</${e.type}> ) Refer to our API for more information.`),!0}mapChildrenToProps(t,r){let n={};return e.Children.forEach(t,e=>{if(!e||!e.props)return;const t=e.props,{children:s}=t,i=a(t,K),o=Object.keys(i).reduce((e,t)=>(e[m[t]||t]=i[t],e),{});let{type:c}=e;switch("symbol"==typeof c?c=c.toString():this.warnOnInvalidChildren(e,s),c){case l.FRAGMENT:r=this.mapChildrenToProps(s,r);break;case l.LINK:case l.META:case l.NOSCRIPT:case l.SCRIPT:case l.STYLE:n=this.flattenArrayTypeChildren({child:e,arrayTypeChildren:n,newChildProps:o,nestedChildren:s});break;default:r=this.mapObjectTypeChildren({child:e,newProps:r,newChildProps:o,nestedChildren:s})}}),this.mapArrayTypeChildrenToProps(n,r)}render(){const t=this.props,{children:r}=t,n=a(t,z);let s=o({},n),{helmetData:i}=n;return r&&(s=this.mapChildrenToProps(r,s)),!i||i instanceof $||(i=new $(i.context,i.instances)),i?/*#__PURE__*/e.createElement(B,o({},s,{context:i.value,helmetData:void 0})):/*#__PURE__*/e.createElement(k.Consumer,null,t=>/*#__PURE__*/e.createElement(B,o({},s,{context:t})))}}F.propTypes={base:r.object,bodyAttributes:r.object,children:r.oneOfType([r.arrayOf(r.node),r.node]),defaultTitle:r.string,defer:r.bool,encodeSpecialCharacters:r.bool,htmlAttributes:r.object,link:r.arrayOf(r.object),meta:r.arrayOf(r.object),noscript:r.arrayOf(r.object),onChangeClientState:r.func,script:r.arrayOf(r.object),style:r.arrayOf(r.object),title:r.string,titleAttributes:r.object,titleTemplate:r.string,prioritizeSeoTags:r.bool,helmetData:r.object},F.defaultProps={defer:!0,encodeSpecialCharacters:!0,prioritizeSeoTags:!1},F.displayName="Helmet";export{F as Helmet,$ as HelmetData,R as HelmetProvider};
//# sourceMappingURL=index.modern.js.map