forked from dart-lang/site-www
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Library tour example tests from dart:html onwards
Contributes to dart-lang#407 and dart-lang#416. Continuation of dart-lang#440
- Loading branch information
Showing
6 changed files
with
333 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// ignore_for_file: unused_element, unused_local_variable | ||
import 'dart:async'; | ||
// #docregion import | ||
import 'dart:html'; | ||
// #enddocregion import | ||
|
||
void miscDeclAnalyzedButNotTested() { | ||
{ | ||
// #docregion querySelector | ||
// Find an element by id (an-id). | ||
Element elem1 = querySelector('#an-id'); | ||
|
||
// Find an element by class (a-class). | ||
Element elem2 = querySelector('.a-class'); | ||
|
||
// Find all elements by tag (<div>). | ||
List<Element> elems1 = querySelectorAll('div'); | ||
|
||
// Find all text inputs. | ||
List<Element> elems2 = | ||
querySelectorAll('input[type="text"]'); | ||
|
||
// Find all elements with the CSS class 'class' | ||
// inside of a <p> that is inside an element with | ||
// the ID 'id'. | ||
List<Element> elems3 = querySelectorAll('#id p.class'); | ||
// #enddocregion querySelector | ||
} | ||
|
||
{ | ||
Element elem; | ||
// #docregion attributes | ||
elem.attributes['someAttribute'] = 'someValue'; | ||
// #enddocregion attributes | ||
} | ||
|
||
{ | ||
// #docregion creating-elements | ||
var elem = new ParagraphElement(); | ||
elem.text = 'Creating is easy!'; | ||
// #enddocregion creating-elements | ||
|
||
// #docregion creating-from-html | ||
var elem2 = | ||
new Element.html('<p>Creating <em>is</em> easy!</p>'); | ||
// #enddocregion creating-from-html | ||
|
||
// #docregion body-children-add | ||
document.body.children.add(elem2); | ||
// #enddocregion body-children-add | ||
|
||
// #docregion nodes-add | ||
querySelector('#inputs').nodes.add(elem); | ||
// #enddocregion nodes-add | ||
|
||
// #docregion replaceWith | ||
querySelector('#status').replaceWith(elem); | ||
// #enddocregion replaceWith | ||
|
||
// #docregion remove | ||
// Find a node by ID, and remove it from the DOM. | ||
querySelector('#expendable').remove(); | ||
// #enddocregion remove | ||
} | ||
|
||
{ | ||
// #docregion classes-add | ||
var elem = querySelector('#message'); | ||
elem.classes.add('warning'); | ||
// #enddocregion classes-add | ||
|
||
// #docregion set-id | ||
var message = new DivElement(); | ||
message.id = 'message2'; | ||
message.text = 'Please subscribe to the Dart mailing list.'; | ||
// #enddocregion set-id | ||
} | ||
|
||
{ | ||
// #docregion elem-set-cascade | ||
var message = new DivElement() | ||
..id = 'message2' | ||
..text = 'Please subscribe to the Dart mailing list.'; | ||
// #enddocregion elem-set-cascade | ||
|
||
// #docregion set-style | ||
message.style | ||
..fontWeight = 'bold' | ||
..fontSize = '3em'; | ||
// #enddocregion set-style | ||
|
||
void submitData() {} | ||
// #docregion onClick | ||
// Find a button by ID and add an event handler. | ||
querySelector('#submitInfo').onClick.listen((e) { | ||
// When the button is clicked, it runs this code. | ||
submitData(); | ||
}); | ||
// #enddocregion onClick | ||
|
||
// #docregion target | ||
document.body.onClick.listen((e) { | ||
final clickedElem = e.target; | ||
// ... | ||
}); | ||
// #enddocregion target | ||
} | ||
|
||
Future tryGetString() async { | ||
String jsonUri = 'data.json'; | ||
// #docregion try-getString | ||
try { | ||
var data = await HttpRequest.getString(jsonUri); | ||
// Process data... | ||
} catch (e) { | ||
// Handle exception... | ||
} | ||
// #enddocregion try-getString | ||
} | ||
|
||
{ | ||
var url, encodedData; | ||
void requestComplete(HttpRequest req) {} | ||
// #docregion new-HttpRequest | ||
var request = new HttpRequest(); | ||
request | ||
..open('POST', url) | ||
..onLoadEnd.listen((e) => requestComplete(request)) | ||
..send(encodedData); | ||
// #enddocregion new-HttpRequest | ||
|
||
// #docregion xxx | ||
11111; | ||
// #enddocregion xxx | ||
|
||
// #docregion xxx | ||
11111; | ||
// #enddocregion xxx | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,5 +5,8 @@ version: 0.0.1 | |
environment: | ||
sdk: '>=1.20.1 <2.0.0' | ||
|
||
dependencies: | ||
browser: any | ||
|
||
dev_dependencies: | ||
test: ^0.12.0 |
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,107 @@ | ||
// #docplaster | ||
@Tags(const ['browser']) | ||
@TestOn('browser') | ||
import 'dart:async'; | ||
import 'dart:html'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('querySelector', () { | ||
final html = | ||
// #docregion anchor-html | ||
'<a id="example" href="http://example.com">link text</a>'; | ||
// #enddocregion anchor-html | ||
document.body.appendHtml(html); | ||
|
||
// #docregion href | ||
var anchor = querySelector('#example') as AnchorElement; | ||
anchor.href = 'http://dartlang.org'; | ||
// #enddocregion href | ||
// The code above should throw no exceptions. | ||
}); | ||
|
||
test('querySelectorAll', () { | ||
// #docregion os-html | ||
final html = '''<!-- In HTML: --> | ||
<p> | ||
<span class="linux">Words for Linux</span> | ||
<span class="macos">Words for Mac</span> | ||
<span class="windows">Words for Windows</span> | ||
</p>'''; | ||
// #enddocregion os-html | ||
document.body.appendHtml(html); | ||
|
||
String determineUserOs() => 'linux'; | ||
// #docregion os | ||
// In Dart: | ||
final osList = ['macos', 'windows', 'linux']; | ||
final userOs = determineUserOs(); | ||
|
||
// For each possible OS... | ||
for (var os in osList) { | ||
// Matches user OS? | ||
bool shouldShow = (os == userOs); | ||
|
||
// Find all elements with class=os. For example, if | ||
// os == 'windows', call querySelectorAll('.windows') | ||
// to find all elements with the class "windows". | ||
// Note that '.$os' uses string interpolation. | ||
for (var elem in querySelectorAll('.$os')) { | ||
elem.hidden = !shouldShow; // Show or hide. | ||
} | ||
} | ||
// #enddocregion os | ||
expect(querySelector('.linux').hidden, isFalse); | ||
expect(querySelector('.macos').hidden, isTrue); | ||
expect(querySelector('.windows').hidden, isTrue); | ||
}); | ||
|
||
test('getString', () async { | ||
final url = 'http://httpbin.org'; | ||
// #docregion getString | ||
Future main() async { | ||
String pageHtml = await HttpRequest.getString(url); | ||
// Do something with pageHtml... | ||
// #enddocregion getString | ||
expect(pageHtml.substring(0, 250), | ||
contains('<title>httpbin')); | ||
// #docregion getString | ||
} | ||
// #enddocregion getString | ||
|
||
await main(); | ||
}); | ||
|
||
test('request', () async { | ||
final url = 'http://httpbin.org/post'; | ||
// #docregion request | ||
Future main() async { | ||
HttpRequest req = | ||
await HttpRequest.request(url, method: 'POST'); | ||
if (req.status == 200) { | ||
// Successful page access... | ||
} | ||
// ... | ||
// #enddocregion request | ||
expect(req.status, 200); | ||
// #docregion request | ||
} | ||
// #enddocregion request | ||
|
||
await main(); | ||
}); | ||
|
||
group('xxxx', () { | ||
test('xxxxx', () { | ||
// #docregion xxxx | ||
11111; | ||
// #enddocregion xxxx | ||
}); | ||
|
||
test('xxxxx', () { | ||
// #docregion xxxx | ||
11111; | ||
// #enddocregion xxxx | ||
}); | ||
}); | ||
} |
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
Oops, something went wrong.