def update_stack(self, options, depth): if depth >= self.stack.length(): # If the depth is gte the stack length, we need more wrappers. depth_levels = range(self.stack.length(), depth + 1) for level in depth_levels: new_wrapper = Wrapper(level, options.wrapper) wrapper_children = DOM.get_children(self.stack.head().elt) # Determine where to append the new wrapper. if len(wrapper_children) == 0: # If there is no content in the current wrapper, we need # to add an intermediary node. wrapper_parent = DOM.create_element( options.element[0], options.element[1]) DOM.append_child(self.stack.head().elt, wrapper_parent) else: # Otherwise we can append at the end of the last child. wrapper_parent = wrapper_children[-1] DOM.append_child(wrapper_parent, new_wrapper.elt) self.stack.append(new_wrapper) else: # Cut the stack to where it now stops, and add new wrapper. self.stack.slice(depth) self.stack.append(Wrapper(depth, options.wrapper))
def clean_up(self): """ Special method to handle a rare corner case: if there is no block at depth 0, we need to add the wrapper that contains the whole tree to the document. """ document_length = len(DOM.get_children(self.document)) if document_length == 0 and self.stack.length() != 0: DOM.append_child(self.document, self.stack.tail().elt)
def set_wrapper(self, options=[], depth=0): if len(options) == 0: element = DOM.create_document_fragment() else: element = DOM.create_element(options[0], options[1]) new_wrapper = [element, depth, options] if depth >= len(self.wrapper_stack): DOM.append_child(DOM.get_children(self.get_wrapper_elt())[-1], element) self.wrapper_stack.append(new_wrapper) else: # Cut the stack to where it now stops, and add new wrapper. self.wrapper_stack = self.wrapper_stack[:depth] + [new_wrapper]
def test_create_node_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(self.style_state.get_style_tags(), ['em', 'strong']) self.assertEqual(DOM.get_tag_name(self.style_state.create_nodes('wow')[0]), 'em') self.assertEqual(DOM.get_tag_name(DOM.get_children(self.style_state.create_nodes('wow')[0])[0]), 'strong')
def test_get_children(self): self.assertEqual(len(DOM.get_children(DOM.create_element('span', {}, DOM.create_element('span'), DOM.create_element('span')))), 2)