-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
210 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
class CssStructure: | ||
|
||
def _insert_tokens_before(self, new, existing): | ||
tokens = new.get_tokens() | ||
firsttoken = existing.get_tokens()[0] | ||
self._insert_tokens_before_token(tokens, firsttoken) | ||
|
||
def _insert_tokens_before_token(self, tokens, token): | ||
i = self.tokens.index(token) | ||
for t in reversed(tokens): | ||
self.tokens.insert(i, t) | ||
|
||
def _insert_tokens_after(self, new, existing): | ||
tokens = new.get_tokens() | ||
existingtokens = existing.get_tokens() | ||
lasttoken = existingtokens[-1] | ||
self._insert_tokens_after_token(tokens, lasttoken) | ||
|
||
def _insert_tokens_after_token(self, tokens, token): | ||
if token == self.tokens[-1]: | ||
self.tokens += tokens | ||
else: | ||
i = self.tokens.index(token) + 1 | ||
for t in reversed(tokens): | ||
self.tokens.insert(i, t) | ||
|
||
def __str__(self): | ||
return ''.join([str(t) for t in self.tokens]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from css_parser.rule import Rule | ||
from css_parser.declaration import Declaration | ||
|
||
class TestCases: | ||
|
||
# ---------------------------------------- | ||
|
||
def test_prepend_declaration(self): | ||
tests = [ | ||
# two declarations, add one | ||
{'input': 'body{' \ | ||
' margin:0;' \ | ||
' padding:0;' \ | ||
'}', | ||
'to_add': 'color:blue;', | ||
'existing_index': None, | ||
'expected': 'body{color:blue;' \ | ||
' margin:0;' \ | ||
' padding:0;' \ | ||
'}' | ||
}, | ||
{'input': 'body{' \ | ||
' margin:0;' \ | ||
' padding:0;' \ | ||
'}', | ||
'to_add': 'color:blue;', | ||
'existing_index': 0, | ||
'expected': 'body{' \ | ||
' color:blue;margin:0;' \ | ||
' padding:0;' \ | ||
'}' | ||
} | ||
] | ||
|
||
for test in tests: | ||
yield self.check_prepend_declaration, test | ||
|
||
def check_prepend_declaration(self, test): | ||
rule = Rule.from_string(test['input']) | ||
decl = Declaration.from_string(test['to_add']) | ||
existing_rule = None | ||
if test['existing_index'] != None: | ||
existing_rule = rule.declarations[test['existing_index']] | ||
rule.prepend_declaration(decl, existing_rule) | ||
observed = rule.to_string() | ||
for k in test: | ||
print '%s: %s' % (k, test[k]) | ||
print 'observed: %s' % (observed) | ||
assert observed == test['expected'] | ||
|
||
# ---------------------------------------- | ||
|
||
|
||
|
||
|
||
|