Skip to content

Commit

Permalink
Run resolveUrl for protocol-relative urls (#5530)
Browse files Browse the repository at this point in the history
* Run resolveUrl for protocol-relative urls

This part of ABS_URL is trying to match on absolute paths, like `/images/logo.png` not protocol-relative urls like `//example.com/images/logo.png`

Added a test and confirmed that it fails without this change.

Also cleaned up a some over-aggressive modulizer escaping in resolveurl-elements.js

Upstreaming cl/245302268
  • Loading branch information
rictic committed Apr 26, 2019
1 parent 03b2c66 commit 733cf68
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/utils/resolve-url.js
Expand Up @@ -10,7 +10,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
import './boot.js';

let CSS_URL_RX = /(url\()([^)]*)(\))/g;
let ABS_URL = /(^\/)|(^#)|(^[\w-\d]*:)/;
let ABS_URL = /(^\/[^\/])|(^#)|(^[\w-\d]*:)/;
let workingURL;
let resolveDoc;
/**
Expand Down
8 changes: 8 additions & 0 deletions test/unit/resolveurl.html
Expand Up @@ -154,6 +154,14 @@
assert.equal(actual, expected);
});

test('resolveUrl when called with a protocol-relative url', function () {
const el = document.querySelector('x-resolve');
const expected = `https://example.com/foo`;
const actual =
el.resolveUrl('//example.com/foo', 'https://example.org/bar');
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
Expand Down
18 changes: 9 additions & 9 deletions test/unit/sub/resolveurl-elements.js
Expand Up @@ -31,16 +31,16 @@ class PR extends PolymerElement {
}
</style>
<div id="div" class="logo" style\$="background-image: url('[[importPath]]foo.z');"></div>
<img id="img" src\$="[[importPath]]foo.z">
<a id="a" href\$="[[importPath]]foo.z">Foo</a>
<zonk id="import" url\$="[[importPath]]foo.z"></zonk>
<zonk id="resolveUrl" url\$="[[resolveUrl('foo.z')]]"></zonk>
<zonk id="resolveUrlHash" url\$="[[resolveUrl('#foo')]]"></zonk>
<zonk id="resolveUrlAbs" url\$="[[resolveUrl('/foo')]]"></zonk>
<zonk id="root" url\$="[[rootPath]]foo.z"></zonk>
<a id="rel" href\$="[[importPath]]../foo.z?123">Foo</a>
<img id="img" src$="[[importPath]]foo.z">
<a id="a" href$="[[importPath]]foo.z">Foo</a>
<zonk id="import" url$="[[importPath]]foo.z"></zonk>
<zonk id="resolveUrl" url$="[[resolveUrl('foo.z')]]"></zonk>
<zonk id="resolveUrlHash" url$="[[resolveUrl('#foo')]]"></zonk>
<zonk id="resolveUrlAbs" url$="[[resolveUrl('/foo')]]"></zonk>
<zonk id="root" url$="[[rootPath]]foo.z"></zonk>
<a id="rel" href$="[[importPath]]../foo.z?123">Foo</a>
<a id="action" action="foo.z">Foo</a>
<form id="formAction" action\$="[[importPath]]foo.z"></form>
<form id="formAction" action$="[[importPath]]foo.z"></form>
<a id="hash" href="#foo.z">Foo</a>
<a id="absolute" href="/foo.z">Foo</a>
<a id="protocol" href="data:foo.z">Foo</a>
Expand Down

0 comments on commit 733cf68

Please sign in to comment.