Skip to content

Commit

Permalink
Only define global functions qp(), htmlqp(), and html5qp() if they ha…
Browse files Browse the repository at this point in the history
…ven't been defined already
  • Loading branch information
jakejackson1 committed Oct 31, 2024
1 parent 4a7ca5d commit 6f8813c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 65 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
QueryPath Changelog
===========================

# 4.0.1

- Only define global functions qp(), htmlqp(), and html5qp() if they haven't been defined already.

# 4.0.0

- Reverse logic in DomQuery::html5() so that DomQuery::html5() returns the content of the current match, and DomQuery::html5('') replaces the content of the current matches. This matches the existing logic used in DomQuery::html().
Expand Down
136 changes: 71 additions & 65 deletions src/qp_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* QueryPath functions.
*
* This file holds the QueryPath functions, qp() and htmlqp().
* This file holds the QueryPath global functions, qp(), htmlqp(), and html5qp())
*
* Usage:
*
Expand Down Expand Up @@ -47,7 +47,8 @@
* QueryPathExtension, and also at some of the simple extensions, such as QPXML.
*/

/**
if (! function_exists('qp')) {

Check warning on line 50 in src/qp_functions.php

View check run for this annotation

Codecov / codecov/patch

src/qp_functions.php#L50

Added line #L50 was not covered by tests
/**
* Build a new Query Path.
* This builds a new Query Path object. The new object can be used for
* reading, search, and modifying a document.
Expand Down Expand Up @@ -155,71 +156,76 @@
* @return mixed|DOMQuery
* Or possibly another QueryPath-like object if you overrode QueryPath_class.
*/
function qp($document = null, $selector = null, array $options = [])
{
return QueryPath::with($document, $selector, $options);
function qp($document = null, $selector = null, array $options = [])
{
return QueryPath::with($document, $selector, $options);
}
}

/**
* A special-purpose version of {@link qp()} designed specifically for HTML.
*
* XHTML (if valid) can be easily parsed by {@link qp()} with no problems. However,
* because of the way that libxml handles HTML, there are several common steps that
* need to be taken to reliably parse non-XML HTML documents. This function is
* a convenience tool for configuring QueryPath to parse HTML.
*
* The following options are automatically set unless overridden:
* - ignore_parser_warnings: TRUE
* - convert_to_encoding: ISO-8859-1 (the best for the HTML parser).
* - convert_from_encoding: auto (autodetect encoding)
* - use_parser: html
*
* Parser warning messages are also suppressed, so if the parser emits a warning,
* the application will not be notified. This is equivalent to
* calling @code@qp()@endcode.
*
* Warning: Character set conversions will only work if the Multi-Byte (mb) library
* is installed and enabled. This is usually enabled, but not always.
*
* @ingroup querypath_core
*
* @param mixed $document
* @param string|null $selector
* @param array $options
*
* @return mixed|DOMQuery
* @see qp()
*/
function htmlqp($document = null, $selector = null, array $options = [])
{
return QueryPath::withHTML($document, $selector, $options);
if (! function_exists('htmlqp')) {

Check warning on line 165 in src/qp_functions.php

View check run for this annotation

Codecov / codecov/patch

src/qp_functions.php#L165

Added line #L165 was not covered by tests
/**
* A special-purpose version of {@link qp()} designed specifically for HTML.
*
* XHTML (if valid) can be easily parsed by {@link qp()} with no problems. However,
* because of the way that libxml handles HTML, there are several common steps that
* need to be taken to reliably parse non-XML HTML documents. This function is
* a convenience tool for configuring QueryPath to parse HTML.
*
* The following options are automatically set unless overridden:
* - ignore_parser_warnings: TRUE
* - convert_to_encoding: ISO-8859-1 (the best for the HTML parser).
* - convert_from_encoding: auto (autodetect encoding)
* - use_parser: html
*
* Parser warning messages are also suppressed, so if the parser emits a warning,
* the application will not be notified. This is equivalent to
* calling @code@qp()@endcode.
*
* Warning: Character set conversions will only work if the Multi-Byte (mb) library
* is installed and enabled. This is usually enabled, but not always.
*
* @ingroup querypath_core
*
* @param mixed $document
* @param string|null $selector
* @param array $options
*
* @return mixed|DOMQuery
* @see qp()
*/
function htmlqp($document = null, $selector = null, array $options = [])
{
return QueryPath::withHTML($document, $selector, $options);
}
}

/**
* Parse HTML5 documents.
*
* This uses HTML5-PHP to parse the document. In actuality, this parser does
* a fine job with pre-HTML5 documents in most cases, though really old HTML
* (like 2.0) may have some substantial quirks.
*
* <b>Supported Options</b>
* Any options supported by HTML5-PHP are allowed here. Additionally, the
* following options have meaning to QueryPath.
* - QueryPath_class
*
*
* @param mixed $document
* @param string|null $selector
* A CSS3 selector.
*
* @param array $options
* An associative array of options, which is passed on into HTML5-PHP. Note
* that the standard QueryPath options may be ignored for this function,
* since it uses a different parser.
*
* @return mixed|DOMQuery
*/
function html5qp($document = null, $selector = null, array $options = [])
{
return QueryPath::withHTML5($document, $selector, $options);
if (! function_exists('html5qp')) {

Check warning on line 202 in src/qp_functions.php

View check run for this annotation

Codecov / codecov/patch

src/qp_functions.php#L202

Added line #L202 was not covered by tests
/**
* Parse HTML5 documents.
*
* This uses HTML5-PHP to parse the document. In actuality, this parser does
* a fine job with pre-HTML5 documents in most cases, though really old HTML
* (like 2.0) may have some substantial quirks.
*
* <b>Supported Options</b>
* Any options supported by HTML5-PHP are allowed here. Additionally, the
* following options have meaning to QueryPath.
* - QueryPath_class
*
*
* @param mixed $document
* @param string|null $selector
* A CSS3 selector.
*
* @param array $options
* An associative array of options, which is passed on into HTML5-PHP. Note
* that the standard QueryPath options may be ignored for this function,
* since it uses a different parser.
*
* @return mixed|DOMQuery
*/
function html5qp($document = null, $selector = null, array $options = [])
{
return QueryPath::withHTML5($document, $selector, $options);

Check warning on line 229 in src/qp_functions.php

View check run for this annotation

Codecov / codecov/patch

src/qp_functions.php#L229

Added line #L229 was not covered by tests
}
}

0 comments on commit 6f8813c

Please sign in to comment.