@@ -142,7 +142,7 @@ protected static function _loadXml($input, $options)
142
142
{
143
143
$ hasDisable = function_exists ('libxml_disable_entity_loader ' );
144
144
$ internalErrors = libxml_use_internal_errors (true );
145
- if ($ hasDisable && ! $ options ['loadEntities ' ]) {
145
+ if ($ hasDisable && empty ( $ options ['loadEntities ' ]) ) {
146
146
libxml_disable_entity_loader (true );
147
147
}
148
148
$ flags = 0 ;
@@ -162,7 +162,46 @@ protected static function _loadXml($input, $options)
162
162
} catch (Exception $ e ) {
163
163
throw new XmlException ('Xml cannot be read. ' . $ e ->getMessage (), null , $ e );
164
164
} finally {
165
- if ($ hasDisable && !$ options ['loadEntities ' ]) {
165
+ if ($ hasDisable && empty ($ options ['loadEntities ' ])) {
166
+ libxml_disable_entity_loader (false );
167
+ }
168
+ libxml_use_internal_errors ($ internalErrors );
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Parse the input html string and create either a SimpleXmlElement object or a DOMDocument.
174
+ *
175
+ * @param string $input The input html string to load.
176
+ * @param array $options The options to use. See Xml::build()
177
+ * @return \SimpleXMLElement|\DOMDocument
178
+ * @throws \Cake\Utility\Exception\XmlException
179
+ */
180
+ public static function loadHtml ($ input , $ options )
181
+ {
182
+ $ hasDisable = function_exists ('libxml_disable_entity_loader ' );
183
+ $ internalErrors = libxml_use_internal_errors (true );
184
+ if ($ hasDisable && empty ($ options ['loadEntities ' ])) {
185
+ libxml_disable_entity_loader (true );
186
+ }
187
+ $ flags = 0 ;
188
+ if (!empty ($ options ['parseHuge ' ])) {
189
+ $ flags |= LIBXML_PARSEHUGE ;
190
+ }
191
+ try {
192
+ $ xml = new DOMDocument ();
193
+ $ xml ->loadHTML ($ input , $ flags );
194
+
195
+ if ($ options ['return ' ] === 'simplexml ' || $ options ['return ' ] === 'simplexmlelement ' ) {
196
+ $ flags |= LIBXML_NOCDATA ;
197
+ $ xml = simplexml_import_dom ($ xml );
198
+ }
199
+
200
+ return $ xml ;
201
+ } catch (Exception $ e ) {
202
+ throw new XmlException ('Xml cannot be read. ' . $ e ->getMessage (), null , $ e );
203
+ } finally {
204
+ if ($ hasDisable && empty ($ options ['loadEntities ' ])) {
166
205
libxml_disable_entity_loader (false );
167
206
}
168
207
libxml_use_internal_errors ($ internalErrors );
0 commit comments