Skip to content

Instantly share code, notes, and snippets.

@jcarbaugh
Created August 26, 2011 18:12
Show Gist options
  • Save jcarbaugh/1174023 to your computer and use it in GitHub Desktop.
Save jcarbaugh/1174023 to your computer and use it in GitHub Desktop.
Can a div be used inside a figure in HTML5? Based on an older version of the spec.
METADATA_ELEMENTS = ('command', 'link', 'meta', 'noscript', 'script', 'style')
PHRASING_ELEMENTS = ('a', 'abbr', 'area', 'audio', 'b', 'bdo', 'br', 'button',
'canvas', 'cite', 'code', 'command', 'datalist', 'del', 'dfn', 'em', 'embed',
'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'map', 'mark',
'meter', 'noscript', 'object', 'output', 'progress', 'q', 'ruby', 'samp',
'script', 'select', 'small', 'span', 'strong', 'sub', 'sup', 'textarea',
'time', 'var', 'video')
FLOW_ELEMENTS = ('address', 'article', 'aside', 'blockquote',
'details', 'div', 'dl', 'fieldset', 'figure', 'footer', 'form',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr',
'menu', 'nav', 'ol', 'p', 'pre', 'section', 'table', 'ul') + PHRASING_ELEMENTS
TRANSPARENT_ELEMENTS = ('a', 'audio', 'canvas', 'del', 'ins', 'map',
'noscript', 'object', 'video')
ELEMENTS = {
'a': None,
'abbr': PHRASING_ELEMENTS,
'address': FLOW_ELEMENTS,
'area': None,
'article': ('style',) + FLOW_ELEMENTS,
'aside': ('style',) + FLOW_ELEMENTS,
'audio': ('source',),
'b': PHRASING_ELEMENTS,
'base': None,
'bdo': PHRASING_ELEMENTS,
'blockquote': FLOW_ELEMENTS,
'body': FLOW_ELEMENTS,
'br': None,
'button': FLOW_ELEMENTS,
'canvas': None,
'caption': FLOW_ELEMENTS,
'cite': PHRASING_ELEMENTS,
'code': PHRASING_ELEMENTS,
'col': None,
'colgroup': ('col',),
'command': None,
'datalist': ('option',) + PHRASING_ELEMENTS,
'dd': FLOW_ELEMENTS,
'del': None,
'details': ('summary',) + FLOW_ELEMENTS,
'dfn': PHRASING_ELEMENTS,
'div': ('style',) + FLOW_ELEMENTS,
'dl': ('dt','dd'),
'dt': PHRASING_ELEMENTS,
'em': PHRASING_ELEMENTS,
'embed': None,
'fieldset': ('legend',) + FLOW_ELEMENTS,
'figcaption': PHRASING_ELEMENTS,
'figure': ('figcaption',) + FLOW_ELEMENTS,
'footer': FLOW_ELEMENTS,
'form': FLOW_ELEMENTS,
'h1': PHRASING_ELEMENTS,
'h2': PHRASING_ELEMENTS,
'h3': PHRASING_ELEMENTS,
'h4': PHRASING_ELEMENTS,
'h5': PHRASING_ELEMENTS,
'h6': PHRASING_ELEMENTS,
'head': ('title','base') + METADATA_ELEMENTS,
'header': FLOW_ELEMENTS,
'hgroup': ('h1','h2','h3','h4','h5','h6'),
'hr': None,
'html': ('head','body'),
'i': PHRASING_ELEMENTS,
'iframe': None,
'img': None,
'input': None,
'ins': None,
'kbd': PHRASING_ELEMENTS,
'keygen': None,
'label': PHRASING_ELEMENTS,
'legend': PHRASING_ELEMENTS,
'li': FLOW_ELEMENTS,
'link': None,
'map': None,
'mark': PHRASING_ELEMENTS,
'menu': ('li',) + FLOW_ELEMENTS,
'meta': None,
'meter': PHRASING_ELEMENTS,
'nav': FLOW_ELEMENTS,
'noscript': ('link','meta','style'),
'object': ('param',),
'ol': ('li',),
'optgroup': ('option',),
'option': None,
'output': PHRASING_ELEMENTS,
'p': PHRASING_ELEMENTS,
'param': None,
'pre': PHRASING_ELEMENTS,
'progress': PHRASING_ELEMENTS,
'q': PHRASING_ELEMENTS,
'rp': PHRASING_ELEMENTS,
'rt': PHRASING_ELEMENTS,
'ruby': ('rt','rp') + PHRASING_ELEMENTS,
'samp': PHRASING_ELEMENTS,
'script': None,
'section': ('style',) + FLOW_ELEMENTS,
'select': ('optgroup','option'),
'small': PHRASING_ELEMENTS,
'source': None,
'span': PHRASING_ELEMENTS,
'strong': PHRASING_ELEMENTS,
'style': None,
'sub': PHRASING_ELEMENTS,
'summary': PHRASING_ELEMENTS,
'sup': PHRASING_ELEMENTS,
'table': ('caption','colgroup','tbody','thead','tfoot','tr'),
'tbody': ('tr',),
'td': FLOW_ELEMENTS,
'textarea': None,
'tfoot': ('tr',),
'th': FLOW_ELEMENTS,
'thead': ('tr',),
'time': PHRASING_ELEMENTS,
'title': None,
'tr': ('td','th'),
'ul': ('li',),
'var': PHRASING_ELEMENTS,
'video': ('source',),
}
class HTML5Error(Exception):
pass
def can_contain(outer, inner):
if outer in TRANSPARENT_ELEMENTS:
original_outer = outer
outer = raw_input(">>> %s is transparent, what is the parent element? " % outer)
elems = ELEMENTS.get(outer, [])
if elems is not None:
return inner in elems
if __name__ == '__main__':
import sys
def cmd_contains(*args):
(outer, inner) = args
is_valid = can_contain(outer, inner)
if is_valid is None:
print "%s cannot contain any elements" % outer
else:
contains = "can" if is_valid else "cannot"
print "%s %s contain %s" % (outer, contains, inner)
if len(sys.argv) < 3:
raise ValueError("you must specify a command: html5spec.py <parent element> <child element>")
cmd_contains(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment