From ef4bb28be13e99f96c9ace5936cf26a174a0e5f0 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sat, 12 Feb 2011 10:13:28 -0800 Subject: Change API angular.compile(element)([scope], [element/true]) --- CHANGELOG.md | 1 + src/Angular.js | 51 ++- src/Compiler.js | 22 +- src/widgets.js | 8 +- test/AngularSpec.js | 725 +++++++++++++++++++------------------ test/BinderSpec.js | 110 +++--- test/CompilerSpec.js | 7 +- test/ResourceSpec.js | 4 +- test/ScenarioSpec.js | 12 +- test/ValidatorsSpec.js | 10 +- test/directivesSpec.js | 4 +- test/markupSpec.js | 3 +- test/service/invalidWidgetsSpec.js | 6 +- test/service/routeSpec.js | 4 +- test/widgetsSpec.js | 18 +- 15 files changed, 524 insertions(+), 461 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 979d2435..04d511d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ recommended way to deal with initializing scope is to put it in the root constructor controller. To migrate simply remove the call to $init() and move any code you had before $init() to the root controller. +- Change API angular.compile(..) to angular.compile(element)([scope], [element/true]) diff --git a/src/Angular.js b/src/Angular.js index 9b2c7ea6..9eaeb093 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -793,21 +793,50 @@ function merge(src, dst) { * @function * * @description - * Compiles a piece of HTML or DOM into a {@link angular.scope scope} object. + * Compiles a piece of HTML string or DOM into a view and produces a linking function, which can + * then be used to link {@link angular.scope scope} and the template together. The compilation + * process walks the DOM tree and tries to match DOM elements to {@link angular.markup markup}, + * {@link angular.attrMarkup attrMarkup}, {@link angular.widget widgets}, and + * {@link angular.directive directives}. For each match it executes coresponding markup, \ + * attrMarkup, widget or directive template function and collects the instance functions into a + * single linking function which is then returned. The linking function can then be used + * many-times-over on clones of compiled DOM structure, (For example when compiling + * {@link angular.widget.@ng:repeat repeater} the resulting linking function is called once for + * each item in the collection. The `ng:repeat` does this by cloning the template DOM once for + * each item in collection and then calling the linking function to link the cloned template + * with the a new scope for each item in the collection.) + *
- var scope1 = angular.compile(window.document);
+ var mvc1 = angular.compile(window.document)();
+ mvc1.view; // compiled view elment
+ mvc1.scope; // scope bound to the element
- var scope2 = angular.compile('click me');
+ var mvc2 = angular.compile('click me')();
*
- * @param {string|DOMElement} element Element to compile.
- * @param {Object=} parentScope Scope to become the parent scope of the newly compiled scope.
- * @returns {Object} Compiled scope object.
+ * @param {string|DOMElement} element Element or HTML to compile into a template function.
+ * @returns {function([scope][, element])} a template function which is used to bind element
+ * and scope. Where:
+ *
+ * * `scope` - {@link angular.scope scope} A scope to bind to. If none specified, then a new
+ * root scope is created.
+ * * `element` - {@link angular.element element} Element to use as the template. If none
+ * specified then reuse the element from `angular.compile(element)`. If `true`
+ * then clone the `angular.compile(element)`. The element must be either the same
+ * element as `angular.compile(element)` or an identical clone to
+ * `angular.compile(element)`. Using an element with differnt structure will cause
+ * unpredictable behavior.
+ *
+ * Calling the template function returns object: `{scope:?, view:?}`, where:
+ *
+ * * `view` - the DOM element which represents the compiled template. Either same or clone of
+ * `element` specifed in compile or template function.
+ * * `scope` - scope to which the element is bound to. Either a root scope or scope specified
+ * in the template function.
*/
-function compile(element, parentScope) {
- var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget),
- $element = jqLite(element);
- return compiler.compile($element)($element, parentScope);
+function compile(element) {
+ return new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget)
+ .compile(element);
}
/////////////////////////////////////////////////
@@ -989,7 +1018,7 @@ function toKeyValue(obj) {
function angularInit(config){
if (config.autobind) {
// TODO default to the source of angular.js
- var scope = compile(window.document, _null, {'$config':config}),
+ var scope = compile(window.document)(null, createScope({'$config':config})),
$browser = scope.$service('$browser');
if (config.css)
diff --git a/src/Compiler.js b/src/Compiler.js
index 6aee40b8..890f2510 100644
--- a/src/Compiler.js
+++ b/src/Compiler.js
@@ -80,30 +80,30 @@ function Compiler(markup, attrMarkup, directives, widgets){
}
Compiler.prototype = {
- compile: function(element) {
- element = jqLite(element);
+ compile: function(templateElement) {
+ templateElement = jqLite(templateElement);
var index = 0,
template,
- parent = element.parent();
+ parent = templateElement.parent();
if (parent && parent[0]) {
parent = parent[0];
for(var i = 0; i < parent.childNodes.length; i++) {
- if (parent.childNodes[i] == element[0]) {
+ if (parent.childNodes[i] == templateElement[0]) {
index = i;
}
}
}
- template = this.templatize(element, index, 0) || new Template();
- return function(element, parentScope){
- element = jqLite(element);
- var scope = parentScope && parentScope.$eval
- ? parentScope
- : createScope(parentScope);
+ template = this.templatize(templateElement, index, 0) || new Template();
+ return function(scope, element){
+ scope = scope || createScope();
+ element = element === true
+ ? templateElement.cloneNode()
+ : (jqLite(element) || templateElement);
element.data($$scope, scope);
template.attach(element, scope);
scope.$element = element;
scope.$eval();
- return scope;
+ return {scope:scope, view:element};
};
},
diff --git a/src/widgets.js b/src/widgets.js
index 58c22081..14d6fe10 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -676,7 +676,7 @@ angularWidget('ng:include', function(element){
xhr('GET', src, function(code, response){
element.html(response);
childScope = useScope || createScope(scope);
- compiler.compile(element)(element, childScope);
+ compiler.compile(element)(childScope);
scope.$eval(onloadExp);
});
} else {
@@ -793,7 +793,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
var caseElement = switchCase.element.cloneNode();
element.append(caseElement);
childScope.$tryEval(switchCase.change, element);
- switchCase.template(caseElement, childScope);
+ switchCase.template(childScope, caseElement);
}
});
});
@@ -945,7 +945,7 @@ angularWidget("@ng:repeat", function(expression, element){
(index == collectionLength - 1 ? 'last' : 'middle');
lastElement.after(cloneElement = element.cloneNode());
cloneElement.attr('ng:repeat-index', index);
- linker(cloneElement, childScope);
+ linker(childScope, cloneElement);
children.push(childScope);
}
childScope.$eval();
@@ -1067,7 +1067,7 @@ angularWidget('ng:view', function(element) {
if (src) {
$xhr('GET', src, function(code, response){
element.html(response);
- compiler.compile(element)(element, childScope);
+ compiler.compile(element)(childScope);
});
} else {
element.html('');
diff --git a/test/AngularSpec.js b/test/AngularSpec.js
index 010dce7c..8ff0631d 100644
--- a/test/AngularSpec.js
+++ b/test/AngularSpec.js
@@ -1,350 +1,389 @@
-beforeEach(function(){
- compileCache = {};
-});
-
-describe('case', function(){
- it('should change case', function(){
- expect(lowercase('ABC90')).toEqual('abc90');
- expect(manualLowercase('ABC90')).toEqual('abc90');
- expect(uppercase('abc90')).toEqual('ABC90');
- expect(manualUppercase('abc90')).toEqual('ABC90');
- });
-});
-
-describe("copy", function(){
- it("should return same object", function (){
- var obj = {};
- var arr = [];
- expect(copy({}, obj)).toBe(obj);
- expect(copy([], arr)).toBe(arr);
- });
-
- it("should copy Date", function(){
- var date = new Date(123);
- expect(copy(date) instanceof Date).toBeTruthy();
- expect(copy(date).getTime()).toEqual(123);
- expect(copy(date) === date).toBeFalsy();
- });
-
- it("should copy array", function(){
- var src = [1, {name:"value"}];
- var dst = [{key:"v"}];
- expect(copy(src, dst)).toBe(dst);
- expect(dst).toEqual([1, {name:"value"}]);
- expect(dst[1]).toEqual({name:"value"});
- expect(dst[1]).not.toBe(src[1]);
- });
-
- it('should copy empty array', function() {
- var src = [];
- var dst = [{key: "v"}];
- expect(copy(src, dst)).toEqual([]);
- expect(dst).toEqual([]);
- });
-
- it("should copy object", function(){
- var src = {a:{name:"value"}};
- var dst = {b:{key:"v"}};
- expect(copy(src, dst)).toBe(dst);
- expect(dst).toEqual({a:{name:"value"}});
- expect(dst.a).toEqual(src.a);
- expect(dst.a).not.toBe(src.a);
- });
-
- it("should copy primitives", function(){
- expect(copy(null)).toEqual(null);
- expect(copy('')).toBe('');
- expect(copy('lala')).toBe('lala');
- expect(copy(123)).toEqual(123);
- expect(copy([{key:null}])).toEqual([{key:null}]);
- });
-
-});
-
-describe('equals', function(){
- it('should return true if same object', function(){
- var o = {};
- expect(equals(o, o)).toEqual(true);
- expect(equals(1, '1')).toEqual(true);
- expect(equals(1, '2')).toEqual(false);
- });
-
- it('should recurse into object', function(){
- expect(equals({}, {})).toEqual(true);
- expect(equals({name:'misko'}, {name:'misko'})).toEqual(true);
- expect(equals({name:'misko', age:1}, {name:'misko'})).toEqual(false);
- expect(equals({name:'misko'}, {name:'misko', age:1})).toEqual(false);
- expect(equals({name:'misko'}, {name:'adam'})).toEqual(false);
- expect(equals(['misko'], ['misko'])).toEqual(true);
- expect(equals(['misko'], ['adam'])).toEqual(false);
- expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
- });
-
- it('should ignore $ member variables', function(){
- expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
- expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
- expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
- });
-
- it('should ignore functions', function(){
- expect(equals({func: function() {}}, {bar: function() {}})).toEqual(true);
- });
-
- it('should work well with nulls', function() {
- expect(equals(null, '123')).toBe(false);
- expect(equals('123', null)).toBe(false);
-
- var obj = {foo:'bar'};
- expect(equals(null, obj)).toBe(false);
- expect(equals(obj, null)).toBe(false);
-
- expect(equals(null, null)).toBe(true);
- });
-
- it('should work well with undefined', function() {
- expect(equals(undefined, '123')).toBe(false);
- expect(equals('123', undefined)).toBe(false);
-
- var obj = {foo:'bar'};
- expect(equals(undefined, obj)).toBe(false);
- expect(equals(obj, undefined)).toBe(false);
-
- expect(equals(undefined, undefined)).toBe(true);
- });
-});
-
-describe('parseKeyValue', function() {
- it('should parse a string into key-value pairs', function() {
- expect(parseKeyValue('')).toEqual({});
- expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'});
- expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'});
- expect(parseKeyValue('escaped%20key=escaped%20value')).
+describe('angular', function(){
+ describe('case', function(){
+ it('should change case', function(){
+ expect(lowercase('ABC90')).toEqual('abc90');
+ expect(manualLowercase('ABC90')).toEqual('abc90');
+ expect(uppercase('abc90')).toEqual('ABC90');
+ expect(manualUppercase('abc90')).toEqual('ABC90');
+ });
+ });
+
+ describe("copy", function(){
+ it("should return same object", function (){
+ var obj = {};
+ var arr = [];
+ expect(copy({}, obj)).toBe(obj);
+ expect(copy([], arr)).toBe(arr);
+ });
+
+ it("should copy Date", function(){
+ var date = new Date(123);
+ expect(copy(date) instanceof Date).toBeTruthy();
+ expect(copy(date).getTime()).toEqual(123);
+ expect(copy(date) === date).toBeFalsy();
+ });
+
+ it("should copy array", function(){
+ var src = [1, {name:"value"}];
+ var dst = [{key:"v"}];
+ expect(copy(src, dst)).toBe(dst);
+ expect(dst).toEqual([1, {name:"value"}]);
+ expect(dst[1]).toEqual({name:"value"});
+ expect(dst[1]).not.toBe(src[1]);
+ });
+
+ it('should copy empty array', function() {
+ var src = [];
+ var dst = [{key: "v"}];
+ expect(copy(src, dst)).toEqual([]);
+ expect(dst).toEqual([]);
+ });
+
+ it("should copy object", function(){
+ var src = {a:{name:"value"}};
+ var dst = {b:{key:"v"}};
+ expect(copy(src, dst)).toBe(dst);
+ expect(dst).toEqual({a:{name:"value"}});
+ expect(dst.a).toEqual(src.a);
+ expect(dst.a).not.toBe(src.a);
+ });
+
+ it("should copy primitives", function(){
+ expect(copy(null)).toEqual(null);
+ expect(copy('')).toBe('');
+ expect(copy('lala')).toBe('lala');
+ expect(copy(123)).toEqual(123);
+ expect(copy([{key:null}])).toEqual([{key:null}]);
+ });
+
+ });
+
+ describe('equals', function(){
+ it('should return true if same object', function(){
+ var o = {};
+ expect(equals(o, o)).toEqual(true);
+ expect(equals(1, '1')).toEqual(true);
+ expect(equals(1, '2')).toEqual(false);
+ });
+
+ it('should recurse into object', function(){
+ expect(equals({}, {})).toEqual(true);
+ expect(equals({name:'misko'}, {name:'misko'})).toEqual(true);
+ expect(equals({name:'misko', age:1}, {name:'misko'})).toEqual(false);
+ expect(equals({name:'misko'}, {name:'misko', age:1})).toEqual(false);
+ expect(equals({name:'misko'}, {name:'adam'})).toEqual(false);
+ expect(equals(['misko'], ['misko'])).toEqual(true);
+ expect(equals(['misko'], ['adam'])).toEqual(false);
+ expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
+ });
+
+ it('should ignore $ member variables', function(){
+ expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
+ expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
+ expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
+ });
+
+ it('should ignore functions', function(){
+ expect(equals({func: function() {}}, {bar: function() {}})).toEqual(true);
+ });
+
+ it('should work well with nulls', function() {
+ expect(equals(null, '123')).toBe(false);
+ expect(equals('123', null)).toBe(false);
+
+ var obj = {foo:'bar'};
+ expect(equals(null, obj)).toBe(false);
+ expect(equals(obj, null)).toBe(false);
+
+ expect(equals(null, null)).toBe(true);
+ });
+
+ it('should work well with undefined', function() {
+ expect(equals(undefined, '123')).toBe(false);
+ expect(equals('123', undefined)).toBe(false);
+
+ var obj = {foo:'bar'};
+ expect(equals(undefined, obj)).toBe(false);
+ expect(equals(obj, undefined)).toBe(false);
+
+ expect(equals(undefined, undefined)).toBe(true);
+ });
+ });
+
+ describe('parseKeyValue', function() {
+ it('should parse a string into key-value pairs', function() {
+ expect(parseKeyValue('')).toEqual({});
+ expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'});
+ expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'});
+ expect(parseKeyValue('escaped%20key=escaped%20value')).
toEqual({'escaped key': 'escaped value'});
- expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''});
- expect(parseKeyValue('flag1&key=value&flag2')).
+ expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''});
+ expect(parseKeyValue('flag1&key=value&flag2')).
toEqual({flag1: true, key: 'value', flag2: true});
+ });
});
-});
-describe('toKeyValue', function() {
- it('should parse key-value pairs into string', function() {
- expect(toKeyValue({})).toEqual('');
- expect(toKeyValue({simple: 'pair'})).toEqual('simple=pair');
- expect(toKeyValue({first: '1', second: '2'})).toEqual('first=1&second=2');
- expect(toKeyValue({'escaped key': 'escaped value'})).
+ describe('toKeyValue', function() {
+ it('should parse key-value pairs into string', function() {
+ expect(toKeyValue({})).toEqual('');
+ expect(toKeyValue({simple: 'pair'})).toEqual('simple=pair');
+ expect(toKeyValue({first: '1', second: '2'})).toEqual('first=1&second=2');
+ expect(toKeyValue({'escaped key': 'escaped value'})).
toEqual('escaped%20key=escaped%20value');
- expect(toKeyValue({emptyKey: ''})).toEqual('emptyKey=');
- });
-
- it('should parse true values into flags', function() {
- expect(toKeyValue({flag1: true, key: 'value', flag2: true})).toEqual('flag1&key=value&flag2');
- });
-});
-
-
-describe ('rngScript', function() {
- it('should match angular.js', function() {
- expect('angular.js'.match(rngScript)).not.toBeNull();
- expect('../angular.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular.js'.match(rngScript)).not.toBeNull();
-
- expect('foo.js'.match(rngScript)).toBeNull();
- expect('foo/foo.js'.match(rngScript)).toBeNull();
- expect('my-angular-app.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular.min.js', function() {
- expect('angular.min.js'.match(rngScript)).not.toBeNull();
- expect('../angular.min.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular.min.js'.match(rngScript)).not.toBeNull();
-
- expect('my-angular-app.min.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app.min.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular-bootstrap.js', function() {
- expect('angular-bootstrap.js'.match(rngScript)).not.toBeNull();
- expect('../angular-bootstrap.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-bootstrap.js'.match(rngScript)).not.toBeNull();
-
- expect('my-angular-app-bootstrap.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app-bootstrap.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular-0.9.0.js', function() {
- expect('angular-0.9.0.js'.match(rngScript)).not.toBeNull();
- expect('../angular-0.9.0.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-0.9.0.js'.match(rngScript)).not.toBeNull();
-
- expect('my-angular-app-0.9.0.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app-0.9.0.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular-0.9.0.min.js', function() {
- expect('angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
- expect('../angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
-
- expect('my-angular-app-0.9.0.min.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app-0.9.0.min.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular-0.9.0-de0a8612.js', function() {
- expect('angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
- expect('../angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
-
- expect('my-angular-app-0.9.0-de0a8612.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app-0.9.0-de0a8612.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular-0.9.0-de0a8612.min.js', function() {
- expect('angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
- expect('../angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
-
- expect('my-angular-app-0.9.0-de0a8612.min.js'.match(rngScript)).toBeNull();
- expect('foo/../my-angular-app-0.9.0-de0a8612.min.js'.match(rngScript)).toBeNull();
- });
-
- it('should match angular-scenario.js', function() {
- expect('angular-scenario.js'.match(rngScript)).not.toBeNull();
- expect('angular-scenario.min.js'.match(rngScript)).not.toBeNull();
- expect('../angular-scenario.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-scenario.min.js'.match(rngScript)).not.toBeNull();
- });
-
- it('should match angular-scenario-0.9.0(.min).js', function() {
- expect('angular-scenario-0.9.0.js'.match(rngScript)).not.toBeNull();
- expect('angular-scenario-0.9.0.min.js'.match(rngScript)).not.toBeNull();
- expect('../angular-scenario-0.9.0.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-scenario-0.9.0.min.js'.match(rngScript)).not.toBeNull();
- });
-
- it('should match angular-scenario-0.9.0-de0a8612(.min).js', function() {
- expect('angular-scenario-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
- expect('angular-scenario-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
- expect('../angular-scenario-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
- expect('foo/angular-scenario-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
- });
-});
-
-
-describe('angularJsConfig', function() {
- it('should find angular.js script tag and config', function() {
- var doc = { getElementsByTagName: function(tagName) {
- expect(tagName).toEqual('script');
- return [{nodeName: 'SCRIPT', src: 'random.js'},
- {nodeName: 'SCRIPT', src: 'angular.js'},
- {nodeName: 'SCRIPT', src: 'my-angular-app.js'}];
- }
- };
-
- expect(angularJsConfig(doc)).toEqual({base_url: '',
- ie_compat: 'angular-ie-compat.js',
- ie_compat_id: 'ng-ie-compat'});
- });
-
-
- it('should extract angular config from the ng: attributes', function() {
- var doc = { getElementsByTagName: function(tagName) {
- expect(lowercase(tagName)).toEqual('script');
- return [{nodeName: 'SCRIPT',
- src: 'angularjs/angular.js',
- attributes: [{name: 'ng:autobind', value:undefined},
- {name: 'ng:css', value: 'css/my_custom_angular.css'},
- {name: 'ng:ie-compat', value: 'myjs/angular-ie-compat.js'},
- {name: 'ng:ie-compat-id', value: 'ngcompat'}] }];
- }};
-
- expect(angularJsConfig(doc)).toEqual({base_url: 'angularjs/',
- autobind: true,
- css: 'css/my_custom_angular.css',
- ie_compat: 'myjs/angular-ie-compat.js',
- ie_compat_id: 'ngcompat'});
- });
-
-
- it('should extract angular autobind config from the script hashpath attributes', function() {
- var doc = { getElementsByTagName: function(tagName) {
- expect(lowercase(tagName)).toEqual('script');
- return [{nodeName: 'SCRIPT',
- src: 'angularjs/angular.js#autobind'}];
- }};
-
- expect(angularJsConfig(doc)).toEqual({base_url: 'angularjs/',
- autobind: true,
- ie_compat: 'angularjs/angular-ie-compat.js',
- ie_compat_id: 'ng-ie-compat'});
- });
-
-
- it("should default to versioned ie-compat file if angular file is versioned", function() {
- var doc = { getElementsByTagName: function(tagName) {
- expect(lowercase(tagName)).toEqual('script');
- return [{nodeName: 'SCRIPT',
- src: 'js/angular-0.9.0.js'}];
- }};
-
- expect(angularJsConfig(doc)).toEqual({base_url: 'js/',
- ie_compat: 'js/angular-ie-compat-0.9.0.js',
- ie_compat_id: 'ng-ie-compat'});
- });
-
-
- it("should default to versioned ie-compat file if angular file is versioned and minified", function() {
- var doc = { getElementsByTagName: function(tagName) {
- expect(lowercase(tagName)).toEqual('script');
- return [{nodeName: 'SCRIPT',
- src: 'js/angular-0.9.0-cba23f00.min.js'}];
- }};
-
- expect(angularJsConfig(doc)).toEqual({base_url: 'js/',
- ie_compat: 'js/angular-ie-compat-0.9.0-cba23f00.js',
- ie_compat_id: 'ng-ie-compat'});
- });
-});
-
-
-describe('angular service', function() {
- it('should override services', function() {
- var scope = createScope();
- angular.service('fake', function() { return 'old'; });
- angular.service('fake', function() { return 'new'; });
-
- expect(scope.$service('fake')).toEqual('new');
- });
-
- it('should not preserve properties on override', function() {
- angular.service('fake', {$one: true}, {$two: true}, {three: true});
- var result = angular.service('fake', {$four: true});
-
- expect(result.$one).toBeUndefined();
- expect(result.$two).toBeUndefined();
- expect(result.three).toBeUndefined();
- expect(result.$four).toBe(true);
- });
-
- it('should not preserve non-angular properties on override', function() {
- angular.service('fake', {one: true}, {two: true});
- var result = angular.service('fake', {third: true});
-
- expect(result.one).not.toBeDefined();
- expect(result.two).not.toBeDefined();
- expect(result.third).toBeTruthy();
- });
-});
-
-describe('isDate', function() {
- it('should return true for Date object', function() {
- expect(isDate(new Date())).toBe(true);
- });
-
- it('should return false for non Date objects', function() {
- expect(isDate([])).toBe(false);
- expect(isDate('')).toBe(false);
- expect(isDate(23)).toBe(false);
- expect(isDate({})).toBe(false);
+ expect(toKeyValue({emptyKey: ''})).toEqual('emptyKey=');
+ });
+
+ it('should parse true values into flags', function() {
+ expect(toKeyValue({flag1: true, key: 'value', flag2: true})).toEqual('flag1&key=value&flag2');
+ });
+ });
+
+
+ describe ('rngScript', function() {
+ it('should match angular.js', function() {
+ expect('angular.js'.match(rngScript)).not.toBeNull();
+ expect('../angular.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular.js'.match(rngScript)).not.toBeNull();
+
+ expect('foo.js'.match(rngScript)).toBeNull();
+ expect('foo/foo.js'.match(rngScript)).toBeNull();
+ expect('my-angular-app.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular.min.js', function() {
+ expect('angular.min.js'.match(rngScript)).not.toBeNull();
+ expect('../angular.min.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular.min.js'.match(rngScript)).not.toBeNull();
+
+ expect('my-angular-app.min.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app.min.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular-bootstrap.js', function() {
+ expect('angular-bootstrap.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-bootstrap.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-bootstrap.js'.match(rngScript)).not.toBeNull();
+
+ expect('my-angular-app-bootstrap.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app-bootstrap.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular-0.9.0.js', function() {
+ expect('angular-0.9.0.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-0.9.0.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-0.9.0.js'.match(rngScript)).not.toBeNull();
+
+ expect('my-angular-app-0.9.0.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app-0.9.0.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular-0.9.0.min.js', function() {
+ expect('angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-0.9.0.min.js'.match(rngScript)).not.toBeNull();
+
+ expect('my-angular-app-0.9.0.min.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app-0.9.0.min.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular-0.9.0-de0a8612.js', function() {
+ expect('angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
+
+ expect('my-angular-app-0.9.0-de0a8612.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app-0.9.0-de0a8612.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular-0.9.0-de0a8612.min.js', function() {
+ expect('angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
+
+ expect('my-angular-app-0.9.0-de0a8612.min.js'.match(rngScript)).toBeNull();
+ expect('foo/../my-angular-app-0.9.0-de0a8612.min.js'.match(rngScript)).toBeNull();
+ });
+
+ it('should match angular-scenario.js', function() {
+ expect('angular-scenario.js'.match(rngScript)).not.toBeNull();
+ expect('angular-scenario.min.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-scenario.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-scenario.min.js'.match(rngScript)).not.toBeNull();
+ });
+
+ it('should match angular-scenario-0.9.0(.min).js', function() {
+ expect('angular-scenario-0.9.0.js'.match(rngScript)).not.toBeNull();
+ expect('angular-scenario-0.9.0.min.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-scenario-0.9.0.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-scenario-0.9.0.min.js'.match(rngScript)).not.toBeNull();
+ });
+
+ it('should match angular-scenario-0.9.0-de0a8612(.min).js', function() {
+ expect('angular-scenario-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
+ expect('angular-scenario-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
+ expect('../angular-scenario-0.9.0-de0a8612.js'.match(rngScript)).not.toBeNull();
+ expect('foo/angular-scenario-0.9.0-de0a8612.min.js'.match(rngScript)).not.toBeNull();
+ });
+ });
+
+
+ describe('angularJsConfig', function() {
+ it('should find angular.js script tag and config', function() {
+ var doc = { getElementsByTagName: function(tagName) {
+ expect(tagName).toEqual('script');
+ return [{nodeName: 'SCRIPT', src: 'random.js'},
+ {nodeName: 'SCRIPT', src: 'angular.js'},
+ {nodeName: 'SCRIPT', src: 'my-angular-app.js'}];
+ }
+ };
+
+ expect(angularJsConfig(doc)).toEqual({base_url: '',
+ ie_compat: 'angular-ie-compat.js',
+ ie_compat_id: 'ng-ie-compat'});
+ });
+
+
+ it('should extract angular config from the ng: attributes', function() {
+ var doc = { getElementsByTagName: function(tagName) {
+ expect(lowercase(tagName)).toEqual('script');
+ return [{nodeName: 'SCRIPT',
+ src: 'angularjs/angular.js',
+ attributes: [{name: 'ng:autobind', value:undefined},
+ {name: 'ng:css', value: 'css/my_custom_angular.css'},
+ {name: 'ng:ie-compat', value: 'myjs/angular-ie-compat.js'},
+ {name: 'ng:ie-compat-id', value: 'ngcompat'}] }];
+ }};
+
+ expect(angularJsConfig(doc)).toEqual({base_url: 'angularjs/',
+ autobind: true,
+ css: 'css/my_custom_angular.css',
+ ie_compat: 'myjs/angular-ie-compat.js',
+ ie_compat_id: 'ngcompat'});
+ });
+
+
+ it('should extract angular autobind config from the script hashpath attributes', function() {
+ var doc = { getElementsByTagName: function(tagName) {
+ expect(lowercase(tagName)).toEqual('script');
+ return [{nodeName: 'SCRIPT',
+ src: 'angularjs/angular.js#autobind'}];
+ }};
+
+ expect(angularJsConfig(doc)).toEqual({base_url: 'angularjs/',
+ autobind: true,
+ ie_compat: 'angularjs/angular-ie-compat.js',
+ ie_compat_id: 'ng-ie-compat'});
+ });
+
+
+ it("should default to versioned ie-compat file if angular file is versioned", function() {
+ var doc = { getElementsByTagName: function(tagName) {
+ expect(lowercase(tagName)).toEqual('script');
+ return [{nodeName: 'SCRIPT',
+ src: 'js/angular-0.9.0.js'}];
+ }};
+
+ expect(angularJsConfig(doc)).toEqual({base_url: 'js/',
+ ie_compat: 'js/angular-ie-compat-0.9.0.js',
+ ie_compat_id: 'ng-ie-compat'});
+ });
+
+
+ it("should default to versioned ie-compat file if angular file is versioned and minified", function() {
+ var doc = { getElementsByTagName: function(tagName) {
+ expect(lowercase(tagName)).toEqual('script');
+ return [{nodeName: 'SCRIPT',
+ src: 'js/angular-0.9.0-cba23f00.min.js'}];
+ }};
+
+ expect(angularJsConfig(doc)).toEqual({base_url: 'js/',
+ ie_compat: 'js/angular-ie-compat-0.9.0-cba23f00.js',
+ ie_compat_id: 'ng-ie-compat'});
+ });
+ });
+
+
+ describe('angular service', function() {
+ it('should override services', function() {
+ var scope = createScope();
+ angular.service('fake', function() { return 'old'; });
+ angular.service('fake', function() { return 'new'; });
+
+ expect(scope.$service('fake')).toEqual('new');
+ });
+
+ it('should not preserve properties on override', function() {
+ angular.service('fake', {$one: true}, {$two: true}, {three: true});
+ var result = angular.service('fake', {$four: true});
+
+ expect(result.$one).toBeUndefined();
+ expect(result.$two).toBeUndefined();
+ expect(result.three).toBeUndefined();
+ expect(result.$four).toBe(true);
+ });
+
+ it('should not preserve non-angular properties on override', function() {
+ angular.service('fake', {one: true}, {two: true});
+ var result = angular.service('fake', {third: true});
+
+ expect(result.one).not.toBeDefined();
+ expect(result.two).not.toBeDefined();
+ expect(result.third).toBeTruthy();
+ });
+ });
+
+ describe('isDate', function() {
+ it('should return true for Date object', function() {
+ expect(isDate(new Date())).toBe(true);
+ });
+
+ it('should return false for non Date objects', function() {
+ expect(isDate([])).toBe(false);
+ expect(isDate('')).toBe(false);
+ expect(isDate(23)).toBe(false);
+ expect(isDate({})).toBe(false);
+ });
+ });
+
+ describe('compile', function(){
+ var mvc;
+ afterEach(function(){
+ dealoc(mvc.view);
+ });
+
+ it('should link to existing node and create scope', function(){
+ mvc = angular.compile('Hello World!', sortedHtml(c.node)); + assertEquals('
Hello World!', sortedHtml(c.view)); }); it('FillInOptionValueWhenMissing', function(){ @@ -503,9 +501,9 @@ describe('Binder', function(){ c.scope.$set('a', 'A'); c.scope.$set('b', 'B'); c.scope.$eval(); - var optionA = childNode(c.node, 0); - var optionB = childNode(c.node, 1); - var optionC = childNode(c.node, 2); + var optionA = childNode(c.view, 0); + var optionB = childNode(c.view, 1); + var optionC = childNode(c.view, 2); expect(optionA.attr('value')).toEqual('A'); expect(optionA.text()).toEqual('A'); @@ -565,7 +563,7 @@ describe('Binder', function(){ ''); c.scope.$eval(); function assertChild(index, disabled) { - var child = childNode(c.node, index); + var child = childNode(c.view, index); assertEquals(sortedHtml(child), disabled, !!child.attr('disabled')); } @@ -581,8 +579,8 @@ describe('Binder', function(){ var c = this.compile('