Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 19d5d37

Browse files
committed
Merge pull request #2 from dart-lang/master
Update upstream
2 parents f463cf1 + e572d9d commit 19d5d37

File tree

24 files changed

+3628
-2030
lines changed

24 files changed

+3628
-2030
lines changed

AUTHORS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Below is a list of people and organizations that have contributed
2+
# to the Dart www site. Names should be added to the list like so:
3+
#
4+
# Name/Organization <email address>
5+
6+
Google Inc.
7+
8+
Matthew Butler <[email protected]>
9+
Chris Buckett <[email protected]>

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Except as otherwise noted, the content of this page is licensed under the
2+
Creative Commons Attribution 3.0 License [1], and code samples are licensed
3+
under the BSD License:
4+
5+
Copyright 2012, the Dart project authors. All rights reserved.
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are
8+
met:
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
* Neither the name of Google Inc. nor the names of its
16+
contributors may be used to endorse or promote products derived
17+
from this software without specific prior written permission.
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
[1] http://creativecommons.org/licenses/by/3.0/

src/site/_includes/language-tour/classes/index.markdown

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,14 @@ The distanceTo() method in the following sample is an
366366
example of an instance method.
367367

368368
{% highlight dart %}
369+
#import('dart:math');
370+
369371
class Point {
370372
num x, y;
371373
Point(this.x, this.y);
372374

373375
num distanceTo(Point other) {
374-
return Math.sqrt(((x-other.x)*(x-other.x)) + ((y-other.y)*(y-other.y)));
376+
return sqrt(((x-other.x)*(x-other.x)) + ((y-other.y)*(y-other.y)));
375377
}
376378
}
377379
{% endhighlight %}
@@ -569,12 +571,14 @@ Static methods (class methods) do not operate on an instance, and thus
569571
do not have access to `this`.
570572

571573
{% highlight dart %}
574+
#import('dart:math');
575+
572576
class Point {
573577
num x, y;
574578
Point(this.x, this.y);
575579

576580
static num distanceBetween(Point a, Point b) {
577-
return Math.sqrt(((a.x-b.x)*(a.x-b.x)) + ((a.y-b.y)*(a.y-b.y)));
581+
return sqrt(((a.x-b.x)*(a.x-b.x)) + ((a.y-b.y)*(a.y-b.y)));
578582
}
579583
}
580584

src/site/_includes/library-tour/html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ which describes the hierarchy of an HTML page).
66

