# CSE 143, Winter 2009, Marty Stepp # Homework 2: HTML Validator (Python) # # Instructor-provided code. You should not modify this file! # a set of tags that don't need to be matched (self-closing) NON_MATCHING_TAGS = ["!doctype", "!--", "area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "link", "meta", "param"] # An HtmlTag object represents an HTML tag, such as or . class HtmlTag: # Constructs an HTML tag with the given element (e.g. "table") and type. # Self-closing tags like
are considered to be "opening" tags, # but return false from the requires_closing_tag method. def __init__(self, element, is_open_tag = True): self.element = element.lower() self.is_open_tag = is_open_tag # Returns True if the given other tag matches this tag; # that is, if they have the same element but opposite types, # such as and . def matches(self, other): return self.element == other.element and self.is_open_tag != other.is_open_tag # Returns true if this tag requires a matching closing tag; usually this # is true, except for certain elements such as br and img. def requires_closing_tag(self): return not self.element in NON_MATCHING_TAGS # Returns a string representation of this HTML tag, such as "". def __str__(self): return "<" + ("" if self.is_open_tag else "/") + self.element + ">"