Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added api page for CSSNestedDeclaration and the style peroperty and a… #36430

Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
804c09b
added api page for CSSNestedDeclaration and the style peroperty and a…
dletorey Oct 21, 2024
3b04b1a
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Oct 28, 2024
a43034f
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Oct 28, 2024
bbda516
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Oct 28, 2024
572c8ee
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Oct 28, 2024
3fd7711
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Oct 28, 2024
b027a3f
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Oct 28, 2024
9066590
updated the see also links and the eamples on CSSNestedDeclaration
dletorey Oct 28, 2024
1afe914
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Oct 29, 2024
b12a538
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Oct 29, 2024
addc205
Merge branch '36115-Implement-the-nested-declarations-rule' of github…
dletorey Oct 29, 2024
84885f4
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Oct 29, 2024
7d5cc32
Merge branch '36115-Implement-the-nested-declarations-rule' of github…
dletorey Oct 29, 2024
90e2c97
updated the .style property
dletorey Oct 29, 2024
88dcd0c
updated the Nested declarations rule section to make it clearer
dletorey Oct 29, 2024
cc1cf3a
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Nov 4, 2024
8077718
Update files/en-us/web/css/css_nesting/using_css_nesting/index.md
dletorey Nov 4, 2024
5e1e43f
fixed domxref link
dletorey Nov 4, 2024
1163d52
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Nov 4, 2024
a84a141
Update files/en-us/web/css/css_nesting/using_css_nesting/index.md
dletorey Nov 5, 2024
95a8f89
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Nov 5, 2024
1ef2f6b
Update files/en-us/web/css/css_nesting/using_css_nesting/index.md
dletorey Nov 5, 2024
214a01c
Update files/en-us/web/css/css_nesting/using_css_nesting/index.md
dletorey Nov 5, 2024
92d4dd6
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Nov 5, 2024
32a0fce
fleshed out the example and what the rules return
dletorey Nov 5, 2024
477450e
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Nov 5, 2024
a78cb6d
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Nov 11, 2024
1303225
Update files/en-us/web/api/cssnesteddeclarations/index.md
dletorey Nov 11, 2024
8a12440
updated description of CSS and includesd the CSSOM representation
dletorey Nov 11, 2024
7ca28d1
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Nov 11, 2024
4e180c7
converted the note
dletorey Nov 11, 2024
bf4fec0
Merge branch 'main' into 36115-Implement-the-nested-declarations-rule
dletorey Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions files/en-us/web/api/css_object_model/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The values of CSS are represented untyped, that is using {{JSxRef("String")}} ob
- {{DOMxRef("CSSStyleSheet")}}
- {{DOMxRef("CSSStyleRule")}}
- {{DOMxRef("CSSSupportsRule")}}
- {{DOMXRef("CSSNestedDeclarations")}}
- {{DOMxRef("FontFace")}}
- {{DOMxRef("FontFaceSet")}}
- {{DOMxRef("FontFaceSetLoadEvent")}}
Expand Down
64 changes: 64 additions & 0 deletions files/en-us/web/api/cssnesteddeclarations/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: CSSNestedDeclarations
slug: Web/API/CSSNestedDeclarations
page-type: web-api-interface
browser-compat: api.CSSNestedDeclarations
---

{{APIRef("CSSOM")}}

The **`CSSNestedDeclarations`** interface of the [CSS Rule API](/en-US/docs/Web/API/CSSRule) is used to group nested {{domxref("CSSRule")}}s.

