draftjs_html 0.24.0 → 0.25.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -9
- data/lib/draftjs_html/from_html/char_list.rb +4 -0
- data/lib/draftjs_html/from_html/depth_stack.rb +3 -3
- data/lib/draftjs_html/from_html/elements.rb +3 -2
- data/lib/draftjs_html/from_html/pending_block.rb +24 -17
- data/lib/draftjs_html/from_html.rb +3 -1
- data/lib/draftjs_html/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20fc136c6cdbd1488b5ba808c3f55caca80f8f748241bc42b53028a544878542
|
4
|
+
data.tar.gz: a39a398da3a5c1513d62d008fb9f80f105a9d4b91137da4b22d072a09727fea1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -1,16 +1,16 @@
|
|
1
1
|
module DraftjsHtml
|
2
2
|
class FromHtml < Nokogiri::XML::SAX::Document
|
3
3
|
class DepthStack
|
4
|
-
def initialize(
|
4
|
+
def initialize(options)
|
5
5
|
@stack = []
|
6
6
|
@nodes = []
|
7
7
|
@list_depth = -1
|
8
8
|
@active_styles = []
|
9
|
-
@
|
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,
|
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
|
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, :
|
4
|
-
def self.from_tag(name, attrs, parent_tagnames, depth,
|
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
|
-
|
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
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
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
|
data/lib/draftjs_html/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2022-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|