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 15 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
49 changes: 49 additions & 0 deletions files/en-us/web/api/cssnesteddeclarations/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
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) allows the nesting of `CSSRules` within each other.

With the nested declaration rule the rules can nested and are all grouped together to retain the order.
dletorey marked this conversation as resolved.
Show resolved Hide resolved

> [!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 following JavaScript code returns an object of nested CSS styles.

```js
let myRules = document.styleSheets[0].cssRules;
Copy link
Collaborator

Choose a reason for hiding this comment

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

As above, this really doesn't tell me anything because I don't see what the types are.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

See my response #36430 (comment)

What do you think a reader can learn from this? I don't particularly might what you show, but it has to show something related to the API we are demonstrating, and really this does not.

If I had to do this I would either have a live example that shows the structure, or add pseudo code. Assuming the second, then something like.


Consider the following

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

This would be parsed into a structure similar to the one shown below. Note that blah blah.

[Note, that I am not sure this is correct - I just copied it from your original. Probably it isn't - ie. I don't think the last declaration is nested]

↳ CSSStyleRule
  .style
    - background-color: silver
  ↳ CSSMediaRule
    ↳ CSSNestedDeclarations
      .style (CSSStyleDeclaration, 1) =
      - color: tomato
  ↳ CSSNestedDeclarations
    .style (CSSStyleDeclaration, 1) =
      - color: black

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is, as this is how the order of the rules are retained.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think a reader can learn from this? I don't particularly might what you show, but it has to show something related to the API we are demonstrating, and really this does not.

Still not answered.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What I would do here is write code that creates this structure. I'd do it for you but your repo is locked for edits. So something like:


This example shows how to construct the following stylesheet using the CSSDOM API.

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

[Insert the code here]

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 that point at the end that you gave me in the other note - this is relevant to usage of this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure that this is correct, either.
The CSSNestedDeclarations API only returns that the there is an object of type CSSNestedDeclarations which in turn are cssRules with styles
https://codepen.io/CodeRedDigital/pen/vYozyrG?editors=0112

Although I believe I has managed to explain this better now

console.log(myRules[0].cssRules[1]); // [object CSSNestedDeclarations]
```

## 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
33 changes: 33 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,37 @@ 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 means that the order of the rules are parsed in the order that they are written in the CSS document.
dletorey marked this conversation as resolved.
Show resolved Hide resolved

With the following CSS:

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

The CSSOM parses the CSS in the following way.
dletorey marked this conversation as resolved.
Show resolved Hide resolved

```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
## 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 +473,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)