Browse files

Implemented Selectors with Newlines

  • Loading branch information...
1 parent d8f23f0 commit 75a0f0cf113c5e3c84455a96321d8bba0dfdb8d4 @bmavity committed Sep 25, 2010
Showing with 12 additions and 222 deletions.
  1. +2 −2 tests/test.js → specs/cssSelectorSpecs.js
  2. +6 −0 {tests → specs}/grammarInvoker.js
  3. +4 −6 src/cssSelector.ometa
  4. +0 −214 tests/selectors.js
View
4 tests/test.js → specs/cssSelectorSpecs.js
@@ -217,13 +217,13 @@ vows.describe('Comma Selectors').addBatch({
'E > F, G > H': shouldParse()
}).run();
-/*
+
vows.describe('Selectors with Newlines').addBatch({
"E,\nF": shouldParse(),
"E\nF": shouldParse(),
"E, F\nG, H": shouldParse()
}).run();
-*/
+
vows.describe('Expression Fallback Selectors').addBatch({
'0%': shouldParse(),
View
6 tests/grammarInvoker.js → specs/grammarInvoker.js
@@ -12,6 +12,7 @@ var createParser = function(parserInitializer) {
} else {
ometa.createParser(contents.toString(), parserInitializer.setParser);
}
+ creatingParser = false;
});
}
};
@@ -40,6 +41,10 @@ var parserInitializer = (function() {
}
};
+ that.reset = function() {
+ createdParser = null;
+ };
+
return that;
})();
@@ -61,3 +66,4 @@ var parse = function(selector, callback) {
module.exports.parse = parse;
+module.exports.reset = parserInitializer.reset;
View
10 src/cssSelector.ometa
@@ -1,5 +1,3 @@
-var sys = require('sys');
-
ometa CssSelector <: Parser {
crChar = '\r',
ffChar = '\f',
@@ -28,7 +26,6 @@ ometa CssSelector <: Parser {
//invalid2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*,
nl = crChar nlChar
| lineEnding,
- w = (' ' | tabOrLineEnding)*,
D = 'd' | 'D',
E = 'e' | 'E',
N = 'n' | 'N',
@@ -37,7 +34,8 @@ ometa CssSelector <: Parser {
V = 'v' | 'V',
- S = ' ' | tabOrLineEnding,
+ S = ' ' -> { ' ' }
+ | '\n' -> { '\n' },
INCLUDES = '~' '=' -> { '~=' },
DASHMATCH = '|' '=' -> { '|=' },
PREFIXMATCH = '^' '=' -> { '^=' },
@@ -68,7 +66,7 @@ ometa CssSelector <: Parser {
selectors_group = selector:pre comma_separated_selector*:post -> { self.add(pre + post.join('')); self },
- comma_separated_selector = COMMA S* selector:sel -> { ', ' + sel },
+ comma_separated_selector = COMMA:com S*:spacing selector:sel -> { com + spacing.join('') + sel },
selector = simple_selector_sequence:sim (combined_sequence)*:additional -> { sim + additional.join('') }
// Css Hack
| combined_sequence*:comb -> { comb.join('') },
@@ -78,7 +76,7 @@ ometa CssSelector <: Parser {
| GREATER:g -> { g }
| TILDE:t S+ -> { t + ' ' }
| TILDE:t -> { t }
- | S+ -> { ' ' },
+ | S+:spacing -> { spacing.join('') },
combined_sequence = combinator:comb simple_selector_sequence:sel -> { comb + sel }
| combinator+:comb simple_selector_sequence:sel -> { comb.join('') + sel },
non_namespaced_selector = (HASH | class | attrib | negation | pseudo):sel -> { sel },
View
214 tests/selectors.js
@@ -1,214 +0,0 @@
-## Selectors
-
- # Taken from http://www.w3.org/TR/css3-selectors/#selectors
- def test_summarized_selectors
- assert_selector_parses('*')
- assert_selector_parses('E')
- assert_selector_parses('E[foo]')
- assert_selector_parses('E[foo="bar"]')
- assert_selector_parses('E[foo~="bar"]')
- assert_selector_parses('E[foo^="bar"]')
- assert_selector_parses('E[foo$="bar"]')
- assert_selector_parses('E[foo*="bar"]')
- assert_selector_parses('E[foo|="en"]')
- assert_selector_parses('E:root')
- assert_selector_parses('E:nth-child(n)')
- assert_selector_parses('E:nth-last-child(n)')
- assert_selector_parses('E:nth-of-type(n)')
- assert_selector_parses('E:nth-last-of-type(n)')
- assert_selector_parses('E:first-child')
- assert_selector_parses('E:last-child')
- assert_selector_parses('E:first-of-type')
- assert_selector_parses('E:last-of-type')
- assert_selector_parses('E:only-child')
- assert_selector_parses('E:only-of-type')
- assert_selector_parses('E:empty')
- assert_selector_parses('E:link')
- assert_selector_parses('E:visited')
- assert_selector_parses('E:active')
- assert_selector_parses('E:hover')
- assert_selector_parses('E:focus')
- assert_selector_parses('E:target')
- assert_selector_parses('E:lang(fr)')
- assert_selector_parses('E:enabled')
- assert_selector_parses('E:disabled')
- assert_selector_parses('E:checked')
- assert_selector_parses('E::first-line')
- assert_selector_parses('E::first-letter')
- assert_selector_parses('E::before')
- assert_selector_parses('E::after')
- assert_selector_parses('E.warning')
- assert_selector_parses('E#myid')
- assert_selector_parses('E:not(s)')
- assert_selector_parses('E F')
- assert_selector_parses('E > F')
- assert_selector_parses('E + F')
- assert_selector_parses('E ~ F')
- end
-
- # Taken from http://www.w3.org/TR/css3-selectors/#selectors,
- # but without the element names
- def test_lonely_selectors
- assert_selector_parses('[foo]')
- assert_selector_parses('[foo="bar"]')
- assert_selector_parses('[foo~="bar"]')
- assert_selector_parses('[foo^="bar"]')
- assert_selector_parses('[foo$="bar"]')
- assert_selector_parses('[foo*="bar"]')
- assert_selector_parses('[foo|="en"]')
- assert_selector_parses(':root')
- assert_selector_parses(':nth-child(n)')
- assert_selector_parses(':nth-last-child(n)')
- assert_selector_parses(':nth-of-type(n)')
- assert_selector_parses(':nth-last-of-type(n)')
- assert_selector_parses(':first-child')
- assert_selector_parses(':last-child')
- assert_selector_parses(':first-of-type')
- assert_selector_parses(':last-of-type')
- assert_selector_parses(':only-child')
- assert_selector_parses(':only-of-type')
- assert_selector_parses(':empty')
- assert_selector_parses(':link')
- assert_selector_parses(':visited')
- assert_selector_parses(':active')
- assert_selector_parses(':hover')
- assert_selector_parses(':focus')
- assert_selector_parses(':target')
- assert_selector_parses(':lang(fr)')
- assert_selector_parses(':enabled')
- assert_selector_parses(':disabled')
- assert_selector_parses(':checked')
- assert_selector_parses('::first-line')
- assert_selector_parses('::first-letter')
- assert_selector_parses('::before')
- assert_selector_parses('::after')
- assert_selector_parses('.warning')
- assert_selector_parses('#myid')
- assert_selector_parses(':not(s)')
- end
-
- def test_attribute_selectors_with_identifiers
- assert_selector_parses('[foo~=bar]')
- assert_selector_parses('[foo^=bar]')
- assert_selector_parses('[foo$=bar]')
- assert_selector_parses('[foo*=bar]')
- assert_selector_parses('[foo|=en]')
- end
-
- def test_nth_selectors
- assert_selector_parses(':nth-child(-n)')
- assert_selector_parses(':nth-child(+n)')
-
- assert_selector_parses(':nth-child(even)')
- assert_selector_parses(':nth-child(odd)')
-
- assert_selector_parses(':nth-child(50)')
- assert_selector_parses(':nth-child(-50)')
- assert_selector_parses(':nth-child(+50)')
-
- assert_selector_parses(':nth-child(2n+3)')
- assert_selector_parses(':nth-child(2n-3)')
- assert_selector_parses(':nth-child(+2n-3)')
- assert_selector_parses(':nth-child(-2n+3)')
- assert_selector_parses(':nth-child(-2n+ 3)')
-
- assert_equal(<<CSS, render(<<SCSS))
-:nth-child(2n + 3) {
-a: b; }
-CSS
-:nth-child( 2n + 3 ) {
-a: b; }
-SCSS
- end
-
- def test_negation_selectors
- assert_selector_parses(':not(foo|bar)')
- assert_selector_parses(':not(*|bar)')
-
- assert_selector_parses(':not(foo|*)')
- assert_selector_parses(':not(*|*)')
-
- assert_selector_parses(':not(#blah)')
- assert_selector_parses(':not(.blah)')
-
- assert_selector_parses(':not([foo])')
- assert_selector_parses(':not([foo^="bar"])')
- assert_selector_parses(':not([baz|foo~="bar"])')
-
- assert_selector_parses(':not(:hover)')
- assert_selector_parses(':not(:nth-child(2n + 3))')
-
- # Not technically allowed, but what the heck
- assert_selector_parses(':not(:not(#foo))')
- assert_selector_parses(':not(a#foo.bar)')
- assert_selector_parses(':not(#foo .bar > baz)')
- assert_selector_parses(':not(h1, h2, h3)')
- end
-
- def test_moz_any_selector
- assert_selector_parses(':-moz-any(h1, h2, h3)')
- assert_selector_parses(':-moz-any(.foo)')
- assert_selector_parses(':-moz-any(foo bar, .baz > .bang)')
- end
-
- def test_namespaced_selectors
- assert_selector_parses('foo|E')
- assert_selector_parses('*|E')
- assert_selector_parses('foo|*')
- assert_selector_parses('*|*')
- end
-
- def test_namespaced_attribute_selectors
- assert_selector_parses('[foo|bar=baz]')
- assert_selector_parses('[*|bar=baz]')
- assert_selector_parses('[foo|bar|=baz]')
- end
-
- def test_comma_selectors
- assert_selector_parses('E, F')
- assert_selector_parses('E F, G H')
- assert_selector_parses('E > F, G > H')
- end
-
- def test_selectors_with_newlines
- assert_selector_parses("E,\nF")
- assert_selector_parses("E\nF")
- assert_selector_parses("E, F\nG, H")
- end
-
- def test_expression_fallback_selectors
- assert_selector_parses('0%')
- assert_selector_parses('60%')
- assert_selector_parses('100%')
- assert_selector_parses('12px')
- assert_selector_parses('"foo"')
- end
-
- def test_functional_pseudo_selectors
- assert_selector_parses(':foo("bar")')
- assert_selector_parses(':foo(bar)')
- assert_selector_parses(':foo(12px)')
- assert_selector_parses(':foo(+)')
- assert_selector_parses(':foo(-)')
- assert_selector_parses(':foo(+"bar")')
- assert_selector_parses(':foo(-++--baz-"bar"12px)')
- end
-
- def test_selector_hacks
- assert_selector_parses('> E')
- assert_selector_parses('+ E')
- assert_selector_parses('~ E')
- assert_selector_parses('> > E')
- assert_equal <<CSS, render(<<SCSS)
-> > E {
-a: b; }
-CSS
->> E {
-a: b; }
-SCSS
-
- assert_selector_parses('E*')
- assert_selector_parses('E*.foo')
- assert_selector_parses('E*:hover')
- end
-

0 comments on commit 75a0f0c

Please sign in to comment.