@prospective.co/procss 0.1.8 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prospective.co/procss",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "A simple CSS parsing and transformation framework.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,25 +22,48 @@ use crate::ast::token::*;
22
22
  use crate::parser::*;
23
23
  use crate::render::*;
24
24
 
25
+ /// pseudo-selectors can be "pseudo-class" or "pseudo-element", and we are only
26
+ /// concerned about the distinction between them in regards to their syntax.
27
+ #[derive(Clone, Debug, Eq, PartialEq, Hash)]
28
+ pub enum PseudoMode {
29
+ PseudoClass,
30
+ PseudoElement,
31
+ }
32
+
25
33
  /// A pseudo-selector component of a `Selector`, including optional argument
26
34
  /// selector (parenthesis delimited).
27
35
  #[derive(Clone, Debug, Eq, PartialEq, Hash)]
28
36
  pub struct Pseudo<'a> {
29
37
  property: &'a str,
30
38
  value: Option<SelectorTerm<'a, Option<&'a str>>>,
39
+ mode: PseudoMode,
31
40
  }
32
41
 
33
42
  impl<'a> ParseCss<'a> for Pseudo<'a> {
34
43
  fn parse<E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, Self, E> {
35
- let (input, property) = preceded(tuple((tag(":"), opt(tag(":")))), parse_symbol)(input)?;
44
+ let (input, mode) = tuple((tag(":"), opt(tag(":"))))(input)?;
45
+ let (input, property) = parse_symbol(input)?;
36
46
  let (input, value) = opt(delimited(tag("("), SelectorTerm::parse, tag(")")))(input)?;
37
- Ok((input, Pseudo { property, value }))
47
+ let mode = mode
48
+ .1
49
+ .map(|_| PseudoMode::PseudoElement)
50
+ .unwrap_or(PseudoMode::PseudoClass);
51
+
52
+ Ok((input, Pseudo {
53
+ property,
54
+ value,
55
+ mode,
56
+ }))
38
57
  }
39
58
  }
40
59
 
41
60
  impl<'a> RenderCss for Pseudo<'a> {
42
61
  fn render(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43
- write!(f, ":{}", self.property)?;
62
+ match self.mode {
63
+ PseudoMode::PseudoClass => write!(f, ":{}", self.property)?,
64
+ PseudoMode::PseudoElement => write!(f, "::{}", self.property)?,
65
+ };
66
+
44
67
  if let Some(x) = self.value.as_ref() {
45
68
  write!(f, "(")?;
46
69
  x.render(f)?;
@@ -283,7 +306,7 @@ mod tests {
283
306
  tag: Some("div"),
284
307
  pseudo,
285
308
  ..
286
- })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{property: "hover", value: None })
309
+ })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{property: "hover", value: None, mode: PseudoMode::PseudoClass })
287
310
  )
288
311
  }
289
312
 
@@ -295,7 +318,7 @@ mod tests {
295
318
  tag: Some("div"),
296
319
  pseudo,
297
320
  ..
298
- })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{ property: "not", value: Some(_) })
321
+ })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{ property: "not", value: Some(_), mode: PseudoMode::PseudoClass })
299
322
  )
300
323
  }
301
324
 
@@ -307,7 +330,7 @@ mod tests {
307
330
  tag: Some("div"),
308
331
  pseudo,
309
332
  ..
310
- })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{ property: "nth-child", value: Some(_) })
333
+ })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{ property: "nth-child", value: Some(_), mode: PseudoMode::PseudoClass })
311
334
  )
312
335
  }
313
336
 
@@ -322,4 +345,28 @@ mod tests {
322
345
  Ok("div:nth-child(2)")
323
346
  )
324
347
  }
