WordPress/wp-includes/js/wp-a11y.js

108 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
* @output wp-includes/js/wp-a11y.js
*/
/** @namespace wp */
window.wp = window.wp || {};
( function ( wp, $ ) {
'use strict';
var $containerPolite,
$containerAssertive,
previousMessage = '';
/**
* Update the ARIA live notification area text node.
*
* @since 4.2.0
* @since 4.3.0 Introduced the 'ariaLive' argument.
*
File Editors: Introduce sandboxed live editing of PHP files with rollbacks for both themes and plugins. * Edits to active plugins which cause PHP fatal errors will no longer auto-deactivate the plugin. Supersedes #39766. * Introduce sandboxed PHP file edits for active themes, preventing accidental whitescreening of a user's site when introducing a fatal error. * After writing a change to a PHP file for an active theme or plugin, perform loopback requests on the file editor admin screens and the homepage to check for fatal errors. If a fatal error is encountered, roll back the edited file and display the error to the user to fix and try again. * Introduce a secure way to scrape PHP fatal errors from a site via `wp_start_scraping_edited_file_errors()` and `wp_finalize_scraping_edited_file_errors()`. * Moves file modifications from `theme-editor.php` and `plugin-editor.php` to common `wp_edit_theme_plugin_file()` function. * Refactor themes and plugin editors to submit file changes via Ajax instead of doing full page refreshes when JS is available. * Use `get` method for theme/plugin dropdowns. * Improve styling of plugin editors, including width of plugin/theme dropdowns. * Improve notices API for theme/plugin editor JS component. * Strip common base directory from plugin file list. See #24048. * Factor out functions to list editable file types in `wp_get_theme_file_editable_extensions()` and `wp_get_plugin_file_editable_extensions()`. * Scroll to line in editor that has linting error when attempting to save. See #41886. * Add checkbox to dismiss lint errors to proceed with saving. See #41887. * Only style the Update File button as disabled instead of actually disabling it for accessibility reasons. * Ensure that value from CodeMirror is used instead of `textarea` when CodeMirror is present. * Add "Are you sure?" check when leaving editor when there are unsaved changes. Supersedes [41560]. See #39766, #24048, #41886. Props westonruter, Clorith, melchoyce, johnbillion, jjj, jdgrimes, azaozz. Fixes #21622, #41887. Built from https://develop.svn.wordpress.org/trunk@41721 git-svn-id: http://core.svn.wordpress.org/trunk@41555 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-10-04 02:20:45 +02:00
* @param {String} message The message to be announced by Assistive Technologies.
* @param {String} [ariaLive] The politeness level for aria-live. Possible values:
* polite or assertive. Default polite.
* @returns {void}
*/
function speak( message, ariaLive ) {
// Clear previous messages to allow repeated strings being read out.
clear();
// Ensure only text is sent to screen readers.
message = $( '<p>' ).html( message ).text();
/*
* Safari 10+VoiceOver don't announce repeated, identical strings. We use
* a `no-break space` to force them to think identical strings are different.
* See ticket #36853.
*/
if ( previousMessage === message ) {
message = message + '\u00A0';
}
previousMessage = message;
if ( $containerAssertive && 'assertive' === ariaLive ) {
$containerAssertive.text( message );
} else if ( $containerPolite ) {
$containerPolite.text( message );
}
}
/**
* Build the live regions markup.
*
* @since 4.3.0
*
* @param {String} ariaLive Optional. Value for the 'aria-live' attribute, default 'polite'.
*
* @return {Object} $container The ARIA live region jQuery object.
*/
function addContainer( ariaLive ) {
ariaLive = ariaLive || 'polite';
var $container = $( '<div>', {
'id': 'wp-a11y-speak-' + ariaLive,
'aria-live': ariaLive,
'aria-relevant': 'additions text',
'aria-atomic': 'true',
'class': 'screen-reader-text wp-a11y-speak-region'
});
$( document.body ).append( $container );
return $container;
}
/**
* Clear the live regions.
*
* @since 4.3.0
*/
function clear() {
$( '.wp-a11y-speak-region' ).text( '' );
}
/**
* Initialize wp.a11y and define ARIA live notification area.
*
* @since 4.2.0
* @since 4.3.0 Added the assertive live region.
*/
$( document ).ready( function() {
$containerPolite = $( '#wp-a11y-speak-polite' );
$containerAssertive = $( '#wp-a11y-speak-assertive' );
if ( ! $containerPolite.length ) {
$containerPolite = addContainer( 'polite' );
}
if ( ! $containerAssertive.length ) {
$containerAssertive = addContainer( 'assertive' );
}
});
/** @namespace wp.a11y */
wp.a11y = wp.a11y || {};
wp.a11y.speak = speak;
}( window.wp, window.jQuery ));