|
94 | 94 |
|
95 | 95 | function resolve(elm, attr) { |
96 | 96 | // in order to handle <base> and attributes which aren't properly |
97 | | - // reflected as URLs, insert a temporary <img> element just before |
98 | | - // elm and resolve using its src attribute. the <img> element must |
| 97 | + // reflected as URLs, insert a temporary <a> element just before |
| 98 | + // elm and resolve using its href property. the <a> element must |
99 | 99 | // be created using the parent document due IE security policy. |
100 | 100 | var url = elm.getAttribute(attr); |
101 | 101 | if (!url) |
102 | 102 | return ''; |
103 | 103 | var a = ancestor(elm); |
104 | 104 | var p = elm.parentNode; |
105 | | - var img = (a.createElement ? a : document).createElement('img'); |
| 105 | + var div = (a.createElement ? a : document).createElement('div'); |
106 | 106 | try { |
107 | | - img.setAttribute('src', url); |
| 107 | + // Setting the href attribute/property on an <a> element |
| 108 | + // directly doesn't trigger resolution against the base URL in |
| 109 | + // IE, but creating an <a> element with innerHTML does. We have |
| 110 | + // to do some acrobatics to avoid having to HTML-escape the URL. |
| 111 | + // See http://stackoverflow.com/a/22918332/250798 |
| 112 | + div.innerHTML = '<a></a>'; |
| 113 | + div.firstChild.href = url; |
| 114 | + div.innerHTML = div.innerHTML; |
108 | 115 | if (p) |
109 | | - p.insertBefore(img, elm); |
110 | | - url = img.src; |
| 116 | + p.insertBefore(div, elm); |
| 117 | + url = div.firstChild.href; |
111 | 118 | if (p) |
112 | | - p.removeChild(img); |
| 119 | + p.removeChild(div); |
113 | 120 | } catch (e) { |
114 | 121 | // IE>6 throws "TypeError: Access is denied." for mailto: |
115 | 122 | // URLs. This is annoying, but harmless to ignore. |
|
0 commit comments