draftjs_html 0.23.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c01b8b4b8ca17c90c9d53f7d85caebc7fe6c18aab14a79ff635110d8a0fb81dc
4
- data.tar.gz: 6e0d85454c9d8ee4e6998e8e398c72c01f6097fc3c5e9b61c165fb07ac01af0f
3
+ metadata.gz: 20fc136c6cdbd1488b5ba808c3f55caca80f8f748241bc42b53028a544878542
4
+ data.tar.gz: a39a398da3a5c1513d62d008fb9f80f105a9d4b91137da4b22d072a09727fea1
5
5
  SHA512:
6
- metadata.gz: 76cdd86c495a5900653f060f8d4f9702a29b1a7b3ebdb792b00e3f7071642c2fd7547d56aa1e3f6684ab6f873e8c377a09d8fe0c344e2ecf400db106076ebe66
7
- data.tar.gz: 0c65eac79da80da23d667c62352542dc03717e3db1999fffb35f5a7ac19a1afe871345c1c7449dd29c0570fd780d17c1ea44f3c75b02c034e7cd224eb2c002bb
6
+ metadata.gz: c7926f0d00a1b322a1e76b8a37c36010b356d7cb3cde97662378476e02787ca8ea09227b64a496dd778ef630c70e80f7cc6db69d39f7c84c165b185a2b367627
7
+ data.tar.gz: 2cddf8a0b594755ae959935f8df954a7543d3208ab4c7ad55ab95b5fe28bb9f802ec140f11dcde9b728cc924e65d077afa5248734098c1013612ab3083e6ed28
data/README.md CHANGED
@@ -229,15 +229,6 @@ The callable should return a Hash with symbol keys. The supported values are:
229
229
  - `data` (optional, default `{}`)
230
230
  - an arbitrary data-bag (Hash) of entity data
231
231
 
232
- #### `:is_semantic_markup:`
233
-
234
- Defaults to `true`.
235
-
236
- By setting to `false`, the user is stating they want to treat `div` tags as semantic,
237
- block-level tags. In some markup (emails, for example), there are no semantic tags
238
- (read, no `p` tags), so the only indications of whitespace and structure come from
239
- `div` tags. This flag will flush content wrapped in a `div` as a DraftJS block.
240
-
241
232
  ## Development
242
233
 
243
234
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
@@ -60,6 +60,10 @@ module DraftjsHtml
60
60
  size > 0
61
61
  end
62
62
 
63
+ def more_than_whitespace?
64
+ !text.match?(/^[[:space:]]*$/)
65
+ end
66
+
63
67
  def atomic?
64
68
  @chars.any? && @chars.all?(&:atomic?)
65
69
  end
@@ -1,16 +1,16 @@
1
1
  module DraftjsHtml
2
2
  class FromHtml < Nokogiri::XML::SAX::Document
3
3
  class DepthStack
4
- def initialize(is_semantic_markup: true)
4
+ def initialize(options)
5
5
  @stack = []
6
6
  @nodes = []
7
7
  @list_depth = -1
8
8
  @active_styles = []
9
- @is_semantic_markup = is_semantic_markup
9
+ @options = options
10
10
  end
11
11
 
12
12
  def push(tagname, attrs)
13
- @stack << PendingBlock.from_tag(tagname, attrs, @nodes.dup, @list_depth, is_semantic_markup: @is_semantic_markup)
13
+ @stack << PendingBlock.from_tag(tagname, attrs, @nodes.dup, @list_depth, options: @options)
14
14
  track_block_node(tagname)
15
15
  end
16
16
 
@@ -1,8 +1,9 @@
1
1
  module DraftjsHtml
2
2
  class FromHtml < Nokogiri::XML::SAX::Document
3
3
  INLINE_STYLE_ELEMENTS = HtmlDefaults::HTML_STYLE_TAGS_TO_STYLE.keys.freeze
4
- LIST_PARENT_ELEMENTS = %w[ol ul table].freeze
5
- INLINE_NON_STYLE_ELEMENTS = %w[a abbr cite font img output q samp span thead tbody td time var].freeze
4
+ LIST_PARENT_ELEMENTS = %w[ol ul].freeze
5
+ INLINE_NON_STYLE_ELEMENTS = %w[a abbr cite font img output q samp span table thead tbody td time var].freeze
6
6
  BLOCK_CONTENT_ELEMENTS = %w[p dl h1 h2 h3 h4 h5 h6].freeze
7
+ FLUSH_BOUNDARIES = %w[OPENING div ol ul li tr].freeze
7
8
  end
8
9
  end
@@ -1,7 +1,7 @@
1
1
  module DraftjsHtml
2
2
  class FromHtml < Nokogiri::XML::SAX::Document
