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

Adding width and height as parameters to image url #46917

Merged
merged 5 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,54 @@ describe('UrlFormat', function () {
.to.be('<span ng-non-bindable><audio controls preload="none" src="http://elastic.co"></span>');
});

it('outputs an <image> if type === "img"', function () {
const url = new UrlFormat({ type: 'img' });
describe('outputs an <image> if type === "img"', function () {
gospodarsky marked this conversation as resolved.
Show resolved Hide resolved
it('default', function () {
const url = new UrlFormat({ type: 'img' });

expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co"></span>');
expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co" ' +
'style="width:auto; height:auto; max-width:none; max-height:none;"></span>');
});

it('with correct width and height set', function () {
const url = new UrlFormat({ type: 'img', width: '12', height: '55' });

expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co" ' +
'style="width:auto; height:auto; max-width:12px; max-height:55px;"></span>');
});

it('with correct width and height set if no width specified', function () {
const url = new UrlFormat({ type: 'img', height: '55' });

expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co" ' +
'style="width:auto; height:auto; max-width:none; max-height:55px;"></span>');
});

it('with correct width and height set if no height specified', function () {
const url = new UrlFormat({ type: 'img', width: '22' });

expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co" ' +
'style="width:auto; height:auto; max-width:22px; max-height:none;"></span>');
});

it('only accepts valid numbers for width', function () {
const url = new UrlFormat({ type: 'img', width: 'not a number' });

expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co" ' +
'style="width:auto; height:auto; max-width:none; max-height:none;"></span>');
});

it('only accepts valid numbers for height', function () {
const url = new UrlFormat({ type: 'img', height: 'not a number' });

expect(url.convert('http://elastic.co', 'html'))
.to.be('<span ng-non-bindable><img src="http://elastic.co" alt="A dynamically-specified image located at http://elastic.co" ' +
'style="width:auto; height:auto; max-width:none; max-height:none;"></span>');
});
});

describe('url template', function () {
Expand Down
15 changes: 13 additions & 2 deletions src/legacy/core_plugins/kibana/common/field_formats/types/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function createUrlFormat(FieldFormat) {
return {
type: DEFAULT_URL_TYPE,
urlTemplate: null,
labelTemplate: null
labelTemplate: null,
width: null,
height: null,
};
}

Expand Down Expand Up @@ -91,6 +93,15 @@ export function createUrlFormat(FieldFormat) {
};
}

_generateImgHtml(url, imageLabel) {
const isValidWidth = !isNaN(parseInt(this.param('width')));
const isValidHeight = !isNaN(parseInt(this.param('height')));
const maxWidth = isValidWidth ? `${this.param('width')}px` : 'none';
const maxHeight = isValidHeight ? `${this.param('height')}px` : 'none';

return `<img src="${url}" alt="${imageLabel}" style="width:auto; height:auto; max-width:${maxWidth}; max-height:${maxHeight};">`;
}

static id = 'url';
static title = 'Url';
static fieldType = [
Expand Down Expand Up @@ -127,7 +138,7 @@ export function createUrlFormat(FieldFormat) {
? `A dynamically-specified image located at ${url}`
: label;

return `<img src="${url}" alt="${imageLabel}">`;
return this._generateImgHtml(url, imageLabel);
default:
const inWhitelist = whitelistUrlSchemes.some(scheme => url.indexOf(scheme) === 0);
if (!inWhitelist && !parsedUrl) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading