def test_render_styles_data(self): blocks = [ { 'key': '5s7g9', 'text': 'test', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [], 'entityRanges': [], }, ] def component(props): self.assertEqual(props['blocks'], blocks) self.assertEqual(props['block'], blocks[0]) self.assertEqual(props['inline_style_range']['style'], 'ITALIC') return None style_state = StyleState({ 'ITALIC': component, }) style_state.apply(Command('start_inline_style', 0, 'ITALIC')) style_state.render_styles('Test text', blocks[0], blocks) style_state.apply(Command('stop_inline_style', 9, 'ITALIC'))
def render_block(self, block, entity_map, wrapper_state): if block['inlineStyleRanges'] or block['entityRanges']: content = DOM.create_element() entity_state = EntityState(self.entity_decorators, entity_map) style_state = StyleState(self.style_map) for (text, commands) in self.build_command_groups(block): for command in commands: entity_state.apply(command) style_state.apply(command) # Decorators are not rendered inside entities. if entity_state.has_no_entity() and self.has_decorators: decorated_node = render_decorators(self.composite_decorators, text, block, wrapper_state.blocks) else: decorated_node = text styled_node = style_state.render_styles(decorated_node, block, wrapper_state.blocks) entity_node = entity_state.render_entities(styled_node) if entity_node is not None: DOM.append_child(content, entity_node) # Check whether there actually are two different nodes, confirming we are not inserting an upcoming entity. if styled_node != entity_node and entity_state.has_no_entity(): DOM.append_child(content, styled_node) # Fast track for blocks which do not contain styles nor entities, which is very common. elif self.has_decorators: content = render_decorators(self.composite_decorators, block['text'], block, wrapper_state.blocks) else: content = block['text'] return wrapper_state.element_for(block, content)
def render_block(self, block, entity_map, wrapper_state): content = DOM.create_element() entity_state = EntityState(self.entity_decorators, entity_map) style_state = StyleState(self.style_map) for (text, commands) in self.build_command_groups(block): for command in commands: entity_state.apply(command) style_state.apply(command) # Decorators are not rendered inside entities. if text and entity_state.has_no_entity() and len( self.composite_decorators) > 0: decorated_node = render_decorators(self.composite_decorators, text, block) else: decorated_node = text styled_node = style_state.render_styles(decorated_node) entity_node = entity_state.render_entities(styled_node) if entity_node is not None: DOM.append_child(content, entity_node) if styled_node != entity_node: DOM.append_child(content, styled_node) return wrapper_state.element_for(block, content)
def render_block(self, block: Block, entity_map: EntityMap, wrapper_state: WrapperState) -> Element: has_styles = "inlineStyleRanges" in block and block["inlineStyleRanges"] has_entities = "entityRanges" in block and block["entityRanges"] has_decorators = should_render_decorators(self.composite_decorators, block["text"]) if has_styles or has_entities: content = DOM.create_element() entity_state = EntityState(self.entity_options, entity_map) style_state = StyleState( self.style_options) if has_styles else None for (text, commands) in self.build_command_groups(block): for command in commands: entity_state.apply(command) if style_state: style_state.apply(command) # Decorators are not rendered inside entities. if has_decorators and entity_state.has_no_entity(): decorated_node = render_decorators( self.composite_decorators, text, block, wrapper_state.blocks, ) else: decorated_node = text if style_state: styled_node = style_state.render_styles( decorated_node, block, wrapper_state.blocks) else: styled_node = decorated_node entity_node = entity_state.render_entities( styled_node, block, wrapper_state.blocks) if entity_node is not None: DOM.append_child(content, entity_node) # Check whether there actually are two different nodes, confirming we are not inserting an upcoming entity. if (styled_node != entity_node and entity_state.has_no_entity()): DOM.append_child(content, styled_node) # Fast track for blocks which do not contain styles nor entities, which is very common. elif has_decorators: content = render_decorators( self.composite_decorators, block["text"], block, wrapper_state.blocks, ) else: content = block["text"] return wrapper_state.element_for(block, content)
def test_render_styles_data(self): blocks = [{ "key": "5s7g9", "text": "test", "type": "unstyled", "depth": 0, "inlineStyleRanges": [], "entityRanges": [], }] def component(props): self.assertEqual(props["blocks"], blocks) self.assertEqual(props["block"], blocks[0]) self.assertEqual(props["inline_style_range"]["style"], "ITALIC") return None style_state = StyleState(Options.map_styles({"ITALIC": component})) style_state.apply(Command("start_inline_style", 0, "ITALIC")) style_state.render_styles("Test text", blocks[0], blocks) style_state.apply(Command("stop_inline_style", 9, "ITALIC"))
def render_block(self, block, entity_map, wrapper_state): if 'inlineStyleRanges' in block and block[ 'inlineStyleRanges'] or 'entityRanges' in block and block[ 'entityRanges']: content = DOM.create_element() entity_state = EntityState(self.entity_options, entity_map) style_state = StyleState(self.style_options) for (text, commands) in self.build_command_groups(block): for command in commands: entity_state.apply(command) style_state.apply(command) # Decorators are not rendered inside entities. if entity_state.has_no_entity() and self.has_decorators: decorated_node = render_decorators( self.composite_decorators, text, block, wrapper_state.blocks) else: decorated_node = text styled_node = style_state.render_styles( decorated_node, block, wrapper_state.blocks) entity_node = entity_state.render_entities(styled_node) if entity_node is not None: DOM.append_child(content, entity_node) # Check whether there actually are two different nodes, confirming we are not inserting an upcoming entity. if styled_node != entity_node and entity_state.has_no_entity( ): DOM.append_child(content, styled_node) # Fast track for blocks which do not contain styles nor entities, which is very common. elif self.has_decorators: content = render_decorators(self.composite_decorators, block['text'], block, wrapper_state.blocks) else: content = block['text'] return wrapper_state.element_for(block, content)
def render_block(self, block, entity_map, wrapper_state): content = DOM.create_element() if block['inlineStyleRanges'] or block['entityRanges']: entity_state = EntityState(self.entity_decorators, entity_map) style_state = StyleState(self.style_map) for (text, commands) in self.build_command_groups(block): for command in commands: entity_state.apply(command) style_state.apply(command) # Decorators are not rendered inside entities. if text and entity_state.has_no_entity() and len( self.composite_decorators) > 0: decorated_node = render_decorators( self.composite_decorators, text, block, wrapper_state.blocks) else: decorated_node = text styled_node = style_state.render_styles( decorated_node, block, wrapper_state.blocks) entity_node = entity_state.render_entities(styled_node) if entity_node is not None: DOM.append_child(content, entity_node) if styled_node != entity_node: DOM.append_child(content, styled_node) # Fast track for blocks which do not contain styles nor entities, which is very common. else: if len(self.composite_decorators) > 0: decorated_node = render_decorators(self.composite_decorators, block['text'], block, wrapper_state.blocks) else: decorated_node = block['text'] DOM.append_child(content, decorated_node) return wrapper_state.element_for(block, content)
class TestStyleState(unittest.TestCase): def setUp(self): DOM.use(DOM.STRING) self.style_state = StyleState( Options.map_styles(style_map) # type: ignore ) def test_init(self): self.assertIsInstance(self.style_state, StyleState) def test_apply_start_inline_style(self): self.style_state.apply(Command("start_inline_style", 0, "ITALIC")) self.assertEqual(self.style_state.styles, ["ITALIC"]) def test_apply_stop_inline_style(self): self.style_state.apply(Command("start_inline_style", 0, "ITALIC")) self.style_state.apply(Command("stop_inline_style", 0, "ITALIC")) self.assertEqual(self.style_state.styles, []) def test_is_empty_default(self): self.assertEqual(self.style_state.is_empty(), True) def test_is_empty_styled(self): self.style_state.apply(Command("start_inline_style", 0, "ITALIC")) self.assertEqual(self.style_state.is_empty(), False) def test_render_styles_unstyled(self): self.assertEqual(self.style_state.render_styles("Test text", {}, []), "Test text") def test_render_styles_unicode(self): self.assertEqual(self.style_state.render_styles("🍺", {}, []), "🍺") def test_render_styles_styled(self): self.style_state.apply(Command("start_inline_style", 0, "ITALIC")) self.assertEqual( DOM.render_debug( self.style_state.render_styles("Test text", {}, [])), "<em>Test text</em>", ) self.style_state.apply(Command("stop_inline_style", 9, "ITALIC")) def test_render_styles_styled_multiple(self): self.style_state.apply(Command("start_inline_style", 0, "BOLD")) self.style_state.apply(Command("start_inline_style", 0, "ITALIC")) self.assertEqual( DOM.render_debug( self.style_state.render_styles("Test text", {}, [])), "<strong><em>Test text</em></strong>", ) def test_render_styles_attributes(self): self.style_state.apply(Command("start_inline_style", 0, "KBD")) self.assertEqual( DOM.render_debug( self.style_state.render_styles("Test text", {}, [])), '<kbd class="o-keyboard-shortcut">Test text</kbd>', ) self.style_state.apply(Command("stop_inline_style", 9, "KBD")) def test_render_styles_component(self): self.style_state.apply(Command("start_inline_style", 0, "IMPORTANT")) self.assertEqual( DOM.render_debug( self.style_state.render_styles("Test text", {}, [])), '<strong style="color: red;">Test text</strong>', ) self.style_state.apply(Command("stop_inline_style", 9, "IMPORTANT")) def test_render_styles_component_multiple(self): self.style_state.apply(Command("start_inline_style", 0, "IMPORTANT")) self.style_state.apply(Command("start_inline_style", 0, "SHOUT")) self.assertEqual( DOM.render_debug( self.style_state.render_styles("Test text", {}, [])), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>', ) self.style_state.apply(Command("stop_inline_style", 9, "IMPORTANT")) self.style_state.apply(Command("stop_inline_style", 9, "SHOUT")) def test_render_styles_component_multiple_invert(self): self.style_state.apply(Command("start_inline_style", 0, "SHOUT")) self.style_state.apply(Command("start_inline_style", 0, "IMPORTANT")) self.assertEqual( DOM.render_debug( self.style_state.render_styles("Test text", {}, [])), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>', ) self.style_state.apply(Command("stop_inline_style", 9, "SHOUT")) self.style_state.apply(Command("stop_inline_style", 9, "IMPORTANT")) def test_render_styles_data(self): blocks = [{ "key": "5s7g9", "text": "test", "type": "unstyled", "depth": 0, "inlineStyleRanges": [], "entityRanges": [], }] def component(props): self.assertEqual(props["blocks"], blocks) self.assertEqual(props["block"], blocks[0]) self.assertEqual(props["inline_style_range"]["style"], "ITALIC") return None style_state = StyleState(Options.map_styles({"ITALIC": component})) style_state.apply(Command("start_inline_style", 0, "ITALIC")) style_state.render_styles("Test text", blocks[0], blocks) style_state.apply(Command("stop_inline_style", 9, "ITALIC"))
class TestStyleState(unittest.TestCase): def setUp(self): DOM.use(DOM.STRING) self.style_state = StyleState(style_map) def test_init(self): self.assertIsInstance(self.style_state, StyleState) def test_apply_start_inline_style(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.styles, ['ITALIC']) def test_apply_stop_inline_style(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.style_state.apply(Command('stop_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.styles, []) def test_is_empty_default(self): self.assertEqual(self.style_state.is_empty(), True) def test_is_empty_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.is_empty(), False) def test_render_styles_unstyled(self): self.assertEqual(self.style_state.render_styles('Test text'), 'Test text') def test_render_styles_unicode(self): self.assertEqual(self.style_state.render_styles('🍺'), '🍺') def test_render_styles_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<em>Test text</em>') self.style_state.apply(Command('stop_inline_style', 9, 'ITALIC')) def test_render_styles_styled_multiple(self): self.style_state.apply(Command('start_inline_style', 0, 'BOLD')) self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<strong><em>Test text</em></strong>') def test_render_styles_attributes(self): self.style_state.apply(Command('start_inline_style', 0, 'KBD')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<kbd class="o-keyboard-shortcut">Test text</kbd>') self.style_state.apply(Command('stop_inline_style', 9, 'KBD')) def test_render_styles_component(self): self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<strong style="color: red;">Test text</strong>') self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) def test_render_styles_component_multiple(self): self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.style_state.apply(Command('start_inline_style', 0, 'SHOUT')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>') self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT')) def test_render_styles_component_multiple_invert(self): self.style_state.apply(Command('start_inline_style', 0, 'SHOUT')) self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>') self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT')) self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT'))
class TestStyleState(unittest.TestCase): def setUp(self): DOM.use(DOM.STRING) self.style_state = StyleState(style_map) def test_init(self): self.assertIsInstance(self.style_state, StyleState) def test_apply_start_inline_style(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.styles, ['ITALIC']) def test_apply_stop_inline_style(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.style_state.apply(Command('stop_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.styles, []) def test_is_empty_default(self): self.assertEqual(self.style_state.is_empty(), True) def test_is_empty_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.is_empty(), False) def test_render_styles_unstyled(self): self.assertEqual(self.style_state.render_styles('Test text', {}, []), 'Test text') def test_render_styles_unicode(self): self.assertEqual(self.style_state.render_styles('🍺', {}, []), '🍺') def test_render_styles_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual( DOM.render_debug( self.style_state.render_styles('Test text', {}, [])), '<em>Test text</em>') self.style_state.apply(Command('stop_inline_style', 9, 'ITALIC')) def test_render_styles_styled_multiple(self): self.style_state.apply(Command('start_inline_style', 0, 'BOLD')) self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual( DOM.render_debug( self.style_state.render_styles('Test text', {}, [])), '<strong><em>Test text</em></strong>') def test_render_styles_attributes(self): self.style_state.apply(Command('start_inline_style', 0, 'KBD')) self.assertEqual( DOM.render_debug( self.style_state.render_styles('Test text', {}, [])), '<kbd class="o-keyboard-shortcut">Test text</kbd>') self.style_state.apply(Command('stop_inline_style', 9, 'KBD')) def test_render_styles_component(self): self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.assertEqual( DOM.render_debug( self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;">Test text</strong>') self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) def test_render_styles_component_multiple(self): self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.style_state.apply(Command('start_inline_style', 0, 'SHOUT')) self.assertEqual( DOM.render_debug( self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>' ) self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT')) def test_render_styles_component_multiple_invert(self): self.style_state.apply(Command('start_inline_style', 0, 'SHOUT')) self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.assertEqual( DOM.render_debug( self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>' ) self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT')) self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) def test_render_styles_data(self): blocks = [ { 'key': '5s7g9', 'text': 'test', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [], 'entityRanges': [], }, ] def component(props): self.assertEqual(props['blocks'], blocks) self.assertEqual(props['block'], blocks[0]) self.assertEqual(props['inline_style_range']['style'], 'ITALIC') return None style_state = StyleState({ 'ITALIC': component, }) style_state.apply(Command('start_inline_style', 0, 'ITALIC')) style_state.render_styles('Test text', blocks[0], blocks) style_state.apply(Command('stop_inline_style', 9, 'ITALIC'))
class TestStyleState(unittest.TestCase): def setUp(self): DOM.use(DOM.STRING) self.style_state = StyleState(style_map) def test_init(self): self.assertIsInstance(self.style_state, StyleState) def test_apply_start_inline_style(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.styles, ['ITALIC']) def test_apply_stop_inline_style(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.style_state.apply(Command('stop_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.styles, []) def test_is_empty_default(self): self.assertEqual(self.style_state.is_empty(), True) def test_is_empty_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(self.style_state.is_empty(), False) def test_render_styles_unstyled(self): self.assertEqual(self.style_state.render_styles('Test text', {}, []), 'Test text') def test_render_styles_unicode(self): self.assertEqual(self.style_state.render_styles('🍺', {}, []), '🍺') def test_render_styles_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<em>Test text</em>') self.style_state.apply(Command('stop_inline_style', 9, 'ITALIC')) def test_render_styles_styled_multiple(self): self.style_state.apply(Command('start_inline_style', 0, 'BOLD')) self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<strong><em>Test text</em></strong>') def test_render_styles_attributes(self): self.style_state.apply(Command('start_inline_style', 0, 'KBD')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<kbd class="o-keyboard-shortcut">Test text</kbd>') self.style_state.apply(Command('stop_inline_style', 9, 'KBD')) def test_render_styles_component(self): self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;">Test text</strong>') self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) def test_render_styles_component_multiple(self): self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.style_state.apply(Command('start_inline_style', 0, 'SHOUT')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>') self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT')) def test_render_styles_component_multiple_invert(self): self.style_state.apply(Command('start_inline_style', 0, 'SHOUT')) self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT')) self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;"><span style="text-transform: uppercase;">Test text</span></strong>') self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT')) self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT')) def test_render_styles_data(self): blocks = [ { 'key': '5s7g9', 'text': 'test', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [], 'entityRanges': [], }, ] def component(props): self.assertEqual(props['blocks'], blocks) self.assertEqual(props['block'], blocks[0]) self.assertEqual(props['inline_style_range']['style'], 'ITALIC') return None style_state = StyleState({ 'ITALIC': component, }) style_state.apply(Command('start_inline_style', 0, 'ITALIC')) style_state.render_styles('Test text', blocks[0], blocks) style_state.apply(Command('stop_inline_style', 9, 'ITALIC'))