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

Commit

Permalink
refactor(all): use dart idioms
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and mhevery committed Jan 7, 2014
1 parent e829e8a commit c6fd557
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 100 deletions.
6 changes: 3 additions & 3 deletions lib/core/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class NgFilter {
@NgInjectableService()
class FilterMap extends AnnotationMap<NgFilter> {
Injector _injector;
FilterMap(Injector injector, MetadataExtractor extractMetadata) : super(injector, extractMetadata) {
this._injector = injector;
}
FilterMap(Injector injector, MetadataExtractor extractMetadata) :
this._injector = injector,
super(injector, extractMetadata);

call(String name) {
var filter = new NgFilter(name:name);
Expand Down
10 changes: 6 additions & 4 deletions lib/core/interpolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ part of angular.core;

String _startSymbol = '{{';
String _endSymbol = '}}';
num _startSymbolLength = _startSymbol.length;
num _endSymbolLength = _endSymbol.length;
int _startSymbolLength = _startSymbol.length;
int _endSymbolLength = _endSymbol.length;

class Interpolation {
final String template;
Expand Down Expand Up @@ -55,6 +55,7 @@ class Interpolate {
int index = 0;
int length = template.length;
bool hasInterpolation = false;
bool shouldAddSeparator = true;
String exp;
List<String> separators = [];
List<ParsedGetter> watchExpressions = [];
Expand All @@ -70,10 +71,11 @@ class Interpolate {
} else {
// we did not find anything, so we have to add the remainder to the chunks array
separators.add(template.substring(index));
index = length;
shouldAddSeparator = false;
break;
}
}
if (separators.length == watchExpressions.length) {
if (shouldAddSeparator) {
separators.add('');
}
if (!mustHaveExpression || hasInterpolation) {
Expand Down
17 changes: 4 additions & 13 deletions lib/core_dom/block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ class Block implements ElementWrapper {
dom.Node parentElement = previousElement.parentNode;
bool preventDefault = false;

Function insertDomElements = () {
for(var i = 0, ii = elements.length; i < ii; i++) {
parentElement.insertBefore(elements[i], insertBeforeElement);
}
};
Function insertDomElements = () =>
elements.forEach((el) => parentElement.insertBefore(el, insertBeforeElement));

if (onInsert != null) {
onInsert({
Expand Down Expand Up @@ -118,12 +115,9 @@ class Block implements ElementWrapper {
var previousElements = previousBlock.elements,
previousElement = previousElements[previousElements.length - 1],
insertBeforeElement = previousElement.nextNode,
parentElement = previousElement.parentNode,
blockElements = elements;
parentElement = previousElement.parentNode;

for(var i = 0, ii = blockElements.length; i < ii; i++) {
parentElement.insertBefore(blockElements[i], insertBeforeElement);
}
elements.forEach((el) => parentElement.insertBefore(el, insertBeforeElement));

// Remove block from list
previous.next = next;
Expand All @@ -146,12 +140,9 @@ class Block implements ElementWrapper {
* [Block]s can be added in parent [Block]. BlockHoles wrap a DOM element,
* and act as references which allows more blocks to be added.
*/

class BlockHole extends ElementWrapper {
List<dom.Node> elements;

ElementWrapper previous;

ElementWrapper next;

BlockHole(this.elements);
Expand Down
12 changes: 4 additions & 8 deletions lib/core_dom/block_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ part of angular.core.dom;
*
* The BoundBlockFactory needs [Scope] to be created.
*/

class BoundBlockFactory {
BlockFactory blockFactory;

Expand All @@ -26,14 +25,12 @@ class BoundBlockFactory {
* BlockFactory is used to create new [Block]s. BlockFactory is created by the
* [Compiler] as a result of compiling a template.
*/

class BlockFactory {
final List directivePositions;
final List<dom.Node> templateElements;
final Profiler _perf;
final Expando _expando;


BlockFactory(this.templateElements, this.directivePositions, this._perf, this._expando);

BoundBlockFactory bind(Injector injector) =>
Expand All @@ -58,12 +55,12 @@ class BlockFactory {
var preRenderedIndexOffset = 0;
var directiveDefsByName = {};

for (num i = 0, ii = directivePositions.length; i < ii;) {
num index = directivePositions[i++];
for (int i = 0, ii = directivePositions.length; i < ii;) {
int index = directivePositions[i++];

List<DirectiveRef> directiveRefs = directivePositions[i++];
List childDirectivePositions = directivePositions[i++];
var nodeListIndex = index + preRenderedIndexOffset;
int nodeListIndex = index + preRenderedIndexOffset;
dom.Node node = nodeList[nodeListIndex];

var timerId;
Expand All @@ -79,7 +76,7 @@ class BlockFactory {
}

var childInjector = _instantiateDirectives(block, parentInjector, node,
directiveRefs, parentInjector.get(Parser));
directiveRefs, parentInjector.get(Parser));

if (childDirectivePositions != null) {
_link(block, node.nodes, childDirectivePositions, childInjector);
Expand Down Expand Up @@ -280,7 +277,6 @@ class BlockCache {
* the shadowDom, fetching template, importing styles, setting up attribute
* mappings, publishing the controller, and compiling and caching the template.
*/

class _ComponentFactory {

final dom.Element element;
Expand Down
6 changes: 1 addition & 5 deletions lib/core_dom/common.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
part of angular.core.dom;

List<dom.Node> cloneElements(elements) {
var clones = [];
for(var i = 0, ii = elements.length; i < ii; i++) {
clones.add(elements[i].clone(true));
}
return clones;
return elements.map((el) => el.clone(true)).toList();
}

typedef ApplyMapping(NodeAttrs attrs, Scope scope, Object dst);
Expand Down
2 changes: 1 addition & 1 deletion lib/core_dom/compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Compiler {
}

_compileBlock(NodeCursor domCursor, NodeCursor templateCursor,
List<DirectiveRef> useExistingDirectiveRefs) {
List<DirectiveRef> useExistingDirectiveRefs) {
if (domCursor.nodeList().length == 0) return null;

var directivePositions = null; // don't pre-create to create sparse tree and prevent GC pressure.
Expand Down
42 changes: 13 additions & 29 deletions lib/core_dom/cookies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class BrowserCookies {
var cookiePath;
var baseElement;


BrowserCookies() {
// Injecting document produces the error 'Caught Compile-time error during mirrored execution:
// <'file:///mnt/data/b/build/slave/dartium-lucid32-full-trunk/build/src/out/Release/gen/blink/
Expand All @@ -26,7 +25,7 @@ class BrowserCookies {

var baseElementList = _document.getElementsByName('base');
if (baseElementList.isEmpty) return;
baseElement = _document.getElementsByName('base').first;
baseElement = baseElementList.first;
cookiePath = _baseHref();
}

Expand All @@ -43,28 +42,22 @@ class BrowserCookies {
.replaceAll('=', '%3D')
.replaceAll(';', '%3B');


_updateLastCookies() {
var cookieLength, cookieArray, cookie, i, index;

if (_document.cookie != lastCookieString) {
lastCookieString = _document.cookie;
cookieArray = lastCookieString.split("; ");
List<String> cookieArray = lastCookieString.split("; ");
lastCookies = {};

for (i = 0; i < cookieArray.length; i++) {
cookie = cookieArray[i];
index = cookie.indexOf('=');
// The first value that is seen for a cookie is the most specific one.
// Values for the same cookie name that follow are for less specific paths.
// Hence we reverse the array.
cookieArray.reversed.forEach((cookie) {
var index = cookie.indexOf('=');
if (index > 0) { //ignore nameless cookies
var name = _unescape(cookie.substring(0, index));
// the first value that is seen for a cookie is the most
// specific one. values for the same cookie name that
// follow are for less specific paths.
if (!lastCookies.containsKey(name)) {
lastCookies[name] = _unescape(cookie.substring(index + 1));
}
lastCookies[name] = _unescape(cookie.substring(index + 1));
}
}
});
}
return lastCookies;
}
Expand All @@ -78,13 +71,11 @@ class BrowserCookies {
* Sets a cookie. Setting a cookie to [null] deletes the cookie.
*/
operator[]=(name, value) {
var cookieLength, cookieArray, cookie, i, index;

if (identical(value, null)) {
_document.cookie = "${_escape(name)}=;path=$cookiePath;expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
if (value is String) {
cookieLength = (_document.cookie = "${_escape(name)}=${_escape(value)};path=$cookiePath").length + 1;
var cookieLength = (_document.cookie = "${_escape(name)}=${_escape(value)};path=$cookiePath").length + 1;

// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
// - 300 cookies
Expand All @@ -96,7 +87,6 @@ class BrowserCookies {
}
}
}

}

get all => _updateLastCookies();
Expand All @@ -106,10 +96,8 @@ class BrowserCookies {
* Cookies service
*/
class Cookies {

BrowserCookies _browserCookies;

Cookies(BrowserCookies this._browserCookies);
Cookies(this._browserCookies);

/**
* Returns the value of given cookie key
Expand All @@ -119,15 +107,11 @@ class Cookies {
/**
* Sets a value for given cookie key
*/
operator[]=(name, value) {
this._browserCookies[name] = value;
}
operator[]=(name, value) => this._browserCookies[name] = value;

/**
* Remove given cookie
*/
remove(name) {
this._browserCookies[name] = null;
}
remove(name) => this._browserCookies[name] = null;
}

28 changes: 8 additions & 20 deletions lib/core_dom/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class DefaultTransformDataHttpInterceptor implements HttpInterceptor {
};

Function requestError, responseError;

}

/**
Expand Down Expand Up @@ -232,13 +231,7 @@ class HttpResponse {
* header.
*/
headers([String key]) {
if (key == null) {
return _headers;
}
if (_headers.containsKey(key)) {
return _headers[key];
}
return null;
return key == null ? _headers : _headers[key];
}

/**
Expand Down Expand Up @@ -495,16 +488,11 @@ class Http {

// Strip content-type if data is undefined
if (config.data == null) {
List<String> toRemove = [];
headers.forEach((h, _) {
if (h.toUpperCase() == 'CONTENT-TYPE') {
toRemove.add(h);
};
});
toRemove.forEach((x) => headers.remove(x));
new List.from(headers.keys)
.where((h) => h.toUpperCase() == 'CONTENT-TYPE')
.forEach((h) => headers.remove(h));
}


return request(
null,
config: config,
Expand Down Expand Up @@ -651,15 +639,15 @@ class Http {
static Map<String, String> parseHeaders(dom.HttpRequest value) {
var headers = value.getAllResponseHeaders();

var parsed = {}, key, val, i;
var parsed = {};

if (headers == null) return parsed;

headers.split('\n').forEach((line) {
i = line.indexOf(':');
var i = line.indexOf(':');
if (i == -1) return;
key = line.substring(0, i).trim().toLowerCase();
val = line.substring(i + 1).trim();
var key = line.substring(0, i).trim().toLowerCase();
var val = line.substring(i + 1).trim();

if (key != '') {
if (parsed.containsKey(key)) {
Expand Down
12 changes: 3 additions & 9 deletions lib/core_dom/node_cursor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of angular.core.dom;
class NodeCursor {
List<dynamic> stack = [];
List<dom.Node> elements;
num index;
int index;

NodeCursor(this.elements) : index = 0;

Expand All @@ -30,18 +30,12 @@ class NodeCursor {
nodeList() {
if (!isValid()) return []; // or should we return null?

var nodes = [];

for(var i = 0, ii = cursorSize(); i < ii; i++) {
nodes.add(elements[index + i]);
}

return nodes;
return elements.sublist(index, index + cursorSize());
}

descend() {
var childNodes = elements[index].nodes;
var hasChildren = !!(childNodes != null && childNodes.length > 0);
var hasChildren = childNodes != null && childNodes.length > 0;

if (hasChildren) {
stack.add(index);
Expand Down
9 changes: 3 additions & 6 deletions lib/core_dom/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ class _Directive {


class _ContainsSelector {
NgAnnotation annotation;
RegExp regexp;
final NgAnnotation annotation;
final RegExp regexp;

_ContainsSelector(this.annotation, String regexp) {
this.regexp = new RegExp(regexp);
}
_ContainsSelector(this.annotation, String regexp) : regexp = new RegExp(regexp);
}

RegExp _SELECTOR_REGEXP = new RegExp(r'^(?:([\w\-]+)|(?:\.([\w\-]+))|(?:\[([\w\-]+)(?:=([^\]]*))?\]))');
Expand Down Expand Up @@ -209,7 +207,6 @@ List<_SelectorPart> _splitCss(String selector) {
}

/**
*
* Factory method for creating a [DirectiveSelector].
*/
DirectiveSelector directiveSelectorFactory(DirectiveMap directives) {
Expand Down
Loading

0 comments on commit c6fd557

Please sign in to comment.