Skip to content

Commit

Permalink
fix: decode string when containing hexcode
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosVillasenor committed Aug 27, 2024
1 parent fa7e648 commit d29c1f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/js/menu/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ClickableComponent from '../clickable-component.js';
import Component from '../component.js';
import {createEl} from '../utils/dom.js';
import { containsHexCode, decodeString } from '../utils/str.js';

/** @import Player from '../player' */

Expand Down Expand Up @@ -68,10 +69,18 @@ class MenuItem extends ClickableComponent {
tabIndex: -1
}, props), attrs);

const createTextContent = () => {
if (containsHexCode(this.options_.label)) {
return this.localize(decodeString(this.options_.label));

Check warning on line 74 in src/js/menu/menu-item.js

View check run for this annotation

Codecov / codecov/patch

src/js/menu/menu-item.js#L74

Added line #L74 was not covered by tests
}

return this.localize(this.options_.label);
};

// swap icon with menu item text.
const menuItemEl = createEl('span', {
className: 'vjs-menu-item-text',
textContent: this.localize(this.options_.label)
textContent: createTextContent()
});

// If using SVG icons, the element with vjs-icon-placeholder will be added separately.
Expand Down
27 changes: 27 additions & 0 deletions src/js/utils/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,30 @@ export const toTitleCase = function(string) {
export const titleCaseEquals = function(str1, str2) {
return toTitleCase(str1) === toTitleCase(str2);
};

/**
*
* @param {string} string
* The string that will be tested
*
* @return {boolean}
* Whether the string contains a Hex Code
*/
export const containsHexCode = (string) => {
return /(&#x[0-9a-fA-F]{2,4};)/.test(string);
};

/**
*
* @param {string} string
* The string that will be decoded
*
* @return {string}
* Decoded string without problematic characters
*/
export const decodeString = (string) => {
// eslint-disable-next-line no-undef
const doc = new DOMParser().parseFromString(string, 'text/html');

Check warning on line 78 in src/js/utils/str.js

View check run for this annotation

Codecov / codecov/patch

src/js/utils/str.js#L78

Added line #L78 was not covered by tests

return doc.documentElement.textContent;

Check warning on line 80 in src/js/utils/str.js

View check run for this annotation

Codecov / codecov/patch

src/js/utils/str.js#L80

Added line #L80 was not covered by tests
};

0 comments on commit d29c1f7

Please sign in to comment.