Skip to content

Commit 8399d16

Browse files
committed
Give style rendering components access to the current block, blocks list, and current style type as inline_style_range.style. Fix #87
1 parent aca06dd commit 8399d16

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

CHANGELOG.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
### Added
88

9-
* Give block rendering components access to the whole `blocks` list.
10-
* Give block rendering components access to the whole `block`, when the component is rendered for a block.
11-
* Give text decorators renders to the whole `blocks` list.
12-
* Give text decorators renderers access to the whole `block`.
9+
* Give block rendering components access to the current `block`, when the component is rendered for a block, and the `blocks` list.
10+
* Give text decorators renderers access to the current `block` and `blocks` list.
11+
* Give style rendering components access to the current `block`, `blocks` list, and current style type as `inline_style_range.style` ([#87](https://github.com/springload/draftjs_exporter/issues/87)).
1312

1413
### Changed
1514

draftjs_exporter/dom.py

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def create_element(cls, type_=None, props=None, *children):
7878
props.pop('block', None)
7979
props.pop('blocks', None)
8080
props.pop('entity', None)
81+
props.pop('inline_style_range', None)
8182

8283
# Convert style object to style string, like the DOM would do.
8384
if 'style' in props and isinstance(props['style'], dict):

draftjs_exporter/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def render_block(self, block, entity_map, wrapper_state):
7575
else:
7676
decorated_node = text
7777

78-
styled_node = style_state.render_styles(decorated_node)
78+
styled_node = style_state.render_styles(decorated_node, block, wrapper_state.blocks)
7979
entity_node = entity_state.render_entities(styled_node)
8080

8181
if entity_node is not None:

draftjs_exporter/style_state.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ def apply(self, command):
2323
def is_empty(self):
2424
return not self.styles
2525

26-
def render_styles(self, text_node):
27-
node = text_node
26+
def render_styles(self, decorated_node, block, blocks):
27+
node = decorated_node
2828
if not self.is_empty():
2929
# Nest the tags.
30-
for s in sorted(self.styles, reverse=True):
31-
opt = Options.for_style(self.style_map, s)
32-
node = DOM.create_element(opt.element, opt.props, node)
30+
for style in sorted(self.styles, reverse=True):
31+
opt = Options.for_style(self.style_map, style)
32+
props = dict(opt.props)
33+
props['block'] = block
34+
props['blocks'] = blocks
35+
props['inline_style_range'] = {
36+
'style': style,
37+
}
38+
node = DOM.create_element(opt.element, props, node)
3339

3440
return node

tests/test_style_state.py

+34-8
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,67 @@ def test_is_empty_styled(self):
5555
self.assertEqual(self.style_state.is_empty(), False)
5656

5757
def test_render_styles_unstyled(self):
58-
self.assertEqual(self.style_state.render_styles('Test text'), 'Test text')
58+
self.assertEqual(self.style_state.render_styles('Test text', {}, []), 'Test text')
5959

6060
def test_render_styles_unicode(self):
61-
self.assertEqual(self.style_state.render_styles('🍺'), '🍺')
61+
self.assertEqual(self.style_state.render_styles('🍺', {}, []), '🍺')
6262

6363
def test_render_styles_styled(self):
6464
self.style_state.apply(Command('start_inline_style', 0, 'ITALIC'))
65-
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<em>Test text</em>')
65+
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<em>Test text</em>')
6666
self.style_state.apply(Command('stop_inline_style', 9, 'ITALIC'))
6767

6868
def test_render_styles_styled_multiple(self):
6969
self.style_state.apply(Command('start_inline_style', 0, 'BOLD'))
7070
self.style_state.apply(Command('start_inline_style', 0, 'ITALIC'))
71-
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<strong><em>Test text</em></strong>')
71+
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<strong><em>Test text</em></strong>')
7272

7373
def test_render_styles_attributes(self):
7474
self.style_state.apply(Command('start_inline_style', 0, 'KBD'))
75-
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<kbd class="o-keyboard-shortcut">Test text</kbd>')
75+
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<kbd class="o-keyboard-shortcut">Test text</kbd>')
7676
self.style_state.apply(Command('stop_inline_style', 9, 'KBD'))
7777

7878
def test_render_styles_component(self):
7979
self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT'))
80-
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text')), '<strong style="color: red;">Test text</strong>')
80+
self.assertEqual(DOM.render_debug(self.style_state.render_styles('Test text', {}, [])), '<strong style="color: red;">Test text</strong>')
8181
self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT'))
8282

8383
def test_render_styles_component_multiple(self):
8484
self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT'))
8585
self.style_state.apply(Command('start_inline_style', 0, 'SHOUT'))
86-
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>')
86+
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>')
8787
self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT'))
8888
self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT'))
8989

9090
def test_render_styles_component_multiple_invert(self):
9191
self.style_state.apply(Command('start_inline_style', 0, 'SHOUT'))
9292
self.style_state.apply(Command('start_inline_style', 0, 'IMPORTANT'))
93-
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>')
93+
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>')
9494
self.style_state.apply(Command('stop_inline_style', 9, 'SHOUT'))
9595
self.style_state.apply(Command('stop_inline_style', 9, 'IMPORTANT'))
96+
97+
def test_render_styles_data(self):
98+
blocks = [
99+
{
100+
'key': '5s7g9',
101+
'text': 'test',
102+
'type': 'unstyled',
103+
'depth': 0,
104+
'inlineStyleRanges': [],
105+
'entityRanges': [],
106+
},
107+
]
108+
109+
def component(props):
110+
self.assertEqual(props['blocks'], blocks)
111+
self.assertEqual(props['block'], blocks[0])
112+
self.assertEqual(props['inline_style_range']['style'], 'ITALIC')
113+
return None
114+
115+
style_state = StyleState({
116+
'ITALIC': component,
117+
})
118+
119+
style_state.apply(Command('start_inline_style', 0, 'ITALIC'))
120+
style_state.render_styles('Test text', blocks[0], blocks)
121+
style_state.apply(Command('stop_inline_style', 9, 'ITALIC'))

0 commit comments

Comments
 (0)