Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inconsistent top-level document helpers #161

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
| `ContextEvent` | `WebGLContextEvent` |
| `WebGL` | `WebGLRenderingContext` |

- Deprecate the top-level `createElementTag`, `createCanvasElement`,
`createIFrameElement`, and `querySelector` functions.
Instead, use the standard creation and query methods on `document`.

## 0.4.2

- Undeprecate some APIs and helpers library that were deprecated in `0.4.1`.
Expand Down
43 changes: 40 additions & 3 deletions lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,58 @@ export 'helpers/http.dart';
export 'helpers/lists.dart';
export 'helpers/renames.dart';

HTMLElement createElementTag(String s) =>
document.createElement(s) as HTMLElement;
/// Create an [HTMLElement] with the specified [tagName].
/// If no element with [tagName] exists, returns an [HTMLUnknownElement].
///
/// Deprecated in favor of creating the element like other HTML elements:
///
/// ```dart
/// final anchor = document.createElement('a') as HTMLElement;
/// ```
@Deprecated('Directly use document.createElement instead.')
HTMLElement createElementTag(String tagName) =>
document.createElement(tagName) as HTMLElement;

/// Create an [HTMLCanvasElement] in the current [document].
///
/// Deprecated in favor of creating the element like other HTML elements:
///
/// ```dart
/// final canvas = document.createElement('canvas') as HTMLCanvasElement
/// ..width = 256
/// ..height = 256;
/// ```
@Deprecated('Directly use document.createElement instead.')
HTMLCanvasElement createCanvasElement({int? width, int? height}) {
final result = document.createElement('canvas') as HTMLCanvasElement;
if (width != null) result.width = width;
if (height != null) result.height = height;
return result;
}

/// Create an [HTMLIFrameElement] in the current [document].
///
/// Deprecated in favor of creating the element like other HTML elements:
///
/// ```dart
/// final embed = document.createElement('iframe') as HTMLIFrameElement;
/// ```
@Deprecated('Directly use document.createElement instead.')
HTMLIFrameElement createIFrameElement() =>
document.createElement('iframe') as HTMLIFrameElement;

@JS('Audio')
external JSFunction get _audioConstructor;
HTMLAudioElement createAudioElement() => _audioConstructor.callAsConstructor();

Element? querySelector(String selectors) => document.querySelector(selectors);
/// Finds and returns the first element within the [document]
/// that matches the specified CSS [selector] string.
/// If no match is found, `null` is returned.
///
/// Deprecated in favor of querying directly on the [document]:
///
/// ```dart
/// final dartDiv = document.querySelector('div.dart');
/// ```
@Deprecated('Directly use document.querySelector instead.')
Element? querySelector(String selector) => document.querySelector(selector);