blob: 52a7de01debfabfd3706d9450598d548bdd6692d [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-linked-text</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<script src="../../../scripts/util.js"></script>
<link rel="import" href="gr-linked-text.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-linked-text>
<div id="output"></div>
</gr-linked-text>
</template>
</test-fixture>
<script>
suite('gr-linked-text tests', function() {
var element;
var sandbox;
setup(function() {
element = fixture('basic');
sandbox = sinon.sandbox.create();
element.config = {
ph: {
match: '([Bb]ug|[Ii]ssue)\\s*#?(\\d+)',
link: 'https://code.google.com/p/gerrit/issues/detail?id=$2'
},
changeid: {
match: '(I[0-9a-f]{8,40})',
link: '#/q/$1'
},
changeid2: {
match: 'Change-Id: +(I[0-9a-f]{8,40})',
link: '#/q/$1'
},
googlesearch: {
match: 'google:(.+)',
link: 'https://bing.com/search?q=$1', // html should supercede link.
html: '<a href="https://google.com/search?q=$1">$1</a>',
},
hashedhtml: {
match: 'hash:(.+)',
html: '<a href="#/awesomesauce">$1</a>',
},
disabledconfig: {
match: 'foo:(.+)',
link: 'https://google.com/search?q=$1',
enabled: false,
},
};
});
teardown(function() {
sandbox.restore();
});
test('URL pattern was parsed and linked.', function() {
// Reguar inline link.
var url = 'https://code.google.com/p/gerrit/issues/detail?id=3650';
element.content = url;
var linkEl = element.$.output.childNodes[0];
assert.equal(linkEl.target, '_blank');
assert.equal(linkEl.rel, 'noopener');
assert.equal(linkEl.href, url);
assert.equal(linkEl.textContent, url);
});
test('Bug pattern was parsed and linked', function() {
// "Issue/Bug" pattern.
element.content = 'Issue 3650';
var linkEl = element.$.output.childNodes[0];
var url = 'https://code.google.com/p/gerrit/issues/detail?id=3650';
assert.equal(linkEl.target, '_blank');
assert.equal(linkEl.href, url);
assert.equal(linkEl.textContent, 'Issue 3650');
element.content = 'Bug 3650';
linkEl = element.$.output.childNodes[0];
assert.equal(linkEl.target, '_blank');
assert.equal(linkEl.rel, 'noopener');
assert.equal(linkEl.href, url);
assert.equal(linkEl.textContent, 'Bug 3650');
});
test('Change-Id pattern was parsed and linked', function() {
// "Change-Id:" pattern.
var changeID = 'I11d6a37f5e9b5df0486f6c922d8836dfa780e03e';
var prefix = 'Change-Id: ';
element.content = prefix + changeID;
var textNode = element.$.output.childNodes[0];
var linkEl = element.$.output.childNodes[1];
assert.equal(textNode.textContent, prefix);
var url = '/q/' + changeID;
assert.equal(linkEl.target, '_blank');
// Since url is a path, the host is added automatically.
assert.isTrue(linkEl.href.endsWith(url));
assert.equal(linkEl.textContent, changeID);
});
test('Multiple matches', function() {
element.content = 'Issue 3650\nIssue 3450';
var linkEl1 = element.$.output.childNodes[0];
var linkEl2 = element.$.output.childNodes[2];
assert.equal(linkEl1.target, '_blank');
assert.equal(linkEl1.href,
'https://code.google.com/p/gerrit/issues/detail?id=3650');
assert.equal(linkEl1.textContent, 'Issue 3650');
assert.equal(linkEl2.target, '_blank');
assert.equal(linkEl2.href,
'https://code.google.com/p/gerrit/issues/detail?id=3450');
assert.equal(linkEl2.textContent, 'Issue 3450');
});
test('Change-Id pattern parsed before bug pattern', function() {
// "Change-Id:" pattern.
var changeID = 'I11d6a37f5e9b5df0486f6c922d8836dfa780e03e';
var prefix = 'Change-Id: ';
// "Issue/Bug" pattern.
var bug = 'Issue 3650';
var changeUrl = '/q/' + changeID;
var bugUrl = 'https://code.google.com/p/gerrit/issues/detail?id=3650';
element.content = prefix + changeID + bug;
var textNode = element.$.output.childNodes[0];
var changeLinkEl = element.$.output.childNodes[1];
var bugLinkEl = element.$.output.childNodes[2];
assert.equal(textNode.textContent, prefix);
assert.equal(changeLinkEl.target, '_blank');
assert.isTrue(changeLinkEl.href.endsWith(changeUrl));
assert.equal(changeLinkEl.textContent, changeID);
assert.equal(bugLinkEl.target, '_blank');
assert.equal(bugLinkEl.href, bugUrl);
assert.equal(bugLinkEl.textContent, 'Issue 3650');
});
test('html field in link config', function() {
element.content = 'google:do a barrel roll';
var linkEl = element.$.output.childNodes[0];
assert.equal(linkEl.getAttribute('href'),
'https://google.com/search?q=do a barrel roll');
assert.equal(linkEl.textContent, 'do a barrel roll');
});
test('removing hash from links', function() {
element.content = 'hash:foo';
var linkEl = element.$.output.childNodes[0];
assert.isTrue(linkEl.href.endsWith('/awesomesauce'));
assert.equal(linkEl.textContent, 'foo');
});
test('disabled config', function() {
element.content = 'foo:baz';
assert.equal(element.$.output.innerHTML, 'foo:baz');
});
test('R=email labels link correctly', function() {
element.removeZeroWidthSpace = true;
element.content = 'R=\u200Btest@google.com';
assert.equal(element.$.output.textContent, 'R=test@google.com');
assert.equal(element.$.output.innerHTML.match(/(R=<a)/g).length, 1);
});
test('CC=email labels link correctly', function() {
element.removeZeroWidthSpace = true;
element.content = 'CC=\u200Btest@google.com';
assert.equal(element.$.output.textContent, 'CC=test@google.com');
assert.equal(element.$.output.innerHTML.match(/(CC=<a)/g).length, 1);
});
test('overlapping links', function() {
element.config = {
b1: {
match: '(B:\\s*)(\\d+)',
html: '$1<a href="ftp://foo/$2">$2</a>',
},
b2: {
match: '(B:\\s*\\d+\\s*,\\s*)(\\d+)',
html: '$1<a href="ftp://foo/$2">$2</a>',
},
};
element.content = '- B: 123, 45';
var links = Polymer.dom(element.root).querySelectorAll('a');
assert.equal(links.length, 2);
assert.equal(element.$$('span').textContent, '- B: 123, 45');
assert.equal(links[0].href, 'ftp://foo/123');
assert.equal(links[0].textContent, '123');
assert.equal(links[1].href, 'ftp://foo/45');
assert.equal(links[1].textContent, '45');
});
test('_contentOrConfigChanged called with config', function() {
var contentStub = sandbox.stub(element, '_contentChanged');
var contentConfigStub = sandbox.stub(element, '_contentOrConfigChanged');
element.content = 'some text';
assert.isTrue(contentStub.called);
assert.isTrue(contentConfigStub.called);
});
});
suite('gr-linked-text with null config', function() {
var element;
var sandbox;
setup(function() {
element = fixture('basic');
sandbox = sinon.sandbox.create();
});
teardown(function() {
sandbox.restore();
});
test('_contentOrConfigChanged not called without config', function() {
var contentStub = sandbox.stub(element, '_contentChanged');
var contentConfigStub = sandbox.stub(element, '_contentOrConfigChanged');
element.content = 'some text';
assert.isTrue(contentStub.called);
assert.isFalse(contentConfigStub.called);
});
});
</script>