standalone.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <html>
  2. <head>
  3. <script src="lib.js"></script>
  4. <script src="ometa-base.js"></script>
  5. <script src="parser.js"></script>
  6. <script src="bs-js-compiler.js"></script>
  7. <script src="bs-ometa-compiler.js"></script>
  8. <script src="bs-ometa-optimizer.js"></script>
  9. <script src="bs-ometa-js-compiler.js"></script>
  10. <script src="ometa-script-tag.js"></script>
  11. <script type="text/x-ometa-js">
  12. ometa CssSelector <: Parser {
  13. crChar = '\r',
  14. ffChar = '\f',
  15. nlChar = '\n',
  16. tabChar = '\t',
  17. lineEnding = crChar | ffChar | nlChar,
  18. tabOrLineEnding = tabChar | lineEnding,
  19. ident = '-' nmstart:s nmchar*:cs -> { '-' + s + cs.join('') }
  20. | nmstart:s nmchar*:cs -> { s + cs.join('') },
  21. name = nmchar+:n -> { n.join('') },
  22. nmstart = ('_' | letter | nonascii | escape):n -> { n },
  23. nonascii = '',
  24. unicode = '',
  25. escape = unicode | '',
  26. nmchar = '_' | '-' | letterOrDigit | nonascii | escape,
  27. num = digit+: d -> d.join('')
  28. | digit* '.' digit+,
  29. string = (string1 | string2):s -> { s },
  30. //string1 = '\"' (~(lineEnding | '\"') | '\\' nl | nonascii | escape)*:s '\"' -> { '\"' + s.join('') + '\"' },
  31. string1 = '"' letter*:s '"' -> { '"' + s.join('') + '"' },
  32. string2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*:s '\'' -> { '\'' + s.join('') + '\'' },
  33. //invalid = invalid1 | invalid2,
  34. //invalid1 = '\"' (~(lineEnding | '\"') | '\\' nl | nonascii | escape)*,
  35. //invalid2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*,
  36. nl = crChar nlChar
  37. | lineEnding,
  38. D = 'd' | 'D',
  39. E = 'e' | 'E',
  40. N = 'n' | 'N',
  41. O = 'o' | 'O',
  42. T = 't' | 'T',
  43. V = 'v' | 'V',
  44. S = ' ' -> { ' ' }
  45. | '\n' -> { '\n' },
  46. INCLUDES = '~' '=' -> { '~=' },
  47. DASHMATCH = '|' '=' -> { '|=' },
  48. PREFIXMATCH = '^' '=' -> { '^=' },
  49. SUFFIXMATCH = '$' '=' -> { '$=' },
  50. SUBSTRINGMATCH = '*' '=' -> { '*=' },
  51. IDENT = ident,
  52. STRING = string:s -> { s },
  53. FUNCTION = ident:i '(' -> { i + '(' },
  54. NUMBER = num:n -> { n },
  55. HASH = '#' name:n -> { '#' + n },
  56. PLUS = S+ '+' -> { ' +' }
  57. | '+' -> { '+' },
  58. // First line of next selector is a Css Hack
  59. GREATER = '>' '>' -> { '> >' }
  60. | S+ '>' -> { ' >' }
  61. | '>' -> { '>' },
  62. COMMA = S+ ',' -> { ' ,' }
  63. | ',' -> { ',' },
  64. TILDE = S+ '~' -> { ' ~' }
  65. | '~' -> { '~' },
  66. NOT = ':' N O T '(' -> { ':not(' },
  67. ATKEYWORD = '@' ident,
  68. INVALID = invalid,
  69. PERCENTAGE = num:n '%' -> { n + '%' },
  70. DIMENSION = num:n ident:i -> { n + i },
  71. CDO = '<' '!' '-' '-',
  72. CDC = '-' '-' '>',
  73. selectors_group = selector:pre comma_separated_selector*:post -> { self.add(pre + post.join('')); self },
  74. comma_separated_selector = COMMA:com S*:spacing selector:sel -> { com + spacing.join('') + sel },
  75. selector = simple_selector_sequence:sim (combined_sequence)*:additional -> { sim + additional.join('') }
  76. // Css Hack
  77. | combined_sequence*:comb -> { comb.join('') },
  78. combinator = PLUS:p S+ -> { p + ' ' }
  79. | PLUS:p -> { p }
  80. | GREATER:g S+ -> { g + ' ' }
  81. | GREATER:g -> { g }
  82. | TILDE:t S+ -> { t + ' ' }
  83. | TILDE:t -> { t }
  84. | S+:spacing -> { spacing.join('') },
  85. combined_sequence = combinator:comb simple_selector_sequence:sel -> { comb + sel }
  86. | combinator+:comb simple_selector_sequence:sel -> { comb.join('') + sel },
  87. non_namespaced_selector = (HASH | class | attrib | negation | pseudo):sel -> { sel },
  88. simple_selector_sequence = namespace_prefix:pre '*' non_namespaced_selector*:post -> { pre + '*' + post.join('') }
  89. | namespace_prefix:pre element_name:ele non_namespaced_selector*:post -> { pre + ele + post.join('') }
  90. | '*' non_namespaced_selector*:post -> { '*' + post.join('') }
  91. | element_name:ele non_namespaced_selector*:post -> { ele + post.join('') }
  92. | non_namespaced_selector+:sels -> { sels.join('') }
  93. // Css Hack
  94. | expression:ex -> { ex },
  95. namespace_prefix = ('*' | IDENT):pre '|' -> { pre + '|' }
  96. | '|' -> { '|' },
  97. // First line of the next selector is a Css Hack
  98. element_name = IDENT:i '*' -> { i + '*' }
  99. | IDENT:i -> { i },
  100. class = '.' IDENT:i -> { '.' + i },
  101. attrib = '[' S* possible_namespaced_attrib:att ']' -> { '[' + att + ']' },
  102. possible_namespaced_attrib = namespace_prefix:pre ident_with_possible_postfix:post -> { pre + post }
  103. | ident_with_possible_postfix:post -> { post },
  104. ident_with_possible_postfix = IDENT:left S* attrib_match:match S* (IDENT | STRING):right S* -> { left + match + right }
  105. | IDENT:i S* -> { i },
  106. attrib_match = (PREFIXMATCH | SUFFIXMATCH | SUBSTRINGMATCH | equals_match | INCLUDES | DASHMATCH):m -> { m },
  107. equals_match = '=' -> { '=' },
  108. pseudo = ':' ':' (functional_pseudo | IDENT):i -> { '::' + i }
  109. | ':' (functional_pseudo | IDENT):i -> { ':' + i },
  110. functional_pseudo = FUNCTION:f S* full_expression:e ')' -> { f + e + ')' }
  111. // Css Hack for :-moz-any(...)
  112. | FUNCTION:f S* selectors_group:sel ')' -> { f + sel + ')' },
  113. expression_content = (PLUS | '-' | PERCENTAGE | DIMENSION | NUMBER | STRING | IDENT):e -> { e },
  114. expression = expression_content:ec S+ expression:e -> { ec + ' ' + e }
  115. | expression_content:ec expression:e -> { ec + e }
  116. | expression_content:ec S* -> { ec },
  117. full_expression = (expression)+:ea -> { ea.join('') },
  118. negation = NOT:n S* negation_arg:na S* ')' -> { n + na + ')' },
  119. //negation_arg = (type_selector | universal | HASH | class | attrib | pseudo):na -> { na }
  120. //Technically not allowed, but here for scss compatibility
  121. negation_arg = (selectors_group):na -> { na }
  122. }
  123. CssSelector.initialize = function() {
  124. var s;
  125. this.toString = function() {
  126. return s;
  127. };
  128. this.add = function(st) {
  129. s = st;
  130. };
  131. };
  132. console.log(CssSelector.matchAll("E,\nF", "selectors_group").toString());
  133. console.log(CssSelector.matchAll("E\nF", "selectors_group").toString());
  134. console.log(CssSelector.matchAll("E, F\nG, H", "selectors_group").toString());
  135. </script>
  136. <body>
  137. hello world
  138. </body>
  139. </html>