HTMLKit
HTMLKit copied to clipboard
the tags inside <head> can't be converted, because they don't have a closing tag
I get the following error
Error Domain=NSXMLParserErrorDomain Code=76 "Line 5: Opening and ending tag mismatch: meta line 4 and head
" UserInfo={NSLocalizedDescription=Line 5: Opening and ending tag mismatch: meta line 4 and head
}
example Html file content is below. When I remove the content of the head tag, I get the parsed swift code without any problem.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Playground code:
do {
let path = Bundle.main.path(forResource: "ConvertUntitled", ofType: "html")!
try Converter.default.convert(file: URL(fileURLWithPath: path), option: .print)
} catch {
print(error)
}
The workaround is to add </meta>, </link> etc.
Hey @atacan , thats true. Its cause we use the XML classes in the Swift Foundation to convert HTML into HTMLKit. XML needs to have a close tag. I think <meta charset="utf-8" /> should have worked too.
I stumbled about this issue too. I am planning to implement a catch and recovery, but not sure if thats what the user want. Maybe a recovery description in the console would be enough. Still it is not an easy task, cause I need to know the position of the content, wich is tickling the issue.
I already experimented with it in my dev-fork. Many times it works. Sometimes not. But not sure yet, what's the best for the user. Let me know what you think.
To be fair, the code in the head tag may be the least often needed use case. It may be easier to write it directly in swift code. At least, my use case is more about converting some components I copy from the CSS frameworks' websites, as opposed to converting a complete html file. Making the error more descriptive telling the user they need to close the meta or link tags might be enough.
Sure, thanks for your feedback. Did the converter work for you? I mean on some other cases?
Yes. I'll open a pull request soon implementing the idea I posted in the Discussion section. Later I'll publish the small Mac app for that string-to-string conversion.