/
resolveurl.html
181 lines (164 loc) · 8.01 KB
/
resolveurl.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<meta charset="UTF-8">
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<script>
Polymer = {
rootPath: 'earlyRootPath/'
};
</script>
<link rel="import" href="../../polymer.html">
<link id="elements" rel="import" href="sub/resolveurl-elements.html">
</head>
<body>
<x-resolve></x-resolve>
<dom-module id="x-late">
<template>
<style>
:host {
background: url('foo.png');
}
</style>
<img id="root" src$="[[rootPath]]foo.png">
<img id="import" src$="[[importPath]]foo.png">
</template>
</dom-module>
<dom-module id="x-resolve">
</dom-module>
<script>
HTMLImports.whenReady(function() {
class XResolve extends Polymer.Element {
static get is() { return 'x-resolve'; }
}
customElements.define(XResolve.is, XResolve);
});
</script>
<script>
suite('ResolveUrl', function() {
const testStylesAndAttributes = (elementName, folder) => () => {
var el = document.createElement(elementName);
document.body.appendChild(el);
var resolvedUrl = new RegExp(`${folder}/foo\\.z`);
var styleHashUrl = /url\('#bar'\)/;
var styleAbsUrl = /url\('\/zot'\)/;
var style = el.shadowRoot.querySelector('style') || document.querySelector(`style[scope=${elementName}]`);
assert.match(style.textContent, resolvedUrl, 'url not relative to main document');
assert.match(style.textContent, styleHashUrl, 'hash url incorrectly resolved');
assert.match(style.textContent, styleAbsUrl, 'absolute url incorrectly resolved');
assert.match(el.$.div.getAttribute('style'), resolvedUrl, 'style url not relative to main document');
assert.match(el.$.img.src, resolvedUrl, 'src url not relative to main document');
assert.match(el.$.a.href, resolvedUrl, 'href url not relative to main document');
assert.match(el.$.import.getAttribute('url'), resolvedUrl, 'url url not relative to main document');
assert.match(el.$.resolveUrl.getAttribute('url'), resolvedUrl, 'url url not relative to main document');
assert.equal(el.$.resolveUrlHash.getAttribute('url'), '#foo', 'url url not relative to main document');
assert.equal(el.$.resolveUrlAbs.getAttribute('url'), '/foo', 'url url not relative to main document');
assert.notMatch(el.$.root.getAttribute('url'), resolvedUrl, 'url url not relative to main document');
assert.notMatch(el.$.rel.href, resolvedUrl, 'relative href url not relative to main document');
assert.match(el.$.rel.href, /\?123$/, 'relative href does not preserve query string');
assert.equal(el.$.action.getAttribute('action'), 'foo.z', 'action attribute relativized for incorrect element type');
assert.match(el.$.formAction.action, resolvedUrl, 'action attribute relativized for incorrect element type');
assert.equal(el.$.hash.getAttribute('href'), '#foo.z', 'hash-only url should not be resolved');
assert.equal(el.$.absolute.getAttribute('href'), '/foo.z', 'absolute urls should not be resolved');
assert.equal(el.$.protocol.getAttribute('href'), 'data:foo.z', 'urls with other protocols should not be resolved');
document.body.removeChild(el);
};
test('Urls in styles and attributes', testStylesAndAttributes('p-r', 'sub'));
test('Urls in styles and attributes (importMeta)', testStylesAndAttributes('p-r-im', 'http://class.com/mymodule'));
test('Urls in styles and attributes (importMeta, hybrid)', testStylesAndAttributes('p-r-hybrid', 'http://hybrid.com/mymodule'));
test('url changes via setting importPath/rootPath on element instance', function() {
var el = document.createElement('p-r');
document.body.appendChild(el);
el.rootPath = 'instanceRoot/';
el.importPath = 'instanceImport/';
var matchRoot = /instanceRoot\//;
var matchImport = /instanceImport\//;
assert.match(el.$.div.getAttribute('style'), matchImport);
assert.match(el.$.import.getAttribute('url'), matchImport);
assert.match(el.$.root.getAttribute('url'), matchRoot);
document.body.removeChild(el);
});
test('rootPath set early', function() {
class XEarly extends Polymer.Element {
static get is() { return 'x-late';}
}
customElements.define('x-early', XEarly);
var el = document.createElement('x-early');
document.body.appendChild(el);
var matchRoot = /earlyRootPath\//i;
assert.match(el.$.root.getAttribute('src'), matchRoot);
document.body.removeChild(el);
});
test('url changes via setting importPath/rootPath when defining element', function() {
Polymer.rootPath = 'defineRoot/';
class XLate extends Polymer.Element {
static get is() { return 'x-late';}
static get importPath() { return 'defineImport/';}
}
customElements.define(XLate.is, XLate);
var el = document.createElement('x-late');
document.body.appendChild(el);
var matchRoot = /defineRoot\//i;
var matchImport = /defineImport\//i;
var style = el.shadowRoot.querySelector('style') || document.querySelector('style[scope=x-late]');
assert.match(style.textContent, matchImport);
assert.match(el.$.import.getAttribute('src'), matchImport);
assert.match(el.$.root.getAttribute('src'), matchRoot);
document.body.removeChild(el);
});
test('resolveUrl api', function() {
var el = document.createElement('p-r');
var expected = document.location.href.replace(/[?#].*$/, '');
var actual = el.resolveUrl('../resolveurl.html');
assert.equal(actual, expected);
});
test('resolveUrl api with base', function() {
var el = document.createElement('p-r');
var expected = 'http://example.com/resolveurl.html';
var actual = el.resolveUrl('resolveurl.html', 'http://example.com/');
assert.equal(actual, expected);
});
test('resolveUrl api with data base', function() {
var el = document.createElement('p-r');
var expected = 'resolveurl.html';
var actual = el.resolveUrl('resolveurl.html', 'data:text/html,some%20data');
assert.equal(actual, expected);
});
test('resolveUrl api, when defined in main doc', function() {
var el = document.querySelector('x-resolve');
var expected = document.location.href.replace(/[?#].*$/, '');
expected = expected.split('/');
expected.pop();
expected = expected.join('/') + '/foo/bar.png';
var actual = el.resolveUrl('foo/../foo/bar.png');
assert.equal(actual, expected);
});
test('resolveUrl api with assetpath', function() {
var el = document.createElement('p-r-ap');
// Manually calculate expected URL, to avoid dependence on
// URL object for this test for IE! Otherwise, would do this:
// var importPath = document.querySelector('#elements').href;
// var expected = new URL('../../assets/Beaker2.jpg', importPath);
var expected = document.location.href.replace(/[?#].*$/, '');
expected = expected.split('/');
expected.pop();
expected.pop();
expected = expected.join('/');
expected = expected + '/assets/Beaker2.jpg';
var actual = el.resolveUrl('Beaker2.jpg');
assert.equal(actual, expected);
});
});
</script>
</body>
</html>