-
Notifications
You must be signed in to change notification settings - Fork 161
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
fix Images with style=background-image: url("img.png"); aren't loaded
(close #1212)
#1293
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,6 +193,22 @@ class NativeMethods { | |
if (textAreaValueDescriptor && typeof textAreaValueDescriptor.set === 'function') | ||
this.textAreaValueSetter = textAreaValueDescriptor.set; | ||
|
||
// stylesheets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (win.CSSStyleDeclaration) { | ||
this.CSSStyleDeclarationGetPropertyValue = win.CSSStyleDeclaration.prototype.getPropertyValue; | ||
this.CSSStyleDeclarationSetProperty = win.CSSStyleDeclaration.prototype.setProperty; | ||
} | ||
|
||
if (win.MSStyleCSSProperties) { | ||
this.MSStyleCSSPropertiesGetPropertyValue = win.MSStyleCSSProperties.prototype.getPropertyValue; | ||
this.MSStyleCSSPropertiesSetProperty = win.MSStyleCSSProperties.prototype.setProperty; | ||
} | ||
|
||
if (win.CSS2Property) { | ||
this.CSS2PropertyGetPropertyValue = win.CSS2Property.prototype.getPropertyValue; | ||
this.CSS2PropertySetProperty = win.CSS2Property.prototype.setProperty; | ||
} | ||
|
||
this.refreshClasses(win); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import { processScript } from '../../../processing/script'; | |
import styleProcessor from '../../../processing/style'; | ||
import * as destLocation from '../../utils/destination-location'; | ||
import { processHtml } from '../../utils/html'; | ||
import { isSubDomain, parseUrl, getProxyUrl, convertToProxyUrl, stringifyResourceType } from '../../utils/url'; | ||
import { isSubDomain, parseUrl, getProxyUrl, parseProxyUrl, convertToProxyUrl, stringifyResourceType } from '../../utils/url'; | ||
import { isFirefox, isIE9, isIE } from '../../utils/browser'; | ||
import { isCrossDomainWindows, isImgElement, isBlob } from '../../utils/dom'; | ||
import { isPrimitiveType } from '../../utils/types'; | ||
|
@@ -68,6 +68,29 @@ export default class WindowSandbox extends SandboxBase { | |
return String(reason); | ||
} | ||
|
||
static _wrapCSSGetPropertyValueIfNecessary (constructor, nativeGetPropertyValue) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename function parameter to the |
||
if (nativeGetPropertyValue) { | ||
constructor.prototype.getPropertyValue = function (...args) { | ||
const value = nativeGetPropertyValue.apply(this, args); | ||
|
||
return styleProcessor.cleanUp(value, parseProxyUrl); | ||
}; | ||
} | ||
} | ||
|
||
static _wrapCSSSetPropertyIfNecessary (constructor, nativeSetProperty) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (nativeSetProperty) { | ||
constructor.prototype.setProperty = function (...args) { | ||
const value = args[1]; | ||
|
||
if (typeof value === 'string') | ||
args[1] = styleProcessor.process(value, getProxyUrl); | ||
|
||
return nativeSetProperty.apply(this, args); | ||
}; | ||
} | ||
} | ||
|
||
handleEvent (event) { | ||
if (event.type === 'unhandledrejection' && !event.defaultPrevented) { | ||
const reason = WindowSandbox._formatUnhandledRejectionReason(event.reason); | ||
|
@@ -404,5 +427,19 @@ export default class WindowSandbox extends SandboxBase { | |
// NOTE: stab for ie9 and ie10 (GH-801) | ||
if (window.XDomainRequest) | ||
window.XDomainRequest = window.XMLHttpRequest; | ||
|
||
WindowSandbox._wrapCSSGetPropertyValueIfNecessary(window.CSSStyleDeclaration, | ||
nativeMethods.CSSStyleDeclarationGetPropertyValue); | ||
WindowSandbox._wrapCSSGetPropertyValueIfNecessary(window.MSStyleCSSProperties, | ||
nativeMethods.MSStyleCSSPropertiesGetPropertyValue); | ||
WindowSandbox._wrapCSSGetPropertyValueIfNecessary(window.CSS2Property, | ||
nativeMethods.CSS2PropertyGetPropertyValue); | ||
|
||
WindowSandbox._wrapCSSSetPropertyIfNecessary(window.CSSStyleDeclaration, | ||
nativeMethods.CSSStyleDeclarationSetProperty); | ||
WindowSandbox._wrapCSSSetPropertyIfNecessary(window.MSStyleCSSProperties, | ||
nativeMethods.MSStyleCSSPropertiesSetProperty); | ||
WindowSandbox._wrapCSSSetPropertyIfNecessary(window.CSS2Property, | ||
nativeMethods.CSS2PropertySetProperty); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,47 +198,33 @@ test('CSSStyleSheet.href', function () { | |
urlUtils.parseProxyUrl = storedGetProxyUrl; | ||
}); | ||
|
||
if (browserUtils.isWebKit) { | ||
test('url in stylesheet properties', function () { | ||
var el = document.createElement('div'); | ||
var url = 'http://some.domain.com/image.png'; | ||
var proxyUrl = urlUtils.getProxyUrl(url); | ||
var quote = (function () { | ||
var div = document.createElement('div'); | ||
|
||
div.style.backgroundImage = 'url(http://example.com/img.jpg)'; | ||
|
||
return div.style.backgroundImage.match(/url\((.*)http:\/\/example.com\/img.jpg/)[1]; | ||
})(); | ||
var getExpected = function (value) { | ||
return 'url(' + quote + value + quote + ')'; | ||
}; | ||
|
||
eval(processScript('el.style.backgroundImage="url(' + url + ')"')); | ||
strictEqual(getProperty(el.style, 'backgroundImage'), getExpected(url), 'backgroundImage'); | ||
strictEqual(el.style.backgroundImage, getExpected(proxyUrl), 'backgroundImage'); | ||
|
||
eval(processScript('el.style.background="url(' + url + ')"')); | ||
strictEqual(getProperty(el.style, 'background'), getExpected(url), 'background'); | ||
strictEqual(el.style.background, getExpected(proxyUrl), 'background'); | ||
|
||
eval(processScript('el.style.listStyle="url(' + url + ')"')); | ||
strictEqual(getProperty(el.style, 'listStyle'), getExpected(url), 'listStyle'); | ||
strictEqual(el.style.listStyle, getExpected(proxyUrl), 'listStyle'); | ||
|
||
eval(processScript('el.style.listStyleImage="url(' + url + ')"')); | ||
strictEqual(getProperty(el.style, 'listStyleImage'), getExpected(url), 'listStyleImage'); | ||
strictEqual(el.style.listStyleImage, getExpected(proxyUrl), 'listStyleImage'); | ||
|
||
eval(processScript('el.style.cssText="background-image: url(' + url + ')"')); | ||
strictEqual(getProperty(el.style, 'cssText'), 'background-image: ' + getExpected(url) + ';', 'cssText'); | ||
strictEqual(el.style.cssText, 'background-image: ' + getExpected(proxyUrl) + ';', 'cssText'); | ||
|
||
eval(processScript('el.style.cursor="url(' + url + '), auto"')); | ||
strictEqual(getProperty(el.style, 'cursor'), getExpected(url) + ', auto', 'cursor'); | ||
strictEqual(el.style.cursor, getExpected(proxyUrl) + ', auto', 'cursor'); | ||
test('url in stylesheet properties', function () { | ||
var el = document.createElement('div'); | ||
var url = 'http://some.domain.com/image.png'; | ||
var proxyUrl = urlUtils.getProxyUrl(url); | ||
var cssProperties = ['background', 'backgroundImage', 'background-image', 'borderImage', 'border-image', | ||
'borderImageSource', 'border-image-source', 'listStyle', 'list-style', 'listStyleImage', | ||
'list-style-image', 'cssText', 'cursor']; | ||
|
||
cssProperties.forEach(function (prop) { | ||
var value = 'url(' + url + ')'; | ||
|
||
// NOTE: We cannot get url through 'borderImage' or 'border-image' properties | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var propForChecking = prop === 'borderImage' || prop === 'border-image' ? 'borderImageSource' : prop; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if (prop === 'cssText') | ||
value = 'background:' + value; | ||
|
||
el.style[prop] = value; | ||
|
||
var controlValue = el.style[propForChecking]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var proxiedValue = controlValue && controlValue.replace(url, proxyUrl); | ||
|
||
eval(processScript('el.style["' + prop + '"]="' + value + '"')); | ||
strictEqual(getProperty(el.style, propForChecking), controlValue, prop); | ||
strictEqual(el.style[propForChecking], proxiedValue, prop); | ||
}); | ||
} | ||
}); | ||
|
||
test('get style body', function () { | ||
var style = document.createElement('style'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -375,3 +375,15 @@ test('the constructor field of a function should return a wrapped Function objec | |
Function.prototype.toString = nativeToString; | ||
/*eslint-enable no-extend-native*/ | ||
}); | ||
|
||
test('getPropertyValue and setProperty methods of css object should be overridden (GH-1212)', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also check It should returns non-proxied value. |
||
var div = document.createElement('div'); | ||
var url = 'http://some.domain.com/image.png'; | ||
var proxyUrl = urlUtils.getProxyUrl(url); | ||
|
||
div.style.setProperty('background', 'url(' + url + ')', ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test for non-string url value case |
||
|
||
ok(div.style.background.indexOf(proxyUrl) !== -1); | ||
ok(div.style.getPropertyValue('background').indexOf(proxyUrl) === -1); | ||
ok(div.style.getPropertyValue('background').indexOf(url) !== -1); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_createForStyleProperty (property)