-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Documentation: Document how to add a new CSS property #3564
Open
AtkinsSJ
wants to merge
1
commit into
LadybirdBrowser:master
Choose a base branch
from
AtkinsSJ:document-css-properties
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Adding or Modifying a CSS Property | ||
|
||
There are several different places you need to make changes in order to add or modify a CSS property. | ||
These are listed below in the order that Ladybird deals with them, starting at parsing and ending with them being used. | ||
|
||
## Data | ||
|
||
The first place you will need to go to is `CSS/Properties.json`. This file contains the definition for each | ||
property, and is used to generate the `PropertyID` enum and a selection of functions. You may also need to | ||
modify `CSS/Keywords.json` and `CSS/Enums.json`. See [CSSGeneratedFiles.md](CSSGeneratedFiles.md) for details. | ||
|
||
## Parsing | ||
|
||
For many properties, there is no need to add custom parsing code. Properties that take a single value, or shorthands | ||
that are a list of their longhand properties, will be parsed automatically using the data in `Properties.json`. | ||
However, there are many CSS properties with more complicated grammar and so they require custom parsing. | ||
|
||
Property-parsing code goes in `CSS/Parser/PropertyParsing.cpp`, and `CSS/Parser/Parser.h`. First, | ||
`Parser::parse_css_value()` is called, which has a switch for specific properties. Call your method from there. It | ||
should return a `RefPtr` to a `CSSStyleValue` or one of its subclasses. | ||
|
||
For shorthands, you should normally use `ShorthandStyleValue`, which automatically expands its longhand values. You | ||
might need to modify `ShorthandStyleValue::to_string` if your shorthand has special serialization rules. For example, | ||
`border-radius` serializes with a `/` separating the horizontal and vertical components. | ||
|
||
If your property's value can't be represented with an existing type, you might need to add a new style value class. | ||
If you need to do this, pester @AtkinsSJ until he gets around to documenting it. ;^) | ||
|
||
## Computed style | ||
|
||
After parsing and style computation, longhand properties are stored as `CSSStyleValue` pointers in | ||
`ComputedProperties`. Any shorthands have been expanded out, and so we do not need to store them directly. | ||
|
||
These longhands then need to be converted to a more usable form. To do this, add a getter to `ComputedProperties` with | ||
the same name as the property. It should return a type that holds the value in a compact form. Be aware that anything | ||
involving numbers or dimensions may be a calculation, so store it in one of the `FooOrCalculated` types. | ||
|
||
Then, `CSS/ComputedValues.h` contains three classes that are relevant: | ||
- `ComputedValues` holds the computed value of each property, in a flat format. Depending on whether the property is | ||
inherited or not, it needs adding to the `m_inherited` or `m_noninherited` structs, with a corresponding getter. | ||
- `MutableComputedValues` also needs a setter for the value. | ||
- `InitialValues` has a getter for the default value of the property. This isn't always needed, for example if the | ||
default computed value is an empty `Optional` or `Vector`. | ||
|
||
Style is copied from `ComputedProperties` to `ComputedValues` in `NodeWithStyle::apply_style()`. Each property is | ||
copied individually. | ||
|
||
Then, read the value of your property with that `ComputedValues` getter we added. For example, this code reads the | ||
computed values of `visibility` and `opacity`: | ||
|
||
```c++ | ||
bool Paintable::is_visible() const | ||
{ | ||
auto const& computed_values = this->computed_values(); | ||
return computed_values.visibility() == CSS::Visibility::Visible && computed_values.opacity() != 0; | ||
} | ||
``` | ||
|
||
## JavaScript | ||
|
||
Some properties have special rules for getting the computed value from JS. For these, you will need to add to | ||
`ResolvedCSSStyleDeclaration::style_value_for_property()`. Shorthands that are constructed in an unusual way (as in, not | ||
using `ShorthandStyleValue`) also need handling inside `CSSStyleDeclaration::get_property_internal()`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something about custom serialization rules here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure what you mean, but I've added an example of a custom serialization rule, does that work?