Skip to content

Commit c05cdac

Browse files
Throw IOException from writeHTML() when the file cannot be written
writeHTML() installed ParseException's error handler where the other writers install IOException's. testFailWriteHTML() called writeXML(), so the failure path went untested; it now calls writeHTML(). Fixes #92 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 389b069 commit c05cdac

3 files changed

Lines changed: 5 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ QueryPath Changelog
3131
- Fix `QueryPath\ParseException` and `QueryPath\IOException` losing the PHP warning they were raised from (issue #91). The message was the error number, as in `2 (/path/to/src/DOMQuery.php: 1343)`, and the code was always `0`. The message now carries the warning text and the code is the PHP error level (for example `E_WARNING`), so code that matches on the old message text will need updating
3232
- Fix `tag()` emitting an "Undefined property" warning and returning `NULL` when the first match is not an element, such as a text, comment or processing instruction node returned by `contents()`. It now returns an empty string, as it does for an empty match set (issue #93)
3333
- Fix QueryPath's error handler staying installed for the rest of the request when an exception other than a `QueryPath\Exception` (a `TypeError`, or one thrown by a stream wrapper, for example) escaped from parsing a document, inserting markup, or `writeXML()`/`writeHTML()` (issue #95)
34+
- Fix `writeHTML()` throwing `QueryPath\ParseException` instead of `QueryPath\IOException` when the file cannot be written, as `writeXML()` and `writeHTML5()` do (issue #92). `IOException` extends `ParseException`, so existing `catch (ParseException $e)` blocks still catch it
3435

3536
# 4.1.0
3637

‎src/DOMQuery.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ public function writeHTML($path = null)
13761376
if ($path === null) {
13771377
print $this->saveDocumentHTML();
13781378
} else {
1379-
self::withErrorHandler(ParseException::class, function () use ($path) {
1379+
self::withErrorHandler(IOException::class, function () use ($path) {
13801380
$this->saveDocumentHTML($path);
13811381
});
13821382
}

‎tests/QueryPath/DOMQueryTest.php‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,12 +1497,7 @@ public function testFailWriteXML()
14971497
{
14981498
$this->expectException(IOException::class);
14991499

1500-
try {
1501-
qp()->writeXML('./test/no-writing.xml');
1502-
} catch (Exception $e) {
1503-
//print $e->getMessage();
1504-
throw $e;
1505-
}
1500+
qp()->writeXML('./test/no-writing.xml');
15061501
}
15071502

15081503
public function testFailWriteXMLReportsWarning()
@@ -1518,24 +1513,14 @@ public function testFailWriteXHTML()
15181513
{
15191514
$this->expectException(IOException::class);
15201515

1521-
try {
1522-
qp()->writeXHTML('./test/no-writing.xml');
1523-
} catch (IOException $e) {
1524-
//print $e->getMessage();
1525-
throw $e;
1526-
}
1516+
qp()->writeXHTML('./test/no-writing.xml');
15271517
}
15281518

15291519
public function testFailWriteHTML()
15301520
{
15311521
$this->expectException(IOException::class);
15321522

1533-
try {
1534-
qp('<?xml version="1.0"?><foo/>')->writeXML('./test/no-writing.xml');
1535-
} catch (IOException $e) {
1536-
// print $e->getMessage();
1537-
throw $e;
1538-
}
1523+
htmlqp('<html><body><p>bar</p></body></html>')->writeHTML('./test/no-writing.html');
15391524
}
15401525

15411526
public function testFailWriteHTML5()

0 commit comments

Comments
 (0)