3
- PendingBlock = Struct.new(:tagname, :attrs, :chars, :entities, :pending_entities, :parent_tagnames, :depth, :is_semantic_markup, keyword_init: true) do
4
- def self.from_tag(name, attrs, parent_tagnames, depth, is_semantic_markup: true)
3
+ PendingBlock = Struct.new(:tagname, :attrs, :chars, :entities, :pending_entities, :parent_tagnames, :depth, :options, keyword_init: true) do
4
+ def self.from_tag(name, attrs, parent_tagnames, depth, options: {})
5
5
  self.new(
6
6
  tagname: name,
7
7
  attrs: attrs,
@@ -10,7 +10,7 @@ module DraftjsHtml
10
10
  pending_entities: [],
11
11
  depth: depth,
12
12
  parent_tagnames: parent_tagnames,
13
- is_semantic_markup: is_semantic_markup,
13
+ options: options
14
14
  )
15
15
  end
16
16
 
@@ -23,9 +23,7 @@ module DraftjsHtml
23
23
  end
24
24
 
25
25
  def flushable?
26
- %w[OPENING ol ul li table].include?(parent_tagnames.last) ||
27
- (parent_tagnames.last == 'div' && tagname != 'div') ||
28
- (!is_semantic_markup && tagname == 'div')
26
+ FLUSH_BOUNDARIES.include?(parent_tagnames.last)
29
27
  end
30
28
 
31
29
  def consume(other_pending_block)
@@ -35,21 +33,23 @@ module DraftjsHtml
35
33
  end
36
34
 
37
35
  def flush_to(draftjs)
38
- if text_buffer.any?
39
- text_buffer.each_line do |line|
40
- block_type = line.atomic? ? 'atomic' : block_name
41
- draftjs.typed_block(block_type, line.text, depth: [depth, 0].max)
36
+ text_buffer.each_line do |line|
37
+ block_type = line.atomic? ? 'atomic' : block_name
38
+ next unless should_flush_line?(line)
42
39
 
43
- line.entity_ranges.each do |entity_range|
44
- entity = entity_range.entity
45
- draftjs.apply_entity entity[:type], entity_range.range, data: entity[:data], mutability: entity.fetch(:mutability, 'IMMUTABLE')
46
- end
40
+ draftjs.typed_block(block_type, line.text, depth: [depth, 0].max)
47
41
 
48
- line.style_ranges.each do |style_range|
49
- draftjs.inline_style(style_range.style, style_range.range)
50
- end
42
+ line.entity_ranges.each do |entity_range|
43
+ entity = entity_range.entity
44
+ draftjs.apply_entity entity[:type], entity_range.range, data: entity[:data], mutability: entity.fetch(:mutability, 'IMMUTABLE')
45
+ end
46
+
47
+ line.style_ranges.each do |style_range|
48
+ draftjs.inline_style(style_range.style, style_range.range)
51
49
  end
52
50
  end
51
+
52
+ self.text_buffer = CharList.new
53
53
  end
54
54
 
55
55
  def block_name
@@ -65,6 +65,13 @@ module DraftjsHtml
65
65
  def text_buffer=(other)
66
66
  self[:chars] = other
67
67
  end
68
+
69
+ def should_flush_line?(chars)
70
+ return true if chars.atomic?
71
+ return true unless options[:squeeze_whitespace_blocks]
72
+
73
+ options[:squeeze_whitespace_blocks] && chars.more_than_whitespace?
74
+ end
68
75
  end
69
76
  end
70
77
  end
@@ -11,8 +11,8 @@ module DraftjsHtml
11
11
  def initialize(options = {})
12
12
  @draftjs = Draftjs::RawBuilder.new
13
13
  @parser = Nokogiri::HTML4::SAX::Parser.new(self)
14
- @depth_stack = DepthStack.new(is_semantic_markup: options.fetch(:is_semantic_markup, true))
15
14
  @options = ensure_options!(options.dup)
15
+ @depth_stack = DepthStack.new(@options)
16
16
  end
17
17
 
18
18
  def convert(raw_html)
@@ -56,6 +56,7 @@ module DraftjsHtml
56
56
  when *FromHtml::LIST_PARENT_ELEMENTS
57
57
  @depth_stack.push_parent(name, attrs)
58
58
  else
59
+ @depth_stack.flush_to(@draftjs) if FromHtml::FLUSH_BOUNDARIES.include?(name)
59
60
  @depth_stack.push(name, attributes)
60
61
  end
61
62
 
@@ -101,6 +102,7 @@ module DraftjsHtml
101
102
  when 'img' then { type: 'IMAGE', mutability: 'IMMUTABLE', atomic: true, data: attrs }
102
103
  end
103
104
  }
105
+ opts[:squeeze_whitespace_blocks] ||= false
104
106
  opts
105
107
  end
106
108
  end
@@ -43,7 +43,7 @@ module DraftjsHtml
43
43
 
44
44
  ENTITY_CONVERSION_MAP = {
45
45
  'LINK' => ->(entity, content, *) {
46
- attributes = entity.data.slice('url', 'href', 'rel', 'target', 'title', 'className').each_with_object({}) do |(attr, value), h|
46
+ attributes = entity.data.slice('href', 'url', 'rel', 'target', 'title', 'className').each_with_object({}) do |(attr, value), h|
47
47
  h[ENTITY_ATTRIBUTE_NAME_MAP.fetch(attr, attr)] = value
48
48
  end
49
49
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.23.0"
4
+ VERSION = "0.25.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draftjs_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-15 00:00:00.000000000 Z
11
+ date: 2022-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri