Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 607 - Format http(s) links and emails as <a> in query results #4257

Merged
merged 2 commits into from
Sep 21, 2018
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
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 {{
* expression: (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
64 changes: 64 additions & 0 deletions src/misc/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,47 @@ 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.
* @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 +468,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;