/* * This file is part of the TYPO3 CMS project. * * It is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License, either version 2 * of the License, or any later version. * * For the full copyright and license information, please read the * LICENSE.txt file that was distributed with this source code. * * The TYPO3 project - inspiring people to share! */ import{property as b,state as h,customElement as $}from"lit/decorators.js";import{LitElement as v,html as s,nothing as d}from"lit";import{classMap as R}from"lit/directives/class-map.js";import p from"@typo3/core/ajax/ajax-request.js";import y from"@typo3/core/event/regular-event.js";import i from"~labels/backend.modules.content_security_policy";import"bootstrap";var a=function(r,e,t,l){var o=arguments.length,c=o<3?e:l===null?l=Object.getOwnPropertyDescriptor(e,t):l,u;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")c=Reflect.decorate(r,e,t,l);else for(var m=r.length-1;m>=0;m--)(u=r[m])&&(c=(o<3?u(c):o>3?u(e,t,c):u(e,t))||c);return o>3&&c&&Object.defineProperty(e,t,c),c},f;(function(r){r.fixable="fixable",r.irrelevant="irrelevant",r.suspicious="suspicious"})(f||(f={}));let n=class extends v{constructor(){super(...arguments),this.selectedScope=null,this.reports=[],this.selectedReport=null,this.suggestions=[],this.previouslyFocusedRow=null}connectedCallback(){super.connectedCallback(),this.fetchReports(),this.peripheralEvent=new y("click",(e,t)=>{t.dataset.cspReportsHandler==="refresh"&&(e.preventDefault(),this.fetchReports())}),this.peripheralEvent.delegateTo(document,"[data-csp-reports-handler]")}disconnectedCallback(){super.disconnectedCallback(),this.peripheralEvent?.release()}updated(e){super.updated(e),e.has("selectedReport")&&this.selectedReport!==null&&this.updateComplete.then(()=>{this.querySelector("#report-details")?.focus()})}createRenderRoot(){return this}render(){return s`
${this.renderNavigation()}
${this.reports.length===0?s``:d} ${this.reports.map(e=>s`this.selectReport(e)} @keydown=${t=>this.handleReportKeydown(t,e)} tabindex=0 role=button aria-label=${i.get("module.label.showDetails",[e.details.effectiveDirective,e.details.blockedUri])}>`)}
${i.get("module.label.created")}${i.get("module.label.scope")}${i.get("module.label.violation")}${i.get("module.label.uri")}
${i.get("module.label.noEntriesAvailable")}
${e.created}${e.scope}${e.count} ${e.details.effectiveDirective}${this.shortenUri(e.details.blockedUri)}${e.attributes.join(", ")}
${this.renderGuide()} ${this.renderSelectedReport()}
`}renderNavigation(){return s`
`}renderGuide(){return s`${this.selectedReport?d:s`

${i.get("module.label.guide.no_record_selected")}

`}`}renderSelectedReport(){const e=this.selectedReport;return s`${e?s`
this.handleDetailsKeydown(t)}>

${i.get("module.label.details")}

${i.get("module.label.directive")} / ${i.get("module.label.disposition")}
${e.details.effectiveDirective} / ${e.details.disposition}
${i.get("module.label.document_uri")}
${e.details.documentUri} ${this.renderCodeLocation(e)}
${e.details.sourceFile&&e.details.sourceFile!==e.details.documentUri?s`
${i.get("module.label.source_file")}
${e.details.sourceFile}
`:d}
${i.get("module.label.blocked_uri")}
${e.details.blockedUri}
${e.details.scriptSample?s`
${i.get("module.label.sample")}
${e.details.scriptSample}
`:d} ${e.meta.agent?s`
${i.get("module.label.user_agent")}
${e.meta.agent}
`:d}
${i.get("module.label.uuid")}
${e.uuid}
${i.get("module.label.summary")}
${e.summary}
${this.suggestions.length>0?s`

${i.get("module.label.suggestions")}

`:d} ${this.suggestions.map(t=>s`

${t.label||t.identifier}

${t.collection.mutations.map(l=>s`

${l.mode} ${l.directive}: ${l.sources.join(" ")}

`)}
`)}
`:d}`}renderCodeLocation(e){if(!e.details.lineNumber)return d;const t=[e.details.lineNumber];return e.details.columnNumber&&t.push(e.details.columnNumber),s`(${t.join(":")})`}selectReport(e){this.suggestions=[],e!==null&&this.selectedReport!==e?(this.previouslyFocusedRow=document.activeElement,this.selectedReport=e,this.invokeHandleReportAction(e).then(t=>this.suggestions=t)):(this.selectedReport=null,this.previouslyFocusedRow&&this.updateComplete.then(()=>{this.previouslyFocusedRow?.focus(),this.previouslyFocusedRow=null}))}handleReportKeydown(e,t){if(e.key==="Enter"||e.key==="Space"||e.key===" "){e.preventDefault(),this.selectReport(t);return}const l=e.currentTarget;let o=null;switch(e.key){case"ArrowDown":e.preventDefault(),o=l.nextElementSibling;break;case"ArrowUp":e.preventDefault(),o=l.previousElementSibling;break;case"Home":e.preventDefault(),o=l.parentElement?.firstElementChild;break;case"End":e.preventDefault(),o=l.parentElement?.lastElementChild;break;default:return}o&&o.hasAttribute("tabindex")&&o.focus()}handleDetailsKeydown(e){e.key==="Escape"&&(e.preventDefault(),this.selectReport(null))}focusFirstReportRow(){this.updateComplete.then(()=>{this.querySelector('tbody tr[tabindex="0"]')?.focus()})}selectScope(e){this.selectedScope=e,this.fetchReports()}fetchReports(){this.invokeFetchReportsAction().then(e=>this.reports=e)}filterReports(...e){e.includes(this.selectedReport?.uuid)&&(this.selectedReport=null),this.reports=this.reports.filter(t=>!e.includes(t.uuid))}invokeFetchReportsAction(){return new p(this.controlUri).post({action:"fetchReports",scope:this.selectedScope||""}).then(e=>e.resolve("application/json"))}invokeHandleReportAction(e){return new p(this.controlUri).post({action:"handleReport",uuid:e.uuid}).then(t=>t.resolve("application/json"))}invokeMutateReportAction(e,t){const l=this.reports.filter(o=>o.mutationHashes.includes(t.hash)).map(o=>o.summary);return new p(this.controlUri).post({action:"mutateReport",scope:e.scope,hmac:t.hmac,suggestion:t,summaries:l}).then(o=>o.resolve("application/json")).then(o=>this.filterReports(...o.uuids))}invokeMuteReportAction(e){new p(this.controlUri).post({action:"muteReport",summaries:[e.summary]}).then(t=>t.resolve("application/json")).then(t=>this.filterReports(...t.uuids)).then(()=>this.focusFirstReportRow())}invokeDeleteReportAction(e){new p(this.controlUri).post({action:"deleteReport",summaries:[e.summary]}).then(t=>t.resolve("application/json")).then(t=>this.filterReports(...t.uuids)).then(()=>this.focusFirstReportRow())}invokeDeleteReportsAction(){new p(this.controlUri).post({action:"deleteReports",scope:this.selectedScope||""}).then(e=>e.resolve("application/json")).then(()=>this.fetchReports()).then(()=>this.selectReport(null))}shortenUri(e){if(e==="inline")return e;try{return new URL(e).hostname}catch{return e}}};a([b({type:Array})],n.prototype,"scopes",void 0),a([b({type:String})],n.prototype,"controlUri",void 0),a([h()],n.prototype,"selectedScope",void 0),a([h()],n.prototype,"reports",void 0),a([h()],n.prototype,"selectedReport",void 0),a([h()],n.prototype,"suggestions",void 0),n=a([$("typo3-backend-security-csp-reports")],n);export{n as CspReports};