GWT CSS 字符串解析器

发布于 2024-08-28 10:33:11 字数 1233 浏览 15 评论 0原文

gwt 是否有内置解析器用于将基于 css 的字符串转换为 java 结构?

IE 我有兴趣编写自己的小部件,例如高度/宽度:

public class MyWidget extends Composite{

  private double width;
  private Style.Unit widthUnit;
  private double height;
  private Style.Unit heightUnit;

  /**
  * @width - width of my widget in CSS dimensions (ie. "10px", "1em" ...)
  * @height - height of my widget in CSS dimensions (ie. "10px", "1em" ...)
  */
  public MyWidget(String width, String height){
  // Parse out strings and set dimensions / units
  }
}

我可以轻松地对此进行一些正则表达式,但我想我会看看是否有某种内置机制,我不想这样做重新发明...

另外,我当然考虑过在构造函数中只接受双宽度,Style.Unit widthUnit,但是 UiObject 的 setWidth 需要一个 CSS 字符串,所以我仍然需要进行一些字符串修改,尽管相反。理想情况下,我有 2 个构造函数来通过简单的机制来正确解析字符串/维度和单位来实现这两种可能性。

编辑:

忘记提及我想解析这些值的原因:

我正在尝试编写一个简单的滚动面板(想想 iphone 导航),因此当用户单击按钮时,会出现一个新的滚动面板“item”从右侧滚动。

在后端,这将是:
1) 获取新的滚动项
2)扩展滚动包装器以伴随新项目(这是我需要实际宽度的地方)
2)附加到项目滚动包装
3)动画滚动包装器(通过向左滑动 1 个项目的宽度)以将新添加的项目公开到面板包装器的可见部分

我需要原始宽度的原因是因为我想按添加的项目的宽度扩展滚动器面板到它。如果我只是获取字符串,我不能准确地说 setWidth( currentWidth + itemWidth)

基本上我试图模仿 JQuery 工具滚动

Does gwt have a built in parser for converting css based strings to java constructs?

IE I'm interested in writing my own widget that takes for instance height/width:

public class MyWidget extends Composite{

  private double width;
  private Style.Unit widthUnit;
  private double height;
  private Style.Unit heightUnit;

  /**
  * @width - width of my widget in CSS dimensions (ie. "10px", "1em" ...)
  * @height - height of my widget in CSS dimensions (ie. "10px", "1em" ...)
  */
  public MyWidget(String width, String height){
  // Parse out strings and set dimensions / units
  }
}

I could easily do some regex on this but I thought I'd see if there was some sort of built in mechanism for this, I'd hate to re-invent...

Also, I of course thought about just accepting double width, Style.Unit widthUnit in the constructor, but UiObject's setWidth takes a CSS string, so I still have to do some String munging, albeit the other way around. Ideally I'd have 2 constructors to achieve both possibilities with a simple mechanism to parse the Strings / dimensions and Units properly.

EDIT:

Forgot to mention the REASON that i'd like to parse out these values:

I'm trying to write a simple scroll panel (think iphone nav) so when a user clicks a button, a new 'item' scrolls in from the right.

On the backend this would be:
1) Fetch new scroll item
2) Extend scroll wrapper to accompany new item (this is where i need actual widths)
2) Append to item scroll wrapper
3) Animate scroll wrapper (by sliding left the width of 1 item) to expose newly added item to visible section of panel wrapper

The reason I need original widths is because I'd like to extend my scroller panel by the width of the item added to it. If I'm just getting strings, I can't exactly say setWidth( currentWidth + itemWidth)

Basically I'm trying to mimic JQuery Tools Scroller

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-09-04 10:33:11

GWT 目前使用 Fute 来解析 CSS,但是有一个关于迁移到 CSS Parser 的讨论 (http://cssparser.sourceforge。网/)。您可以直接使用这些解析器之一。

但是,对于您提到的用例 - 我认为您不需要那么重的东西。 GWT 实际上并不解析宽度和高度字符串,它只是将其传递给浏览器 DOM,然后浏览器对其进行适当的计算。你也应该这样做。

这意味着,您的 API 只需要一个字符串。调用者可以传递 ems 或 pxs 或百分比。您的 API 并不重要,因为它只会将其推送到浏览器以供理解。

GWT currently uses Flute to parse CSS, however there is a discussion to migrate to CSS Parser (http://cssparser.sourceforge.net/). You could directly use one of those parsers.

BUT, for the use case you mention - I don't think you need anything that heavy. GWT actually doesn't parse the width and height strings, it just passes it to browsers DOM and then the browser evaluates it appropriately. You should do the same.

Meaning, your API just takes a string. The caller can pass ems or pxs or percentages. Your API isn't concerned, because it will just push it to the browser to understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文