Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

chore(website): fix parsing in function list #2541

Merged
merged 1 commit into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions website/js/api-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,8 @@
$anchorScroll();
};

/**
* Use the $sce service to trust the html rendered in the view.
* @param html
*/
$scope.trust = function(html) {
if (!html) {
return;
}

// Does it come with a type? Types come escaped as [description](theType).
var match;
while (match = html.match(/(\[(.*?)\]\((.*?)\))/)) {
var link = '<a href="' +
(match[3].match(/^https?:\/\//) ? '' : '#/api?view=') + match[3] +
'">' + match[2] + '</a>';
html = html.replace(match[1], link);
}

return $sce.trustAsHtml(html);
return trustHTML($sce, html);
};
};

Expand Down Expand Up @@ -153,8 +136,25 @@

// Add short description.
if (item.description) {
item.shortDescription =
item.description.substring(0., item.description.indexOf('.') + 1);
// Find the correct portion of the description

// The following parsing is OK most of the time
var sentenceEnd = item.description.search(/\.\s|\.$/) + 1 || Infinity;
var paragraphEnd = item.description.indexOf('</p>') + 4;
if (paragraphEnd == 3) {
paragraphEnd = Infinity
}
var shortDescription = item.description.substring(0, Math.min(
item.description.length, sentenceEnd, paragraphEnd)).trim();

// Remove <p> tags
if (shortDescription.substr(0,3) == '<p>') {
shortDescription = shortDescription.substr(3);
if (shortDescription.substr(-4) == '</p>') {
shortDescription = shortDescription.substr(0, shortDescription.length - 4);
}
}
item.shortDescription = shortDescription;
}
});

Expand Down Expand Up @@ -201,7 +201,6 @@

var addItemToList = function(item, depth) {
if (item.inList) {
console.log(item.name);
return;
}
item.treeClasses = 'depth-' + depth;
Expand All @@ -217,7 +216,6 @@
});
}
if (item.extends) {
console.log(item.base.name);
var parent = self.itemsByName[item.base.name];
if (parent != null) {
addItemToList(parent, depth + 1);
Expand Down
5 changes: 5 additions & 0 deletions website/js/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
scope: {
list: '=ptorFunctionList'
},
controller: function($scope, $sce) {
$scope.trust = function(html) {
return trustHTML($sce, html);
};
},
templateUrl: 'partials/ptor-function-list.html'
};
});
Expand Down
25 changes: 25 additions & 0 deletions website/js/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Use the $sce service to trust the html rendered in the view.
* Also parse links
*
* @param $sce The $sce service from Angular
* @param {String} html The HTML to trust
* @return {*} An object that can be passed to $sce.getTrustedHtml(value) to
* obtain the original value
*/
function trustHTML($sce, html) {
if (!html) {
return;
}

// Does it come with a type? Types come escaped as [description](theType).
var match;
while (match = html.match(/(\[(.*?)\]\((.*?)\))/)) {
var link = '<a href="' +
(match[3].match(/^https?:\/\//) ? '' : '#/api?view=') + match[3] +
'">' + match[2] + '</a>';
html = html.replace(match[1], link);
}

return $sce.trustAsHtml(html);
}
2 changes: 1 addition & 1 deletion website/partials/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ <h4>Returns</h4>
<tbody>
<tr>
<td ng-bind-html="trust(currentItem.returnString)"></td>
<td>{{currentItem.returns.description}}</td>
<td ng-bind-html="trust(currentItem.returns.description)"></td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion website/partials/ptor-function-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ng-bind="item.displayName">
</a>
</td>
<td ng-bind="item.shortDescription"></td>
<td ng-bind-html="trust(item.shortDescription)"></td>
</tr>
</tbody>
</table>
Expand Down
20 changes: 18 additions & 2 deletions website/test/unit/api-controller-spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';

describe('ApiCtrl', function() {
var $controller_, $httpBackend, $location, $rootScope_, ctrl, scope;
var $controller_, $httpBackend, $location, $sce, $rootScope_, ctrl, scope;

beforeEach(module('protractorApp'));

beforeEach(inject(
function($controller, $rootScope, _$httpBackend_, _$location_) {
function($controller, $rootScope, _$httpBackend_, _$location_, _$sce_) {
$controller_ = $controller;
$httpBackend = _$httpBackend_;
$location = _$location_;
$sce = _$sce_;
$rootScope_ = $rootScope;
}));

Expand Down Expand Up @@ -205,4 +206,19 @@ describe('ApiCtrl', function() {
expect(item.base.items.length).toBe(1);
});
});

describe('trustHTML', function() {
it('should reject falsy html', function() {
expect(trustHTML($sce, false)).toBe(undefined);
});

it('should run truster', function() {
expect($sce.getTrustedHtml(trustHTML($sce, 'html'))).toBe('html');
});

it('should expand links', function() {
expect($sce.getTrustedHtml(trustHTML($sce, '[description](type)'))).toBe(
'<a href="#/api?view=type">description</a>');
});
});
});