From a85ee6280577fa11cecab2272373f3667321784c Mon Sep 17 00:00:00 2001 From: Harald Pehl Date: Tue, 24 Oct 2023 10:13:18 +0200 Subject: [PATCH] Add overloaded style() methods to HasHTMLElement --- CHANGELOG.md | 11 +++++++ .../org/jboss/elemento/HasHTMLElement.java | 33 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c8a8a310..8878daee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Added + +- Add overloaded `style()` methods to `HasHTMLElement`: + + - `style(String style)`: Appends the given style to the existing styles of an element. + - `style(String style, boolean append)`: Appends or sets the given style of an element. + - `style(String property, int value)`: Sets the given style property on an element. + - `style(String property, String value)`: Sets the given style property on an element. + - `style(String property, int value, boolean important)`: Sets the given style property on an element. + - `style(String property, String value, boolean important)`: Sets the given style property on an element. + ## [1.2.4] - 2023-10-18 ### Added diff --git a/core/src/main/java/org/jboss/elemento/HasHTMLElement.java b/core/src/main/java/org/jboss/elemento/HasHTMLElement.java index 54850da6c..046034907 100644 --- a/core/src/main/java/org/jboss/elemento/HasHTMLElement.java +++ b/core/src/main/java/org/jboss/elemento/HasHTMLElement.java @@ -31,7 +31,38 @@ default B title(String title) { /** Appends the given style to the existing styles of this element. */ default B style(String style) { - element().style.cssText += style; + return style(style, true); + } + + /** Appends or sets the given style to the styles of this element. */ + default B style(String style, boolean append) { + if (append) { + element().style.cssText += style; + } else { + element().style.cssText = style; + } + return that(); + } + + /** Sets the given style on this element. */ + default B style(String property, int value) { + return style(property, String.valueOf(value), false); + } + + /** Sets the given style on this element. */ + default B style(String property, String value) { + return style(property, value, false); + } + + /** Sets the given style on this element. */ + default B style(String property, int value, boolean important) { + return style(property, String.valueOf(value), important); + } + + /** Sets the given style on this element. */ + default B style(String property, String value, boolean important) { + String priority = important ? "important" : ""; + element().style.setProperty(property, value, property); return that(); }