Skip to content

Commit

Permalink
Fix support for svg documents in the atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyba committed Sep 6, 2013
1 parent 085be5e commit 6b4ffab
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
2 changes: 1 addition & 1 deletion javascript/atoms/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ bot.action.prepareToInteractWith_ = function(element, opt_coords) {
// necessary, not scrolling at all if the element is already in view.
var doc = goog.dom.getOwnerDocument(element);
goog.style.scrollIntoContainerView(element,
goog.userAgent.WEBKIT ? doc.body : doc.documentElement);
goog.userAgent.WEBKIT && doc.body ? doc.body : doc.documentElement);

// NOTE: Ideally, we would check that any provided coordinates fall
// within the bounds of the element, but this has proven difficult, because:
Expand Down
7 changes: 5 additions & 2 deletions javascript/atoms/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,8 @@ bot.dom.getOverflowState = function(elem, opt_coord) {
// the overflow style of the body, and the body is really overflow:visible.
var overflowElem = e;
if (htmlOverflowStyle == 'visible') {
if (e == htmlElem) {
// Note: bodyElem will be null/undefined in SVG documents.
if (e == htmlElem && bodyElem) {
overflowElem = bodyElem;
} else if (e == bodyElem) {
return {x: 'visible', y: 'visible'};
Expand Down Expand Up @@ -869,6 +870,8 @@ bot.dom.getClientRect = function(elem) {
} else {
var nativeRect;
try {
// TODO: in IE and Firefox, getBoundingClientRect includes stroke width,
// but getBBox does not.
nativeRect = elem.getBoundingClientRect();
} catch (e) {
// On IE < 9, calling getBoundingClientRect on an orphan element raises
Expand All @@ -881,7 +884,7 @@ bot.dom.getClientRect = function(elem) {

// In IE, the element can additionally be offset by a border around the
// documentElement or body element that we have to subtract.
if (goog.userAgent.IE) {
if (goog.userAgent.IE && goog.dom.getOwnerDocument(elem).body) {
var doc = goog.dom.getOwnerDocument(elem);
rect.left -= doc.documentElement.clientLeft + doc.body.clientLeft;
rect.top -= doc.documentElement.clientTop + doc.body.clientTop;
Expand Down
30 changes: 30 additions & 0 deletions javascript/atoms/test/svg_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script type="text/javascript">
goog.require('bot.action');
goog.require('bot.dom');
goog.require('bot.inject');
goog.require('bot.locators');
goog.require('bot.test');
goog.require('goog.testing.jsunit');
Expand Down Expand Up @@ -58,6 +59,25 @@
assertSvgRectApprox(-10, -10, 10, 10, rect);
}

function testClickingOnElementInSvgDocument() {
var frame = window.frames[0];
var rect = bot.locators.findElement({id: 'rect'}, frame.document);
assertEquals('blue', rect.getAttribute('fill'));
bot.action.click(rect);
assertEquals('green', rect.getAttribute('fill'));
}

function testExecutingScriptInSvgDocument() {
var frame = window.frames[0];
var bgEl = bot.locators.findElement({id: 'bg'}, frame.document);

assertEquals('red', bgEl.getAttribute('fill'));
bot.inject.executeScript(
'document.getElementById("bg").setAttribute("fill", "yellow")',
[], false, frame);
assertEquals('yellow', bgEl.getAttribute('fill'));
}

function assertSvgRectApprox(left, top, width, height, rect) {
// The bounding box attributes of the SVG element won't be precisely
// obeyed by the browser.The rect should be at least offset by its x and y
Expand All @@ -80,5 +100,15 @@
<text id="text" fill="black" font-size="12" font-family="Arial" x="25" y="20">Apple</text>
<path id="path" d="M 100 15 L 200 15" stroke="red" stroke-width="10"/>
<rect id="transformed" transform="translate(-20, -20)" fill="green" stroke="none" width="10" height="10" x="10" y="10"/>
</svg>
<div>
<script>
// Don't attempt to load our svg document in browsers that do not support
// SVG (I'm looking at you, IE<9). Doing so will cause problems when
// running tests with WebDriver.
var src = shouldRunTests() ? "testdata/rect.svg" : "";
document.write('<iframe src="' + src + '"></' + 'iframe>');
</script>
</div>
</body>
</html>
5 changes: 5 additions & 0 deletions javascript/atoms/test/testdata/rect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions third_party/closure/goog/dom/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,13 @@ goog.dom.getDocumentScrollElement = function() {
* @private
*/
goog.dom.getDocumentScrollElement_ = function(doc) {
// Safari (2 and 3) needs body.scrollLeft in both quirks mode and strict mode.
return !goog.userAgent.WEBKIT && goog.dom.isCss1CompatMode_(doc) ?
doc.documentElement : doc.body;
// WebKit needs body.scrollLeft in both quirks mode and strict mode. We also
// default to the documentElement if the document does not have a body (e.g.
// a SVG document).
if (!goog.userAgent.WEBKIT && goog.dom.isCss1CompatMode_(doc)) {
return doc.documentElement;
}
return doc.body || doc.documentElement;
};


Expand Down
4 changes: 2 additions & 2 deletions third_party/closure/goog/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ goog.style.getBoundingClientRect_ = function(el) {

// Patch the result in IE only, so that this function can be inlined if
// compiled for non-IE.
if (goog.userAgent.IE) {
if (goog.userAgent.IE && el.ownerDocument.body) {

// In IE, most of the time, 2 extra pixels are added to the top and left
// due to the implicit 2-pixel inset border. In IE6/7 quirks mode and
Expand Down Expand Up @@ -1727,7 +1727,7 @@ goog.style.getIePixelBorder_ = function(element, prop) {
* @return {!goog.math.Box} The computed border widths.
*/
goog.style.getBorderBox = function(element) {
if (goog.userAgent.IE) {
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) {
var left = goog.style.getIePixelBorder_(element, 'borderLeft');
var right = goog.style.getIePixelBorder_(element, 'borderRight');
var top = goog.style.getIePixelBorder_(element, 'borderTop');
Expand Down

0 comments on commit 6b4ffab

Please sign in to comment.