Skip to content

Commit 9448336

Browse files
Merge pull request #6 from shashankaccolite/Fixes-#122
Fix-#122(Update the froala-sanitize.js)
2 parents e43f32c + 932aefd commit 9448336

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

src/froala-sanitize.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,71 @@ function $SanitizeProvider() {
587587
replace(/>/g, '>');
588588
}
589589

590+
// Custom logic for accepting certain style options only - angularFroala
591+
// Currently allows only the color, background-color, text-align, direction, font-family, font-size, float, width and height attributes
592+
// all other attributes should be easily done through classes.
593+
function validStyles(styleAttr) {
594+
var result = '';
595+
var styleArray = styleAttr.split(';');
596+
angular.forEach(styleArray, function (value) {
597+
var v = value.split(':');
598+
if (v.length === 2) {
599+
var key = trim(v[0].toLowerCase());
600+
value = trim(v[1].toLowerCase());
601+
if (
602+
(key === 'color' || key === 'background-color') && (
603+
value.match(/^rgb\([0-9%,\. ]*\)$/i) ||
604+
value.match(/^rgba\([0-9%,\. ]*\)$/i) ||
605+
value.match(/^hsl\([0-9%,\. ]*\)$/i) ||
606+
value.match(/^hsla\([0-9%,\. ]*\)$/i) ||
607+
value.match(/^#[0-9a-f]{3,6}$/i) ||
608+
value.match(/^[a-z]*$/i)
609+
) ||
610+
key === 'text-align' && (
611+
value === 'left' ||
612+
value === 'right' ||
613+
value === 'center' ||
614+
value === 'justify'
615+
) ||
616+
key === 'float' && (
617+
value === 'left' ||
618+
value === 'right' ||
619+
value === 'none'
620+
) ||
621+
key === 'direction' && (
622+
value === 'rtl' ||
623+
value === 'ltr'
624+
) ||
625+
key === 'font-family' && (
626+
value === 'arial,helvetica,sans-serif' ||
627+
value === 'georgia,serif' ||
628+
value === 'impact,charcoal,sans-serif' ||
629+
value === 'tahoma,geneva,sans-serif' ||
630+
value === "'times new roman',times,serif" ||
631+
value === 'verdana,geneva,sans-serif'
632+
) ||
633+
(key === 'width' || key === 'height' || key === 'font-size' || key === 'margin-left') && (
634+
value.match(/[0-9\.]*(px|em|rem|%)/)
635+
)
636+
) {
637+
result += key + ': ' + value + ';';
638+
}
639+
}
640+
});
641+
return result;
642+
}
643+
644+
// this function is used to manually allow specific attributes on specific tags with certain prerequisites
645+
function validCustomTag(tag, attrs, lkey, value) {
646+
// catch the div placeholder for the iframe replacement
647+
if (tag === 'img' && attrs['ta-insert-video']) {
648+
if (lkey === 'ta-insert-video' || lkey === 'allowfullscreen' || lkey === 'frameborder' || (lkey === 'contenteditble' && value === 'false')) {
649+
return true;
650+
}
651+
}
652+
return false;
653+
}
654+
590655
/**
591656
* create an HTML/XML writer which writes to buffer
592657
* @param {Array} buf use buf.join('') to get out sanitized html string
@@ -612,7 +677,7 @@ function $SanitizeProvider() {
612677
forEach(attrs, function(value, key) {
613678
var lkey = lowercase(key);
614679
var isImage = (tag === 'img' && lkey === 'src') || (lkey === 'background');
615-
if (validAttrs[lkey] === true &&
680+
if ((lkey === 'style' && (value = validStyles(value)) !== '') || validCustomTag(tag, attrs, lkey, value) || validAttrs[lkey] === true &&
616681
(uriAttrs[lkey] !== true || uriValidator(value, isImage))) {
617682
out(' ');
618683
out(key);

0 commit comments

Comments
 (0)