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
// 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.


/// Vendor prefixes
/// Sort order is such that -o- sorts before -webkit- and -ms- sorts after -webkit-
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum VendorPrefix
{
	/// -o- prefix (legacy Opera Presto prefix).
	o,
	
	/// -moz- prefix.
	moz,
	
	/// -webkit- prefix (Is sometimes also used by IE, Edge and Blink-based browsers (Chrome and Opera)).
	webkit,
	
	/// -ms- prefix.
	ms,
	
	/// -servo- prefix
	servo,
	
	/// An unrecognised prefix, usually implies unusual or mistaken CSS
	Unrecognised(String),
}

impl ToCss for VendorPrefix
{
	fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result
	{
		use self::VendorPrefix::*;
		
		match *self
		{
			o => dest.write_str("-o-"),
			
			moz => dest.write_str("-moz-"),
			
			webkit => dest.write_str("-webkit-"),
			
			ms => dest.write_str("-ms-"),
			
			servo => dest.write_str("-servo-"),
			
			Unrecognised(ref prefix) =>
			{
				dest.write_char('-')?;
				dest.write_str(prefix.as_str())?;
				dest.write_char('-')
			},
		}
	}
}

impl VendorPrefix
{
	/// Finds a prefix for an ascii lower case name, returning the prefix (if any) and the unprefixed name
	/// Is not confused by CSS custom properties which start `--`
	#[inline(always)]
	pub fn findPrefixIfAnyForAsciiLowerCaseName<'i>(asciiLowerCaseName: String) -> (Option<VendorPrefix>, String)
	{
		if asciiLowerCaseName.len() < 3
		{
			return (None, asciiLowerCaseName);
		}
		
		{
			let (firstCharacter, remainder) = asciiLowerCaseName.split_at(1);
			
			if firstCharacter == "-" && !remainder.starts_with('-')
			{
				let mut split = remainder.splitn(2, '-');
				let prefix = split.next().unwrap();
				let unprefixedRemainder = split.next().unwrap();
				
				use self::VendorPrefix::*;
				
				return match prefix
				{
					"o" => (Some(o), unprefixedRemainder.to_owned()),
					
					"moz" => (Some(moz), unprefixedRemainder.to_owned()),
					
					"webkit" => (Some(webkit), unprefixedRemainder.to_owned()),
					
					"ms" => (Some(ms), unprefixedRemainder.to_owned()),
					
					"servo" => (Some(servo), unprefixedRemainder.to_owned()),
					
					_ => (Some(Unrecognised(prefix.to_owned())), unprefixedRemainder.to_owned()),
				};
			}
		}
		
		return (None, asciiLowerCaseName);
	}
	
	/// Prefixes a name with a vendor prefix, eg 'background' might become '-moz-background' if Self is `moz`
	#[inline(always)]
	pub fn prefix(&self, name: &str) -> String
	{
		use self::VendorPrefix::*;
		
		fn knownPrefix(prefix: &str, name: &str) -> String
		{
			let mut prefixed = String::with_capacity(prefix.len() + name.len());
			prefixed.push_str(prefix);
			prefixed.push_str(name);
			prefixed
		}
		
		match self
		{
			&o => knownPrefix("-o-", name),
			
			&moz => knownPrefix("-moz-", name),
			
			&webkit => knownPrefix("-webkit-", name),
			
			&ms => knownPrefix("-ms-", name),
			
			&servo => knownPrefix("-servo-", name),
			
			&Unrecognised(ref prefix) =>
			{
				let mut prefixed = String::with_capacity(1 + prefix.len() + 1 + name.len());
				prefixed.push('-');
				prefixed.push_str(prefix);
				prefixed.push('-');
				prefixed.push_str(name);
				prefixed
			},
		}
	}
}