77
Other common uses of dart:html are manipulating styles (_CSS_),
88
getting data using HTTP requests
9-
(_XMLHttpRequest_, or _XHR_),
9+
(_HttpRequest_ (previously known as XMLHttpRequest)),
1010
and exchanging data using [WebSockets](#html-websockets).
1111

1212
<aside class="note" markdown="1">
1313
**Note:**
14-
HTML5 (and dart:html) have many additional APIs that this section doesn't cover.
14+
HTML5, and dart:html, have many additional APIs that this
15+
section doesn't cover.
1516
These APIs include background worker tasks,
1617
audio,
1718
2D and 3D graphics,
1819
database,
1920
geolocation,
2021
speech input,
2122
and more.
22-
XMLHttpRequest will soon be covered in this section.
23+
HttpRequest will soon be covered in this section.
2324
</aside>
2425

2526
Only web apps can use dart:html&mdash;not command-line apps.
2627

27-
2828
### Importing the HTML library
2929

3030
To use the HTML library in your web app,

src/site/articles/idiomatic-dart/index.html

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,29 @@ <h3>Named constructors</h3>
6666
<p>Like most dynamically-typed languages, Dart doesn't support overloading. With methods, this isn't much of a limitation because you can always use a different name, but constructors aren't so lucky. To alleviate that, Dart lets you define <em>named constructors</em>:</p>
6767

6868
{% highlight dart %}
69+
#import('dart:math');
70+
6971
class Point {
7072
num x, y;
7173
Point(this.x, this.y);
7274
Point.zero() : x = 0, y = 0;
7375
Point.polar(num theta, num radius) {
74-
x = Math.cos(theta) * radius;
75-
y = Math.sin(theta) * radius;
76+
x = cos(theta) * radius;
77+
y = sin(theta) * radius;
7678
}
7779
}
7880
{% endhighlight dart 0 %}
7981

8082
<p>Here our Point class has three constructors, a normal one and two named ones. You can use them like so:</p>
8183

8284
{% highlight dart %}
83-
var a = new Point(1, 2);
84-
var b = new Point.zero();
85-
var c = new Point.polar(Math.PI, 4.0);
85+
#import('dart:math');
86+
87+
main() {
88+
var a = new Point(1, 2);
89+
var b = new Point.zero();
90+
var c = new Point.polar(PI, 4.0);
91+
}
8692
{% endhighlight dart 0 %}
8793

8894
<p>Note that we're still using <code>new</code> here when we invoke the named constructor. It isn't just a static method.</p>
@@ -255,9 +261,11 @@ <h2 id="top-level-definitions">Top-level definitions</h2>
255261
<p>Dart is a "pure" object-oriented language in that everything you can place in a variable is a real object (no mutant "primitives") and every object is an instance of some class. It's not a <em>dogmatic</em> OOP language though. You aren't required to place everything you define inside some class. Instead, you are free to define functions, variables, and even getters and setters at the top level if you want.</p>
256262

257263
{% highlight dart %}
264+
#import('dart:math');
265+
258266
num abs(num value) => value < 0 ? -value : value;
259267

260-
final TWO_PI = Math.PI * 2.0;
268+
final TWO_PI = PI * 2.0;
261269

262270
int get today() {
263271
final date = new Date.now();
@@ -267,7 +275,7 @@ <h2 id="top-level-definitions">Top-level definitions</h2>
267275

268276
<p>Even in languages that don't require you to place everything inside a class or object, like JavaScript, it's still common to do so as a form of namespacing: top-level definitions with the same name could inadvertently collide. To address that, Dart has a library system that allows you to import definitions from other libraries with a prefix applied to disambiguate it. That means you shouldn't <em>need</em> to defensively squirrel your definitions inside classes.</p>
269277

270-
<p>We're still exploring what this actually means for how we define libraries. Most of our code does place definitions inside classes, like Math. It's hard to tell if this is just an ingrained habit we have from other languages or a practice that's also good for Dart. This is an area we want feedback on.</p>
278+
<p>We're still exploring what this actually means for how we define libraries. For example, the project used to have a Math class, but we moved all functionality from that class to top-level methods inside the dart:math library.</p>
271279

272280
<p>We do have some examples where we use top-level definitions. The first you'll run into is <code>main()</code> which is expected to be defined at the top level. If you work with the DOM, the familiar <code>document</code> and <code>window</code> "variables" are actually top-level getters in Dart.</p>
273281

@@ -304,8 +312,11 @@ <h2 id="strings-and-interpolation">Strings and interpolation</h2>
304312
placing them inside curly braces:</p>
305313

306314
{% highlight dart %}
307-
var r = 2;
308-
print('The area of a circle with radius $r is ${Math.PI * r * r}');
315+
#import('dart:math');
316+
main() {
317+
var r = 2;
318+
print('The area of a circle with radius $r is ${PI * r * r}');
319+
}
309320
{% endhighlight dart 0 %}
310321

311322
<h2 id="operators">Operators</h2>

src/site/articles/json-web-service/index.markdown

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ has-permalinks: true
1212
_Written by Chris Buckett<br>
1313
April 2012_
1414

15-
Most client-side Dart apps need a way to communicate with a server, and sending JSON via [XMLHttpRequest](https://developer.mozilla.org/en/XMLHttpRequest) is the preferred way to do this. This article discusses communicating with a server using the [XMLHttpRequest API](http://api.dartlang.org/html/XMLHttpRequest.html) from the [dart:html](http://api.dartlang.org/html.html) library and parsing JSON data using the [dart:json](http://api.dartlang.org/json.html) library. It then goes on to show how to provide strong type information about that JSON data through the use of JsonObject and Dart's interface feature.
15+
Most client-side Dart apps need a way to communicate with a server, and sending JSON via [XMLHttpRequest](https://developer.mozilla.org/en/XMLHttpRequest) is the preferred way to do this. This article discusses communicating with a server using the [HttpRequest API](http://api.dartlang.org/html/HttpRequest.html) from the [dart:html](http://api.dartlang.org/html.html) library and parsing JSON data using the [dart:json](http://api.dartlang.org/json.html) library. It then goes on to show how to provide strong type information about that JSON data through the use of JsonObject and Dart's interface feature.
1616

1717
#### Contents
1818

@@ -27,7 +27,7 @@ Most client-side Dart apps need a way to communicate with a server, and sending
2727
<li><a href="#parsing-json">Parsing JSON</a>
2828
<ol>
2929
<li><a href="#jsonobject">Introducing JsonObject</a></li>
30-
<li><a href="#note-on-jsonp">A note on JSONP and XMLHttpRequest</a></li>
30+
<li><a href="#note-on-jsonp">A note on JSONP and HttpRequest</a></li>
3131
</ol>
3232
</li>
3333
<li><a href="#summary">Summary</a></li>
@@ -54,43 +54,45 @@ The same web service accepts data on the same URL with an HTTP POST. The web se
5454

5555
<h2 id="connecting-to-server">Connecting to the server</h2>
5656

57-
When communicating with a web service, use the XMLHttpRequest API from the dart:html library. XMLHttpRequest is a standard way to programmatically send and receive data to and from web servers.
57+
When communicating with a web service, use the HttpRequest API from the dart:html library.
58+
HttpRequest is a standard way to programmatically send and receive data to and from web servers.
5859

5960
<h3 id="getting-data">Getting data from the server</h3>
6061

61-
Get objects from the server using HTTP GET. XMLHttpRequest provides a named constructor called getTEMPNAME that takes a URL and a callback function that's invoked when the server responds.
62+
Get objects from the server using HTTP GET. HttpRequest provides a named constructor called
63+
<code>get</code> that takes a URL and a callback function that's invoked when the server responds.
6264

6365
{% highlight dart %}
64-
getLanguageData(String languageName, onSuccess(XMLHttpRequest req)) {
66+
getLanguageData(String languageName, onSuccess(HttpRequest req)) {
6567
var url = "http://my-site.com/programming-languages/$languageName";
6668

6769
// call the web server asynchronously
68-
var request = new XMLHttpRequest.getTEMPNAME(url, onSuccess);
70+
var request = new HttpRequest.get(url, onSuccess);
6971
}
7072

7173
// print the raw json response text from the server
72-
onSuccess(XMLHttpRequest req) {
74+
onSuccess(HttpRequest req) {
7375
print(req.responseText); // print the received raw JSON text
7476
}
7577

7678
getLanguageData("dart", onSuccess);
7779
{% endhighlight %}
7880

79-
Note: getTEMPNAME is a convenience constructor, and its name will change. The full XMLHttpRequest API is still an option for HTTP GET, if you need more control over the API.
81+
Note: getTEMPNAME is a convenience constructor, and its name will change. The full HttpRequest API is still an option for HTTP GET, if you need more control over the API.
8082

8183
<h3 id="saving-object">Saving objects on the server</h3>
8284

83-
To create a new object on the server, use the raw XMLHttpRequest API with the HTTP POST method. Use the readyStateChange listener to be notified when the request is complete. The example below calls an onSuccess function when the request is complete:
85+
To create a new object on the server, use the raw HttpRequest API with the HTTP POST method. Use the readyStateChange listener to be notified when the request is complete. The example below calls an onSuccess function when the request is complete:
8486

8587
{% highlight dart %}
86-
saveLanguageData(String data, onSuccess(XMLHttpRequest req)) {
87-
XMLHttpRequest req = new XMLHttpRequest(); // create a new XHR
88+
saveLanguageData(String data, onSuccess(HttpRequest req)) {
89+
HttpRequest req = new HttpRequest(); // create a new XHR
8890

8991
var url = "http://example.com/programming-languages/";
9092
req.open("POST", url); // POST to send data
9193

9294
req.on.readyStateChange.add((Event e) {
93-
if (req.readyState == XMLHttpRequest.DONE &&
95+
if (req.readyState == HttpRequest.DONE &&
9496
(req.status == 200 || req.status == 0)) {
9597
onSuccess(req); // called when the POST successfully completes
9698
}
@@ -100,7 +102,7 @@ saveLanguageData(String data, onSuccess(XMLHttpRequest req)) {
100102
}
101103

102104
// print the raw json response text from the server
103-
onSuccess(XMLHttpRequest req) {
105+
onSuccess(HttpRequest req) {
104106
print(req.responseText); // print the received raw JSON text
105107
}
106108

@@ -111,7 +113,7 @@ saveLanguageData(stringData, onSuccess); // send the data to
111113

112114
<h2 id="parsing-json">Parsing JSON</h2>
113115

114-
Now that you have seen how XMLHttpRequest GETs data from the server back to the client, and POSTs data from the client to the server, the next step is to make use of the JSON data in the client application.
116+
Now that you have seen how HttpRequest GETs data from the server back to the client, and POSTs data from the client to the server, the next step is to make use of the JSON data in the client application.
115117

116118
The dart:json library provides two static functions, JSON.parse() and JSON.stringify().
117119

@@ -130,10 +132,10 @@ print(parsedMap["language"]); // dart
130132

131133
JSON also works for more complex data structures, such as nested maps inside of lists.
132134

133-
Use JSON.parse() to convert the XMLHttpRequest's response from raw text to an actual Dart object:
135+
Use JSON.parse() to convert the HttpRequest's response from raw text to an actual Dart object:
134136

135137
{% highlight dart %}
136-
onSuccess(XMLHttpRequest req) {
138+
onSuccess(HttpRequest req) {
137139
Map data = JSON.parse(req.responseText); // parse response text
138140
print(data["language"]); // dart
139141
print(data["targets"][0]); // dartium
@@ -169,7 +171,7 @@ JsonObject uses Dart's noSuchMethod method support, which enables objects to int
169171
Here is an example of using JsonObject instead of a raw Map:
170172

171173
{% highlight dart %}
172-
onSuccess(XMLHttpRequest req) {
174+
onSuccess(HttpRequest req) {
173175
// decode the JSON response text using JsonObject
174176
var data = new JsonObject.fromJsonString(req.responseText);
175177

@@ -253,14 +255,14 @@ Language data = new JsonObject.fromJsonString(req.responseText);
253255
String json = JSON.stringify(data);
254256

255257
// and POST it back to the server
256-
XMLHttpRequest req = new XMLHttpRequest();
258+
HttpRequest req = new HttpRequest();
257259
req.open("POST", url);
258260
req.send(json);
259261
{% endhighlight %}
260262

261-
<h3 id="note-on-jsonp">A note on JSONP and XMLHttpRequest</h3>
263+
<h3 id="note-on-jsonp">A note on JSONP and HttpRequest</h3>
262264

263-
One caveat: Make sure your app is served from the same origin (domain name, port, and application layer protocol) as the web service you are trying to access with XMLHttpRequest. Otherwise your app will hit the Access-Control-Allow-Origin restriction. This is a security restriction to prevent loading data from a different server than the one serving the client app.
265+
One caveat: Make sure your app is served from the same origin (domain name, port, and application layer protocol) as the web service you are trying to access with HttpRequest. Otherwise your app will hit the Access-Control-Allow-Origin restriction. This is a security restriction to prevent loading data from a different server than the one serving the client app.
264266

265267
You can get around this restriction in a couple of ways. The first is to use an emerging technology known as [Cross-Origin Resource Sharing](https://developer.mozilla.org/en/http_access_control) (CORS), which is starting to become implemented by web servers. The second, older way is to use a workaround called JSONP, which makes use of JavaScript callbacks. To use [JSONP with Dart](http://blog.sethladd.com/2012/03/jsonp-with-dart.html), you need to use window.postMessage to allow the JavaScript callbacks to communicate with your Dart code.
266268

@@ -271,7 +273,7 @@ This article showed how a client-side Dart application communicates with a JSON-
271273
<h2 id="resources">Resources</h2>
272274

273275
* [dart:json](http://api.dartlang.org/json/JSON.html)
274-
* [XmlHttpRequest](http://api.dartlang.org/html/XMLHttpRequest.html)
276+
* [HttpRequest](http://api.dartlang.org/html/HttpRequest.html)
275277
* [JsonObject](https://github.com/chrisbu/dartwatch-JsonObject)
276278
* [Using JSONP with Dart](http://blog.sethladd.com/2012/03/jsonp-with-dart.html)
277279
* [About access-control restrictions](https://developer.mozilla.org/en/http_access_control)

src/site/articles/mocking-with-dart/index.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ themselves can be `expect()`-style `Matcher`s, for example:
393393
{% highlight dart %}
394394
m.when(callsTo('sqrt', isNegative)).
395395
alwaysThrow('No imaginary number support');
396-
m.when(callsTo('sqrt', isNonNegative)).alwaysCall((x) => Math.sqrt(x));
396+
m.when(callsTo('sqrt', isNonNegative)).alwaysCall((x) => sqrt(x));
397397
{% endhighlight %}
398398

399399
You don't need to provide argument matchers in `callsTo()` for all arguments of a method, but you do need

src/site/articles/puzzlers/chapter-2.markdown

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,12 @@ So the mysterious behavior is explained.
677677
What happens when we do a naive translation to Dart?
678678

679679
{% highlight dart %}
680+
#import('dart:math');
681+
680682
main() {
681683
StringBuffer word = null;
682-
switch ((Math.random() * 2).toInt()) {
684+
var random = new Random();
685+
switch ((random.nextDouble() * 2).toInt()) {
683686
case 1: word = new StringBuffer('P');
684687
case 2: word = new StringBuffer('G');
685688
default: word = new StringBuffer('M');

src/site/articles/style-guide/index.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,10 @@ together.
612612

613613
<div class="good">
614614
{% highlight dart good %}
615+
#import('dart:math');
616+
// ...
615617
/* Rolls both [Dice] and returns the highest rolled value. */
616-
num greatestRoll(Dice a, Dice b) => Math.max(a.roll(), b.roll());
618+
num greatestRoll(Dice a, Dice b) => max(a.roll(), b.roll());
617619
{% endhighlight %}
618620
</div>
619621

src/site/css/style.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/site/docs/editor/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ <h3>Using autocomplete</h3>
337337
<p>
338338
Type a class, interface, or variable name, and then type a period.
339339
<br />
340-
For example, type <code>document.</code> or <code>Math.</code>
340+
For example, type <code>document.</code>
341341
and pause a moment.
342342
Once the suggestions appear,
343343
continue typing to pare down the list.
@@ -348,9 +348,9 @@ <h3>Using autocomplete</h3>
348348
<p>
349349
Type <b>Ctl+Space</b>.
350350
<br />
351-
For example, type <code>Mat</code>, then Ctl+Space
351+
For example, type <code>Str</code>, then Ctl+Space
352352
to see a list of classes and interfaces
353-
that start with "Mat".
353+
that start with "Str".
354354
</p>
355355
</li>
356356
</ul>
@@ -410,9 +410,9 @@ <h4>Finding where an API is declared</h4>
410410

411411
<p>
412412
The editor displays the file that declares the item.
413-
For example, if you Command-click Math,
413+
For example, if you Command-click String,
414414
the file that declares the
415-
<a href="http://api.dartlang.org/dart_core/Math.html"><code>Math</code></a> type appears.
415+
<code>String</code> type appears.
416416
</p>
417417
</li>
418418
</ol>

0 commit comments

Comments
 (0)