Skip to content

Commit

Permalink
created function to parse html string and render anchor tags within m…
Browse files Browse the repository at this point in the history
…odalMainText
  • Loading branch information
Marco Monti authored and l-besenyei committed Aug 5, 2024
1 parent 5a0e246 commit c42c36c
Show file tree
Hide file tree
Showing 8 changed files with 532 additions and 490 deletions.
763 changes: 388 additions & 375 deletions build/cookieconsent.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/cookieconsent.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/cookieconsent.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs_src/src/assets/js/cookieconsent.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ The script is being controlled mainly by a configuration object which is passed
```javascript
<script>
window.CookieConsent.init({
// More link URL on bar.
modalMainTextMoreLink: null,
// How long to wait until bar or initial modal comes up.
UITimeout: 1000,
// Show 'bar' or 'modal' initial layout
Expand Down Expand Up @@ -158,6 +156,7 @@ The script is being controlled mainly by a configuration object which is passed
barLinkSetting: 'Cookie Settings',
barBtnAcceptAll: 'Accept all cookies',
modalMainTitle: 'Cookie settings',
// You can insert <a> tags within this prop to render links
modalMainText: 'Cookies are small pieces of data sent from a website and stored on the user\'s computer by the user\'s web browser while the user is browsing. Your browser stores each message in a small file, called cookie. When you request another page from the server, your browser sends the cookie back to the server. Cookies were designed to be a reliable mechanism for websites to remember information or to record the user\'s browsing activity.',
modalBtnSave: 'Save current settings',
modalBtnAcceptAll: 'Accept all cookies and close',
Expand Down
1 change: 0 additions & 1 deletion src/lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default class Configuration {
cookieExists: false,
cookieVersion: 1,
mode: 'bar',
modalMainTextMoreLink: null,
showRejectAllButton: false,
UITimeout: 1000,
noUI: false,
Expand Down
10 changes: 2 additions & 8 deletions src/lib/Interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ export default class Interface {
el('div.ccm__content',
el('div.ccm__content__heading',
el('strong#ccm__content__title', Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'modalMainTitle'), { 'aria-label': 'Cookie Consent options'}),
el('p',
Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'modalMainText'),
(window.CookieConsent.config.modalMainTextMoreLink) ? el('a', { href: window.CookieConsent.config.modalMainTextMoreLink, target: '_blank', rel: 'noopener noreferrer' }, Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'learnMore')) : null
),
Utilities.parseAndCreateParagraph(Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'modalMainText')),
el('button.ccm__cheading__close', '×', { 'aria-label': Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'closeAriaLabel') })
),
el('div.ccm__content__body',
Expand All @@ -212,10 +209,7 @@ export default class Interface {
el('div.ccm__content',
el('div.ccm__content__heading',
el('strong#init__ccm__content__title', Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'modalMainTitle'), { 'aria-label': 'Cookie Consent'}),
el('p',
Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'modalMainText'),
(window.CookieConsent.config.modalMainTextMoreLink) ? el('a', { href: window.CookieConsent.config.modalMainTextMoreLink, target: '_blank', rel: 'noopener noreferrer' }, Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'learnMore')) : null
),
Utilities.parseAndCreateParagraph(Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'modalMainText')),
),
el('div.ccm__footer',
el('button.consent-decline', Language.getTranslation(window.CookieConsent.config, window.CookieConsent.config.language.current, 'initModalBtnDisagree')),
Expand Down
239 changes: 138 additions & 101 deletions src/lib/Utilities.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,140 @@
export default class Utilities {

static ready(fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}

static objectType(obj){
return Object.prototype.toString.call(obj).slice(8, -1);
}

static lightenDarkenColor(col, amt) {

let usePound = false;

// Handling HEX color format
if (col[0] === "#") {
col = col.slice(1);
usePound = true;

let num = parseInt(col, 16);
let r = (num >> 16) + amt;
let g = ((num >> 8) & 0x00FF) + amt;
let b = (num & 0x0000FF) + amt;

r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));

return (usePound ? "#" : "") + ((r << 16) | (g << 8) | b).toString(16).padStart(6, '0');
}

// Handling RGBA and RGB color format
if (col.startsWith("rgb")) {
let parts = col.match(/rgba?\((\d{1,3})\s*,?\s*(\d{1,3})\s*,?\s*(\d{1,3})(?:\s*\/\s*(\d+\.?\d*%?))?\)/i);

if (parts) {
let r = parseInt(parts[1]) + amt;
let g = parseInt(parts[2]) + amt;
let b = parseInt(parts[3]) + amt;
let a = parts[4] ? parseFloat(parts[4]) : 1;

r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));

// Conditionally format output depending on alpha presence
if (parts[4]) {
return `rgba(${r}, ${g}, ${b}, ${a})`;
} else {
return `rgb(${r}, ${g}, ${b})`;
}
}
}
return col; // Return the original if format is not recognized
}

static removeCookie() {
document.cookie = `cconsent=; expires=Thu, 01 Jan 1980 00:00:00 UTC; path=/;`;
//remove localStorage consentMode obj
localStorage.removeItem('consentMode')
window.CookieConsent.config.cookieExists = false
}

// Create an array of services from Cookieconsent global object
// Filter based on category or leave empty is all is wanted
static listGlobalServices(category) {
let categories = [];

// Global config objectnot set
if (typeof window.CookieConsent === 'undefined') return categories;

// Category is not specified or opposite
if (typeof category === 'undefined') {
for (let key in window.CookieConsent.config.services) {
categories.push(key);
}
} else {
for (let key in window.CookieConsent.config.services) {
if (window.CookieConsent.config.services[key].category === category) categories.push(key);
}
}

return categories;
}

static dispatchEvent(elem, event) {
var event;

if (typeof(Event) === 'function') {
event = new Event(event);
} else {
event = document.createEvent('Event');
event.initEvent(event, true, true);
}

elem.dispatchEvent(event);
}
import { el } from 'redom';

export default class Utilities {
static ready(fn) {
if (
document.attachEvent
? document.readyState === 'complete'
: document.readyState !== 'loading'
) {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}

static objectType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}

static lightenDarkenColor(col, amt) {
let usePound = false;

// Handling HEX color format
if (col[0] === '#') {
col = col.slice(1);
usePound = true;

let num = parseInt(col, 16);
let r = (num >> 16) + amt;
let g = ((num >> 8) & 0x00ff) + amt;
let b = (num & 0x0000ff) + amt;

r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));

return (
(usePound ? '#' : '') +
((r << 16) | (g << 8) | b).toString(16).padStart(6, '0')
);
}

// Handling RGBA and RGB color format
if (col.startsWith('rgb')) {
let parts = col.match(
/rgba?\((\d{1,3})\s*,?\s*(\d{1,3})\s*,?\s*(\d{1,3})(?:\s*\/\s*(\d+\.?\d*%?))?\)/i
);

if (parts) {
let r = parseInt(parts[1]) + amt;
let g = parseInt(parts[2]) + amt;
let b = parseInt(parts[3]) + amt;
let a = parts[4] ? parseFloat(parts[4]) : 1;

r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));

// Conditionally format output depending on alpha presence
if (parts[4]) {
return `rgba(${r}, ${g}, ${b}, ${a})`;
} else {
return `rgb(${r}, ${g}, ${b})`;
}
}
}
return col; // Return the original if format is not recognized
}

static removeCookie() {
document.cookie = `cconsent=; expires=Thu, 01 Jan 1980 00:00:00 UTC; path=/;`;
//remove localStorage consentMode obj
localStorage.removeItem('consentMode');
window.CookieConsent.config.cookieExists = false;
}

// Create an array of services from Cookieconsent global object
// Filter based on category or leave empty is all is wanted
static listGlobalServices(category) {
let categories = [];

// Global config objectnot set
if (typeof window.CookieConsent === 'undefined') return categories;

// Category is not specified or opposite
if (typeof category === 'undefined') {
for (let key in window.CookieConsent.config.services) {
categories.push(key);
}
} else {
for (let key in window.CookieConsent.config.services) {
if (window.CookieConsent.config.services[key].category === category)
categories.push(key);
}
}

return categories;
}

static dispatchEvent(elem, event) {
var event;

if (typeof Event === 'function') {
event = new Event(event);
} else {
event = document.createEvent('Event');
event.initEvent(event, true, true);
}

elem.dispatchEvent(event);
}

// Parse HTML and creates paragraph with links
static parseAndCreateParagraph(htmlString) {
const paragraph = el('p');
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlString;

tempDiv.childNodes.forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
paragraph.appendChild(el('span', node.textContent));
} else if (
node.nodeType === Node.ELEMENT_NODE &&
node.tagName.toLowerCase() === 'a'
) {
const href = node.getAttribute('href');
const text = node.textContent;
paragraph.appendChild(
el(
'a',
{ href: href, target: '_blank', rel: 'noopener noreferrer' },
text
)
);
}
});

return paragraph;
}
}

0 comments on commit c42c36c

Please sign in to comment.