All files / coral-component-icon/src/scripts Icon.js

94.09% Statements 175/186
85.82% Branches 121/141
100% Functions 31/31
94.09% Lines 175/186

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584                                                      2x 2x   2x   2x         2x 2x   2x       2x 32x 32x                 2x             2x             2x             2x             2x             2x                 2x 13476x                                               2x                                       2x         2x     2x   2x 14x       2x                                 2x 2x   2x           21702x   21702x 21702x 21702x                 2x     14164x   14164x 13812x     13812x   13812x 336x   214x     122x   122x 74x   4x     70x       48x       122x       336x 336x           13476x       13812x 108480x     436x   13376x 13376x 13376x         14164x 14164x                           62540x 62540x 62540x   62540x   62540x 3940x 58600x 82x 58518x 1712x 1712x 1712x         62540x 62540x   62540x 62540x       62540x 90x 90x 62450x 2122x   2122x 2122x   60328x 8064x                         48760x 3940x   44820x               13057x     13057x 48x               12426x   12426x   12426x 2624x     12426x   12426x 2x 2x 2x 2x           62544x     23348x 23348x   23348x 16732x   16732x                           17688x     30556x   30556x   30556x 20618x   20618x     20618x 2x       20616x 2x   2x   2x       20614x 41228x   41228x 1646x       20614x   14186x   52x 52x 52x 52x   14134x       20614x                           70x     45104x 45104x 45104x   45104x   45104x     21252x   21252x     21252x   21252x 30x   30x     21252x               54282x     8x             74744x     616x         32x                       33542x 33542x 33542x 33542x   33542x 256x     32x 18x     14x       33542x                             2x 2x   2x       2x     2x   2x 2x         2x         31682x                   22140x        
/**
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
import {BaseComponent} from '../../../coral-base-component';
import {transform, validate, commons, i18n} from '../../../coral-utils';
import ICON_MAP from '../../../coral-compat/data/iconMap';
import SPECTRUM_ICONS_PATH from '../resources/spectrum-icons.svg';
import SPECTRUM_ICONS_COLOR_PATH from '../resources/spectrum-icons-color.svg';
import SPECTRUM_CSS_ICONS_PATH from '../resources/spectrum-css-icons.svg';
import loadIcons from './loadIcons';
import {Decorator} from '../../../coral-decorator';
import {SPECTRUM_ICONS, SPECTRUM_ICONS_COLOR, SPECTRUM_CSS_ICONS} from './iconCollection';
 
const SPECTRUM_ICONS_IDENTIFIER = 'spectrum-';
const SPECTRUM_COLORED_ICONS_IDENTIFIER = [
  'ColorLight',
  'Color_Light',
  'ColorDark',
  'Color_Dark',
  'ColorActive',
  'Color_Active',
  // Unique colored icons
  'AdobeExperienceCloudColor',
I  'AdobeExperiencePlatformColor',
];
 
let resourcesPath = (commons.options.icons || '').trim();
if (resourcesPath.length && resourcesPath[resourcesPath.length - 1] !== '/') {
  resourcesPath += '/';
}
 
I// @IE11
const IS_IE11 = !window.ActiveXObject && 'ActiveXObject' in window;
let iconsExternal = commons.options.iconsExternal || 'on';
if (IS_IE11) {
  iconsExternal = 'off';
}
 
const resolveIconsPath = (iconsPath) => {
  const path = commons._script.src;
  return `${path.split('/').slice(0, -iconsPath.split('/').length).join('/')}/${iconsPath}`;
};
 
/**
 Regex used to match URLs. Assume it's a URL if it has a slash, colon, or dot.
 
 @ignore
 */
const URL_REGEX = /\/|:|\./g;
 
/**
 Regex used to match unresolved templates e.g. for data-binding
 
 @ignore
 */
const TEMPLATE_REGEX = /.*\{\{.+\}\}.*/g;
 
