mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 01:00:05 +00:00
LibWeb: Use correct error for invalid rule in nested insertRule
When we try to insert a disallowed (non-nested) statement into a CSSGroupingRule we should throw a `HierarchyRequestError` as it being disallowed is a "constraint specified by CSS". Previously we would rely on `Parser::is_valid_in_the_current_context` and throw a Syntax error. There are more constraints to be implemented.
This commit is contained in:
parent
ef7ba02842
commit
80ea865b19
Notes:
github-actions[bot]
2025-06-23 11:53:52 +00:00
Author: https://github.com/Calme1709
Commit: 80ea865b19
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5158
Reviewed-by: https://github.com/AtkinsSJ ✅
Reviewed-by: https://github.com/tcl3
4 changed files with 139 additions and 9 deletions
|
@ -63,18 +63,17 @@ WebIDL::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView,
|
||||||
// NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
|
// NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
|
||||||
// spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
|
// spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
|
||||||
// if that variant holds a CSSRule already.
|
// if that variant holds a CSSRule already.
|
||||||
Parser::ParsingParams parsing_params { realm() };
|
|
||||||
parsing_params.rule_context = rule_context();
|
|
||||||
|
|
||||||
CSSRule* new_rule = nullptr;
|
CSSRule* new_rule = nullptr;
|
||||||
if (rule.has<StringView>()) {
|
if (rule.has<StringView>()) {
|
||||||
new_rule = parse_css_rule(parsing_params, rule.get<StringView>());
|
new_rule = parse_css_rule(Parser::ParsingParams { realm() }, rule.get<StringView>());
|
||||||
} else {
|
} else {
|
||||||
new_rule = rule.get<CSSRule*>();
|
new_rule = rule.get<CSSRule*>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. If new rule is a syntax error, and nested is set, perform the following substeps:
|
// 4. If new rule is a syntax error, and nested is set, perform the following substeps:
|
||||||
if (!new_rule && nested == Nested::Yes) {
|
if (!new_rule && nested == Nested::Yes) {
|
||||||
|
Parser::ParsingParams parsing_params { realm() };
|
||||||
|
parsing_params.rule_context = rule_context();
|
||||||
// - Set declarations to the results of performing parse a CSS declaration block, on argument rule.
|
// - Set declarations to the results of performing parse a CSS declaration block, on argument rule.
|
||||||
auto declarations = parse_css_property_declaration_block(parsing_params, rule.get<StringView>());
|
auto declarations = parse_css_property_declaration_block(parsing_params, rule.get<StringView>());
|
||||||
|
|
||||||
|
@ -130,7 +129,8 @@ WebIDL::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rule_is_disallowed)
|
// FIXME: There are more constraints that we should check here - Parser::is_valid_in_the_current_context is probably a good reference for that.
|
||||||
|
if (rule_is_disallowed || (nested == Nested::Yes && first_is_one_of(new_rule->type(), CSSRule::Type::Import, CSSRule::Type::Namespace)))
|
||||||
return WebIDL::HierarchyRequestError::create(realm(), "Cannot insert rule at specified index."_string);
|
return WebIDL::HierarchyRequestError::create(realm(), "Cannot insert rule at specified index."_string);
|
||||||
|
|
||||||
// 7. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
|
// 7. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
|
||||||
|
|
|
@ -2,12 +2,12 @@ Harness status: OK
|
||||||
|
|
||||||
Found 34 tests
|
Found 34 tests
|
||||||
|
|
||||||
30 Pass
|
32 Pass
|
||||||
4 Fail
|
2 Fail
|
||||||
Pass @media is CSSGroupingRule
|
Pass @media is CSSGroupingRule
|
||||||
Pass @media rule type
|
Pass @media rule type
|
||||||
Pass Empty @media rule length
|
Pass Empty @media rule length
|
||||||
Fail insertRule of @import into @media
|
Pass insertRule of @import into @media
|
||||||
Pass insertRule into empty @media at bad index
|
Pass insertRule into empty @media at bad index
|
||||||
Pass insertRule into @media updates length
|
Pass insertRule into @media updates length
|
||||||
Pass insertRule of valid @media into @media
|
Pass insertRule of valid @media into @media
|
||||||
|
@ -24,7 +24,7 @@ Pass Return value of insertRule into @media
|
||||||
Pass @supports is CSSGroupingRule
|
Pass @supports is CSSGroupingRule
|
||||||
Pass @supports rule type
|
Pass @supports rule type
|
||||||
Pass Empty @supports rule length
|
Pass Empty @supports rule length
|
||||||
Fail insertRule of @import into @supports
|
Pass insertRule of @import into @supports
|
||||||
Pass insertRule into empty @supports at bad index
|
Pass insertRule into empty @supports at bad index
|
||||||
Pass insertRule into @supports updates length
|
Pass insertRule into @supports updates length
|
||||||
Pass insertRule of valid @media into @supports
|
Pass insertRule of valid @media into @supports
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
Harness status: OK
|
||||||
|
|
||||||
|
Found 7 tests
|
||||||
|
|
||||||
|
7 Pass
|
||||||
|
Pass index before first
|
||||||
|
Pass index after final
|
||||||
|
Pass index not specified
|
||||||
|
Pass index exceeds length
|
||||||
|
Pass CSS parsing error
|
||||||
|
Pass constraint violation
|
||||||
|
Pass disallowed namespace rule
|
|
@ -0,0 +1,118 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>CSSOM - CSSGroupingRule - insertRule</title>
|
||||||
|
<link rel="help" href="https://drafts.csswg.org/cssom/#the-cssgroupingrule-interface">
|
||||||
|
<script src="../../resources/testharness.js"></script>
|
||||||
|
<script src="../../resources/testharnessreport.js"></script>
|
||||||
|
<script>
|
||||||
|
function create(t) {
|
||||||
|
var style = document.createElement('style');
|
||||||
|
style.appendChild(document.createTextNode('@media all { * {} }'));
|
||||||
|
document.head.appendChild(style);
|
||||||
|
t.add_cleanup(function() {
|
||||||
|
document.head.removeChild(style);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_true(!!style.sheet, 'setup - sheet defined');
|
||||||
|
assert_equals(
|
||||||
|
style.sheet.cssRules.length, 1, 'setup - grouping rule created'
|
||||||
|
);
|
||||||
|
assert_equals(
|
||||||
|
style.sheet.cssRules[0].cssRules.length, 1, 'setup - rule created'
|
||||||
|
);
|
||||||
|
return style.sheet.cssRules[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
var result;
|
||||||
|
|
||||||
|
result = groupingRule.insertRule('.foo {}', 0);
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 2);
|
||||||
|
assert_not_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
assert_equals(groupingRule.cssRules[1].cssText, first);
|
||||||
|
assert_equals(result, 0, 'result');
|
||||||
|
}, 'index before first');
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
var result;
|
||||||
|
|
||||||
|
result = groupingRule.insertRule('.foo {}', 1);
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 2);
|
||||||
|
assert_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
assert_not_equals(groupingRule.cssRules[1].cssText, first);
|
||||||
|
assert_equals(result, 1);
|
||||||
|
}, 'index after final');
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
var result;
|
||||||
|
|
||||||
|
result = groupingRule.insertRule('.foo {}');
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 2);
|
||||||
|
assert_not_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
assert_equals(groupingRule.cssRules[1].cssText, first);
|
||||||
|
assert_equals(result, 0);
|
||||||
|
}, 'index not specified');
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
|
||||||
|
assert_throws_dom('IndexSizeError', function() {
|
||||||
|
// The syntax error is intentional; it verifies that the insertion
|
||||||
|
// index is validated prior to the CSS text.
|
||||||
|
groupingRule.insertRule('???', 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 1);
|
||||||
|
assert_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
}, 'index exceeds length');
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
|
||||||
|
assert_throws_dom('SyntaxError', function() {
|
||||||
|
groupingRule.insertRule('???', 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 1);
|
||||||
|
assert_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
}, 'CSS parsing error');
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
|
||||||
|
assert_throws_dom('HierarchyRequestError', function() {
|
||||||
|
groupingRule.insertRule('@import url("foo.css");', 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 1);
|
||||||
|
assert_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
}, 'constraint violation');
|
||||||
|
|
||||||
|
test(function (t) {
|
||||||
|
var groupingRule = create(t);
|
||||||
|
var first = groupingRule.cssRules[0].cssText;
|
||||||
|
|
||||||
|
assert_throws_dom('HierarchyRequestError', function() {
|
||||||
|
groupingRule.insertRule('@namespace url(http://www.w3.org/1999/xhtml);', 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_equals(groupingRule.cssRules.length, 1);
|
||||||
|
assert_equals(groupingRule.cssRules[0].cssText, first);
|
||||||
|
}, 'disallowed namespace rule');
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue