forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogical.rs
More file actions
155 lines (140 loc) · 4.15 KB
/
logical.rs
File metadata and controls
155 lines (140 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use cssparser::SourceLocation;
use crate::rules::{CssRule, CssRuleList, style::StyleRule};
use parcel_selectors::SelectorList;
use crate::selector::{SelectorIdent, SelectorString};
use crate::declaration::{DeclarationBlock, DeclarationList};
use crate::vendor_prefix::VendorPrefix;
use crate::compat::Feature;
use crate::targets::Browsers;
use crate::traits::ToCss;
use crate::printer::Printer;
use crate::error::PrinterError;
use parcel_selectors::{
parser::{Selector, Component},
attr::{AttrSelectorOperator, ParsedCaseSensitivity}
};
use crate::properties::{
Property,
PropertyId,
custom::CustomProperty,
};
#[derive(Debug)]
pub(crate) struct LogicalProperties {
targets: Option<Browsers>,
pub used: bool
}
impl LogicalProperties {
pub fn new(targets: Option<Browsers>) -> LogicalProperties {
LogicalProperties {
used: false,
targets,
}
}
pub fn is_supported(&self, feature: Feature) -> bool {
if let Some(targets) = self.targets {
feature.is_compatible(targets)
} else {
true
}
}
pub fn add(&mut self, dest: &mut DeclarationList, property_id: PropertyId, ltr: Property, rtl: Property) {
self.used = true;
dest.push(Property::Logical(LogicalProperty {
property_id,
ltr: Some(Box::new(ltr)),
rtl: Some(Box::new(rtl))
}));
}
pub fn add_inline(&mut self, dest: &mut DeclarationList, left: PropertyId, right: PropertyId, start: Option<Property>, end: Option<Property>) {
self.used = true;
dest.push(Property::Logical(LogicalProperty {
property_id: left,
ltr: start.clone().map(|v| Box::new(v)),
rtl: end.clone().map(|v| Box::new(v)),
}));
dest.push(Property::Logical(LogicalProperty {
property_id: right,
ltr: end.map(|v| Box::new(v)),
rtl: start.map(|v| Box::new(v)),
}));
}
pub fn to_rules(&mut self, dest: &mut CssRuleList) {
// Generate rules for [dir="ltr"] and [dir="rtl"] to define --ltr and --rtl vars.
macro_rules! style_rule {
($dir: ident, $ltr: expr, $rtl: expr) => {
dest.0.push(CssRule::Style(StyleRule {
selectors: SelectorList(smallvec::smallvec![
Selector::from_vec2(vec![
Component::AttributeInNoNamespace {
local_name: SelectorIdent("dir".into()),
operator: AttrSelectorOperator::Equal,
value: SelectorString(stringify!($dir).into()),
case_sensitivity: ParsedCaseSensitivity::CaseSensitive,
never_matches: false
}
])
]),
rules: CssRuleList(vec![]),
vendor_prefix: VendorPrefix::empty(),
declarations: DeclarationBlock {
important_declarations: vec![],
declarations: vec![
Property::Custom(CustomProperty {
name: "--ltr".into(),
value: $ltr.into()
}),
Property::Custom(CustomProperty {
name: "--rtl".into(),
value: $rtl.into()
})
]
},
loc: SourceLocation {
line: 0,
column: 0
}
}));
};
}
if self.used {
style_rule!(ltr, "initial", " ");
style_rule!(rtl, " ", "initial");
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LogicalProperty {
pub property_id: PropertyId,
pub ltr: Option<Box<Property>>,
pub rtl: Option<Box<Property>>
}
impl ToCss for LogicalProperty {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
if let Some(ltr) = &self.ltr {
dest.write_str("var(--ltr,")?;
dest.whitespace()?;
ltr.value_to_css(dest)?;
dest.write_char(')')?;
}
if self.ltr.is_some() && self.rtl.is_some() {
dest.whitespace()?;
}
if let Some(rtl) = &self.rtl {
dest.write_str("var(--rtl,")?;
dest.whitespace()?;
rtl.value_to_css(dest)?;
dest.write_char(')')?;
}
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub(crate) enum PropertyCategory {
Logical,
Physical
}
impl Default for PropertyCategory {
fn default() -> PropertyCategory {
PropertyCategory::Physical
}
}