If this [Token] is preceded by the [Token] `other` then a separating token (e.g. a comment) will need to be inserted between these the two tokens during serialization, in order for them to be able to be re-tokenized as the same tokens. For example an Ident ("a") adjacent to an Ident ("b"), if serialized without whitespace, would create a single Ident ("ab"). The rules for estbalishing whether or n
(&self, second: Token)
| 1091 | /// assert!(first.needs_separator_for(second)); |
| 1092 | /// ``` |
| 1093 | pub fn needs_separator_for(&self, second: Token) -> bool { |
| 1094 | if second == AssociatedWhitespaceRules::EnforceBefore && *self != Kind::Whitespace |
| 1095 | || *self == AssociatedWhitespaceRules::EnforceAfter && second != Kind::Whitespace |
| 1096 | { |
| 1097 | // We need whitespace after, unless the next token is actually whitespace. |
| 1098 | return true; |
| 1099 | } |
| 1100 | if *self == AssociatedWhitespaceRules::BanAfter { |
| 1101 | return false; |
| 1102 | } |
| 1103 | match self.kind() { |
| 1104 | Kind::Ident => { |
| 1105 | (matches!(second.kind(), Kind::Number | Kind::Dimension) && |
| 1106 | // numbers with a `-` need separating, but with `+` they do not. |
| 1107 | (!second.has_sign() || second.value() < 0.0)) |
| 1108 | || matches!(second.kind(), Kind::Ident | Kind::Function | Kind::Url | Kind::BadUrl) |
| 1109 | || matches!(second.char(), Some('(' | '-')) |
| 1110 | || second.is_cdc() |
| 1111 | } |
| 1112 | Kind::AtKeyword | Kind::Hash | Kind::Dimension => { |
| 1113 | (matches!(second.kind(), Kind::Number | Kind::Dimension) && |
| 1114 | // numbers with a `-` need separating, but with `+` they do not. |
| 1115 | (!second.has_sign() || second.value() < 0.0)) |
| 1116 | || matches!(second.kind(), Kind::Ident | Kind::Function | Kind::Url | Kind::BadUrl) |
| 1117 | || matches!(second.char(), Some('-')) |
| 1118 | || second.is_cdc() |
| 1119 | } |
| 1120 | Kind::Number => { |
| 1121 | matches!( |
| 1122 | second.kind(), |
| 1123 | Kind::Ident | Kind::Function | Kind::Url | Kind::BadUrl | Kind::Number | Kind::Dimension |
| 1124 | ) || matches!(second.char(), Some('%')) |
| 1125 | || second.is_cdc() |
| 1126 | } |
| 1127 | _ => match self.char() { |
| 1128 | Some('#') => { |
| 1129 | matches!( |
| 1130 | second.kind(), |
| 1131 | Kind::Ident | Kind::Function | Kind::Url | Kind::BadUrl | Kind::Number | Kind::Dimension |
| 1132 | ) || matches!(second.char(), Some('-')) |
| 1133 | || second.is_cdc() |
| 1134 | } |
| 1135 | Some('-') => { |
| 1136 | matches!( |
| 1137 | second.kind(), |
| 1138 | Kind::Ident | Kind::Function | Kind::Url | Kind::BadUrl | Kind::Number | Kind::Dimension |
| 1139 | ) || matches!(second.char(), Some('-')) |
| 1140 | || second.is_cdc() |
| 1141 | } |
| 1142 | Some('@') => { |
| 1143 | matches!(second.kind(), Kind::Ident | Kind::Function | Kind::Url | Kind::BadUrl) |
| 1144 | || matches!(second.char(), Some('-')) |
| 1145 | || second.is_cdc() |
| 1146 | } |
| 1147 | Some('.') => matches!(second.kind(), Kind::Number | Kind::Dimension), |
| 1148 | Some('+') => matches!(second.kind(), Kind::Number | Kind::Dimension), |
| 1149 | Some('/') => matches!(second.char(), Some('*' | '/')), |
| 1150 | _ => false, |