def test_block_media(): block = Block( Comment("comment", False, False), Block(String("hello"), String("There!")), ) block.append(Media("fizz")) assert block.has_properties() is False assert block.has_media() is True
def test_block_properties(): block = Block( Comment("comment", False, False), Block(String("hello"), String("There!")), ) block.append(Property(["foo", "bar"], Expression())) assert block.has_properties() is True assert block.has_media() is False
def test_block_append(): block = Block( Comment("comment", False, False), Block(String("hello"), String("There!")), ) block.append(Literal("Literal")) block.append(true) assert block.nodes == [Literal("Literal"), true] assert block.has_properties() is False assert block.has_media() is False assert block.is_empty() is False
def bubble(self, node: Node): props = [] others = [] def filter_props(block): for node in block.nodes: node = self.visit(node) if node.node_name == "property": props.append(node) elif node.node_name == "block": filter_props(node) else: others.append(node) filter_props(node.block) if props: selector = Selector([Literal("&")]) selector.lineno = node.lineno selector.column = node.column selector.filename = node.filename selector.value = "&" group = Group(lineno=self.parser.lineno, column=self.parser.column) group.lineno = node.lineno group.column = node.column group.filename = node.filename block = Block(node.block, group) block.lineno = node.lineno block.column = node.column block.filename = node.filename for prop in props: block.append(prop) group.append(selector) group.block = block node.block.nodes = [] node.block.append(group) for other in others: node.block.append(other) group = self.closest_group(node.block) if group: node.group = group.clone() node.bubbled = True
def test_atblock(): atblock = Atblock() assert atblock.node_name == "atblock" assert atblock.block is None assert atblock.nodes == [] block = Block(Node(), Node()) block.append(null) block.append(Node()) block.append(true) atblock.block = block assert atblock.nodes == [null, Node(), true]
def visit_import(self, imported): literal = False self.result += 1 path = self.visit(imported.path).first() node_name = "require" if imported.once else "import" self.result -= 1 log.debug(f"import {path}") # url() passed if hasattr(path, "function_name") and path.function_name == "url": if hasattr(imported, "once") and imported.once: raise StilusError("You cannot @require a url") return imported # ensure string if not hasattr(path, "string"): raise StilusError(f"@{node_name} string expected") name = path = path.string # absolute URL or hash m = re.match(r"(?:url\s*\(\s*)?[\'\"]?(?:#|(?:https?:)?\/\/)", path, re.I) if m: if imported.once: raise StilusError("You cannot @require a url") return imported # literal if path.endswith(".css") or '.css"' in path: literal = True if not imported.once and not self.include_css: return imported # support optional .styl if not literal and not path.endswith(".styl"): path += ".styl" # lookup found = utils.find(path, self.paths, self.filename) if not found: found = utils.lookup_index(name, self.paths, self.filename, parser=self.parser) # throw if import failed if not found: raise TypeError(f"failed to locate @{node_name} file in {path}" f" {self.paths}") block = Block(None, None, lineno=self.parser.lineno, column=self.parser.column) for f in found: block.append( self.import_file( imported, f, literal, lineno=self.parser.lineno, column=self.parser.column, )) return block