<commit_before>// Copyright 2017, University of Colorado Boulder

/**
 * A method that is meant to be composed with Node. This method is responsible for updating DOM Elements the
 * AccessiblePeer when A11y settings on the Node change. This method should only be called when it is necessary to
 * recreate DOM elements. If small changes, or content changes are needed than it is preferable to just updating
 * those pieces manually (better performance).
 *
 * This file is responsible for creating the AccessiblePeer for each AccessibleInstance of Node.
 *
 * NOTE: This function assumes that "this" is of type {Node}, such that this function is added to a Node's prototype.
 *
 * @author <NAME> (PhET Interactive Simulations)
 * @author <NAME> (PhET Interactive Simulations)
 */

define( function( require ) {
  'use strict';

  // modules
  var AccessibilityUtil = require( 'SCENERY/accessibility/AccessibilityUtil' );
  var AccessiblePeer = require( 'SCENERY/accessibility/AccessiblePeer' );
  var scenery = require( 'SCENERY/scenery' );

  // constants
  var A_TAG = AccessibilityUtil.TAGS.A;
  var INPUT_TAG = AccessibilityUtil.TAGS.INPUT;
  var LABEL_TAG = AccessibilityUtil.TAGS.LABEL;

  // these elements require a minimum width to be visible in Safari
  var ELEMENTS_REQUIRE_WIDTH = [ INPUT_TAG, A_TAG ];

  /**
   * Invalidate our current accessible content, triggering recomputation
   * of anything that depended on the old accessible content. This can be
   * combined with a client (subType) implementation of invalidateAccessibleContent as well.
   *
   * Set's the `accessibleContent` of "this" (hopefully of type {Node}).
   *
   * @protected
   */
  var invalidateAccessibleContent = function() {
    var self = this;

    // iteration variable used through this function
    var i = 0;

    // for each accessible peer, clear the container parent if it exists since we will be reinserting labels and
    // the dom element in createPeer
    this.updateAccessiblePeers( function( accessiblePeer ) {
      var containerElement = accessiblePeer.containerParent;
      while ( containerElement && containerElement.hasChildNodes() ) {
        containerElement.removeChild( containerElement.lastChild );
      }
    } );

    // if any parents are flagged as removed from the accessibility tree, set content to null
    var contentDisplayed = this._accessibleContentDisplayed;
    for ( i = 0; i < this._parents.length; i++ ) {
      if ( !this._parents[ i ].accessibleContentDisplayed ) {
        contentDisplayed = false;
      }
    }

    var accessibleContent = null;
    if ( contentDisplayed && this._tagName ) {
      accessibleContent = {
        createPeer: function( accessibleInstance ) {

          var uniqueId = accessibleInstance.trail.getUniqueId();

          // create the base DOM element representing this accessible instance
          var primarySibling = createElement( self._tagName, self.focusable, {
            namespace: self._accessibleNamespace
          } );
          primarySibling.id = uniqueId;

          // create the container parent for the dom siblings
          var containerElement = null;
          if ( self._containerTagName ) {
            containerElement = createElement( self._containerTagName, false );
            containerElement.id = 'container-' + uniqueId;

            // provide the aria-role if it is specified
            if ( self._containerAriaRole ) {
              containerElement.setAttribute( 'role', self._containerAriaRole );
            }
          }

          // create the label DOM element representing this instance
          var labelSibling = null;
          if ( self._labelTagName ) {
            labelSibling = createElement( self._labelTagName, false );
            labelSibling.id = 'label-' + uniqueId;

            if ( self._labelTagName.toUpperCase() === LABEL_TAG ) {
              labelSibling.setAttribute( 'for', uniqueId );
            }
          }

          // create the description DOM element representing this instance
          var descriptionSibling = null;
          if ( self._descriptionTagName ) {
            descriptionSibling = createElement( self._descriptionTagName, false );
            descriptionSibling.id = 'description-' + uniqueId;
          }

          var accessiblePeer = AccessiblePeer.createFromPool( accessibleInstance, primarySibling, {
            containerParent: containerElement,
            labelSibling: labelSibling,
            descriptionSibling: descriptionSibling
          } );
          accessibleInstance.peer = accessiblePeer;

          // set the accessible label now that the element has been recreated again, but not if the tagName
          // has been cleared out
          if ( self._labelContent && self._labelTagName !== null ) {
            var isLabelTag = self._labelTagName.toUpperCase() === LABEL_TAG;
            accessiblePeer.setLabelSiblingContent( self._labelContent, isLabelTag );
          }

          // restore the innerContent
          if ( self._innerContent && self._descriptionTagName !== null ) {
            accessiblePeer.setPrimarySiblingContent( self._innerContent );
          }

          // set the accessible description, but not if the tagName has been cleared out.
          if ( self._descriptionContent && self._descriptionTagName !== null ) {
            accessiblePeer.setDescriptionSiblingContent( self._descriptionContent );
          }

          // set if using aria-label
          if ( self._ariaLabel ) {
            self.setAriaLabel( self._ariaLabel );
          }

          // restore visibility
          self.setAccessibleVisible( self._accessibleVisible );

          // restore checked
          self.setAccessibleChecked( self._accessibleChecked );

          // restore input value
          self._inputValue && self.setInputValue( self._inputValue );<commit_msg>accessiblePeer should set content for its own primary siblings, https://github.com/phetsims/scenery/issues/800<commit_after>// Copyright 2017, University of Colorado Boulder

/**
 * A method that is meant to be composed with Node. This method is responsible for updating DOM Elements the
 * AccessiblePeer when A11y settings on the Node change. This method should only be called when it is necessary to
 * recreate DOM elements. If small changes, or content changes are needed than it is preferable to just updating
 * those pieces manually (better performance).
 *
 * This file is responsible for creating the AccessiblePeer for each AccessibleInstance of Node.
 *
 * NOTE: This function assumes that "this" is of type {Node}, such that this function is added to a Node's prototype.
 *
 * @author <NAME> (PhET Interactive Simulations)
 * @author <NAME> (PhET Interactive Simulations)
 */

define( function( require ) {
  'use strict';

  // modules
  var AccessibilityUtil = require( 'SCENERY/accessibility/AccessibilityUtil' );
  var AccessiblePeer = require( 'SCENERY/accessibility/AccessiblePeer' );
  var scenery = require( 'SCENERY/scenery' );

  // constants
  var A_TAG = AccessibilityUtil.TAGS.A;
  var INPUT_TAG = AccessibilityUtil.TAGS.INPUT;
  var LABEL_TAG = AccessibilityUtil.TAGS.LABEL;

  // these elements require a minimum width to be visible in Safari
  var ELEMENTS_REQUIRE_WIDTH = [ INPUT_TAG, A_TAG ];

  /**
   * Invalidate our current accessible content, triggering recomputation
   * of anything that depended on the old accessible content. This can be
   * combined with a client (subType) implementation of invalidateAccessibleContent as well.
   *
   * Set's the `accessibleContent` of "this" (hopefully of type {Node}).
   *
   * @protected
   */
  var invalidateAccessibleContent = function() {
    var self = this;

    // iteration variable used through this function
    var i = 0;

    // for each accessible peer, clear the container parent if it exists since we will be reinserting labels and
    // the dom element in createPeer
    this.updateAccessiblePeers( function( accessiblePeer ) {
      var containerElement = accessiblePeer.containerParent;
      while ( containerElement && containerElement.hasChildNodes() ) {
        containerElement.removeChild( containerElement.lastChild );
      }
    } );

    // if any parents are flagged as removed from the accessibility tree, set content to null
    var contentDisplayed = this._accessibleContentDisplayed;
    for ( i = 0; i < this._parents.length; i++ ) {
      if ( !this._parents[ i ].accessibleContentDisplayed ) {
        contentDisplayed = false;
      }
    }

    var accessibleContent = null;
    if ( contentDisplayed && this._tagName ) {
      accessibleContent = {
        createPeer: function( accessibleInstance ) {

          var uniqueId = accessibleInstance.trail.getUniqueId();

          // create the base DOM element representing this accessible instance
          var primarySibling = createElement( self._tagName, self.focusable, {
            namespace: self._accessibleNamespace
          } );
          primarySibling.id = uniqueId;

          // create the container parent for the dom siblings
          var containerElement = null;
          if ( self._containerTagName ) {
            containerElement = createElement( self._containerTagName, false );
            containerElement.id = 'container-' + uniqueId;

            // provide the aria-role if it is specified
            if ( self._containerAriaRole ) {
              containerElement.setAttribute( 'role', self._containerAriaRole );
            }
          }

          // create the label DOM element representing this instance
          var labelSibling = null;
          if ( self._labelTagName ) {
            labelSibling = createElement( self._labelTagName, false );
            labelSibling.id = 'label-' + uniqueId;

            if ( self._labelTagName.toUpperCase() === LABEL_TAG ) {
              labelSibling.setAttribute( 'for', uniqueId );
            }
          }

          // create the description DOM element representing this instance
          var descriptionSibling = null;
          if ( self._descriptionTagName ) {
            descriptionSibling = createElement( self._descriptionTagName, false );
            descriptionSibling.id = 'description-' + uniqueId;
          }

          var accessiblePeer = AccessiblePeer.createFromPool( accessibleInstance, primarySibling, {
            containerParent: containerElement,
            labelSibling: labelSibling,
            descriptionSibling: descriptionSibling
          } );
          accessibleInstance.peer = accessiblePeer;

          // set the accessible label now that the element has been recreated again, but not if the tagName
          // has been cleared out
          if ( self._labelContent && self._labelTagName !== null ) {
            var isLabelTag = self._labelTagName.toUpperCase() === LABEL_TAG;
            accessiblePeer.setLabelSiblingContent( self._labelContent, isLabelTag );
          }

          // restore the innerContent
          if ( self._innerContent && self._innerContent !== null ) {
            accessiblePeer.setPrimarySiblingContent( self._innerContent );
          }

          // set the accessible description, but not if the tagName has been cleared out.
          if ( self._descriptionContent && self._descriptionTagName !== null ) {
            accessiblePeer.setDescriptionSiblingContent( self._descriptionContent );
          }

          // set if using aria-label
          if ( self._ariaLabel ) {
            self.setAriaLabel( self._ariaLabel );
          }

          // restore visibility
          self.setAccessibleVisible( self._accessibleVisible );

          // restore checked
          self.setAccessibleChecked( self._accessibleChecked );

          // restore input value
          self._inputValue && self.setInputValue( self._inputValue );
{'fraction_non_alphanumeric': 0.07810520982859073, 'fraction_numerical': 0.0016043232289115934, 'mean_word_length': 2.60328567082446, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4034985', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2878, 'n_words': 961}