mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
LibWeb: Implement CSSStyleSheet.addRule()
This is a legacy method that has been superseded by `insertRule()`, although it is supported by all modern browser engines.
This commit is contained in:
parent
87b52a1816
commit
3ea318ca8b
5 changed files with 68 additions and 1 deletions
9
Tests/LibWeb/Text/expected/css/CSSStyleSheet-addRule.txt
Normal file
9
Tests/LibWeb/Text/expected/css/CSSStyleSheet-addRule.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
addValue() return value: -1
|
||||
Rule count after calling addRule() with no arguments: 1
|
||||
Rule text: undefined { }
|
||||
Rule count after calling addRule with no index: 2
|
||||
Second rule text: .test { font-size: 14px; }
|
||||
Rule count after calling addRule with index 0: 3
|
||||
Rule text: .test { padding: 100px; }
|
||||
Exception thrown when given a negative index: IndexSizeError
|
||||
Exception thrown when index out of range: IndexSizeError
|
28
Tests/LibWeb/Text/input/css/CSSStyleSheet-addRule.html
Normal file
28
Tests/LibWeb/Text/input/css/CSSStyleSheet-addRule.html
Normal file
|
@ -0,0 +1,28 @@
|
|||
<!DOCTYPE html>
|
||||
<script src="../include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
const sheet = new CSSStyleSheet();
|
||||
println(`addValue() return value: ${sheet.addRule()}`);
|
||||
println(`Rule count after calling addRule() with no arguments: ${sheet.cssRules.length}`);
|
||||
println(`Rule text: ${sheet.cssRules[0].cssText}`);
|
||||
sheet.addRule(".test", "font-size: 14px");
|
||||
println(`Rule count after calling addRule with no index: ${sheet.cssRules.length}`);
|
||||
println(`Second rule text: ${sheet.cssRules[1].cssText}`);
|
||||
sheet.addRule(".test", "padding: 100px", 0);
|
||||
println(`Rule count after calling addRule with index 0: ${sheet.cssRules.length}`);
|
||||
println(`Rule text: ${sheet.cssRules[0].cssText}`);
|
||||
try {
|
||||
sheet.addRule(".test", "padding: 10px", -1);
|
||||
println("FAIL");
|
||||
} catch (e) {
|
||||
println(`Exception thrown when given a negative index: ${e.name}`);
|
||||
}
|
||||
try {
|
||||
sheet.addRule(".test", "padding: 10px", 4);
|
||||
println("FAIL");
|
||||
} catch (e) {
|
||||
println(`Exception thrown when index out of range: ${e.name}`);
|
||||
}
|
||||
});
|
||||
</script>
|
|
@ -257,6 +257,34 @@ WebIDL::ExceptionOr<void> CSSStyleSheet::replace_sync(StringView text)
|
|||
return {};
|
||||
}
|
||||
|
||||
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-addrule
|
||||
WebIDL::ExceptionOr<WebIDL::Long> CSSStyleSheet::add_rule(Optional<String> selector, Optional<String> style, Optional<WebIDL::UnsignedLong> index)
|
||||
{
|
||||
// 1. Let rule be an empty string.
|
||||
StringBuilder rule;
|
||||
|
||||
// 2. Append selector to rule.
|
||||
if (selector.has_value())
|
||||
rule.append(selector.release_value());
|
||||
|
||||
// 3. Append " { " to rule.
|
||||
rule.append('{');
|
||||
|
||||
// 4. If block is not empty, append block, followed by a space, to rule.
|
||||
if (style.has_value() && !style->is_empty())
|
||||
rule.appendff("{} ", style.release_value());
|
||||
|
||||
// 5. Append "}" to rule.
|
||||
rule.append('}');
|
||||
|
||||
// 6. Let index be optionalIndex if provided, or the number of CSS rules in the stylesheet otherwise.
|
||||
// 7. Call insertRule(), with rule and index as arguments.
|
||||
TRY(insert_rule(rule.string_view(), index.value_or(rules().length())));
|
||||
|
||||
// 8. Return -1.
|
||||
return -1;
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
|
||||
WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
|
||||
{
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <LibWeb/CSS/CSSRuleList.h>
|
||||
#include <LibWeb/CSS/CSSStyleRule.h>
|
||||
#include <LibWeb/CSS/StyleSheet.h>
|
||||
#include <LibWeb/WebIDL/Types.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
|
@ -49,6 +50,7 @@ public:
|
|||
CSSRuleList const* css_rules() const { return m_rules; }
|
||||
|
||||
WebIDL::ExceptionOr<unsigned> insert_rule(StringView rule, unsigned index);
|
||||
WebIDL::ExceptionOr<WebIDL::Long> add_rule(Optional<String> selector, Optional<String> style, Optional<WebIDL::UnsignedLong> index);
|
||||
WebIDL::ExceptionOr<void> remove_rule(unsigned index);
|
||||
WebIDL::ExceptionOr<void> delete_rule(unsigned index);
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ interface CSSStyleSheet : StyleSheet {
|
|||
|
||||
// https://drafts.csswg.org/cssom/#legacy-css-style-sheet-members
|
||||
[SameObject, ImplementedAs=css_rules] readonly attribute CSSRuleList rules;
|
||||
// FIXME: long addRule(optional DOMString selector = "undefined", optional DOMString style = "undefined", optional unsigned long index);
|
||||
long addRule(optional DOMString selector = "undefined", optional DOMString style = "undefined", optional unsigned long index);
|
||||
undefined removeRule(unsigned long index);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue