oga 0.3.0 → 0.3.1
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 +4 -4
- data/lib/oga.rb +1 -0
- data/lib/oga/entity_decoder.rb +21 -0
- data/lib/oga/version.rb +1 -1
- data/lib/oga/xml/attribute.rb +31 -6
- data/lib/oga/xml/text.rb +22 -9
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4445e4b345d7892e11706b1901c3a19b3ad8e4b1
|
4
|
+
data.tar.gz: 886eb1880fecd4e068498ff5328d2cf0bc24f50c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ae267cf2e6572b51691bca870f7b825f6cdde8cee6804cb056dff81a0cfc01618d274aff0d402b7e9e59c61ec1211d0df1e6cceaea8f077bdeb86d8b4b46c54
|
7
|
+
data.tar.gz: d99c0848e4906c3059c314da523c963aca4b77a257380da98e86dc87c11485fa3620247e2d1df005c92077503dc36acd0097e855bff0ec08cf65b885447a3bf4
|
data/lib/oga.rb
CHANGED
@@ -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
|
data/lib/oga/version.rb
CHANGED
data/lib/oga/xml/attribute.rb
CHANGED
@@ -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
|
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
|
-
#
|
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
|
data/lib/oga/xml/text.rb
CHANGED
@@ -26,9 +26,8 @@ module Oga
|
|
26
26
|
# @return [String]
|
27
27
|
#
|
28
28
|
def text
|
29
|
-
|
30
|
-
|
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
|
-
|
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.
|
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-
|
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
|