The interface allows the [CSS Object Model (CSSOM](/en-US/docs/Web/API/CSS_Object_Model) to mirror the structure of CSS documents with nested CSS rules, and ensure that rules are parsed and evaluated in the order that they are declared.

> [!NOTE] > [Browser versions](#browser_compatibility) with implementations that do not support this interface may parse nested rules in the wrong order.

{{InheritanceDiagram}}

## Instance properties

_Inherits properties from its ancestor {{domxref("CSSRule")}}._

- {{domxref("CSSNestedDeclarations.style")}} {{ReadOnlyInline}}
- : Returns the values of the nested rules.

## Instance methods

_No specific methods; inherits methods from its ancestor {{domxref("CSSRule")}}._

## Examples

The CSS below includes a selector `.foo` that contains three nested rules.

```css
.foo {
background-color: silver;
@media (screen) {
color: tomato;
}
color: black;
}
```

This is represented by a number of JavaScript objects in the [CSS Object Model](/en-US/docs/Web/API/CSS_Object_Model):

- A {{domxref("CSSStyleRule")}} object that represents the `background-color: silver` rule.
This can be returned via `document.styleSheets[0].cssRules[0]`.
- A {{domxref("CSSMediaRule")}} object that represents the `@media (screen)` rule, and which can be returned via `document.styleSheets[0].cssRules[0].cssRules[0]`.
- The `CSSMediaRule` object contains a `CSSNestedDeclaration` object which represents the `color: tomato` rule nested by the `@media (screen)` rule.
This can be returned via `document.styleSheets[0].cssRules[0].cssRules[0].cssRules[0]`.
dletorey marked this conversation as resolved.
Show resolved Hide resolved
- The final rule is a `CSSNestedDeclaration` object that represents the `color: black` rule in the stylesheet, and which can be returned via `document.styleSheets[0].cssRules[0].cssRules[1]`.

Note that all top-level styles after the first `CSSNestedDeclaration` must also be represented as `CSSNestedDeclaration` objects in order to follow the [CSS nested declarations rule](/en-US/docs/Web/CSS/CSS_nesting/Using_CSS_nesting#nested_declarations_rule)
## Specifications
dletorey marked this conversation as resolved.
Show resolved Hide resolved

{{Specifications}}

## Browser compatibility

{{Compat}}

## See Also

- {{domxref("CSSNestedDeclarations.style")}}
- [The Nested Declarations Rule](/en-US/docs/Web/CSS/CSS_nesting/Using_CSS_nesting#nested_declarations_rule)
55 changes: 55 additions & 0 deletions files/en-us/web/api/cssnesteddeclarations/style/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "CSSNestedDeclarations: style property"
short-title: style
slug: Web/API/CSSNestedDeclarations/style
page-type: web-api-instance-property
browser-compat: api.CSSNestedDeclarations.style
---

{{APIRef("CSSOM")}}

The read-only **`style`** property of the {{domxref("CSSNestedDeclarations")}} interface represents the styles associated with the nested rules.

## Value

An object.

## Examples

This stylesheet contains a nested {{domxref("cssRule","cssRules")}}.

The first `console.log` shows the top-level `style`, the second shows the nested `@media` query with its nested style and the final shows the nested style declared after the `@media` query.

```css
.foo {
font-size: 1.2rem;
@media (screen) {
color: tomato;
background-color: darkgrey;
}
color: black;
}
```

```js
hamishwillee marked this conversation as resolved.
Show resolved Hide resolved
let myRules = document.styleSheets[0].cssRules;
console.log(myRules[0].style);
// { "0": "font-size" }
console.log(myRules[0].cssRules[0].cssRules[0].style);
// { "0": "color", "1": "background-color" }
console.log(myRules[0].cssRules[1].style);
// { "0": "color" }
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See Also

- {{domxref("CSSNestedDeclarations")}}
- [The Nested Declarations Rule](/en-US/docs/Web/CSS/CSS_nesting/Using_CSS_nesting#nested_declarations_rule)
1 change: 1 addition & 0 deletions files/en-us/web/api/cssrule/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The **`CSSRule`** interface represents a single CSS rule. There are several type
- {{DOMXRef("CSSLayerBlockRule")}}
- {{DOMXRef("CSSLayerStatementRule")}}
- {{DOMXRef("CSSPropertyRule")}}
- {{DOMXRef("CSSNestedDeclarations")}}

## Instance properties

Expand Down
42 changes: 42 additions & 0 deletions files/en-us/web/css/css_nesting/using_css_nesting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,46 @@ In the following CSS, we are creating the styles for `.card` and `.card h2`. The

{{EmbedLiveSample('Appending_nesting_selector','100%','250')}}

## Nested declarations rule
dletorey marked this conversation as resolved.
Show resolved Hide resolved

The nested declaration rule is that CSS rules are parsed in the order that they are written in the CSS document.

With the following CSS:

```css
.foo {
background-color: silver;
@media (screen) {
color: tomato;
}
color: black;
}
```

The `background-color` is parsed first and set to silver, then the `@media` rule is evaluated, and finally the `color`.

The CSSOM parses the CSS in the following way:

```txt
dletorey marked this conversation as resolved.
Show resolved Hide resolved
↳ CSSStyleRule
.style
- background-color: silver
↳ CSSMediaRule
↳ CSSNestedDeclarations
.style (CSSStyleDeclaration, 1) =
- color: tomato
↳ CSSNestedDeclarations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this nested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment
All the rules before nesting or at-properties are added at the top-level CSSRules anything after nesting or at-properties are added as nested declarations as that is how the order is preserved. Before CSSNestedDeclarations rules at the same level were hoisted up to the top with other rules whcih changed the order and effected the cascade.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really useful thanks! Reading that I think it is more before and after nesting, and the at-rules is just a form of nesting right?

Anyway, if I can have this misunderstanding others can, so I have added proposals https://github.com/mdn/content/pull/36430/files#r1828495475 , https://github.com/mdn/content/pull/36430/files#r1828504825, #36430 (comment)

.style (CSSStyleDeclaration, 1) =
- color: black
```

dletorey marked this conversation as resolved.
Show resolved Hide resolved
dletorey marked this conversation as resolved.
Show resolved Hide resolved
Note that in order to preserve the parsing order, all the rules before nesting are handled as top-level `CSSRules`, while any top level rules after nesting are represented as `CSSNestedDeclarations`.
That's why the `color-black` is inside a nested declaration even though it is a top level declaration in the original document.

> [!NOTE]
> Support for the rule was added with {{domxref("CSSNestedDeclarations")}}.
> Browsers that [do not support this interface](/en-US/docs/Web/API/CSSNestedDeclarations#browser_compatibility) this interface may parse nested rules in the wrong order.

## Concatenation (is not possible)

In CSS preprocessors such as [Sass](https://sass-lang.com/), it is possible to use nesting to join strings to create new classes. This is common in CSS methodologies such as [BEM](https://getbem.com/naming/).
Expand Down Expand Up @@ -442,3 +482,5 @@ In the following example, there is an invalid selector (`%` is not a valid chara
- [`&` nesting selector](/en-US/docs/Web/CSS/Nesting_selector)
- [Nesting `@` at-rules](/en-US/docs/Web/CSS/CSS_nesting/Nesting_at-rules)
- [Nesting and specificity](/en-US/docs/Web/CSS/CSS_nesting/Nesting_and_specificity)
- {{domxref("CSSNestedDeclarations")}}
- [The Nested Declarations Rule](https://drafts.csswg.org/css-nesting-1/#nested-declarations-rule)
Loading