348
+
349
+ #[test]
350
+ fn test_pesudo_element() {
351
+ assert_matches!(
352
+ SelectorTerm::parse::<()>("div::-webkit-scroll-thumb"),
353
+ Ok(("", SelectorTerm {
354
+ tag: Some("div"),
355
+ pseudo,
356
+ ..
357
+ })) if pseudo.len() == 1 && matches!(pseudo[0], Pseudo{property: "-webkit-scroll-thumb", value: None, mode: PseudoMode::PseudoElement })
358
+ )
359
+ }
360
+
361
+ #[test]
362
+ fn test_pesudo_element_renders_correctly() {
363
+ assert_matches!(
364
+ SelectorTerm::<Option<&str>>::parse::<nom::error::VerboseError<&str>>(
365
+ "div::-webkit-scroll-thumb"
366
+ )
367
+ .map(|x| x.as_css_string())
368
+ .as_deref(),
369
+ Ok("div::-webkit-scroll-thumb")
370
+ )
371
+ }
325
372
  }
package/src/lib.rs CHANGED
@@ -86,7 +86,11 @@ mod parser;
86
86
  mod render;
87
87
  mod transform;
88
88
  pub mod transformers;
89
- mod utils;
89
+ #[cfg(feature = "iotest")]
90
+ pub mod utils;
91
+
92
+ #[cfg(not(feature = "iotest"))]
93
+ pub mod utils;
90
94
 
91
95
  use self::ast::Tree;
92
96
  pub use self::builder::BuildCss;
@@ -30,7 +30,12 @@ fn parse_url(input: &str) -> nom::IResult<&str, &str> {
30
30
  fn into_data_uri<'a>(path: &Path) -> Cow<'a, str> {
31
31
  let contents = fs::read_to_string(path).expect("Error reading file");
32
32
  let encoded = base64::encode(contents);
33
- format!("url(data:image/svg+xml;base64,{})", encoded).into()
33
+ let fff = path.extension().unwrap_or_default().to_string_lossy();
34
+ let fmt = match fff.as_ref() {
35
+ "png" => "png",
36
+ _ => "svg+xml",
37
+ };
38
+ format!("url(\"data:image/{};base64,{}\")", fmt, encoded).into()
34
39
  }
35
40
 
36
41
  fn inline_url_impl<'a>(newpath: &str, flat: &mut Css<'a>) {
@@ -40,7 +45,9 @@ fn inline_url_impl<'a>(newpath: &str, flat: &mut Css<'a>) {
40
45
  .and_then(|x| x.0.is_empty().then_some(Path::new(newpath).join(x.1)));
41
46
 
42
47
  if let Some(path) = &path {
43
- rule.value = into_data_uri(path);
48
+ if path.starts_with(".") || path.starts_with("/") {
49
+ rule.value = into_data_uri(path);
50
+ }
44
51
  }
45
52
  })
46
53
  }
@@ -177,32 +177,54 @@ class BuildCss {
177
177
  }
178
178
  module.exports.BuildCss = BuildCss;
179
179
 
