def test_get_raises_exception(self): with self.assertRaises(ConfigException): self.assertEqual( Options.get(Options.map({ 'BOLD': 'strong', }, 'FALLBACK'), 'ITALIC', 'FALLBACK'), Options('BOLD', 'strong'))
def render_entities(self, style_node): # We have a complete (start, stop) entity to render. if self.completed_entity is not None: entity_details = self.get_entity_details(self.completed_entity) options = Options.get(self.entity_options, entity_details['type'], ENTITY_TYPES.FALLBACK) props = entity_details['data'].copy() props['entity'] = { 'type': entity_details['type'], } if len(self.element_stack) == 1: children = self.element_stack[0] else: children = DOM.create_element() for n in self.element_stack: DOM.append_child(children, n) self.completed_entity = None self.element_stack = [] # Is there still another entity? (adjacent) if so add the current style_node for it. if self.has_entity(): self.element_stack.append(style_node) return DOM.create_element(options.element, props, children) if self.has_entity(): self.element_stack.append(style_node) return None return style_node
def test_get_uses_fallback(self): self.assertEqual( Options.get( Options.map({ 'BOLD': 'strong', 'FALLBACK': 'span', }, 'FALLBACK'), 'ITALIC', 'FALLBACK'), Options('FALLBACK', 'span'))
def test_get_raises_exception(self): with self.assertRaises(ConfigException): self.assertEqual( Options.get( Options.map({"BOLD": "strong"}, "FALLBACK"), "ITALIC", "FALLBACK", ), Options("BOLD", "strong"), )
def test_get_uses_fallback(self): self.assertEqual( Options.get( Options.map({ "BOLD": "strong", "FALLBACK": "span" }, "FALLBACK"), "ITALIC", "FALLBACK", ), Options("FALLBACK", "span"), )
def element_for(self, block, block_content): type_ = block['type'] if 'type' in block else 'unstyled' depth = block['depth'] if 'depth' in block else 0 options = Options.get(self.block_options, type_, BLOCK_TYPES.FALLBACK) props = dict(options.props) props['block'] = block props['blocks'] = self.blocks # Make an element from the options specified in the block map. elt = DOM.create_element(options.element, props, block_content) parent = self.parent_for(options, depth, elt) return parent
def render_entities(self, style_node: Element, block: Block, blocks: Sequence[Block]) -> Element: # We have a complete (start, stop) entity to render. if self.completed_entity is not None: entity_details = self.get_entity_details(self.completed_entity) options = Options.get( self.entity_options, entity_details["type"], ENTITY_TYPES.FALLBACK, ) props = entity_details["data"].copy() props["entity"] = { "type": entity_details["type"], "mutability": entity_details["mutability"] if "mutability" in entity_details else None, "block": block, "blocks": blocks, "entity_range": { "key": self.completed_entity }, } if len(self.element_stack) == 1: children = self.element_stack[0] else: children = DOM.create_element() for n in self.element_stack: DOM.append_child(children, n) self.completed_entity = None self.element_stack = [] # Is there still another entity? (adjacent) if so add the current style_node for it. if self.has_entity(): self.element_stack.append(style_node) return DOM.create_element(options.element, props, children) if self.has_entity(): self.element_stack.append(style_node) return None return style_node
def test_get_works(self): self.assertEqual( Options.get( Options.map( { 'BOLD': 'strong', 'HIGHLIGHT': { 'element': 'strong', 'props': { 'style': { 'textDecoration': 'underline' } }, }, }, 'FALLBACK'), 'BOLD', 'FALLBACK'), Options('BOLD', 'strong'))
def render_styles(self, decorated_node, block, blocks): node = decorated_node if not self.is_empty(): # Nest the tags. for style in sorted(self.styles, reverse=True): options = Options.get(self.style_options, style, INLINE_STYLES.FALLBACK) props = dict(options.props) props['block'] = block props['blocks'] = blocks props['inline_style_range'] = { 'style': style, } node = DOM.create_element(options.element, props, node) return node
def element_for( self, block: Block, block_content: Union[Element, Sequence[Element]]) -> Element: type_ = block["type"] if "type" in block else "unstyled" depth = block["depth"] if "depth" in block else 0 options = Options.get(self.block_options, type_, BLOCK_TYPES.FALLBACK) props = dict(options.props) props["block"] = block props["blocks"] = self.blocks # Make an element from the options specified in the block map. elt = DOM.create_element(options.element, props, block_content) parent = self.parent_for(options, depth, elt) return parent
def render_styles(self, decorated_node: Element, block: Block, blocks: Sequence[Block]) -> Element: node = decorated_node if not self.is_empty(): # This will mutate self.styles, but it’s going to be reset after rendering anyway. self.styles.sort(reverse=True) # Nest the tags. for style in self.styles: options = Options.get(self.style_options, style, INLINE_STYLES.FALLBACK) props = dict(options.props) props["block"] = block props["blocks"] = blocks props["inline_style_range"] = {"style": style} node = DOM.create_element(options.element, props, node) return node
def test_get_works(self): self.assertEqual( Options.get( Options.map( { "BOLD": "strong", "HIGHLIGHT": { "element": "strong", "props": { "style": { "textDecoration": "underline" } }, }, }, "FALLBACK", ), "BOLD", "FALLBACK", ), Options("BOLD", "strong"), )