Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0e666eb

Browse files
committed
test($compile): work around Chrome issue with reported size for <foreignObject>
Since Chrome 53-57+, the reported size of `<foreignObject>` elements and their descendants is affected by global display settings (e.g. font size) and browser settings (e.g. default zoom level). This could cause tests incorrectly failing due to such settings. In order to avoid false negatives, we now compare against the size of the equivalent, hand-written SVG instead of fixed widths/heights. Fixes #15333 Closes #15458
1 parent b748046 commit 0e666eb

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

test/ng/compileSpec.js

+54-14
Original file line numberDiff line numberDiff line change
@@ -440,30 +440,70 @@ describe('$compile', function() {
440440
}));
441441

442442
if (supportsForeignObject()) {
443+
// Supports: Chrome 53-57+
444+
// Since Chrome 53-57+, the reported size of `<foreignObject>` elements and their descendants
445+
// is affected by global display settings (e.g. font size) and browser settings (e.g. default
446+
// zoom level). In order to avoid false negatives, we compare against the size of the
447+
// equivalent, hand-written SVG instead of fixed widths/heights.
448+
var HAND_WRITTEN_SVG =
449+
'<svg width="400" height="400">' +
450+
'<foreignObject width="100" height="100">' +
451+
'<div style="position:absolute;width:20px;height:20px">test</div>' +
452+
'</foreignObject>' +
453+
'</svg>';
454+
443455
it('should handle foreignObject', inject(function() {
444-
element = jqLite('<div><svg-container>' +
445-
'<foreignObject width="100" height="100"><div class="test" style="position:absolute;width:20px;height:20px">test</div></foreignObject>' +
446-
'</svg-container></div>');
456+
element = jqLite(
457+
'<div>' +
458+
// By hand (for reference)
459+
HAND_WRITTEN_SVG +
460+
// By directive
461+
'<svg-container>' +
462+
'<foreignObject width="100" height="100">' +
463+
'<div style="position:absolute;width:20px;height:20px">test</div>' +
464+
'</foreignObject>' +
465+
'</svg-container>' +
466+
'</div>');
447467
$compile(element.contents())($rootScope);
448468
document.body.appendChild(element[0]);
449469

450-
var testElem = element.find('div');
451-
expect(isHTMLElement(testElem[0])).toBe(true);
452-
var bounds = testElem[0].getBoundingClientRect();
453-
expect(bounds.width === 20 && bounds.height === 20).toBe(true);
470+
var referenceElem = element.find('div')[0];
471+
var testElem = element.find('div')[1];
472+
var referenceBounds = referenceElem.getBoundingClientRect();
473+
var testBounds = testElem.getBoundingClientRect();
474+
475+
expect(isHTMLElement(testElem)).toBe(true);
476+
expect(referenceBounds.width).toBeGreaterThan(0);
477+
expect(referenceBounds.height).toBeGreaterThan(0);
478+
expect(testBounds.width).toBe(referenceBounds.width);
479+
expect(testBounds.height).toBe(referenceBounds.height);
454480
}));
455481

456482
it('should handle custom svg containers that transclude to foreignObject that transclude html', inject(function() {
457-
element = jqLite('<div><svg-container>' +
458-
'<my-foreign-object><div class="test" style="width:20px;height:20px">test</div></my-foreign-object>' +
459-
'</svg-container></div>');
483+
element = jqLite(
484+
'<div>' +
485+
// By hand (for reference)
486+
HAND_WRITTEN_SVG +
487+
// By directive
488+
'<svg-container>' +
489+
'<my-foreign-object>' +
490+
'<div style="width:20px;height:20px">test</div>' +
491+
'</my-foreign-object>' +
492+
'</svg-container>' +
493+
'</div>');
460494
$compile(element.contents())($rootScope);
461495
document.body.appendChild(element[0]);
462496

463-
var testElem = element.find('div');
464-
expect(isHTMLElement(testElem[0])).toBe(true);
465-
var bounds = testElem[0].getBoundingClientRect();
466-
expect(bounds.width === 20 && bounds.height === 20).toBe(true);
497+
var referenceElem = element.find('div')[0];
498+
var testElem = element.find('div')[1];
499+
var referenceBounds = referenceElem.getBoundingClientRect();
500+
var testBounds = testElem.getBoundingClientRect();
501+
502+
expect(isHTMLElement(testElem)).toBe(true);
503+
expect(referenceBounds.width).toBeGreaterThan(0);
504+
expect(referenceBounds.height).toBeGreaterThan(0);
505+
expect(testBounds.width).toBe(referenceBounds.width);
506+
expect(testBounds.height).toBe(referenceBounds.height);
467507
}));
468508

469509
// NOTE: This test may be redundant.

0 commit comments

Comments
 (0)