Skip to content

Commit

Permalink
Refactor #2423
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Jan 13, 2022
1 parent d1037df commit 5fdaee0
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 41 deletions.
2 changes: 1 addition & 1 deletion components/lib/api/Api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface FilterMatchModeOptions {
interface APIOptions {
ripple?: boolean;
inputStyle?: InputStyleType;
inlineCssNonce?: string;
nonce?: string;
locale?: string;
appendTo?: AppendToType;
cssTransition?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion components/lib/api/PrimeReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class PrimeReact {

static autoZIndex = true;

static inlineCssNonce = null;
static nonce = null;

static zIndex = {
modal: 1100,
Expand Down
3 changes: 2 additions & 1 deletion components/lib/carousel/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { DomHandler, classNames, UniqueComponentId } from '../utils/Utils';
import { Ripple } from '../ripple/Ripple';
import PrimeReact from '../api/Api';

class CarouselItem extends Component {

Expand Down Expand Up @@ -371,7 +372,7 @@ export class Carousel extends Component {

createStyle() {
if (!this.carouselStyle) {
this.carouselStyle = DomHandler.createInlineStyle();
this.carouselStyle = DomHandler.createInlineStyle(PrimeReact.nonce);
}

let innerHTML = `
Expand Down
4 changes: 2 additions & 2 deletions components/lib/datatable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,12 @@ export class DataTable extends Component {
}

createStyleElement() {
this.styleElement = DomHandler.createInlineStyle();
this.styleElement = DomHandler.createInlineStyle(PrimeReact.nonce);
}

createResponsiveStyle() {
if (!this.responsiveStyleElement) {
this.responsiveStyleElement = DomHandler.createInlineStyle();
this.responsiveStyleElement = DomHandler.createInlineStyle(PrimeReact.nonce);

let innerHTML = `
@media screen and (max-width: ${this.props.breakpoint}) {
Expand Down
2 changes: 1 addition & 1 deletion components/lib/dialog/Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ export class Dialog extends Component {

createStyle() {
if (!this.styleElement) {
this.styleElement = DomHandler.createInlineStyle();
this.styleElement = DomHandler.createInlineStyle(PrimeReact.nonce);

let innerHTML = '';
for (let breakpoint in this.props.breakpoints) {
Expand Down
3 changes: 2 additions & 1 deletion components/lib/galleria/GalleriaThumbnails.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import PrimeReact from '../api/Api';
import { DomHandler, classNames, UniqueComponentId } from '../utils/Utils';
import { Ripple } from '../ripple/Ripple';

Expand Down Expand Up @@ -244,7 +245,7 @@ export class GalleriaThumbnails extends Component {

createStyle() {
if (!this.thumbnailsStyle) {
this.thumbnailsStyle = DomHandler.createInlineStyle();
this.thumbnailsStyle = DomHandler.createInlineStyle(PrimeReact.nonce);
}

let innerHTML = `
Expand Down
2 changes: 1 addition & 1 deletion components/lib/overlaypanel/OverlayPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class OverlayPanel extends Component {

createStyle() {
if (!this.styleElement) {
this.styleElement = DomHandler.createInlineStyle();
this.styleElement = DomHandler.createInlineStyle(PrimeReact.nonce);

let innerHTML = '';
for (let breakpoint in this.props.breakpoints) {
Expand Down
42 changes: 9 additions & 33 deletions components/lib/utils/DomHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import PrimeReact from '../api/Api';

export default class DomHandler {

static innerWidth(el) {
Expand Down Expand Up @@ -871,43 +869,21 @@ export default class DomHandler {
}
}

/**
* Anytime an inline style is created check for CSP Nonce.
* Create React App/Next look for environment variable 'process.env.REACT_APP_CSS_NONCE'.
* Vite look for environment variable 'import.meta.env.VITE_CSS_NONCE'
* Finally look for global variable PrimeReact.inlineCssNonce to set a CSP NONCE.
*
* @see https://github.com/primefaces/primereact/issues/2423
* @return HtmlStyleElement
*/
static createInlineStyle() {
static createInlineStyle(nonce) {
let styleElement = document.createElement('style');
let nonce = '';
// CRA and Next
if (process) {
nonce = process.env.REACT_APP_CSS_NONCE;
}
// Vite
if (!nonce && import.meta.env) {
nonce = import.meta.env.VITE_CSS_NONCE;
}
// global variable
if (!nonce) {
nonce = PrimeReact.inlineCssNonce;
}
if (nonce) {
styleElement.setAttribute('nonce', nonce);
try {
if (!nonce) {
nonce = process.env.REACT_APP_CSS_NONCE;
}
} catch (error) {
// NOOP
}

nonce && styleElement.setAttribute('nonce', nonce);
document.head.appendChild(styleElement);
return styleElement;
}

/**
* Remove a style element from the head and attempt to prevent DOM Exception on fast refresh.
*
* @see https://github.com/primefaces/primereact/issues/2469
* @param {HtmlStyleElement} styleElement the element to remove from head
*/
static removeInlineStyle(styleElement) {
if (this.isExist(styleElement)) {
try {
Expand Down

0 comments on commit 5fdaee0

Please sign in to comment.