Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # postcss-media-query-parser
  2. [![NPM version](http://img.shields.io/npm/v/postcss-media-query-parser.svg)](https://www.npmjs.com/package/postcss-media-query-parser) [![Build Status](https://travis-ci.org/dryoma/postcss-media-query-parser.svg?branch=master)](https://travis-ci.org/dryoma/postcss-media-query-parser)
  3. Media query parser with very simple traversing functionality.
  4. ## Installation and usage
  5. First install it via NPM:
  6. ```
  7. npm install postcss-media-query-parser
  8. ```
  9. Then in your Node.js application:
  10. ```js
  11. import mediaParser from "postcss-media-query-parser";
  12. const mediaQueryString = "(max-width: 100px), not print";
  13. const result = mediaParser(mediaQueryString);
  14. ```
  15. The `result` will be this object:
  16. ```js
  17. {
  18. type: 'media-query-list',
  19. value: '(max-width: 100px), not print',
  20. after: '',
  21. before: '',
  22. sourceIndex: 0,
  23. // the first media query
  24. nodes: [{
  25. type: 'media-query',
  26. value: '(max-width: 100px)',
  27. before: '',
  28. after: '',
  29. sourceIndex: 0,
  30. parent: <link to parent 'media-query-list' node>,
  31. nodes: [{
  32. type: 'media-feature-expression',
  33. value: '(max-width: 100px)',
  34. before: '',
  35. after: '',
  36. sourceIndex: 0,
  37. parent: <link to parent 'media-query' node>,
  38. nodes: [{
  39. type: 'media-feature',
  40. value: 'max-width',
  41. before: '',
  42. after: '',
  43. sourceIndex: 1,
  44. parent: <link to parent 'media-feature-expression' node>,
  45. }, {
  46. type: 'colon',
  47. value: ':',
  48. before: '',
  49. after: ' ',
  50. sourceIndex: 10,
  51. parent: <link to parent 'media-feature-expression' node>,
  52. }, {
  53. type: 'value',
  54. value: '100px',
  55. before: ' ',
  56. after: '',
  57. sourceIndex: 12,
  58. parent: <link to parent 'media-feature-expression' node>,
  59. }]
  60. }]
  61. },
  62. // the second media query
  63. {
  64. type: 'media-query',
  65. value: 'not print',
  66. before: ' ',
  67. after: '',
  68. sourceIndex: 20,
  69. parent: <link to parent 'media-query-list' node>,
  70. nodes: [{
  71. type: 'keyword',
  72. value: 'not',
  73. before: ' ',
  74. after: ' ',
  75. sourceIndex: 20,
  76. parent: <link to parent 'media-query' node>,
  77. }, {
  78. type: 'media-type',
  79. value: 'print',
  80. before: ' ',
  81. after: '',
  82. sourceIndex: 24,
  83. parent: <link to parent 'media-query' node>,
  84. }]
  85. }]
  86. }
  87. ```
  88. One of the likely sources of a string to parse would be traversing [a PostCSS container node](http://api.postcss.org/Root.html) and getting the `params` property of nodes with the name of "atRule":
  89. ```js
  90. import postcss from "postcss";
  91. import mediaParser from "postcss-media-query-parser";
  92. const root = postcss.parse(<contents>);
  93. // ... or any other way to get sucn container
  94. root.walkAtRules("media", (atRule) => {
  95. const mediaParsed = mediaParser(atRule.params);
  96. // Do something with "mediaParsed" object
  97. });
  98. ```
  99. ## Nodes
  100. Node is a very generic item in terms of this parser. It's is pretty much everything that ends up in the parsed result. Each node has these properties:
  101. * `type`: the type of the node (see below);
  102. * `value`: the node's value stripped of trailing whitespaces;
  103. * `sourceIndex`: 0-based index of the node start relative to the source start (excluding trailing whitespaces);
  104. * `before`: a string that contain a whitespace between the node start and the previous node end/source start;
  105. * `after`: a string that contain a whitespace between the node end and the next node start/source end;
  106. * `parent`: a link to this node's parent node (a container).
  107. A node can have one of these types (according to [the 2012 CSS3 standard](https://www.w3.org/TR/2012/REC-css3-mediaqueries-20120619/)):
  108. * `media-query-list`: that is the root level node of the parsing result. A [container](#containers); its children can have types of `url` and `media-query`.
  109. * `url`: if a source is taken from a CSS `@import` rule, it will have a `url(...)` function call. The value of such node will be `url(http://uri-address)`, it is to be parsed separately.
  110. * `media-query`: such nodes correspond to each media query in a comma separated list. In the exapmle above there are two. Nodes of this type are [containers](#containers).
  111. * `media-type`: `screen`, `tv` and other media types.
  112. * `keyword`: `only`, `not` or `and` keyword.
  113. * `media-feature-expression`: an expression in parentheses that checks for a condition of a particular media feature. The value would be like this: `(max-width: 1000px)`. Such nodes are [containers](#containers). They always have a `media-feature` child node, but might not have a `value` child node (like in `screen and (color)`).
  114. * `media-feature`: a media feature, e.g. `max-width`.
  115. * `colon`: present if a media feature expression has a colon (e.g. `(min-width: 1000px)`, compared to `(color)`).
  116. * `value`: a media feature expression value, e.g. `100px` in `(max-width: 1000px)`.
  117. ### Parsing details
  118. postcss-media-query-parser allows for cases of some **non-standard syntaxes** and tries its best to work them around. For example, in a media query from a code with SCSS syntax:
  119. ```scss
  120. @media #{$media-type} and ( #{"max-width" + ": 10px"} ) { ... }
  121. ```
  122. `#{$media-type}` will be the node of type `media-type`, alghough `$media-type`'s value can be `only screen`. And inside `media-feature-expression` there will only be a `media-feature` type node with the value of `#{"max-width" + ": 10px"}` (this example doesn't make much sense, it's for demo purpose).
  123. But the result of parsing **malformed media queries** (such as with incorrect amount of closing parens, curly braces, etc.) can be unexpected. For exapmle, parsing:
  124. ```scss
  125. @media ((min-width: -100px)
  126. ```
  127. would return a media query list with the single `media-query` node that has no child nodes.
  128. ## Containers
  129. Containers are [nodes](#nodes) that have other nodes as children. Container nodes have an additional property `nodes` which is an array of their child nodes. And also these methods:
  130. * `each(callback)` - traverses the direct child nodes of a container, calling `callback` function for each of them. Returns `false` if traversing has stopped by means of `callback` returning `false`, and `true` otherwise.
  131. * `walk([filter, ]callback)` - traverses ALL descendant nodes of a container, calling `callback` function for each of them. Returns `false` if traversing has stopped by means of `callback` returning `false`, and `true` otherwise.
  132. In both cases `callback` takes these parameters:
  133. - `node` - the current node (one of the container's descendats, that the callback has been called against).
  134. - `i` - 0-based index of the `node` in an array of its parent's children.
  135. - `nodes` - array of child nodes of `node`'s parent.
  136. If `callback` returns `false`, the traversing stops.
  137. ## License
  138. MIT