|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | module Rails
|
2 | 4 | module Html
|
3 | 5 | # === Rails::Html::PermitScrubber
|
@@ -77,84 +79,83 @@ def scrub(node)
|
77 | 79 | end
|
78 | 80 |
|
79 | 81 | protected
|
| 82 | + def allowed_node?(node) |
| 83 | + @tags.include?(node.name) |
| 84 | + end |
80 | 85 |
|
81 |
| - def allowed_node?(node) |
82 |
| - @tags.include?(node.name) |
83 |
| - end |
| 86 | + def skip_node?(node) |
| 87 | + node.text? |
| 88 | + end |
84 | 89 |
|
85 |
| - def skip_node?(node) |
86 |
| - node.text? |
87 |
| - end |
| 90 | + def scrub_attribute?(name) |
| 91 | + !@attributes.include?(name) |
| 92 | + end |
88 | 93 |
|
89 |
| - def scrub_attribute?(name) |
90 |
| - !@attributes.include?(name) |
91 |
| - end |
| 94 | + def keep_node?(node) |
| 95 | + if @tags |
| 96 | + allowed_node?(node) |
| 97 | + else |
| 98 | + Loofah::HTML5::Scrub.allowed_element?(node.name) |
| 99 | + end |
| 100 | + end |
92 | 101 |
|
93 |
| - def keep_node?(node) |
94 |
| - if @tags |
95 |
| - allowed_node?(node) |
96 |
| - else |
97 |
| - Loofah::HTML5::Scrub.allowed_element?(node.name) |
| 102 | + def scrub_node(node) |
| 103 | + node.before(node.children) unless prune # strip |
| 104 | + node.remove |
98 | 105 | end
|
99 |
| - end |
100 | 106 |
|
101 |
| - def scrub_node(node) |
102 |
| - node.before(node.children) unless prune # strip |
103 |
| - node.remove |
104 |
| - end |
| 107 | + def scrub_attributes(node) |
| 108 | + if @attributes |
| 109 | + node.attribute_nodes.each do |attr| |
| 110 | + attr.remove if scrub_attribute?(attr.name) |
| 111 | + scrub_attribute(node, attr) |
| 112 | + end |
105 | 113 |
|
106 |
| - def scrub_attributes(node) |
107 |
| - if @attributes |
108 |
| - node.attribute_nodes.each do |attr| |
109 |
| - attr.remove if scrub_attribute?(attr.name) |
110 |
| - scrub_attribute(node, attr) |
| 114 | + scrub_css_attribute(node) |
| 115 | + else |
| 116 | + Loofah::HTML5::Scrub.scrub_attributes(node) |
111 | 117 | end
|
112 |
| - |
113 |
| - scrub_css_attribute(node) |
114 |
| - else |
115 |
| - Loofah::HTML5::Scrub.scrub_attributes(node) |
116 | 118 | end
|
117 |
| - end |
118 | 119 |
|
119 |
| - def scrub_css_attribute(node) |
120 |
| - if Loofah::HTML5::Scrub.respond_to?(:scrub_css_attribute) |
121 |
| - Loofah::HTML5::Scrub.scrub_css_attribute(node) |
122 |
| - else |
123 |
| - style = node.attributes['style'] |
124 |
| - style.value = Loofah::HTML5::Scrub.scrub_css(style.value) if style |
| 120 | + def scrub_css_attribute(node) |
| 121 | + if Loofah::HTML5::Scrub.respond_to?(:scrub_css_attribute) |
| 122 | + Loofah::HTML5::Scrub.scrub_css_attribute(node) |
| 123 | + else |
| 124 | + style = node.attributes["style"] |
| 125 | + style.value = Loofah::HTML5::Scrub.scrub_css(style.value) if style |
| 126 | + end |
125 | 127 | end
|
126 |
| - end |
127 | 128 |
|
128 |
| - def validate!(var, name) |
129 |
| - if var && !var.is_a?(Enumerable) |
130 |
| - raise ArgumentError, "You should pass :#{name} as an Enumerable" |
| 129 | + def validate!(var, name) |
| 130 | + if var && !var.is_a?(Enumerable) |
| 131 | + raise ArgumentError, "You should pass :#{name} as an Enumerable" |
| 132 | + end |
| 133 | + var |
131 | 134 | end
|
132 |
| - var |
133 |
| - end |
134 | 135 |
|
135 |
| - def scrub_attribute(node, attr_node) |
136 |
| - attr_name = if attr_node.namespace |
137 |
| - "#{attr_node.namespace.prefix}:#{attr_node.node_name}" |
138 |
| - else |
139 |
| - attr_node.node_name |
140 |
| - end |
| 136 | + def scrub_attribute(node, attr_node) |
| 137 | + attr_name = if attr_node.namespace |
| 138 | + "#{attr_node.namespace.prefix}:#{attr_node.node_name}" |
| 139 | + else |
| 140 | + attr_node.node_name |
| 141 | + end |
141 | 142 |
|
142 |
| - if Loofah::HTML5::SafeList::ATTR_VAL_IS_URI.include?(attr_name) |
143 |
| - return if Loofah::HTML5::Scrub.scrub_uri_attribute(attr_node) |
144 |
| - end |
| 143 | + if Loofah::HTML5::SafeList::ATTR_VAL_IS_URI.include?(attr_name) |
| 144 | + return if Loofah::HTML5::Scrub.scrub_uri_attribute(attr_node) |
| 145 | + end |
145 | 146 |
|
146 |
| - if Loofah::HTML5::SafeList::SVG_ATTR_VAL_ALLOWS_REF.include?(attr_name) |
147 |
| - Loofah::HTML5::Scrub.scrub_attribute_that_allows_local_ref(attr_node) |
148 |
| - end |
| 147 | + if Loofah::HTML5::SafeList::SVG_ATTR_VAL_ALLOWS_REF.include?(attr_name) |
| 148 | + Loofah::HTML5::Scrub.scrub_attribute_that_allows_local_ref(attr_node) |
| 149 | + end |
149 | 150 |
|
150 |
| - if Loofah::HTML5::SafeList::SVG_ALLOW_LOCAL_HREF.include?(node.name) && attr_name == 'xlink:href' && attr_node.value =~ /^\s*[^#\s].*/m |
151 |
| - attr_node.remove |
152 |
| - end |
| 151 | + if Loofah::HTML5::SafeList::SVG_ALLOW_LOCAL_HREF.include?(node.name) && attr_name == "xlink:href" && attr_node.value =~ /^\s*[^#\s].*/m |
| 152 | + attr_node.remove |
| 153 | + end |
153 | 154 |
|
154 |
| - node.remove_attribute(attr_node.name) if attr_name == 'src' && attr_node.value !~ /[^[:space:]]/ |
| 155 | + node.remove_attribute(attr_node.name) if attr_name == "src" && attr_node.value !~ /[^[:space:]]/ |
155 | 156 |
|
156 |
| - Loofah::HTML5::Scrub.force_correct_attribute_escaping! node |
157 |
| - end |
| 157 | + Loofah::HTML5::Scrub.force_correct_attribute_escaping! node |
| 158 | + end |
158 | 159 | end
|
159 | 160 |
|
160 | 161 | # === Rails::Html::TargetScrubber
|
|
0 commit comments