Class: DraftjsHtml::FromHtml::DepthStack

Inherits:
Object
  • Object
show all
Defined in:
lib/draftjs_html/from_html/depth_stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DepthStack

Returns a new instance of DepthStack.



4
5
6
7
8
9
10
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 4

def initialize(options)
  @stack = []
  @nodes = []
  @list_depth = -1
  @active_styles = []
  @options = options
end

Instance Method Details

#append_text(chars) ⇒ Object



83
84
85
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 83

def append_text(chars)
  current.text_buffer.append(chars, styles: @active_styles) unless chars.empty?
end

#convert_pending_entities(conversion) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 53

def convert_pending_entities(conversion)
  while current.pending_entities.any?
    pending_entity = current.pending_entities.pop
    range = pending_entity[:start]..current_character_offset
    content = current_text_buffer[range]
    user_created_entity = conversion.call(pending_entity[:tagname], content, pending_entity[:attrs])
    next unless user_created_entity

    if content == '' && !user_created_entity[:atomic]
      current.text_buffer.append(' ', entity: user_created_entity)
    elsif content == '' && user_created_entity[:atomic]
      current.text_buffer.append_atomic_entity(user_created_entity)
    else
      current.text_buffer.apply_entity(range, user_created_entity)
    end
  end
end

#create_pending_entity(tagname, attrs) ⇒ Object



49
50
51
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 49

def create_pending_entity(tagname, attrs)
  current.pending_entities << { tagname: tagname, start: current_character_offset + 1, attrs: attrs }
end

#flush_to(draftjs) ⇒ Object



79
80
81
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 79

def flush_to(draftjs)
  current.flush_to(draftjs)
end

#pop(draftjs) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 35

def pop(draftjs)
  return if @stack.empty?
  return if inside_parent?

  if @nodes.last == current.tagname && current.flushable?
    flush_to(draftjs)
  elsif @stack[-2]
    @stack[-2].consume(current)
  end

  @stack.pop
  @nodes.pop
end

#pop_parent(tagname, draftjs) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 22

def pop_parent(tagname, draftjs)
  @nodes.pop
  blocks = []
  while current.depth >= 0
    blocks << @stack.pop
    @nodes.pop
  end
  blocks.reverse_each do |pending_block|
    pending_block.flush_to(draftjs)
  end
  @list_depth -= 1
end

#push(tagname, attrs) ⇒ Object



12
13
14
15
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 12

def push(tagname, attrs)
  @stack << PendingBlock.from_tag(tagname, attrs, @nodes.dup, @list_depth, options: @options)
  track_block_node(tagname)
end

#push_parent(tagname, attrs) ⇒ Object



17
18
19
20
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 17

def push_parent(tagname, attrs)
  @list_depth += 1
  track_block_node(tagname)
end

#style_end(tagname) ⇒ Object



75
76
77
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 75

def style_end(tagname)
  @active_styles.delete_at(@active_styles.index(DraftjsHtml::HtmlDefaults::HTML_STYLE_TAGS_TO_STYLE[tagname]))
end

#style_start(tagname) ⇒ Object



71
72
73
# File 'lib/draftjs_html/from_html/depth_stack.rb', line 71

def style_start(tagname)
  @active_styles += [DraftjsHtml::HtmlDefaults::HTML_STYLE_TAGS_TO_STYLE[tagname]]
end