예제 #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 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)
예제 #4
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)
예제 #5
0
    def render_block(self, block, entity_map):
        element = self.wrapper_state.element_for(block)
        entity_state = EntityState(element, self.entity_decorators, entity_map)

        for (text, commands) in self.build_command_groups(block):
            for command in commands:
                entity_state.apply(command)
                self.style_state.apply(command)

            self.style_state.add_node(entity_state.current_parent(), text)
예제 #6
0
    def render_block(self, block, entity_map):
        element = self.wrapper_state.element_for(block)
        entity_state = EntityState(self.entity_decorators, entity_map)

        for (text, commands) in self.build_command_groups(block):
            for command in commands:
                entity_state.apply(command)
                self.style_state.apply(command)

            style_node = self.style_state.create_node(text)
            entity_state.render_entitities(element, style_node)
예제 #7
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", {}, [])
예제 #8
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', {}, [])
예제 #9
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)
예제 #10
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)
예제 #11
0
 def setUp(self):
     self.entity_state = EntityState(
         Options.map_entities(entity_decorators), entity_map)
예제 #12
0
 def setUp(self):
     self.entity_state = EntityState(entity_decorators, entity_map)
예제 #13
0
 def setUp(self):
     self.entity_state = EntityState(DOM.create_element('div'), entity_decorators, entity_map)