Fix #527: validate cursorColor value and apply default if it fails (#590)

refactor `validateColor`: remove unnecessary variable declaration
This commit is contained in:
James 2016-08-18 09:33:09 +01:00 committed by Leo Lamprecht
parent e2b270b172
commit 125fdd98e5

View file

@ -4,6 +4,7 @@ import Color from 'color';
import hterm from '../hterm';
import Component from '../component';
import { getColorList } from '../utils/colors';
import notify from '../utils/notify';
export default class Term extends Component {
@ -28,7 +29,7 @@ export default class Term extends Component {
this.term.prefs_.set('font-family', props.fontFamily);
this.term.prefs_.set('font-size', props.fontSize);
this.term.prefs_.set('font-smoothing', props.fontSmoothing);
this.term.prefs_.set('cursor-color', Color(props.cursorColor).rgbString());
this.term.prefs_.set('cursor-color', this.validateColor(props.cursorColor, 'rgba(255,255,255,0.5)'));
this.term.prefs_.set('enable-clipboard-notice', false);
this.term.prefs_.set('foreground-color', props.foregroundColor);
this.term.prefs_.set('background-color', props.backgroundColor);
@ -158,6 +159,15 @@ export default class Term extends Component {
return URL.createObjectURL(blob);
}
validateColor (color, alternative = 'rgb(255,255,255)') {
try {
return Color(color).rgbString();
} catch (err) {
notify(`color "${color}" is invalid`);
}
return alternative;
}
componentWillReceiveProps (nextProps) {
if (this.props.url !== nextProps.url) {
// when the url prop changes, we make sure
@ -201,7 +211,7 @@ export default class Term extends Component {
}
if (this.props.cursorColor !== nextProps.cursorColor) {
this.term.prefs_.set('cursor-color', Color(nextProps.cursorColor).rgbString());
this.term.prefs_.set('cursor-color', this.validateColor(nextProps.cursorColor, 'rgba(255,255,255,0.5)'));
}
if (this.props.cursorShape !== nextProps.cursorShape) {