JSoup 是否支持 getCompulatedStyle 或同等功能?
JSoup 在解析 HTML 文档时返回的文档树是否支持单个文档元素上的 getCompulatedStyle?
我想要做的是将 CSS 内联到 HTML 片段中,以便我可以将该片段插入到更大的 HTML 文档中,保留其所有格式,但不会干扰文档中的任何其他格式。
我所做的研究似乎表明,我可以通过迭代文档中的所有元素、对每个元素调用 getCompulatedStyle 并将结果分配为该元素的样式来实现此目的。
是的,我意识到,通过在各个元素上放置一堆冗余/不必要的样式信息,这很可能会使生成的 HTML 变得臃肿,但我愿意付出更大的 HTML 的代价,并且据我所知,嵌入像这样的内联样式是准确保留格式同时使 HTML 片段完全可移植的唯一方法。 (如果您有其他建议来实现该目的,我洗耳恭听。:-)
回到主题...如果我不能在 JSoup 中使用 getCompulatedStyle (或等效项),是否还有另一个 Java HTML+支持 getComputedStyle 或同等功能的 CSS 解析器?
谢谢。
Does the document tree returned by JSoup when it parses an HTML document support getComputedStyle on the individual document elements?
What I would like to do is inline the CSS in an HTML fragment so that I can insert the fragment into a larger HTML document, with all of its formatting preserved but without messing with any other formatting in the document.
The research I've done would seem to suggest that I can accomplish this by iterating through all of the elements in the document, calling getComputedStyle on each one, and assigning the result to be the style for the element.
Yes, I realize that this may very well bloat the resulting HTML by putting a bunch of redundant / unnecessary style information on the individual elements, but I'm willing to pay the price of larger HTML, and as far as I can tell, embedding the style inline like this is the only way to preserve the formatting exactly while also making the HTML fragments fully portable. (If you've got another suggestion for accomplishing that purpose, I'm all ears. :-)
Getting back on topic... If I can't use getComputedStyle (or the equivalent) with JSoup, is there another Java HTML+CSS parser that supports getComputedStyle or the equivalent?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是不可能的。 Jsoup 只是一个支持 CSS 选择器的 HTML 解析器,它不是一个 HTML 渲染器。
您可能想看看 Lobobrowser,它是一个基于 Java 的 HTML 渲染器,支持 JavaScript 等。 我不知道也不保证Lobo 支持
getCompulatedStyle()
。。没有想到其他工具。 HtmlUnit 很接近,因为它也可以访问/调用 JavaScript,但是 一些 Google 结果表明
getCompulatedStyle()
也不适用于 HtmlUnit。毕竟它实际上也不是真正的 HTML 渲染器。That's not possible. Jsoup is just a HTML parser with CSS selector support, it is not a HTML renderer.
You may want to take a look at Lobobrowser which is a Java based HTML renderer supporting JavaScript and like. I do not know nor guarantee that
getComputedStyle()
is supported by Lobo.No other tools comes to mind. HtmlUnit comes close as it can also access/invoke JavaScript, but some Google results suggests that
getComputedStyle()
doesn't work on HtmlUnit as well. It's after all actually also not a real HTML renderer as well.