-
Notifications
You must be signed in to change notification settings - Fork 188
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
feat: implement CSS columns #304
Open
yisibl
wants to merge
7
commits into
parcel-bundler:master
Choose a base branch
from
yisibl:css-columns
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
36c8bbd
feat: support `column-width` and `column-count` properties
yisibl 3fff94c
test: add more case
yisibl 6ba65fd
feat: implement `columns` shorthand
yisibl 80749a2
test: add more shorthand case
yisibl 1503b63
fix: rewrite columns parser
CGQAQ 1a5061d
fix: rewrite columns parser again, all tests passed
CGQAQ a1bbd77
fix: all tests passed include tests other than columns test
CGQAQ 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 |
---|---|---|
|
@@ -7,4 +7,5 @@ dist/ | |
.parcel-cache | ||
node/*.flow | ||
artifacts | ||
npm | ||
npm | ||
.idea |
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
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
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
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,109 @@ | ||
//! CSS properties related to Multi-column Layout. | ||
//! https://www.w3.org/TR/css-multicol-1/ | ||
|
||
use super::{Property, PropertyId}; | ||
use crate::context::PropertyHandlerContext; | ||
use crate::declaration::{DeclarationBlock, DeclarationList}; | ||
use crate::error::{ParserError, PrinterError}; | ||
use crate::macros::{define_shorthand, shorthand_handler}; | ||
use crate::printer::Printer; | ||
use crate::targets::Browsers; | ||
use crate::traits::{Parse, PropertyHandler, Shorthand, ToCss}; | ||
use crate::values::length::{IntegerOrAuto, Length, LengthOrAuto, LengthValue}; | ||
use cssparser::*; | ||
use std::ops::Not; | ||
|
||
define_shorthand! { | ||
/// A value for the [columns](https://www.w3.org/TR/css-multicol-1/#columns) shorthand property. | ||
/// columns = <'column-width'> || <'column-count'> | ||
pub struct Columns { | ||
/// The column-width property. | ||
width: ColumnWidth(LengthOrAuto), | ||
/// The column-count property. | ||
count: ColumnCount(IntegerOrAuto), | ||
} | ||
} | ||
|
||
impl<'i> Parse<'i> for Columns { | ||
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { | ||
let state = input.state(); | ||
let with_or_count = LengthOrAuto::parse(input)?; | ||
|
||
if let LengthOrAuto::Length(Length::Value(LengthValue::Unitless(..))) = with_or_count { | ||
// first is unitless, so it must be column-count | ||
// reparse with column-count | ||
input.reset(&state); | ||
let count = IntegerOrAuto::parse(input)?; | ||
let width = input.try_parse(|input| LengthOrAuto::parse(input)).unwrap_or_default(); | ||
|
||
return Ok(Columns { width, count }); | ||
} else if matches!(with_or_count, LengthOrAuto::Auto).not() | ||
&& matches!( | ||
with_or_count, | ||
LengthOrAuto::Length(Length::Value(LengthValue::Unitless(..))) | ||
) | ||
.not() | ||
{ | ||
// first is neither unitless nor auto, so it must be column-width | ||
let count = input.try_parse(|input| IntegerOrAuto::parse(input)).unwrap_or_default(); | ||
|
||
return Ok(Columns { | ||
width: with_or_count, | ||
count, | ||
}); | ||
} | ||
|
||
// first is auto, check second | ||
let state = input.state(); | ||
let count = input.try_parse(|input| LengthOrAuto::parse(input)).unwrap_or_default(); | ||
|
||
if let LengthOrAuto::Auto = count { | ||
// second is auto, so first must be column-width | ||
Ok(Columns { | ||
width: LengthOrAuto::Auto, | ||
count: IntegerOrAuto::Auto, | ||
}) | ||
} else if matches!(count, LengthOrAuto::Length(Length::Value(LengthValue::Unitless(..)))).not() { | ||
// second is not unitless, so first must be column-count | ||
// reparse with column-with | ||
Ok(Columns { | ||
width: count, | ||
count: IntegerOrAuto::Auto, | ||
}) | ||
} else { | ||
// second is unitless, so first must be column-width | ||
input.reset(&state); | ||
let count = IntegerOrAuto::parse(input)?; | ||
|
||
Ok(Columns { | ||
width: with_or_count, | ||
count, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
impl<'i> ToCss for Columns { | ||
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> | ||
where | ||
W: std::fmt::Write, | ||
{ | ||
if self.width != LengthOrAuto::default() { | ||
self.width.to_css(dest)?; | ||
} | ||
|
||
if self.width != LengthOrAuto::default() && self.count != IntegerOrAuto::default() { | ||
dest.write_str(" ")?; | ||
} | ||
|
||
if self.count != IntegerOrAuto::default() { | ||
self.count.to_css(dest)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
shorthand_handler!(ColumnsHandler -> Columns { | ||
width: ColumnWidth(LengthOrAuto), | ||
count: ColumnCount(IntegerOrAuto), | ||
}); |
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
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.
Rather than parsing as a unitless length, you could instead first try parsing the column count, and if that fails, try parsing the length. When parsing shorthands where order doesn't matter, it can be useful to structure the code using a
loop
, trying to parse each property until values are found. There are many examples, here's one.