All files / src SSVGFilter.js

100% Statements 15/15
91.66% Branches 11/12
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                              84x   84x   84x   84x 84x 5x                     1076627x 1065502x   11125x   11125x   9x   11116x 572x     10544x   11125x          
// @ts-check
 
import { parseDomCallback } from './parser';
import { defaultTo } from './utils';
 
/**
 * @brief filter class
 * @details used to handle onvalue attributes or properties and filter target
 * values.
 */
class SSVGFilter {
  /**
   * @param {Element} element [description]
   */
  constructor(element) {
    this.element = element;
    /** @type {any} */
    this._value = undefined;
    /** @type {any} */
    this._result = undefined;
 
    const attr = this.element.getAttribute('onupdate');
    if (attr) {
      this._onupdate = parseDomCallback(attr, [ '$value', '$unit' ],
        defaultTo(element.ownerDocument, document).defaultView || undefined);
    }
  }
 
  /**
   * @param  {ssvg.$Value} value
   * @param  {string} [unit]
   * @return {string|number}
   */
  filter(value, unit) {
    if (value === this._value) {
      return this._result;
    }
    this._value = value;
    // @ts-ignore: added to the DOM
    if (this.element.onupdate) {
      // @ts-ignore: added to the DOM
      this._result = this.element.onupdate(value, unit);
    }
    else if (this._onupdate) {
      this._result = this._onupdate.call(this.element, value, unit);
    }
    else {
      this._result = (unit) ? (value + unit) : value;
    }
    return this._result;
  }
}
 
export default SSVGFilter;