Skip to content

Commit 118093e

Browse files
authored
Merge branch 'dint-dev:master' into master
2 parents 919494b + 95ec899 commit 118093e

61 files changed

Lines changed: 1067 additions & 425 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: [terrier989]
2+
open_collective: [cryptography]

.github/workflows/dart.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
name: Dart CI
22

3-
on: [push]
3+
on: [push, pull_request]
44

55
jobs:
6-
build:
7-
8-
runs-on: ubuntu-latest
9-
10-
container:
11-
image: google/dart:latest
12-
6+
test:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest]
11+
sdk: [stable, beta, dev]
1312
steps:
14-
- uses: actions/checkout@v1
15-
13+
- uses: actions/checkout@v3
14+
- uses: dart-lang/setup-dart@v1
15+
with:
16+
sdk: ${{ matrix.sdk }}
1617
- name: Install dependencies
1718
run: dart pub get
18-
1919
- name: Run tests
2020
run: dart test --platform vm

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
## 2.2.3
2+
* Fixes dependency constraint issues.
3+
4+
## 2.2.2
5+
* Fixes [Issue #72: "please add flutter 3.10 full support and update package on pub"](https://github.com/dint-dev/universal_html/issues/72).
6+
7+
## 2.2.1
8+
* Fixes small bugs.
9+
10+
## 2.2.0
11+
* Allows users to access HTTP request/response when using `WindowController.openHttp`.
12+
13+
## 2.1.0
14+
* Makes `WindowController` more testing-friendly.
15+
* Adds `EventSourceOutsideBrowser` interface for changing EventSource HTTP headers
16+
([issue #63](https://github.com/dint-dev/universal_html/issues/63)).
17+
* Adds WindowController.defaultHttpClient, which will be used as default HTTP client by EventSource
18+
and other APIs. It's safe because it does not affect cookies, headers, or anything else dangerous.
19+
20+
## 2.0.9
21+
* Fixes various issues.
22+
123
## 2.0.8
224
* Adds missing APIs: Worker ([issue #36](https://github.com/dint-dev/universal_html/issues/36))
325
and a few others.

README.md

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
[![Pub Package](https://img.shields.io/pub/v/universal_html.svg)](https://pub.dartlang.org/packages/universal_html)
2-
[![Github Actions CI](https://github.com/dint-dev/universal_html/workflows/Dart%20CI/badge.svg)](https://github.com/dint-dev/universal_html/actions?query=workflow%3A%22Dart+CI%22)
2+
[![package publisher](https://img.shields.io/pub/publisher/universal_html.svg)](https://pub.dev/packages/universal_html/publisher)
3+
[![Github Actions CI](https://github.com/dint-dev/universal_html/workflows/Dart%20CI/badge.svg)](https://github.com/dint-dev/universal_html/actions)
34

45
# Introduction
56
A cross-platform `dart:html`:
67
* __Eases cross-platform development__
7-
* You can use this package in browsers, mobile, desktop, and server-side VM, and server-side
8-
Javascript (Node.JS, Cloud Functions, etc.).
8+
* You can use this package in browsers, mobile, desktop, and server-side VM, and Node.JS.
99
* Just replace `dart:html` imports with `package:universal_html/html.dart`. Normal
1010
_dart:html_ will continue to be used when application run in browsers.
1111
* __Extensive support for processing HTML and XML documents__
12-
* Parse, manipulate, and print [DOM nodes](https://pub.dev/documentation/universal_html/latest/universal_html/Node-class.html).
13-
* Find DOM nodes with [querySelectorAll](https://pub.dev/documentation/universal_html/latest/universal_html/querySelectorAll.html)
12+
* Parse, manipulate, and print [DOM nodes](https://api.dart.dev/stable/2.19.3/dart-html/Node-class.html).
13+
* Find DOM nodes with [querySelectorAll](https://api.dart.dev/stable/2.19.3/dart-html/querySelectorAll.html)
1414
and other CSS query methods.
15-
* Submit forms and more.
1615
* __EventSource streaming support__
17-
* Implements _dart:html_ [EventSource API](https://developer.mozilla.org/en-US/docs/Web/API/EventSource).
16+
* Cross-platform _dart:html_ `EventSource` ("application/event-stream").
17+
* If you want to customize EventSource HTTP headers outside browsers, see
18+
[EventSourceOutsideBrowser](https://pub.dev/documentation/universal_html/latest/universal_html/EventSourceOutsideBrowser-class.html).
1819

1920
The project is licensed under the [Apache License 2.0](LICENSE). Some of the source code was adopted
20-
from the original [dart:html](https://github.com/dart-lang/sdk/tree/master/tools/dom), which is
21-
documented in the relevant files.
21+
from the original [dart:html](https://github.com/dart-lang/sdk/tree/master/tools/dom) in Dart SDK,
22+
which is documented in the relevant files.
2223

2324
## Documentation
2425
* [API reference](https://pub.dev/documentation/universal_html/latest/)
@@ -34,7 +35,7 @@ documented in the relevant files.
3435
In `pubspec.yaml`:
3536
```yaml
3637
dependencies:
37-
universal_html: ^2.0.8
38+
universal_html: ^2.2.3
3839
```
3940
4041
## 2. Use
@@ -58,20 +59,6 @@ void main() {
5859
}
5960
```
6061

61-
## Implemented APIs
62-
### Summary
63-
* __Document node classes__
64-
* __DOM parsing__
65-
* Use _element.innerHtml_ setter, _DomParser_, or _package:universal_html/parsing.dart_.
66-
* HTML parsing uses [package:html](https://pub.dev/packages/html), CSS parsing uses
67-
[package:csslib](https://pub.dev/packages/csslib), and XML parsing uses our own parser.
68-
* __DOM printing__
69-
* Use _element.innerHtml_ or _element.outerHtml_.
70-
* __DOM events__
71-
* For example, _element.onClick.listen(...)_ receives invocation of _element.click()_.
72-
* __CSS classes__ (_CssStyleDeclaration_, etc.)
73-
* __Most CSS queries__
74-
7562
# Examples
7663
## Parsing HTML
7764
Use [parseHtmlDocument](https://pub.dev/documentation/universal_html/latest/universal_html.parsing/parseHtmlDocument.html):
@@ -99,19 +86,99 @@ void main() {
9986
Load a _Window_ with [WindowController](https://pub.dev/documentation/universal_html/latest/universal_html.controller/WindowController-class.html):
10087

10188
```dart
89+
import 'dart:io' show Cookie;
10290
import 'package:universal_html/controller.dart';
10391
10492
Future main() async {
10593
// Load a document.
10694
final controller = WindowController();
95+
controller.defaultHttpClient.userAgent = 'My Hacker News client';
10796
await controller.openHttp(
97+
method: 'GET',
10898
uri: Uri.parse("https://news.ycombinator.com/"),
99+
onRequest: (HttpClientRequest request) {
100+
// Add custom headers
101+
request.headers.set('Authorization', 'headerValue');
102+
request.cookies.add(Cookie('cookieName', 'cookieValue'));
103+
},
104+
onResponse: (HttpClientResponse response) {
105+
print('Status code: ${response.statusCode}');
106+
},
109107
);
110108
111109
// Select the top story using a CSS query
112-
final topStoryTitle = controller.document.querySelectorAll(".athing > .title").first.text;
110+
final titleElements = controller.document.querySelectorAll(".athing > .title");
111+
final topStoryTitle = titleElements.first.text;
113112
114113
// Print result
115114
print("Top Hacker News story is: $topStoryTitle");
116115
}
116+
```
117+
118+
## EventSource
119+
`EventSource` ([see mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/EventSource))
120+
is a browser API for reading "application/event-stream" streams. It has been supported by browsers
121+
for a long time.
122+
123+
```dart
124+
import 'package:universal_html/html.dart';
125+
126+
Future<void> main() async {
127+
final eventSource = EventSource('http://example.com/events');
128+
await for (var message in event.onMessage) {
129+
print('Event type: ${message.type}');
130+
print('Event data: ${message.data}');
131+
}
132+
}
133+
```
134+
135+
EventSource requests from real browsers are typically authenticated using cookies.
136+
If you want to add cookies or customize other HTTP headers, you need to use
137+
[EventSourceOutsideBrowser](https://pub.dev/documentation/universal_html/latest/universal_html/EventSourceOutsideBrowser-class.html):
138+
```dart
139+
import 'package:universal_html/universal_html.dart';
140+
import 'dart:io' show Cookie;
141+
142+
Future<void> main() async {
143+
final eventSource = EventSource('http://example.com/events');
144+
145+
// The following block will NOT be executed in browsers.
146+
// Because compiler can infer instances of EventSourceOutsideBrowser are never constructed,
147+
// it will not appear in Javascript either.
148+
if (eventSource is EventSourceOutsideBrowser) {
149+
eventSource.onHttpClientRequest = (eventSource, request) {
150+
request.headers.set('Authorization', 'example');
151+
request.cookies.add(Cookie('name', 'value'));
152+
};
153+
eventSource.onHttpClientResponse = (eventSource, request, response) {
154+
// ...
155+
};
156+
}
157+
158+
await for (var message in eventSource.onMessage) {
159+
print('Event:');
160+
print(' type: ${message.type}');
161+
print(' data: ${message.data}');
162+
}
163+
}
164+
```
165+
166+
## Testing
167+
```dart
168+
import 'package:universal_html/controller.dart';
169+
import 'package:test/test.dart';
170+
171+
void main() {
172+
setUp(() {
173+
WindowController.instance = WindowController();
174+
});
175+
176+
test('test #1', () {
177+
// ...
178+
});
179+
180+
test('test #2', () {
181+
// ...
182+
});
183+
}
117184
```

analysis_options.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
include: package:pedantic/analysis_options.yaml
1+
include: package:lints/recommended.yaml
2+
3+
linter:
4+
rules:
5+
avoid_unused_constructor_parameters: false
6+
constant_identifier_names: false
7+
non_constant_identifier_names: false
8+
provide_deprecation_message: false

lib/html.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
/// Cross-platform _dart:html_.
15+
/// Cross-platform "dart:html" library.
1616
///
1717
/// You can choose from the following libraries:
1818
/// * `package:universal_html/html.dart`
@@ -37,7 +37,3 @@ export 'src/_sdk/html.dart'
3737
if (dart.library.html) 'src/_sdk/html.dart' // Browser
3838
if (dart.library.io) 'src/html.dart' // VM
3939
if (dart.library.js) 'src/html.dart'; // Node.JS
40-
export 'src/_sdk/html.dart'
41-
if (dart.library.html) 'src/_sdk/html.dart' // Browser
42-
if (dart.library.io) 'src/html_top_level_functions.dart' // VM
43-
if (dart.library.js) 'src/html_top_level_functions.dart'; // Node.JS

lib/indexed_db.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// Cross-platform "dart:indexed_db" library.
1516
library universal_html.indexed_db;
1617

1718
export 'src/_sdk/indexed_db.dart'

lib/js.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// Cross-platform "dart:js" library.
1516
library universal_html.js;
1617

1718
export 'src/_sdk/js.dart'

lib/js_util.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// Cross-platform "dart:js_util" library.
1516
library universal_html.js_util;
1617

1718
export 'src/_sdk/js_util.dart'

lib/src/_sdk/html.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@
3030
// This file solves the problem.
3131
//
3232
export 'dart:html';
33+
34+
export '../_sdk_html_additions.dart';

0 commit comments

Comments
 (0)