/**
 Regex used to split camel case icon names into more screen-reader friendly alt text.
 
 @ignore
 */
const SPLIT_CAMELCASE_REGEX = /([a-z])([A-Z0-9])/g;
 
/**
 Regex used to match the sized spectrum icon prefix
 
 @ignore
 */
const SPECTRUM_ICONS_IDENTIFIER_REGEX = /^spectrum(?:-css)?-icon(?:-\d{1,3})?-/gi;
 
/**
 Regex used match the variant postfix for an icon
 
 @ignore
 */
const ICONS_VARIANT_POSTFIX_REGEX = /(Outline)?(Filled)?(Small|Medium|Large)?(Color)?_?(Active|Dark|Light)?$/;
 
/**
 Translation hint used for localizing default alt text for an icon
 
 @ignore
 */
const ICON_ALT_TRANSLATION_HINT = 'default icon alt text';
 
/**
 Returns capitalized string. This is used to map the icons with their SVG counterpart.
 
 @ignore
 @param {String} s
 @return {String}
 */
const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1);
 
/**
 Enumeration for {@link Icon} sizes.
 
 @typedef {Object} IconSizeEnum
 
 @property {String} EXTRA_EXTRA_SMALL
 Extra extra small size icon, typically 9px size.
 @property {String} EXTRA_SMALL
 Extra small size icon, typically 12px size.
 @property {String} SMALL
 Small size icon, typically 18px size. This is the default size.
 @property {String} MEDIUM
 Medium size icon, typically 24px size.
 @property {String} LARGE
 Large icon, typically 36px size.
 @property {String} EXTRA_LARGE
 Extra large icon, typically 48px size.
 @property {String} EXTRA_EXTRA_LARGE
 Extra extra large icon, typically 72px size.
 */
const size = {
  EXTRA_EXTRA_SMALL: 'XXS',
  EXTRA_SMALL: 'XS',
  SMALL: 'S',
  MEDIUM: 'M',
  LARGE: 'L',
  EXTRA_LARGE: 'XL',
  EXTRA_EXTRA_LARGE: 'XXL'
};
 
 
/**
 Enumeration for {@link Icon} autoAriaLabel value.
 
 @typedef {Object} IconAutoAriaLabelEnum
 
 @property {String} ON
 The aria-label attribute is automatically set based on the icon name.
 @property {String} OFF
 The aria-label attribute is not set and has to be provided explicitly.
 */
const autoAriaLabel = {
  ON: 'on',
  OFF: 'off'
};
 
// icon's base classname
const CLASSNAME = '_coral-Icon';
 
// builds an array containing all possible size classnames. this will be used to remove classnames when the size
// changes
const ALL_SIZE_CLASSES = [];
for (const sizeValue in size) {
  ALL_SIZE_CLASSES.push(`${CLASSNAME}--size${size[sizeValue]}`);
}
 
// Based on https://github.com/adobe/spectrum-css/tree/master/icons
const sizeMap = {
  XXS: 18,
  XS: 24,
  S: 18,
  M: 24,
  L: 18,
  XL: 24,
  XXL: 24
};
 
