Skip to content

Commit

Permalink
handle 'property' and 'itemprop' values being used as keys
Browse files Browse the repository at this point in the history
  • Loading branch information
edoparearyee committed Oct 5, 2015
1 parent 26cb142 commit 1b458f9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
30 changes: 26 additions & 4 deletions app/js/meta.js
Expand Up @@ -65,14 +65,34 @@ angular.module('sn.meta', ['ngRoute'])
scope: {},
link: function ($scope, $element, $attrs) {

/**
* Can be either 'name', 'itemprop' or 'property'
* @property keyAttr
* @type {Object}
*/
var keyAttr = 'name';

/**
* Determines whether the element using either 'name',
* 'itemprop' or 'property' attributes as it's key.
* @method findKeyValue
*/
var findKeyValue = function findKeyValue() {
if ($attrs.property) {
keyAttr = 'property';
} else if ($attrs.itemprop) {
keyAttr = 'itemprop';
}
};

/**
* @method setMeta
* @param {event} $event - '$routeChangeSuccess' event from ngRoute service
* @param {Object} meta - The requested route object
*/
var setMeta = function setMeta(event, meta){
if ( meta[$attrs.name] ) {
$element.attr('content', meta[$attrs.name]);
if ( meta[ $attrs[keyAttr] ] ) {
$element.attr('content', meta[ $attrs[keyAttr] ]);
}
};

Expand All @@ -89,9 +109,9 @@ angular.module('sn.meta', ['ngRoute'])
if (current &&
current.$$route &&
current.$$route.meta &&
current.$$route.meta[$attrs.name]
current.$$route.meta[ $attrs[keyAttr] ]
) {
content = current.$$route.meta[$attrs.name];
content = current.$$route.meta[ $attrs[keyAttr] ];
}

$element.attr('content', content);
Expand All @@ -111,6 +131,8 @@ angular.module('sn.meta', ['ngRoute'])
$rootScope.$on(snMetaEvents.ROUTE_CHANGE_SUCCESS, onRouteChangeSuccess);
$rootScope.$on(snMetaEvents.ROUTE_CHANGE_ERROR, onRouteChangeError);

findKeyValue();

}
};
}
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/meta.js
Expand Up @@ -47,6 +47,56 @@ describe('sn.meta:meta directive', function() {
expect(element.attr('content')).not.toEqual('some content');
});
});

describe('property as key', function() {

beforeEach(inject(function (_$rootScope_, $compile, $injector) {

element = '<meta property="description" content="Page description. No longer than 155 characters." />';

element = $compile(element)($scope);
$scope.$digest();

}));

it('should render directive with correct meta data', function(){
$rootScope.$broadcast("$routeChangeSuccess", {
$$route: {
meta: {
description: 'pageone description'
}
}
})
expect(element.attr('content')).toEqual('pageone description');

});

});

describe('itemprop as key', function() {

beforeEach(inject(function (_$rootScope_, $compile, $injector) {

element = '<meta itemprop="description" content="Page description. No longer than 155 characters." />';

element = $compile(element)($scope);
$scope.$digest();

}));

it('should render directive with correct meta data', function(){
$rootScope.$broadcast("$routeChangeSuccess", {
$$route: {
meta: {
description: 'pageone description'
}
}
})
expect(element.attr('content')).toEqual('pageone description');

});

});
});

describe('sn.meta:snMeta service', function() {
Expand Down

0 comments on commit 1b458f9

Please sign in to comment.