-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(get/active-element): facility to obtain the currently active …
…element
- Loading branch information
1 parent
fd74412
commit 63cfb17
Showing
8 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
layout: doc-api.html | ||
tags: shadow-dom | ||
--- | ||
|
||
# ally.get.activeElement | ||
|
||
Identifies the element that currently has focus | ||
|
||
|
||
## Description | ||
|
||
This utility is a safeguard for `document.activeElement` presenting invalid references. | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
var element = ally.get.activeElement(); | ||
|
||
var iframeElement = ally.get.activeElement({ | ||
context: iframeDocument, | ||
}); | ||
``` | ||
|
||
### Arguments | ||
|
||
|
||
### Returns | ||
|
||
[`HTMLElement`](https://developer.mozilla.org/en/docs/Web/API/HTMLElement). | ||
|
||
### Throws | ||
|
||
|
||
## Examples | ||
|
||
|
||
## Changes | ||
|
||
* Added in `v#master`. | ||
|
||
|
||
## Notes | ||
|
||
|
||
## Related resources | ||
|
||
* [`ally.get.activeElements`](active-elements.md) | ||
|
||
|
||
## Contributing | ||
|
||
* [module source](https://github.com/medialize/ally.js/blob/master/src/get/active-element.js) | ||
* [document source](https://github.com/medialize/ally.js/blob/master/docs/api/get/active-element.md) | ||
* [unit test](https://github.com/medialize/ally.js/blob/master/test/unit/get.active-element.test.js) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
/* | ||
create ally.get.activeElement() | ||
wrapping ally.get.activeElements() | ||
fix ally.get.activeElements() | ||
https://github.com/jquery/jquery-ui/blob/ffcfb85c9818954adda69e73cf9ba76ea07b554c/ui/safe-active-element.js | ||
*/ | ||
|
||
import getDocument from '../util/get-document'; | ||
|
||
export default function({ context } = {}) { | ||
const _document = getDocument(context); | ||
let activeElement; | ||
|
||
try { | ||
// IE9 throws an "Unspecified error" accessing document.activeElement from an <iframe> | ||
// see https://github.com/jquery/jquery-ui/blob/ffcfb85c9818954adda69e73cf9ba76ea07b554c/ui/safe-active-element.js#L15-L21 | ||
activeElement = _document.activeElement; | ||
} catch (e) { | ||
// ignore | ||
} | ||
|
||
// IE11 may return null instead of an element | ||
// see https://github.com/jquery/jquery-ui/blob/ffcfb85c9818954adda69e73cf9ba76ea07b554c/ui/safe-active-element.js#L23-L28 | ||
// https://github.com/jquery/jquery-ui/blob/ffcfb85c9818954adda69e73cf9ba76ea07b554c/ui/safe-active-element.js#L30-L35 | ||
if (!activeElement || !activeElement.nodeType) { | ||
activeElement = _document.body || _document.documentElement; | ||
} | ||
|
||
return activeElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
define(function(require) { | ||
'use strict'; | ||
|
||
var bdd = require('intern!bdd'); | ||
var expect = require('intern/chai!expect'); | ||
var focusableFixture = require('../helper/fixtures/focusable.fixture'); | ||
var TestFrame = require('../helper/test-frame'); | ||
var getActiveElement = require('ally/get/active-element'); | ||
|
||
bdd.describe('get/active-element', function() { | ||
var fixture; | ||
var frame; | ||
|
||
bdd.before(function() { | ||
fixture = focusableFixture(); | ||
|
||
frame = new TestFrame([ | ||
/*eslint-disable indent */ | ||
'<!DOCTYPE html>', | ||
'<html lang="en">', | ||
'<head>', | ||
'<meta charset="utf-8" />', | ||
'<title>Framed Content</title>', | ||
'</head>', | ||
'<body>', | ||
'<p id="target" tabindex="0">Hello World</p>', | ||
'</body>', | ||
'</html>', | ||
/*eslint-enable indent */ | ||
].join('')); | ||
|
||
return frame.initialize(document.body); | ||
}); | ||
|
||
bdd.after(function() { | ||
fixture.remove(); | ||
fixture = null; | ||
frame.terminate(); | ||
frame = null; | ||
}); | ||
|
||
bdd.it('should return the active element of the current document', function() { | ||
var target = document.getElementById('tabindex-1'); | ||
target.focus(); | ||
|
||
var result = getActiveElement(); | ||
expect(result).to.equal(target); | ||
}); | ||
|
||
bdd.it('should return the active element of the given document', function() { | ||
var target = document.getElementById('tabindex-0'); | ||
target.focus(); | ||
|
||
var result = getActiveElement({ | ||
context: document, | ||
}); | ||
expect(result).to.equal(target); | ||
}); | ||
|
||
bdd.describe('for iframe', function() { | ||
var target; | ||
|
||
bdd.before(function() { | ||
target = frame.document.getElementById('target'); | ||
target.focus(); | ||
}); | ||
|
||
bdd.it('should return the iframe as the active element in document', function() { | ||
var result = getActiveElement({ | ||
context: document, | ||
}); | ||
|
||
expect(result).to.equal(frame.element); | ||
}); | ||
|
||
bdd.it('should return the active element within iframe', function() { | ||
var result = getActiveElement({ | ||
context: frame.document, | ||
}); | ||
|
||
expect(result).to.equal(target); | ||
}); | ||
}); | ||
|
||
}); | ||
}); |