/**
 @class Coral.Icon
 @classdesc An Icon component. Icon ships with a set of SVG icons.
 @htmltag coral-icon
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const Icon = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    this._elements = {};
  }
 
  /**
   Whether aria-label is set automatically. See {@link IconAutoAriaLabelEnum}.
 
   @type {String}
   @default IconAutoAriaLabelEnum.OFF
   */
  get autoAriaLabel() {
    return this._autoAriaLabel || autoAriaLabel.OFF;
  }
 
  set autoAriaLabel(value) {
    value = transform.string(value).toLowerCase();
    value = validate.enumeration(autoAriaLabel)(value) && value || autoAriaLabel.OFF;
    if(validate.valueMustChange(this._autoAriaLabel, value)) {
      this._autoAriaLabel = value;
      this._updateAltText();
    }
  }
 
  /**
   Icon name.
 
   @type {String}
   @default ""
   @htmlattribute icon
   @htmlattributereflected
   */
  get icon() {
    return this._icon || '';
  }
 
  set icon(value) {
    const icon = transform.string(value).trim();
 
    // Avoid rendering the same icon
    if (icon !== this._icon || this.hasAttribute('_context')) {
      this._icon = icon;
      this._reflectAttribute('icon', this._icon);
 
      // Ignore unresolved templates
      if (this._icon.match(TEMPLATE_REGEX)) {
        return;
      }
 
      // Use the existing img
      if (this._hasRawImage) {
        this._elements.image.classList.add(CLASSNAME, `${CLASSNAME}--image`);
        this._updateAltText();
        return;
      }
E
      // Remove image and SVG elements
      ['image', 'svg'].forEach((type) => {
        const el = this._elements[type] || this.querySelector(`.${CLASSNAME}--${type}`);
        if (el) {
          el.remove();
        }
      });
 
      // Sets the desired icon
      if (this._icon) {
        // Detect if it's a URL
        if (this._icon.match(URL_REGEX)) {
          // Create an image and add it to the icon
          this._elements.image = this._elements.image || document.createElement('img');
          this._elements.image.className = `${CLASSNAME} ${CLASSNAME}--image`;
          this._elements.image.src = this.icon;
          this.appendChild(this._elements.image);
        } else {
          this._updateIcon();
        }
      }
 
      this._updateAltText();
    }
  }
 
  /**
   Size of the icon. It accepts both lower and upper case sizes. See {@link IconSizeEnum}.
 
   @type {String}
   @default IconSizeEnum.SMALL
   @htmlattribute size
   @htmlattributereflected
   */
  get size() {
    return this._size || size.SMALL;
  }
 
  set size(value) {
    const oldSize = this._size;
 
    value = transform.string(value).toUpperCase();
    value = validate.enumeration(size)(value) && value || size.SMALL;
    
    this._reflectAttribute('size', value);
    
    if(validate.valueMustChange(this._size, value)) {
      this._size = value;
 
      // removes all the existing sizes
      this.classList.remove(...ALL_SIZE_CLASSES);
      // adds the new size
      this.classList.add(`${CLASSNAME}--size${value}`);
  
      // We need to update the icon if the size changed
      if (oldSize && oldSize !== value && this.contains(this._elements.svg)) {
        this._elements.svg.remove();
        this._updateIcon();
      }
  E
      this._updateAltText();
    }
  }
 
  /** @private */
  get title() {
    return this.getAttribute('title');
  }
 
  set title(value) {
    thisE.setAttribute('title', value);
  }
 
  /** @private */
  get alt() {
    return this.getAttribute('alt');
  }
 
  set alt(value) {
    this.setAttribute('alt', value);
  }
 
  _updateIcon() {
    let iconId = this.icon;
 
    // If icon name is passed, we have to build the icon Id based on the icon name
    if (iconId.indexOf(SPECTRUM_ICONS_IDENTIFIER) !== 0) {
      const iconMapped = ICON_MAP[iconId];
      let iconName;
 
      // Restore default state
      this.removeAttribute('_context');
 
      if (iconMapped) {
        if (iconMapped.spectrumIcon) {
          // Use the default mapped icon
          iconName = iconMapped.spectrumIcon;
        } else {
          // Verify if icon should be light or dark by looking up parents theme
          const closest = this.closest('.coral--light, .coral--dark, .coral--lightest, .coral--darkest');
 
          if (closest) {
            if (closest.classList.contains('coral--light') || closest.classList.contains('coral--lightest')) {
              // Use light icon
              iconName = iconMapped.spectrumIconLight;
            } else {
              // Use dark icon
              iconName = iconMapped.spectrumIconDark;
            }
          }
          // Use light by default
          else {
            iconName = iconMapped.spectrumIconLight;
          }
 
          // Mark icon as contextual icon because the icon name is defined based on the theme
          this.setAttribute('_context', '');
        }
 
        // Inform user about icon name changes
        if (iconName) {
          commons._log('warn', `Coral.Icon: the icon ${iconId} has been deprecated. Please use ${iconName} instead.`);
        } else {
          commons._log('warn', `Coral.Icon: the icon ${iconId} has been removed. Please contact Icons@Adobe.`);
        }
      }
      // In most cases, using the capitalized icon name maps to the spectrum icon name
      else {
        iconName = capitalize(iconId);
      }
 
      // Verify if icon name is a colored icon
      if (SPECTRUM_COLORED_ICONS_IDENTIFIER.some(identifier => iconName.indexOf(identifier) !== -1)) {
        // Colored icons are 24 by default
        iconId = `spectrum-icon-24-${iconName}`;
      } else {
        const sizeAttribute = this.getAttribute('size');
        const iconSize = sizeMap[sizeAttribute && sizeAttribute.toUpperCase() || size.SMALL];
        iconId = `spectrum-icon-${iconSize}-${iconName}`;
      }
    }
 
    // Insert SVG Icon using HTML because DOMly doesn't support document.createElementNS for <use> element
    this.insertAdjacentHTML('beforeend', this.constructor._renderSVG(iconId));
 
    this._elements.svg = this.lastElementChild;
  }
 
  /**
   Updates the aria-label or img alt attribute depending on value of alt, title, icon and autoAriaLabel.
 
   In cases where the alt attribute has been removed or set to an empty string,
   for example, when the alt property is undefined and we add the attribute alt=''
   to explicitly override the default behavior, or when we remove an alt attribute
   thus restoring the default behavior, we make sure to update the alt text.
   @private
   */
  _updateAltText(value) {
    const hasAutoAriaLabel = this.autoAriaLabel === autoAriaLabel.ON;
    const img = this._elements.image;
    const isImage = this.contains(img);
 
    // alt should be prioritized over title
    let altText = typeof this.alt === 'string' ? this.alt : this.title;
 
    if (typeof value === 'string') {
      altText = this.alt || value;
    } else if (isImage) {
      altText = altText || img.getAttribute('alt') || img.getAttribute('title') || '';
    } else if (hasAutoAriaLabel) {
      let iconName = this.icon.replace(SPECTRUM_ICONS_IDENTIFIER_REGEX, '');
      iconName = iconName.replace(ICONS_VARIANT_POSTFIX_REGEX, '');
      altText = i18n.get(iconName.replace(SPLIT_CAMELCASE_REGEX, '$1 $2').toLowerCase(), ICON_ALT_TRANSLATION_HINT);
    }
 
    // If no other role has been set, provide the appropriate
    // role depending on whether or not the icon is an arbitrary image URL.
    const role = this.getAttribute('role');
    const roleOverride = role && (role !== 'presentation' && role !== 'img');
    if (!roleOverride) {
      this.setAttribute('role', isImage ? 'presentation' : 'img');
    }
 
    // Set accessibility attributes accordingly
    if (isImage) {
      hasAutoAriaLabel && this.removeAttribute('aria-label');
      img.setAttribute('alt', altText);
    } else if (altText === '') {
      this.removeAttribute('aria-label');
      if (!roleOverride) {
        this.removeAttribute('role');
      }
    } else if (altText) {
      this.setAttribute('aria-label', altText);
    }
  }
 
  /**
   Whether SVG icons are referenced as external resource (on/off)
 
   @return {String}
   */
  static _iconsExternal() {
    return iconsExternal;
  }
 
  /**
   Returns the SVG markup.
 
   @param {String} iconId
   @param {Array.<String>} additionalClasses
   @return {String}
   */
  static _renderSVG(iconId, additionalClasses = []) {
    additionalClasses.unshift(CLASSNAME);
    additionalClasses.unshift(`${CLASSNAME}--svg`);
 
    let iconPath = `#${iconId}`;
 
    // If not colored icons
    if (this._iconsExternal() === 'on' && !SPECTRUM_COLORED_ICONS_IDENTIFIER.some(identifier => iconId.indexOf(identifier) !== -1)) {
      // Generate spectrum-css-icons path
      if (iconId.indexOf('spectrum-css') === 0) {
        iconPath = resourcesPath ? `${resourcesPath}${SPECTRUM_CSS_ICONS}.svg#${iconId}` : `${resolveIconsPath(SPECTRUM_CSS_ICONS_PATH)}#${iconId}`;
      }
      // Generate spectrum-icons path
      else {
        iconPath = resourcesPath ? `${resourcesPath}${SPECTRUM_ICONS}.svg#${iconId}` : `${resolveIconsPath(SPECTRUM_ICONS_PATH)}#${iconId}`;
      }
    }
 
    return `
      <svg focusable="false" aria-hidden="true" class="${additionalClasses.join(' ')}">
        <use xlink:href="${iconPath}"></use>
      </svg>
    `;
  }
 
  /**
   Returns {@link Icon} sizes.
 
   @return {IconSizeEnum}
   */
  static get size() {
    return size;
  }
 
  /**
   Returns {@link Icon} autoAriaLabel options.
 
   @return {IconAutoAriaLabelEnum}
   */
  static get autoAriaLabel() {
    return autoAriaLabel;
  }
 
  /**
   Loads the SVG icons. It's requesting the icons based on the JS file path by default.
 
   @param {String} [url] SVG icons url.
   */
  static load(url) {
    const resolveIconsPath = (iconsPath) => {
      const path = commons._script.src;
      if (iconsExternal === 'js') {
        iconsPath = iconsPath.replace('.svg', '.js');
      }
 
      return `${path.split('/').slice(0, -iconsPath.split('/').length).join('/')}/${iconsPath}`;
    };
 
    if (url === SPECTRUM_ICONS) {
      url = resolveIconsPath(SPECTRUM_ICONS_PATH);
    } else if (url === SPECTRUM_ICONS_COLOR) {
      url = resolveIconsPath(SPECTRUM_ICONS_COLOR_PATH);
    } else if (url === SPECTRUM_CSS_ICONS) {
      url = resolveIconsPath(SPECTRUM_CSS_ICONS_PATH);
    }
 
    loadIcons(url);
  }
 
  static get _attributePropertyMap() {
    return commons.extend(super._attributePropertyMap, {
      autoarialabel: 'autoAriaLabel'
    });
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['autoarialabel', 'icon', 'size', 'alt', 'title']);
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === 'alt' || name === 'title') {
      this._updateAltText(value);
    } else {
      super.attributeChangedCallback(name, oldValue, value);
    }
  }
 
  /** @ignore */
  connectedCallback() {
    super.connectedCallback();
 
    // Contextual icons need to be checked again
    if (this.hasAttribute('_context')) {
      this.icon = this.icon;
    }
  }
 
  /** @ignore */
  renderI() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // Set default size
    if (!this._size) {
      Ithis.size = size.SMALL;
    }
E
    const img = this.querySelector(`img:not(.${CLASSNAME}--image)`);
    if (img) {
      this._elements.image = img;
      this._hasRawImage = true;
      this.icon = img.getAttribute('src');
      this._hasRawImage = false;
    }
  }
});
 
// Load icon collections by default
const iconCollections = [SPECTRUM_ICONS_COLOR];
let extension = '.svg';
if (Icon._iconsExternal() === 'off' || Icon._iconsExternal() === 'js') {
  iconCollections.push(SPECTRUM_CSS_ICONS);
  iconCollections.push(SPECTRUM_ICONS);
 
  if (Icon._iconsExternal() === 'js') {
    extension = '.js';
  }
}
iconCollections.forEach(iconSet => Icon.load(resourcesPath ? `${resourcesPath}${iconSet}${extension}` : iconSet));
 
export default Icon;