浏览代码

accessibility improvements; announce img/video alt tags, punctuate screen reader text content (#3757, #3772)

Hakim El Hattab 1 月之前
父节点
当前提交
5412639a54
共有 5 个文件被更改,包括 34 次插入3 次删除
  1. 1 1
      dist/reveal.esm.js
  2. 0 0
      dist/reveal.esm.js.map
  3. 1 1
      dist/reveal.js
  4. 0 0
      dist/reveal.js.map
  5. 32 1
      js/reveal.js

文件差异内容过多而无法显示
+ 1 - 1
dist/reveal.esm.js


文件差异内容过多而无法显示
+ 0 - 0
dist/reveal.esm.js.map


文件差异内容过多而无法显示
+ 1 - 1
dist/reveal.js


文件差异内容过多而无法显示
+ 0 - 0
dist/reveal.js.map


+ 32 - 1
js/reveal.js

@@ -394,7 +394,7 @@ export default function( revealElement, options ) {
 
 		// Text node
 		if( node.nodeType === 3 ) {
-			text += node.textContent;
+			text += node.textContent.trim();
 		}
 		// Element node
 		else if( node.nodeType === 1 ) {
@@ -403,10 +403,25 @@ export default function( revealElement, options ) {
 			let isDisplayHidden = window.getComputedStyle( node )['display'] === 'none';
 			if( isAriaHidden !== 'true' && !isDisplayHidden ) {
 
+				// Capture alt text from img and video elements
+				if( node.tagName === 'IMG' || node.tagName === 'VIDEO' ) {
+					let altText = node.getAttribute( 'alt' );
+					if( altText ) {
+						text += ensurePunctuation( altText );
+					}
+				}
+
 				Array.from( node.childNodes ).forEach( child => {
 					text += getStatusText( child );
 				} );
 
+				// Add period after block-level text elements to improve
+				// screen reader experience
+				const textElements = ['P', 'DIV', 'UL', 'OL', 'LI', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE'];
+				if( textElements.includes( node.tagName ) && text.trim() !== '' ) {
+					text = ensurePunctuation( text );
+				}
+
 			}
 
 		}
@@ -417,6 +432,22 @@ export default function( revealElement, options ) {
 
 	}
 
+	/**
+	 * Ensures text ends with proper punctuation by adding a period
+	 * if it doesn't already end with punctuation.
+	 */
+	function ensurePunctuation( text ) {
+
+		const trimmedText = text.trim();
+
+		if( trimmedText === '' ) {
+			return text;
+		}
+
+		return !/[.!?]$/.test(trimmedText) ? trimmedText + '.' : trimmedText;
+
+	}
+
 	/**
 	 * This is an unfortunate necessity. Some actions – such as
 	 * an input field being focused in an iframe or using the

部分文件因为文件数量过多而无法显示