Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/practical/html_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from html.parser import HTMLParser


class HTMLValidator:
"""Check whether HTML content is well-formed, i.e. every tag has a matching close tag.

This only verifies tag balance/nesting via a streaming `html.parser.HTMLParser`, not
conformance to the actual HTML5 element vocabulary or content model — unknown tag names
(e.g. `<foobar>`) are accepted as well-formed as long as they're properly closed.
"""

@staticmethod
def validate_html(path: str) -> bool:
"""Return True if the file at `path` is well-formed HTML, False otherwise.

Reads and parses the file in chunks so memory use stays bounded regardless of file size.
"""
parser = TagChecker()

try:
with open(path, "r", encoding="utf-8") as f:
while chunk := f.read(64000):
parser.feed(chunk)
parser.close()
except FileNotFoundError as e:
raise e
except ValueError:
return False

return parser.tag_stack == []

@staticmethod
def validate_string(string: str) -> bool:
"""Return True if `string` is well-formed HTML, False otherwise."""
parser = TagChecker()

try:
parser.feed(string)
parser.close()
except ValueError:
return False
return parser.tag_stack == []


class TagChecker(HTMLParser):
"""Tracks open tags on a stack, raising ValueError on any mismatched or unclosed tag."""

def __init__(self):
super().__init__()
self.tag_stack = []

def handle_starttag(self, tag, attrs):
self.tag_stack.append(tag)

def handle_endtag(self, tag):
if not self.tag_stack or self.tag_stack[-1] != tag:
raise ValueError(f"Mismatched end tag: {tag}")
self.tag_stack.pop()

def close(self):
"""Flush any buffered data, then raise ValueError if any tags were left unclosed."""
super().close()
if self.tag_stack:
raise ValueError(f"Unclosed tags: {self.tag_stack}")
10 changes: 10 additions & 0 deletions tests/practical/test_data/invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>Test</title>
<head>
<body>
<h1>Hello, World!
<br/>
<a href="https://www.example.com">Example Link</a>
</body>
</html>
10 changes: 10 additions & 0 deletions tests/practical/test_data/valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Hello, World!</h1>
<br/>
<a href="https://www.example.com">Example Link</a>
</body>
</html>
35 changes: 35 additions & 0 deletions tests/practical/test_html_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from src.practical.html_parser import HTMLValidator


def test_html_file_validator():
assert HTMLValidator.validate_html("tests/practical/test_data/valid.html") is True
assert HTMLValidator.validate_html("tests/practical/test_data/invalid.html") is False


def test_invalid_file_path():
with pytest.raises(FileNotFoundError):
assert HTMLValidator.validate_html("tests/practical/test_data/nonexistent.html") is False


def test_invalid_html():
html = "<p>"
assert not HTMLValidator.validate_string(html)

html = "<p>test"
assert not HTMLValidator.validate_string(html)

html = "<h1><p>test</p></h1><p>"
assert not HTMLValidator.validate_string(html)


def test_valid_html():
html = ""
assert HTMLValidator.validate_string(html)

html = "<p></p>"
assert HTMLValidator.validate_string(html)

html = "<p>test</p><test></test>"
assert HTMLValidator.validate_string(html)
Loading