Skip to content

Commit

Permalink
Fix 607 - Automatically format http(s) links and emails as anchor ele…
Browse files Browse the repository at this point in the history
…ments in query results
  • Loading branch information
adube committed Sep 20, 2018
1 parent 453c721 commit a1d2912
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contribs/gmf/src/query/windowComponent.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
></td>
<td
class="details-value"
ng-bind-html="value | ngeoTrustHtml"
ng-bind-html="value | ngeoTrustHtmlAuto"
></td>
</tr>
</table>
Expand Down
24 changes: 24 additions & 0 deletions options/ngeox.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,30 @@ ngeox.ScaleselectorOptions.prototype.dropup;
ngeox.SortableOptions;


/**
* @typedef {{
* regex: (string),
* template: (string)
* }}
*/
ngeox.StringToHtmlReplacement;


/**
* The regex expression that must match to do the replacement.
* @type {string}
*/
ngeox.StringToHtmlReplacement.prototype.expression;


/**
* The template to use to create a new value as replacement if the
* regex matches.
* @type {string}
*/
ngeox.StringToHtmlReplacement.prototype.template;


/**
* A WFS type. To be used with {@link ngeox.WfsPermalinkOptions}.
* @typedef {{
Expand Down
2 changes: 1 addition & 1 deletion src/grid/component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<tr ng-repeat="attributes in ctrl.configuration.data"
ng-class="['row-' + ctrl.configuration.getRowUid(attributes), ctrl.configuration.isRowSelected(attributes) ? 'ngeo-grid-active': '']"
ng-click="ctrl.clickRow(attributes, $event)" ng-mousedown="ctrl.preventTextSelection($event)">
<td ng-repeat="columnDefs in ctrl.configuration.columnDefs" ng-bind-html="attributes[columnDefs.name] | ngeoTrustHtml"></td>
<td ng-repeat="columnDefs in ctrl.configuration.columnDefs" ng-bind-html="attributes[columnDefs.name] | ngeoTrustHtmlAuto"></td>
</tr>
</tbody>
</table>
Expand Down
65 changes: 65 additions & 0 deletions src/misc/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,48 @@ exports.trustHtmlFilter = function($sce) {
exports.filter('ngeoTrustHtml', exports.trustHtmlFilter);


/**
* A filter to mark a value as trusted HTML, with the addition of
* automatically converting any string that matches the
* StringToHtmlReplacements list to HTML.
*
* Usage:
*
* <p ng-bind-html="ctrl.someValue | ngeoTrustHtmlAuto"></p>
*
* If you use it, you don't require the "ngSanitize".
* @return {function(?):string} The filter function.
* @ngInject
* @ngdoc filter
* @param {angular.$sce} $sce Angular sce service.
* @param {!Array.<!ngeox.StringToHtmlReplacement>}
* ngeoStringToHtmlReplacements List of replacements for string to html.
* html.
* @ngname ngeoTrustHtmlAuto
*/
exports.trustHtmlAutoFilter = function($sce, ngeoStringToHtmlReplacements) {
return function(input) {
if (input !== undefined && input !== null) {
if (typeof input === 'string') {
for (const replacement of ngeoStringToHtmlReplacements) {
if (input.match(replacement.expression)) {
input = replacement.template.replace(/\$1/g, input);
break;
}
}
return $sce.trustAsHtml(`${input}`);
} else {
return $sce.trustAsHtml(`${input}`);
}
} else {
return $sce.trustAsHtml('&nbsp;');
}
};
};

exports.filter('ngeoTrustHtmlAuto', exports.trustHtmlAutoFilter);


/**
* A filter used to format a time duration in seconds into a more
* readable form.
Expand Down Expand Up @@ -427,4 +469,27 @@ exports.Duration = function(gettextCatalog) {
exports.filter('ngeoDuration', exports.Duration);


/**
* @type {!Array.<!ngeox.StringToHtmlReplacement>}
* @ngname ngeoStringToHtmlReplacements
*/
exports.StringToHtmlReplacements = [
// Hyperlink
{
expression: /^(https?:\/\/.+)$/gm,
template: '<a target="_blank" href="$1">$1</a>'
},
// Mailto
{
expression: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/gmi,
template: '<a href="mailto:$1">$1</a>'
}
];

exports.constant(
'ngeoStringToHtmlReplacements',
exports.StringToHtmlReplacements
);


export default exports;

0 comments on commit a1d2912

Please sign in to comment.