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

Commit 9105368

Browse files
committed
chore(website): Fix parsing of summaries in function list and return descriptions
Closing #2486 and #2672
1 parent f0e2195 commit 9105368

File tree

6 files changed

+70
-26
lines changed

6 files changed

+70
-26
lines changed

Diff for: website/js/api-controller.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,8 @@
6262
$anchorScroll();
6363
};
6464

65-
/**
66-
* Use the $sce service to trust the html rendered in the view.
67-
* @param html
68-
*/
6965
$scope.trust = function(html) {
70-
if (!html) {
71-
return;
72-
}
73-
74-
// Does it come with a type? Types come escaped as [description](theType).
75-
var match;
76-
while (match = html.match(/(\[(.*?)\]\((.*?)\))/)) {
77-
var link = '<a href="' +
78-
(match[3].match(/^https?:\/\//) ? '' : '#/api?view=') + match[3] +
79-
'">' + match[2] + '</a>';
80-
html = html.replace(match[1], link);
81-
}
82-
83-
return $sce.trustAsHtml(html);
66+
return trustHTML($sce, html);
8467
};
8568
};
8669

@@ -153,8 +136,25 @@
153136

154137
// Add short description.
155138
if (item.description) {
156-
item.shortDescription =
157-
item.description.substring(0., item.description.indexOf('.') + 1);
139+
// Find the correct portion of the description
140+
141+
// The following parsing is OK most of the time
142+
var sentenceEnd = item.description.search(/\.\s|\.$/) + 1 || Infinity;
143+
var paragraphEnd = item.description.indexOf('</p>') + 4;
144+
if (paragraphEnd == 3) {
145+
paragraphEnd = Infinity
146+
}
147+
var shortDescription = item.description.substring(0, Math.min(
148+
item.description.length, sentenceEnd, paragraphEnd)).trim();
149+
150+
// Remove <p> tags
151+
if (shortDescription.substr(0,3) == '<p>') {
152+
shortDescription = shortDescription.substr(3);
153+
if (shortDescription.substr(-4) == '</p>') {
154+
shortDescription = shortDescription.substr(0, shortDescription.length - 4);
155+
}
156+
}
157+
item.shortDescription = shortDescription;
158158
}
159159
});
160160

@@ -201,7 +201,6 @@
201201

202202
var addItemToList = function(item, depth) {
203203
if (item.inList) {
204-
console.log(item.name);
205204
return;
206205
}
207206
item.treeClasses = 'depth-' + depth;
@@ -217,7 +216,6 @@
217216
});
218217
}
219218
if (item.extends) {
220-
console.log(item.base.name);
221219
var parent = self.itemsByName[item.base.name];
222220
if (parent != null) {
223221
addItemToList(parent, depth + 1);

Diff for: website/js/directives.js

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
scope: {
5757
list: '=ptorFunctionList'
5858
},
59+
controller: function($scope, $sce) {
60+
$scope.trust = function(html) {
61+
return trustHTML($sce, html);
62+
};
63+
},
5964
templateUrl: 'partials/ptor-function-list.html'
6065
};
6166
});

Diff for: website/js/shared.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Use the $sce service to trust the html rendered in the view.
3+
* Also parse links
4+
*
5+
* @param $sce The $sce service from Angular
6+
* @param {String} html The HTML to trust
7+
* @return {*} An object that can be passed to $sce.getTrustedHtml(value) to
8+
* obtain the original value
9+
*/
10+
function trustHTML($sce, html) {
11+
if (!html) {
12+
return;
13+
}
14+
15+
// Does it come with a type? Types come escaped as [description](theType).
16+
var match;
17+
while (match = html.match(/(\[(.*?)\]\((.*?)\))/)) {
18+
var link = '<a href="' +
19+
(match[3].match(/^https?:\/\//) ? '' : '#/api?view=') + match[3] +
20+
'">' + match[2] + '</a>';
21+
html = html.replace(match[1], link);
22+
}
23+
24+
return $sce.trustAsHtml(html);
25+
}

Diff for: website/partials/api.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ <h4>Returns</h4>
149149
<tbody>
150150
<tr>
151151
<td ng-bind-html="trust(currentItem.returnString)"></td>
152-
<td>{{currentItem.returns.description}}</td>
152+
<td ng-bind-html="trust(currentItem.returns.description)"></td>
153153
</tr>
154154
</tbody>
155155
</table>

Diff for: website/partials/ptor-function-list.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ng-bind="item.displayName">
1515
</a>
1616
</td>
17-
<td ng-bind="item.shortDescription"></td>
17+
<td ng-bind-html="trust(item.shortDescription)"></td>
1818
</tr>
1919
</tbody>
2020
</table>

Diff for: website/test/unit/api-controller-spec.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22

33
describe('ApiCtrl', function() {
4-
var $controller_, $httpBackend, $location, $rootScope_, ctrl, scope;
4+
var $controller_, $httpBackend, $location, $sce, $rootScope_, ctrl, scope;
55

66
beforeEach(module('protractorApp'));
77

88
beforeEach(inject(
9-
function($controller, $rootScope, _$httpBackend_, _$location_) {
9+
function($controller, $rootScope, _$httpBackend_, _$location_, _$sce_) {
1010
$controller_ = $controller;
1111
$httpBackend = _$httpBackend_;
1212
$location = _$location_;
13+
$sce = _$sce_;
1314
$rootScope_ = $rootScope;
1415
}));
1516

@@ -205,4 +206,19 @@ describe('ApiCtrl', function() {
205206
expect(item.base.items.length).toBe(1);
206207
});
207208
});
209+
210+
describe('trustHTML', function() {
211+
it('should reject falsy html', function() {
212+
expect(trustHTML($sce, false)).toBe(undefined);
213+
});
214+
215+
it('should run truster', function() {
216+
expect($sce.getTrustedHtml(trustHTML($sce, 'html'))).toBe('html');
217+
});
218+
219+
it('should expand links', function() {
220+
expect($sce.getTrustedHtml(trustHTML($sce, '[description](type)'))).toBe(
221+
'<a href="#/api?view=type">description</a>');
222+
});
223+
});
208224
});

0 commit comments

Comments
 (0)