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
// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.


/// A list of CSS rules.
#[derive(Default, Debug, Clone)]
pub struct CssRules(pub Vec<CssRule>);

impl ToCss for CssRules
{
	fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result
	{
		for cssRule in self.0.iter()
		{
			cssRule.to_css(dest)?;
		}
		
		Ok(())
	}
}

impl HasCssRules for CssRules
{
	#[inline(always)]
	fn css_rules(&self) -> &CssRules
	{
		self
	}
	
	#[inline(always)]
	fn css_rules_mut(&mut self) -> &mut CssRules
	{
		self
	}
	
	#[inline(always)]
	fn css_rules_slice(&self) -> &[CssRule]
	{
		&self.0[..]
	}
	
	#[inline(always)]
	fn css_rules_vec(&self) -> &Vec<CssRule>
	{
		&self.0
	}
	
	#[inline(always)]
	fn css_rules_vec_mut(&mut self) -> &mut Vec<CssRule>
	{
		&mut self.0
	}
}

impl CssRules
{
	/// Allows vendor prefixing of at-rules
	#[inline(always)]
	pub fn vendor_prefix_at_rules<AtRule: VendorPrefixedAtRule, CssRuleMatcher: Fn(&CssRule) -> Option<&AtRule>, VendorPrefixer: Fn(usize, &AtRule) -> Vec<CssRule>>(&mut self, remove_unprefixed_at_rule: bool, css_rule_matcher: CssRuleMatcher, vendor_prefixer: VendorPrefixer)
	{
		let mut index = 0;
		while index < self.0.len()
		{
			let newCssRulesToInsert = match css_rule_matcher(unsafe { &self.0.get_unchecked(index) })
			{
				None => None,
				Some(atRule) => if atRule.isNotVendorPrefixed()
				{
					Some(vendor_prefixer(index, atRule))
				}
				else
				{
					None
				},
			};
			
			index += if let Some(mut newCssRulesToInsert) = newCssRulesToInsert
			{
				let indexIncrement = newCssRulesToInsert.len();
				
				// TODO: Inefficient
				for newCssRuleToInsert in newCssRulesToInsert.drain(..)
				{
					self.css_rules_vec_mut().insert(index, newCssRuleToInsert);
				}
				if remove_unprefixed_at_rule
				{
					self.css_rules_vec_mut().remove(index + indexIncrement);
					indexIncrement
				}
				else
				{
					indexIncrement + 1
				}
			}
			else
			{
				1
			};
		}
	}
	
	/// Whether this CSS rules is empty.
	pub fn is_empty(&self) -> bool
	{
		self.0.is_empty()
	}
	
	/// Returns whether all the rules in this list are namespace or import rules.
	fn only_namespace_or_import(&self) -> bool
	{
		use self::CssRule::*;
		
		self.0.iter().all(|r|
		{
			match *r
			{
				Namespace(..) | Import(..) => true,
				_ => false
			}
		})
	}
	
	/// https://drafts.csswg.org/cssom/#remove-a-css-rule
	pub fn remove_rule(&mut self, index: usize) -> Result<(), RulesMutateError>
	{
		use self::CssRule::Namespace;
		use self::RulesMutateError::*;
		
		// Step 1, 2
		if index >= self.0.len()
		{
			return Err(IndexSize);
		}
		
		{
			// Step 3
			let ref rule = self.0[index];
			
			// Step 4
			if let Namespace(..) = *rule
			{
				if !self.only_namespace_or_import()
				{
					return Err(InvalidState);
				}
			}
		}
		
		// Step 5, 6
		self.0.remove(index);
		
		Ok(())
	}
}