def test_build_style_commands_multiple(self): self.assertEqual( str( self.exporter.build_style_commands({ 'key': '5s7g9', 'text': 'Header', 'type': 'header-one', 'depth': 0, 'inlineStyleRanges': [{ 'offset': 0, 'length': 4, 'style': 'ITALIC' }, { 'offset': 9, 'length': 3, 'style': 'BOLD' }], 'entityRanges': [] })), str([ Command('start_inline_style', 0, 'ITALIC'), Command('stop_inline_style', 4, 'ITALIC'), Command('start_inline_style', 9, 'BOLD'), Command('stop_inline_style', 12, 'BOLD'), ]))
def test_render_entities_data(self): blocks = [ { 'key': '5s7g9', 'text': 'test', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [], 'entityRanges': [], }, ] def component(props): self.assertEqual(props['entity']['blocks'], blocks) self.assertEqual(props['entity']['block'], blocks[0]) self.assertEqual(props['entity']['type'], 'LINK') self.assertEqual(props['entity']['mutability'], 'MUTABLE') self.assertEqual(props['entity']['entity_range']['key'], '0') return None entity_state = EntityState(Options.map_entities({ 'LINK': component, }), entity_map) entity_state.apply(Command('start_entity', 0, '0')) entity_state.render_entities('Test text', blocks[0], blocks) entity_state.apply(Command('stop_entity', 9, '0')) entity_state.render_entities('Test text', blocks[0], blocks)
def test_build_entity_commands_multiple(self): self.assertEqual( str( self.exporter.build_entity_commands({ 'key': 'dem5p', 'text': 'some paragraph text', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [], 'entityRanges': [{ 'offset': 5, 'length': 9, 'key': 0 }, { 'offset': 0, 'length': 4, 'key': 1 }] })), str([ Command('start_entity', 5, 0), Command('stop_entity', 14, 0), Command('start_entity', 0, 1), Command('stop_entity', 4, 1), ]))
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_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_entities_data(self): blocks = [{ "key": "5s7g9", "text": "test", "type": "unstyled", "depth": 0, "inlineStyleRanges": [], "entityRanges": [], }] def component(props): self.assertEqual(props["entity"]["blocks"], blocks) self.assertEqual(props["entity"]["block"], blocks[0]) self.assertEqual(props["entity"]["type"], "LINK") self.assertEqual(props["entity"]["mutability"], "MUTABLE") self.assertEqual(props["entity"]["entity_range"]["key"], "0") return None entity_state = EntityState(Options.map_entities({"LINK": component}), entity_map) entity_state.apply(Command("start_entity", 0, "0")) entity_state.render_entities("Test text", blocks[0], blocks) entity_state.apply(Command("stop_entity", 9, "0")) entity_state.render_entities("Test text", blocks[0], blocks)
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 test_apply_stop_entity(self): self.assertEqual(DOM.get_tag_name(self.entity_state.entity_stack[-1][0]), 'fragment') self.assertEqual(self.entity_state.entity_stack[-1][1], {}) self.entity_state.apply(Command('start_entity', 0, 0)) self.entity_state.apply(Command('stop_entity', 5, 0)) self.assertEqual(DOM.get_tag_name(self.entity_state.entity_stack[-1][0]), 'fragment') self.assertEqual(self.entity_state.entity_stack[-1][1], {})
def test_start_stop(self): self.assertEqual( str(Command.start_stop('abracadabra', 0, 5, 'shazam')), str([ Command('start_abracadabra', 0, 'shazam'), Command('stop_abracadabra', 5, 'shazam'), ]))
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_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_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_entities_inline_multiple(self): self.entity_state.apply(Command('start_entity', 0, '0')) self.entity_state.render_entities('Test 1', {}, []) self.entity_state.apply(Command('stop_entity', 5, '0')) self.entity_state.apply(Command('start_entity', 5, '2')) self.assertEqual(DOM.render_debug(self.entity_state.render_entities('Test text', {}, [])), '<a href="http://example.com">Test 1</a>') self.entity_state.render_entities('Test 2', {}, []) self.entity_state.apply(Command('stop_entity', 10, '2')) self.assertEqual(DOM.render_debug(self.entity_state.render_entities('Test text', {}, [])), '<a href="http://test.com"><fragment>Test textTest 2</fragment></a>')
def test_render_entities_inline(self): self.entity_state.apply(Command("start_entity", 0, "0")) self.entity_state.render_entities("Test text", {}, []) self.entity_state.apply(Command("stop_entity", 9, "0")) self.assertEqual( DOM.render_debug( self.entity_state.render_entities("Test text", {}, [])), '<a href="http://example.com">Test text</a>', )
def test_build_command_groups_multiple(self): self.assertEqual(str(self.exporter.build_command_groups({ 'key': 'dem5p', 'text': 'some paragraph text', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [ { 'offset': 0, 'length': 4, 'style': 'ITALIC' }, { 'offset': 9, 'length': 3, 'style': 'BOLD' } ], 'entityRanges': [ { 'offset': 5, 'length': 9, 'key': 0 }, { 'offset': 0, 'length': 4, 'key': 1 } ] })), str([ ('some', [ Command('start_text', 0), Command('start_inline_style', 0, 'ITALIC'), Command('start_entity', 0, 1), ]), (' ', [ Command('stop_inline_style', 4, 'ITALIC'), Command('stop_entity', 4, 1), ]), ('para', [ Command('start_entity', 5, 0), ]), ('gra', [ Command('start_inline_style', 9, 'BOLD'), ]), ('ph', [ Command('stop_inline_style', 12, 'BOLD'), ]), (' text', [ Command('stop_entity', 14, 0), ]), ('', [ Command('stop_text', 19) ]), ]))
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_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_from_ranges_single(self): self.assertEqual(str(Command.from_ranges([ { 'offset': 0, 'length': 4, 'style': 'shazam' } ], 'abracadabra', 'style')), str([ Command('start_abracadabra', 0, 'shazam'), Command('stop_abracadabra', 4, 'shazam'), ]))
def test_render_entities_data_no_mutability(self): def component(props): self.assertEqual(props["entity"]["mutability"], None) return None entity_state = EntityState(Options.map_entities({"LINK": component}), entity_map) entity_state.apply(Command("start_entity", 0, "2")) entity_state.render_entities("Test text", {}, []) entity_state.apply(Command("stop_entity", 9, "2")) entity_state.render_entities("Test text", {}, [])
def test_build_commands_empty(self): self.assertEqual(str(self.exporter.build_commands({ 'key': 'dem5p', 'text': 'some paragraph text', 'type': 'unstyled', 'depth': 0, 'inlineStyleRanges': [], 'entityRanges': [] })), str([ Command('start_text', 0), Command('stop_text', 19), ]))
def test_from_style_ranges_single(self): self.assertEqual(str(Command.from_style_ranges({ 'inlineStyleRanges': [ { 'offset': 0, 'length': 4, 'style': 'shazam', } ] })), str([ Command('start_inline_style', 0, 'shazam'), Command('stop_inline_style', 4, 'shazam'), ]))
def test_render_entities_data_no_mutability(self): def component(props): self.assertEqual(props['entity']['mutability'], None) return None entity_state = EntityState(Options.map_entities({ 'LINK': component, }), entity_map) entity_state.apply(Command('start_entity', 0, '2')) entity_state.render_entities('Test text', {}, []) entity_state.apply(Command('stop_entity', 9, '2')) entity_state.render_entities('Test text', {}, [])
def build_commands(self, block: Block) -> List[Command]: """ Build all of the manipulation commands for a given block. - One pair to set the text. - Multiple pairs for styles. - Multiple pairs for entities. """ style_commands = Command.from_style_ranges(block) entity_commands = Command.from_entity_ranges(block) styles_and_entities = style_commands + entity_commands styles_and_entities.sort(key=attrgetter("index")) return ([Command("start_text", 0)] + styles_and_entities + [Command("stop_text", len(block["text"]))])
def build_commands(self, block): """ Build all of the manipulation commands for a given block. - One pair to set the text. - Multiple pairs for styles. - Multiple pairs for entities. """ style_commands = self.build_style_commands(block) entity_commands = self.build_entity_commands(block) return [Command('start_text', 0)] + sorted( style_commands + entity_commands, key=attrgetter('index')) + [ Command('stop_text', len(block['text'])) ]
def test_add_node_styled(self): self.style_state.apply(Command('start_inline_style', 0, 'ITALIC')) self.assertEqual( DOM.get_tag_name( self.style_state.add_node(DOM.create_element('p'), 'Test text')), 'em') self.assertEqual( self.style_state.add_node(DOM.create_element('p'), 'Test text').get('style'), None) self.assertEqual( DOM.get_text_content( self.style_state.add_node(DOM.create_element('p'), 'Test text')), 'Test text') self.style_state.apply(Command('stop_inline_style', 9, 'ITALIC'))
def test_from_style_ranges_single(self): self.assertEqual( str( Command.from_style_ranges({ "inlineStyleRanges": [{ "offset": 0, "length": 4, "style": "shazam" }] })), str([ Command("start_inline_style", 0, "shazam"), Command("stop_inline_style", 4, "shazam"), ]), )
def test_render_entities_inline_multiple(self): self.entity_state.apply(Command("start_entity", 0, "0")) self.entity_state.render_entities("Test 1", {}, []) self.entity_state.apply(Command("stop_entity", 5, "0")) self.entity_state.apply(Command("start_entity", 5, "2")) self.assertEqual( DOM.render_debug( self.entity_state.render_entities("Test text", {}, [])), '<a href="http://example.com">Test 1</a>', ) self.entity_state.render_entities("Test 2", {}, []) self.entity_state.apply(Command("stop_entity", 10, "2")) self.assertEqual( DOM.render_debug( self.entity_state.render_entities("Test text", {}, [])), '<a href="http://test.com"><fragment>Test textTest 2</fragment></a>', )