Skip to content

Commit

Permalink
fix Testcafe does not respect base tag (close DevExpress#1965) (Dev…
Browse files Browse the repository at this point in the history
  • Loading branch information
LavrovArtem authored Jun 15, 2021
1 parent 06afd8a commit a0b9341
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/client/sandbox/node/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ export default class ElementSandbox extends SandboxBase {
? urlUtils.getProxyUrl(value)
: urlUtils.resolveUrlAsDest(value);
}

if (!nativeMethods.nodeParentNodeGetter.call(el)) {
nativeMethods.objectDefineProperty(el, INTERNAL_PROPS.currentBaseUrl, {
value: urlResolver.getBaseUrl(document),
configurable: true,
writable: true
});
}
}
else if (loweredAttr === 'autocomplete') {
const storedAutocompleteAttr = DomProcessor.getStoredAttrName(attr);
Expand Down
9 changes: 8 additions & 1 deletion src/client/sandbox/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ChildWindowSandbox from '../child-window';
import DocumentTitleStorage from './document/title-storage';
import DocumentTitleStorageInitializer from './document/title-storage-initializer';
import { isIframeWindow } from '../../utils/dom';
import urlResolver from '../../utils/url-resolver';

const ATTRIBUTE_SELECTOR_REG_EX = /\[([\w-]+)(\^?=.+?)]/g;
const ATTRIBUTE_OPERATOR_WITH_HASH_VALUE = /^\W+\s*#/;
Expand Down Expand Up @@ -75,8 +76,10 @@ export default class NodeSandbox extends SandboxBase {

private _processElement (el: Element): void {
const processedContext = el[INTERNAL_PROPS.processedContext];
const isBaseUrlChanged = !!el[INTERNAL_PROPS.currentBaseUrl] &&
el[INTERNAL_PROPS.currentBaseUrl] !== urlResolver.getBaseUrl(this.document);

if (domUtils.isShadowUIElement(el) || processedContext === this.window)
if (!isBaseUrlChanged && (domUtils.isShadowUIElement(el) || processedContext === this.window))
return;

let urlAttrName = null;
Expand All @@ -97,9 +100,13 @@ export default class NodeSandbox extends SandboxBase {
}

// NOTE: We need to reprocess url attribute of element, if it's moved to different window (GH-564)
// or a base element is added dynamically (GH-1965)
if (urlAttrName)
el.setAttribute(urlAttrName, el.getAttribute(urlAttrName));

if (isBaseUrlChanged)
delete el[INTERNAL_PROPS.currentBaseUrl];

this.element.processElement(el);
}

Expand Down
3 changes: 2 additions & 1 deletion src/processing/dom/internal-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export default {
skipNextLoadEventForImage: 'hammerhead|image|skip-next-load-event-flag',
cachedImage: 'hammerhead|image|cached-image',
sandboxIsReattached: 'hammerhead|sandbox-is-reattached',
nativeStrRepresentation: 'hammerhead|native-string-representation'
nativeStrRepresentation: 'hammerhead|native-string-representation',
currentBaseUrl: 'hammerhead|current-base-url'
};
25 changes: 25 additions & 0 deletions test/client/data/node-sandbox/dynamic-base-tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="/hammerhead.js" class="script-hammerhead-shadow-ui"></script>
<script type="text/javascript">
var hammerhead = window['%hammerhead%'];

hammerhead.utils.destLocation.forceLocation('http://localhost/sessionId/https://example.com');
hammerhead.start({ sessionId: 'sessionId' });
</script>
</head>
<body>
<script type="text/javascript">
var base = document.createElement('base');
var script = document.createElement('script');

base.href = 'https://example.com/testpassed/';
script.src = './script-url.js';

document.head.appendChild(base);
document.body.appendChild(script);
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions test/client/fixtures/sandbox/node/element-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,10 @@ test('[registerElement] the lifecycle callbacks should not be called twice (GH-6

document.body.removeChild(testDiv);
});

test('dynamic base (GH-1965)', function () {
return createTestIframe({ src: getSameDomainPageUrl('../../../data/node-sandbox/dynamic-base-tag.html') })
.then(function (iframe) {
strictEqual(iframe.contentWindow.testpassed, '/sessionId!s/https://example.com/testpassed/script-url.js');
});
});

0 comments on commit a0b9341

Please sign in to comment.