draftjs_html 0.31.0 → 0.32.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: 664e4ae07bc1d64294767d98e5a41b2adf8f998dcf6b7109d30b924d2947f931
4
- data.tar.gz: 6e3a4339a7a2c0d812d64b19260a8c57c1ca9b52ee8cb61969645bf1d75a0558
3
+ metadata.gz: 41707ca7969d9183fb2ab221f48e48a7004cb4ed9b8e9f4640bd53fb64c69912
4
+ data.tar.gz: 9ce23beaa990033c0ba30b8e791ee4c147c03efd324ed8eec277404e942f252f
5
5
  SHA512:
6
- metadata.gz: aa6e7d24bf3389ab5cec981ec5e22f0a1ef10e68b51479a33ae54eaa0701b110b5b21fae3c1b7a14936dff54fb29bf52bc3c92a6202511ed463ffe1c770b1e32
7
- data.tar.gz: 3897c00b471a7dc3dec926f90c8047d71d38ff9299acc7a9a8435a52b2d4b4ec204892cbadadf3269131a12e0d87d119b01f8eae81cecfd95727ce08644c0ac4
6
+ metadata.gz: 44427ea0eca8b9c47b5371305f57d70ae182806883b415f5841ea8654abc4c3958f515d84b11aa2ff343f5005cf5d6ada2a704394fe556e1dc0d58e684a0caa1
7
+ data.tar.gz: bb5ef7669e67d1b1bf0e40afc5081941cda97487deeb5fd757e2d06a60861ac4e92b21ae9e93f7bfeed45ac151816bab8df823bdf02caa847d7a13ed2c8815e3
data/README.md CHANGED
@@ -139,7 +139,7 @@ DraftjsHtml.to_html(raw_draftjs, options: {
139
139
  })
140
140
  ```
141
141
 
142
- # This would generate <strong> tags instead of <b> tags around ranges of `BOLD` inline styles.
142
+ **Note:** This would generate <strong> tags instead of <b> tags around ranges of `BOLD` inline styles.
143
143
 
144
144
  #### `:inline_style_renderer`
145
145
 
@@ -229,6 +229,19 @@ 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
+ ### Draftjs parsing
233
+
234
+ If you want to directly manipulate the structure of the Draftjs, you can use the `DraftjsHtml::Draftjs.parse(raw_draftjs)` directly.
235
+ This method assumes that the `raw_draftjs` hash is well-formed & valid, containing the `"blocks"` and `"entityMap"` keys.
236
+
237
+ If you’re dealing with unknown input, you can use `.safe_parse` instead to return a guaranteed, benign object.
238
+ See `DraftjsHtml::Draftjs::NullContent` for its implementation – the API matches that of `DraftjsHtml::Draftjs::Content`.
239
+
240
+ **Note:** Neither of the parse methods parse JSON strings, they expect Ruby hash objects.
241
+
242
+ These parsed objects support rudimentary validation.
243
+ Note that the `DraftjsHtml::Draftjs::NullContent` version always presents as invalid.
244
+
232
245
  ### Spec support
233
246
 
234
247
  To make it easier to test our own code, we've developed a few RSpec matchers that
@@ -4,9 +4,16 @@ module DraftjsHtml
4
4
  module Draftjs
5
5
  class Content
6
6
  def self.parse(raw)
7
+ validate_raw_input!(raw)
7
8
  new(raw['blocks'].map { Block.parse(**_1) }, EntityMap.parse(raw['entityMap']))
8
9
  end
9
10
 
11
+ def self.validate_raw_input!(raw)
12
+ raise InvalidRawDraftjs.new('raw cannot be nil') if raw.nil?
13
+ raise InvalidRawDraftjs.new('raw must contain "blocks" array') unless raw['blocks'].is_a?(Array)
14
+ raise InvalidRawDraftjs.new('raw must contain "entityMap" hash') unless raw['entityMap'].is_a?(Hash)
15
+ end
16
+
10
17
  attr_reader :blocks, :entity_map
11
18
 
12
19
  def initialize(blocks, entity_map)
@@ -28,6 +35,17 @@ module DraftjsHtml
28
35
  ToRaw.new.convert(self)
29
36
  end
30
37
 
38
+ def valid?
39
+ self.class.validate_raw_input!({ 'blocks' => blocks, 'entityMap' => entity_map })
40
+ true
41
+ rescue
42
+ false
43
+ end
44
+
45
+ def invalid?
46
+ !!valid?
47
+ end
48
+
31
49
  private
32
50
 
33
51
  def new_entity_key
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DraftjsHtml
4
+ module Draftjs
5
+ class NullContent
6
+ def blocks
7
+ []
8
+ end
9
+
10
+ def entity_map
11
+ {}
12
+ end
13
+
14
+ def find_entity(_key)
15
+ nil
16
+ end
17
+
18
+ def attach_entity(_entity, _block, _range)
19
+ nil
20
+ end
21
+
22
+ def to_raw
23
+ ToRaw.new.convert(self)
24
+ end
25
+
26
+ def valid?
27
+ false
28
+ end
29
+
30
+ def invalid?
31
+ true
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'draftjs/content'
4
+ require_relative 'draftjs/null_content'
4
5
  require_relative 'draftjs/character_meta'
5
6
  require_relative 'draftjs/applicable_range'
6
7
  require_relative 'draftjs/block'
@@ -14,5 +15,11 @@ module DraftjsHtml
14
15
  def self.parse(raw)
15
16
  Content.parse(raw)
16
17
  end
18
+
19
+ def self.safe_parse(raw)
20
+ Content.parse(raw)
21
+ rescue
22
+ NullContent.new
23
+ end
17
24
  end
18
25
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DraftjsHtml
4
+ Error = Class.new(StandardError)
5
+ InvalidRawDraftjs = Class.new(Error)
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.31.0"
4
+ VERSION = "0.32.0"
5
5
  end
data/lib/draftjs_html.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'draftjs_html/errors'
3
4
  require_relative "draftjs_html/version"
4
5
  require 'nokogiri'
5
6
  require_relative 'draftjs_html/draftjs'
@@ -8,8 +9,6 @@ require_relative 'draftjs_html/to_html'
8
9
  require_relative 'draftjs_html/from_html'
9
10
 
10
11
  module DraftjsHtml
11
- class Error < StandardError; end
12
-
13
12
  def self.to_html(raw_draftjs, options: {})
14
13
  ToHtml.new(options).convert(raw_draftjs)
15
14
  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.31.0
4
+ version: 0.32.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: 2023-08-16 00:00:00.000000000 Z
11
+ date: 2023-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -66,8 +66,10 @@ files:
66
66
  - lib/draftjs_html/draftjs/content.rb
67
67
  - lib/draftjs_html/draftjs/entity.rb
68
68
  - lib/draftjs_html/draftjs/entity_map.rb
69
+ - lib/draftjs_html/draftjs/null_content.rb
69
70
  - lib/draftjs_html/draftjs/raw_builder.rb
70
71
  - lib/draftjs_html/draftjs/to_raw.rb
72
+ - lib/draftjs_html/errors.rb
71
73
  - lib/draftjs_html/from_html.rb
72
74
  - lib/draftjs_html/from_html/char_list.rb
73
75
  - lib/draftjs_html/from_html/depth_stack.rb