@@ -597,37 +597,167 @@ around it.
597
597
[id = Bar i ] /* " i " */
598
598
```
599
599
600
- ## ` processor `
600
+ ## ` Processor `
601
601
602
- ### ` process|processSync(css, [options]) `
602
+ ### ` ProcessorOptions `
603
603
604
- Processes the ` css ` , returning the parsed output. An async method is exposed
605
- as ` process ` .
604
+ * ` lossless ` - When true, whitespace is preserved. Defaults to true.
605
+ * ` updateSelector ` - When true, if any processor methods are passed a postcss
606
+ ` Rule ` node instead of a string, then that Rule's selector is updated
607
+ with the results of the processing.
608
+
609
+ ### ` process|processSync(selectors, [options]) `
610
+
611
+ Processes the ` selectors ` , returning a string from the result of processing.
612
+
613
+ Note: when the ` updateSelector ` option is set, the rule's selector
614
+ will be updated with the resulting string.
615
+
616
+ ** Example:**
606
617
607
618
``` js
619
+ const parser = require (" postcss-selector-parser" );
608
620
const processor = parser ();
609
621
610
- const result = processor .processSync (' .class' );
622
+ let result = processor .processSync (' .class' );
623
+ console .log (result);
611
624
// => .class
612
625
613
626
// Asynchronous operation
614
- processor .process (' .class' ).then (result => /* ... */ );
627
+ let promise = processor .process (' .class' ).then (result => {
628
+ console .log (result)
629
+ // => .class
630
+ });
615
631
616
632
// To have the parser normalize whitespace values, utilize the options
617
- const result = processor .processSync (' .class ' , {lossless: false });
633
+ result = processor .processSync (' .class ' , {lossless: false });
634
+ console .log (result);
618
635
// => .class
619
636
620
637
// For better syntax errors, pass a PostCSS Rule node.
621
638
const postcss = require (' postcss' );
622
- const rule = postcss .rule ({selector: ' a' });
623
- const result = process .process (rule);
639
+ rule = postcss .rule ({selector: ' #foo > a, .class ' });
640
+ processor .process (rule, {lossless: false , updateSelector: true }).then (result => {
641
+ console .log (result);
642
+ // => #foo>a,.class
643
+ console .log (" rule:" , rule .selector );
644
+ // => rule: #foo>a,.class
645
+ })
624
646
```
625
647
626
648
Arguments:
627
649
628
- * ` css (string|object) ` : Either a selector string or a PostCSS Rule node.
650
+ * ` selectors (string|postcss.Rule) ` : Either a selector string or a PostCSS Rule
651
+ node.
629
652
* ` [options] (object) ` : Process options
630
653
631
- Options:
632
654
633
- * ` lossless (boolean) ` : false to normalize the selector whitespace, defaults to true
655
+ ### ` ast|astSync(selectors, [options]) `
656
+
657
+ Like ` process() ` and ` processSync() ` but after
658
+ processing the ` selectors ` these methods return the ` Root ` node of the result
659
+ instead of a string.
660
+
661
+ Note: when the ` updateSelector ` option is set, the rule's selector
662
+ will be updated with the resulting string.
663
+
664
+ ### ` transform|transformSync(selectors, [options]) `
665
+
666
+ Like ` process() ` and ` processSync() ` but after
667
+ processing the ` selectors ` these methods return the value returned by the
668
+ processor callback.
669
+
670
+ Note: when the ` updateSelector ` option is set, the rule's selector
671
+ will be updated with the resulting string.
672
+
673
+ ### Error Handling Within Selector Processors
674
+
675
+ The root node passed to the selector processor callback
676
+ has a method ` error(message, options) ` that returns an
677
+ error object. This method should always be used to raise
678
+ errors relating to the syntax of selectors. The options
679
+ to this method are passed to postcss's error constructor
680
+ ([ documentation] ( http://api.postcss.org/Container.html#error ) ).
681
+
682
+ #### Async Error Example
683
+
684
+ ``` js
685
+ let processor = (root ) => {
686
+ return new Promise ((resolve , reject ) => {
687
+ root .walkClasses ((classNode ) => {
688
+ if (/ ^ (. * )[-_] / .test (classNode .value )) {
689
+ let msg = " classes may not have underscores or dashes in them" ;
690
+ reject (root .error (msg, {
691
+ index: classNode .sourceIndex + RegExp .$1 .length + 1 ,
692
+ word: classNode .value
693
+ }));
694
+ }
695
+ });
696
+ resolve ();
697
+ });
698
+ };
699
+
700
+ const postcss = require (" postcss" );
701
+ const parser = require (" postcss-selector-parser" );
702
+ const selectorProcessor = parser (processor);
703
+ const plugin = postcss .plugin (' classValidator' , (options ) => {
704
+ return (root ) => {
705
+ let promises = [];
706
+ root .walkRules (rule => {
707
+ promises .push (selectorProcessor .process (rule));
708
+ });
709
+ return Promise .all (promises);
710
+ };
711
+ });
712
+ postcss (plugin ()).process (`
713
+ .foo-bar {
714
+ color: red;
715
+ }
716
+ ` .trim (), {from: ' test.css' }).catch ((e ) => console .error (e .toString ()));
717
+
718
+ // CssSyntaxError: classValidator: ./test.css:1:5: classes may not have underscores or dashes in them
719
+ //
720
+ // > 1 | .foo-bar {
721
+ // | ^
722
+ // 2 | color: red;
723
+ // 3 | }
724
+ ```
725
+
726
+ #### Synchronous Error Example
727
+
728
+ ``` js
729
+ let processor = (root ) => {
730
+ root .walkClasses ((classNode ) => {
731
+ if (/ . * [-_] / .test (classNode .value )) {
732
+ let msg = " classes may not have underscores or dashes in them" ;
733
+ throw root .error (msg, {
734
+ index: classNode .sourceIndex ,
735
+ word: classNode .value
736
+ });
737
+ }
738
+ });
739
+ };
740
+
741
+ const postcss = require (" postcss" );
742
+ const parser = require (" postcss-selector-parser" );
743
+ const selectorProcessor = parser (processor);
744
+ const plugin = postcss .plugin (' classValidator' , (options ) => {
745
+ return (root ) => {
746
+ root .walkRules (rule => {
747
+ selectorProcessor .processSync (rule);
748
+ });
749
+ };
750
+ });
751
+ postcss (plugin ()).process (`
752
+ .foo-bar {
753
+ color: red;
754
+ }
755
+ ` .trim (), {from: ' test.css' }).catch ((e ) => console .error (e .toString ()));
756
+
757
+ // CssSyntaxError: classValidator: ./test.css:1:5: classes may not have underscores or dashes in them
758
+ //
759
+ // > 1 | .foo-bar {
760
+ // | ^
761
+ // 2 | color: red;
762
+ // 3 | }
763
+ ```
0 commit comments