oga 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f8a4c2a027e02e07a94874d17bb49492bd09460
4
- data.tar.gz: 2798515da813f4ef194075131846affbb95e25ac
3
+ metadata.gz: 4445e4b345d7892e11706b1901c3a19b3ad8e4b1
4
+ data.tar.gz: 886eb1880fecd4e068498ff5328d2cf0bc24f50c
5
5
  SHA512:
6
- metadata.gz: 8b567514db0296d5c16a815d769d0a38c1f70949d198f971338d25fe00c95b0ff513c698c65b63a5e965097502d51945a81a695f6ae208c1dfb6a7c41b43f602
7
- data.tar.gz: b69129e2b101db42009f8b9f1cac4cc18e3f54f83a78eb6b990689e520e4d027e3c1e47865d76968e3c59f0311bf30fa0362472a151dca2be9b473fa03189509
6
+ metadata.gz: 0ae267cf2e6572b51691bca870f7b825f6cdde8cee6804cb056dff81a0cfc01618d274aff0d402b7e9e59c61ec1211d0df1e6cceaea8f077bdeb86d8b4b46c54
7
+ data.tar.gz: d99c0848e4906c3059c314da523c963aca4b77a257380da98e86dc87c11485fa3620247e2d1df005c92077503dc36acd0097e855bff0ec08cf65b885447a3bf4
data/lib/oga.rb CHANGED
@@ -6,6 +6,7 @@ require 'thread'
6
6
  require_relative 'oga/version'
7
7
  require_relative 'oga/oga'
8
8
  require_relative 'oga/lru'
9
+ require_relative 'oga/entity_decoder'
9
10
 
10
11
  # Load these first so that the native extensions don't have to define the
11
12
  # Oga::XML namespace.
@@ -0,0 +1,21 @@
1
+ module Oga
2
+ module EntityDecoder
3
+ ##
4
+ # @see [decode]
5
+ #
6
+ def self.try_decode(input, html = false)
7
+ return input ? decode(input, html) : nil
8
+ end
9
+
10
+ ##
11
+ # @param [String] input
12
+ # @param [TrueClass|FalseClass] html
13
+ # @return [String]
14
+ #
15
+ def self.decode(input, html = false)
16
+ decoder = html ? HTML::Entities : XML::Entities
17
+
18
+ return decoder.decode(input)
19
+ end
20
+ end # EntityDecoder
21
+ end # Oga
@@ -1,3 +1,3 @@
1
1
  module Oga
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end # Oga
@@ -10,16 +10,12 @@ module Oga
10
10
  # @!attribute [rw] namespace_name
11
11
  # @return [String]
12
12
  #
13
- # @!attribute [rw] value
14
- # The value of the attribute.
15
- # @return [String]
16
- #
17
13
  # @!attribute [r] element
18
14
  # The element this attribute belongs to.
19
15
  # @return [Oga::XML::Element]
20
16
  #
21
17
  class Attribute
22
- attr_accessor :name, :namespace_name, :element, :value
18
+ attr_accessor :name, :namespace_name, :element
23
19
 
24
20
  ##
25
21
  # The default namespace available to all attributes. This namespace can
@@ -67,8 +63,28 @@ module Oga
67
63
  end
68
64
 
69
65
  ##
70
- # Returns the value of the attribute.
66
+ # @param [String] value
71
67
  #
68
+ def value=(value)
69
+ @value = value
70
+ @decoded = false
71
+ end
72
+
73
+ ##
74
+ # Returns the value of the attribute or nil if no explicit value was set.
75
+ #
76
+ # @return [String|NilClass]
77
+ #
78
+ def value
79
+ if !@decoded and @value
80
+ @value = EntityDecoder.try_decode(@value, html?)
81
+ @decoded = true
82
+ end
83
+
84
+ return @value
85
+ end
86
+
87
+ ##
72
88
  # @return [String]
73
89
  #
74
90
  def text
@@ -108,6 +124,15 @@ module Oga
108
124
 
109
125
  return "Attribute(#{segments.join(' ')})"
110
126
  end
127
+
128
+ private
129
+
130
+ ##
131
+ # @return [TrueClass|FalseClass]
132
+ #
133
+ def html?
134
+ return !!@element && @element.html?
135
+ end
111
136
  end # Attribute
112
137
  end # XML
113
138
  end # Oga
@@ -26,9 +26,8 @@ module Oga
26
26
  # @return [String]
27
27
  #
28
28
  def text
29
- unless @decoded
30
- decoder = html? ? HTML::Entities : Entities
31
- @text = decoder.decode(@text)
29
+ if decode_entities?
30
+ @text = EntityDecoder.try_decode(@text, html?)
32
31
  @decoded = true
33
32
  end
34
33
 
@@ -39,15 +38,29 @@ module Oga
39
38
  # @see [Oga::XML::CharacterNode#to_xml]
40
39
  #
41
40
  def to_xml
42
- node = parent
43
-
44
- if node.is_a?(Element) and html? \
45
- and Lexer::LITERAL_HTML_ELEMENTS.include?(node.name)
46
- return super
47
- end
41
+ return super if inside_literal_html?
48
42
 
49
43
  return Entities.encode(super)
50
44
  end
45
+
46
+ private
47
+
48
+ ##
49
+ # @return [TrueClass|FalseClass]
50
+ #
51
+ def decode_entities?
52
+ return !@decoded && !inside_literal_html?
53
+ end
54
+
55
+ ##
56
+ # @return [TrueClass|FalseClass]
57
+ #
58
+ def inside_literal_html?
59
+ node = parent
60
+
61
+ return node.is_a?(Element) && html? &&
62
+ Lexer::LITERAL_HTML_ELEMENTS.include?(node.name)
63
+ end
51
64
  end # Text
52
65
  end # XML
53
66
  end # Oga
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oga
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yorick Peterse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-03 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -165,6 +165,7 @@ files:
165
165
  - lib/oga.rb
166
166
  - lib/oga/css/lexer.rb
167
167
  - lib/oga/css/parser.rb
168
+ - lib/oga/entity_decoder.rb
168
169
  - lib/oga/html/entities.rb
169
170
  - lib/oga/html/parser.rb
170
171
  - lib/oga/html/sax_parser.rb