Skip to content

Commit

Permalink
Add Clipboard API (Modernizr#2573)
Browse files Browse the repository at this point in the history
* Add Clipboard API testing

* Implement sub-modules (but badly)

* Solve ESLint errors 🙄

* Improve the sub-properties detection

* Move feature detect location
  • Loading branch information
Markel authored Jul 23, 2020
1 parent f81995d commit ffad3e7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions feature-detects/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*!
{
"name": "Clipboard API",
"property": "clipboard",
"tags": ["clipboard"],
"authors": ["Markel Ferro (@MarkelFe)"],
"async": true,
"warnings": ["It may return false in non-HTTPS connections as the API is only available in secure contexts"],
"notes": [{
"name": "MDN Docs Clipboard Object",
"href": "https://developer.mozilla.org/en-US/docs/Web/API/Clipboard"
}, {
"name": "MDN Docs Clipboard API",
"href": "https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API"
}]
}
!*/
/* DOC
It tests for the whole clipboard API. The sub-properties `read`, `readText`, `write` and `writeText` are supported. Note: This test does not detect the [clipboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/ClipboardEvent).
```javascript
Modernizr.clipboard // Supports the clipboard API
Modernizr.clipboard.read // Supports the read sub-property
```
*/
define(['Modernizr', 'addTest'], function(Modernizr, addTest) {
Modernizr.addAsyncTest(function() {
var result;
var props = ['read', 'readText', 'write', 'writeText'];
if (navigator.clipboard) {
addTest('clipboard', true);
// The sub-modules checked only if API is available to avoid Edge crashes
for (var i = 0; i < props.length; i++) {
if (navigator.clipboard[props[i]]) {
result = true;
} else {
result = false;
}
addTest('clipboard.' + props[i].toLowerCase(), result);
}
}
else {
addTest('clipboard', false);
}
});
});
1 change: 1 addition & 0 deletions lib/config-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"canvas/todataurl",
"canvas/winding",
"canvastext",
"clipboard",
"contenteditable",
"contextmenu",
"cookies",
Expand Down

0 comments on commit ffad3e7

Please sign in to comment.