示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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", {}, [])
示例#4
0
    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', {}, [])
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
    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)
示例#9
0
    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)
示例#10
0
class TestEntityState(unittest.TestCase):
    def setUp(self):
        self.entity_state = EntityState(
            Options.map_entities(entity_decorators), entity_map)

    def test_init(self):
        self.assertIsInstance(self.entity_state, EntityState)

    def test_apply_start_entity(self):
        self.assertEqual(len(self.entity_state.entity_stack), 0)
        self.entity_state.apply(Command("start_entity", 0, "0"))
        self.assertEqual(self.entity_state.entity_stack[-1], "0")

    def test_apply_stop_entity(self):
        self.assertEqual(len(self.entity_state.entity_stack), 0)
        self.entity_state.apply(Command("start_entity", 0, "0"))
        self.entity_state.apply(Command("stop_entity", 5, "0"))
        self.assertEqual(len(self.entity_state.entity_stack), 0)

    def test_apply_raises(self):
        with self.assertRaises(EntityException):
            self.entity_state.apply(Command("start_entity", 0, "0"))
            self.entity_state.apply(Command("stop_entity", 0, "1"))

    def test_has_no_entity_default(self):
        self.assertEqual(self.entity_state.has_no_entity(), True)

    def test_has_no_entity_styled(self):
        self.entity_state.apply(Command("start_entity", 0, "0"))
        self.assertEqual(self.entity_state.has_no_entity(), False)

    def test_get_entity_details(self):
        self.assertEqual(
            self.entity_state.get_entity_details("0"),
            {
                "data": {
                    "url": "http://example.com"
                },
                "type": "LINK",
                "mutability": "MUTABLE",
            },
        )

    def test_get_entity_details_raises(self):
        with self.assertRaises(EntityException):
            self.entity_state.get_entity_details("1")

    def test_render_entities_unstyled(self):
        self.assertEqual(
            self.entity_state.render_entities("Test text", {}, []),
            "Test text")

    def test_render_entities_unicode(self):
        self.assertEqual(self.entity_state.render_entities("🍺", {}, []), "🍺")

    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_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_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_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", {}, [])
示例#11
0
class TestEntityState(unittest.TestCase):
    def setUp(self):
        self.entity_state = EntityState(Options.map_entities(entity_decorators), entity_map)

    def test_init(self):
        self.assertIsInstance(self.entity_state, EntityState)

    def test_apply_start_entity(self):
        self.assertEqual(len(self.entity_state.entity_stack), 0)
        self.entity_state.apply(Command('start_entity', 0, '0'))
        self.assertEqual(self.entity_state.entity_stack[-1], '0')

    def test_apply_stop_entity(self):
        self.assertEqual(len(self.entity_state.entity_stack), 0)
        self.entity_state.apply(Command('start_entity', 0, '0'))
        self.entity_state.apply(Command('stop_entity', 5, '0'))
        self.assertEqual(len(self.entity_state.entity_stack), 0)

    def test_apply_raises(self):
        with self.assertRaises(EntityException):
            self.entity_state.apply(Command('start_entity', 0, '0'))
            self.entity_state.apply(Command('stop_entity', 0, '1'))

    def test_has_no_entity_default(self):
        self.assertEqual(self.entity_state.has_no_entity(), True)

    def test_has_no_entity_styled(self):
        self.entity_state.apply(Command('start_entity', 0, '0'))
        self.assertEqual(self.entity_state.has_no_entity(), False)

    def test_get_entity_details(self):
        self.assertEqual(self.entity_state.get_entity_details('0'), {
            'data': {
                'url': 'http://example.com'
            },
            'type': 'LINK',
            'mutability': 'MUTABLE',
        })

    def test_get_entity_details_raises(self):
        with self.assertRaises(EntityException):
            self.entity_state.get_entity_details('1')

    def test_render_entities_unstyled(self):
        self.assertEqual(self.entity_state.render_entities('Test text', {}, []), 'Test text')

    def test_render_entities_unicode(self):
        self.assertEqual(self.entity_state.render_entities('🍺', {}, []), '🍺')

    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_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_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_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', {}, [])