180
- module.exports.__wbindgen_error_new = function(arg0, arg1) {
181
- const ret = new Error(getStringFromWasm0(arg0, arg1));
180
+ module.exports.__wbindgen_throw = function(arg0, arg1) {
181
+ throw new Error(getStringFromWasm0(arg0, arg1));
182
+ };
183
+
184
+ module.exports.__wbindgen_string_new = function(arg0, arg1) {
185
+ const ret = getStringFromWasm0(arg0, arg1);
182
186
  return addHeapObject(ret);
183
187
  };
184
188
 
189
+ module.exports.__wbindgen_is_string = function(arg0) {
190
+ const ret = typeof(getObject(arg0)) === 'string';
191
+ return ret;
192
+ };
193
+
194
+ module.exports.__wbindgen_object_drop_ref = function(arg0) {
195
+ takeObject(arg0);
196
+ };
197
+
185
198
  module.exports.__wbg_new_268f7b7dd3430798 = function() {
186
199
  const ret = new Map();
187
200
  return addHeapObject(ret);
188
201
  };
189
202
 
190
- module.exports.__wbindgen_string_new = function(arg0, arg1) {
191
- const ret = getStringFromWasm0(arg0, arg1);
203
+ module.exports.__wbg_set_933729cf5b66ac11 = function(arg0, arg1, arg2) {
204
+ const ret = getObject(arg0).set(getObject(arg1), getObject(arg2));
192
205
  return addHeapObject(ret);
193
206
  };
194
207
 
195
- module.exports.__wbg_set_933729cf5b66ac11 = function(arg0, arg1, arg2) {
196
- const ret = getObject(arg0).set(getObject(arg1), getObject(arg2));
208
+ module.exports.__wbg_new_0b9bfdd97583284e = function() {
209
+ const ret = new Object();
197
210
  return addHeapObject(ret);
198
211
  };
199
212
 
200
- module.exports.__wbindgen_object_drop_ref = function(arg0) {
201
- takeObject(arg0);
213
+ module.exports.__wbg_String_91fba7ded13ba54c = function(arg0, arg1) {
214
+ const ret = String(getObject(arg1));
215
+ const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
216
+ const len0 = WASM_VECTOR_LEN;
217
+ getInt32Memory0()[arg0 / 4 + 1] = len0;
218
+ getInt32Memory0()[arg0 / 4 + 0] = ptr0;
202
219
  };
203
220
 
204
- module.exports.__wbindgen_throw = function(arg0, arg1) {
205
- throw new Error(getStringFromWasm0(arg0, arg1));
221
+ module.exports.__wbindgen_error_new = function(arg0, arg1) {
222
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
223
+ return addHeapObject(ret);
224
+ };
225
+
226
+ module.exports.__wbg_set_20cbc34131e76824 = function(arg0, arg1, arg2) {
227
+ getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
206
228
  };
207
229
 
208
230
  const path = require('path').join(__dirname, 'procss_bg.wasm');
Binary file
@@ -208,27 +208,45 @@ async function load(module, imports) {
208
208
  function getImports() {
209
209
  const imports = {};
210
210
  imports.wbg = {};
211
- imports.wbg.__wbindgen_error_new = function(arg0, arg1) {
212
- const ret = new Error(getStringFromWasm0(arg0, arg1));
211
+ imports.wbg.__wbindgen_throw = function(arg0, arg1) {
212
+ throw new Error(getStringFromWasm0(arg0, arg1));
213
+ };
214
+ imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
215
+ const ret = getStringFromWasm0(arg0, arg1);
213
216
  return addHeapObject(ret);
214
217
  };
218
+ imports.wbg.__wbindgen_is_string = function(arg0) {
219
+ const ret = typeof(getObject(arg0)) === 'string';
220
+ return ret;
221
+ };
222
+ imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
223
+ takeObject(arg0);
224
+ };
215
225
  imports.wbg.__wbg_new_268f7b7dd3430798 = function() {
216
226
  const ret = new Map();
217
227
  return addHeapObject(ret);
218
228
  };
219
- imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
220
- const ret = getStringFromWasm0(arg0, arg1);
221
- return addHeapObject(ret);
222
- };
223
229
  imports.wbg.__wbg_set_933729cf5b66ac11 = function(arg0, arg1, arg2) {
224
230
  const ret = getObject(arg0).set(getObject(arg1), getObject(arg2));
225
231
  return addHeapObject(ret);
226
232
  };
227
- imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
228
- takeObject(arg0);
233
+ imports.wbg.__wbg_new_0b9bfdd97583284e = function() {
234
+ const ret = new Object();
235
+ return addHeapObject(ret);
229
236
  };
230
- imports.wbg.__wbindgen_throw = function(arg0, arg1) {
231
- throw new Error(getStringFromWasm0(arg0, arg1));
237
+ imports.wbg.__wbg_String_91fba7ded13ba54c = function(arg0, arg1) {
238
+ const ret = String(getObject(arg1));
239
+ const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
240
+ const len0 = WASM_VECTOR_LEN;
241
+ getInt32Memory0()[arg0 / 4 + 1] = len0;
242
+ getInt32Memory0()[arg0 / 4 + 0] = ptr0;
243
+ };
244
+ imports.wbg.__wbindgen_error_new = function(arg0, arg1) {
245
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
246
+ return addHeapObject(ret);
247
+ };
248
+ imports.wbg.__wbg_set_20cbc34131e76824 = function(arg0, arg1, arg2) {
249
+ getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
232
250
  };
233
251
 
234
252
  return imports;
Binary file