From 34b7307e843e93ca06e25fa1175055fad77b2b64 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sun, 18 Mar 2012 09:26:11 +0000 Subject: [PATCH] Fix computing of width and height in inline elements so as gquery behaves the same than jquery --- .../gwt/query/client/impl/DocumentStyleImpl.java | 11 +++++++++++ .../google/gwt/query/client/GQueryCoreTestGwt.java | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java index 3e6f58bb..ac836e8d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java @@ -115,7 +115,17 @@ public class DocumentStyleImpl { return JsUtils.camelize(name); } + // inline elements do not have width nor height unless we set it to inline-block + private void fixInlineElement(Element e) { + if (e.getClientHeight() == 0 && e.getClientWidth() == 0 && "inline".equals(curCSS(e, "display", true))) { + setStyleProperty(e, "display", "inline-block"); + setStyleProperty(e, "width", "auto"); + setStyleProperty(e, "height", "auto"); + } + } + public int getHeight(Element e) { + fixInlineElement(e); return (int) (e.getClientHeight() - num(curCSS(e, "paddingTop", true)) - num(curCSS(e, "paddingBottom", true))); } @@ -125,6 +135,7 @@ public class DocumentStyleImpl { } public int getWidth(Element e) { + fixInlineElement(e); return (int) (e.getClientWidth() - num(curCSS(e, "paddingLeft", true)) - num(curCSS(e, "paddingRight", true))); } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 370e3a1a..20eed559 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -1085,6 +1085,14 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals(142, g.outerWidth(true)); } + + public void testWidthHeightInlineElement() { + $(e).html( + "Content 1"); + GQuery g = $("span", e); + assertTrue(g.width() > 0); + assertTrue(g.height() > 0); + } public void testWrapMethod() { String content = "

Test Paragraph.

"; -- 2.39.5