Source code
Revision control
Copy as Markdown
Other Tools
# -*- coding: utf-8 -*-
import os
ccdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
invalid = {
"scheme-trailing-space": "a: foo.com",
"scheme-trailing-tab": "a:\tfoo.com",
"scheme-trailing-newline": "a:\nfoo.com",
"scheme-trailing-cr": "a:\rfoo.com",
"scheme-http-no-slash": "http:foo.com",
"scheme-http-no-slash-colon": "http::@c:29",
"scheme-http-no-slash-square-bracket": "http:[61:27]/:foo",
"scheme-http-backslash": "http:\\\\foo.com\\",
"scheme-http-single-slash": "http:/example.com/",
"scheme-ftp-single-slash": "ftp:/example.com/",
"scheme-https-single-slash": "https:/example.com/",
"scheme-data-single-slash": "data:/example.com/",
"scheme-ftp-no-slash": "ftp:example.com/",
"scheme-https-no-slash": "https:example.com/",
"path-leading-colon-backslash": ":\\",
"path-leading-colon-chars-backslash": ":foo.com\\",
"path-relative-square-brackets": "[61:24:74]:98",
"host-empty": "http://",
}
invalid_absolute = invalid.copy()
invalid_url_code_points = {
"fragment-backslash": "#\\",
"path-contains-space": "/a/ /c",
}
invalid.update(invalid_url_code_points)
invalid_absolute.update(invalid_url_code_points)
valid_absolute = {
"scheme-private": "a:foo.com",
"scheme-private-slash": "foo:/",
"scheme-private-slash-slash": "foo://",
"scheme-private-path": "foo:/bar.com/",
"scheme-private-single-letter": "c:/foo",
"scheme-private-single-slash": "madeupscheme:/example.com/",
"scheme-file-single-slash": "file:/example.com/",
"scheme-ftps-single-slash": "ftps:/example.com/",
"scheme-gopher-single-slash": "gopher:/example.com/",
"scheme-ws-single-slash": "ws:/example.com/",
"scheme-wss-single-slash": "wss:/example.com/",
"scheme-javascript-single-slash": "javascript:/example.com/",
"scheme-mailto-single-slash": "mailto:/example.com/",
"scheme-private-no-slash": "madeupscheme:example.com/",
"scheme-ftps-no-slash": "ftps:example.com/",
"scheme-gopher-no-slash": "gopher:example.com/",
"scheme-wss-no-slash": "wss:example.com/",
"scheme-mailto-no-slash": "mailto:example.com/",
"scheme-data-no-slash": "data:text/plain,foo",
}
valid = valid_absolute.copy()
valid_relative = {
"scheme-schemeless-relative": "//foo/bar",
"path-slash-only-relative": "/",
"path-simple-relative": "/a/b/c",
"path-percent-encoded-slash-relative": "/a%2fc",
"path-percent-encoded-slash-plus-slashes-relative": "/a/%2f/c",
"query-empty-no-path-relative": "?",
"fragment-empty-hash-only-no-path-relative": "#",
"fragment-slash-relative": "#/",
"fragment-semicolon-question-mark-relative": "#;?",
"fragment-non-ascii-relative": "#\u03B2",
}
valid.update(valid_relative)
invalid_absolute.update(valid_relative)
valid_relative_colon_dot = {
"scheme-none-relative": "foo.com",
"path-colon-relative": ":",
"path-leading-colon-letter-relative": ":a",
"path-leading-colon-chars-relative": ":foo.com",
"path-leading-colon-slash-relative": ":/",
"path-leading-colon-hash-relative": ":#",
"path-leading-colon-number-relative": ":23",
"path-slash-colon-number-relative": "/:23",
"path-leading-colon-colon-relative": "::",
"path-colon-colon-number-relative": "::23",
"path-contains-pile-of-poo": "http💩//:foo",
"path-slash-pile-of-poo": "/💩",
}
valid.update(valid_relative_colon_dot)
invalid_file = {
"scheme-file-backslash": "file:c:\\foo\\bar.html",
"scheme-file-single-slash-c-bar": "file:/C|/foo/bar",
}
invalid.update(invalid_file)
valid_file = {
"scheme-file-scheme-only": "file:",
"scheme-file-slash-only": "file:/",
"scheme-file-slash-slash-only": "file://",
"scheme-file-no-slash": "file:test",
}
valid.update(valid_file)
valid_absolute.update(valid_file)
warnings = {
"scheme-data-contains-fragment": "data:text/html,test#test",
}
element_attribute_pairs = [
"a href",
# "a ping", space-separated list of URLs; tested elsewhere
"area href",
# "area ping", space-separated list of URLs; tested elsewhere
"audio src",
"base href",
"blockquote cite",
"button formaction",
"del cite",
"embed src",
"form action",
"iframe src",
"img src", # srcset is tested elsewhere
"input formaction", # type=submit, type=image
"input src", # type=image
"input value", # type=url
"ins cite",
"link href",
"object data",
"q cite",
"script src",
"source src",
"track src",
"video poster",
"video src",
]
template = "<!DOCTYPE html>\n<meta charset=utf-8>\n"
def write_novalid_files():
for el, attr in (pair.split() for pair in element_attribute_pairs):
for desc, url in invalid.items():
if ("area" == el):
f = open(os.path.join(ccdir, "html/elements/area/href/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid href: %s</title>\n' % desc)
f.write('<map name=foo><%s %s="%s" alt></map>\n' % (el, attr, url))
f.close()
elif ("base" == el or "embed" == el):
f = open(os.path.join(ccdir, "html/elements/%s/%s/%s-novalid.html" % (el, attr, desc)), 'w')
f.write(template + '<title>invalid %s: %s</title>\n' % (attr, desc))
f.write('<%s %s="%s">\n' % (el, attr, url))
f.close()
elif ("img" == el):
f = open(os.path.join(ccdir, "html/elements/img/src/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid src: %s</title>\n' % desc)
f.write('<img src="%s" alt>\n' % url)
f.close()
elif ("input" == el and "src" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-image-src/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid src: %s</title>\n' % desc)
f.write('<%s type=image alt="foo" %s="%s">\n' % (el, attr, url))
f.close()
elif ("input" == el and "formaction" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-submit-formaction/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid formaction: %s</title>\n' % desc)
f.write('<%s type=submit %s="%s">\n' % (el, attr, url))
f.close()
f = open(os.path.join(ccdir, "html/elements/input/type-image-formaction/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid formaction: %s</title>\n' % desc)
f.write('<%s type=image alt="foo" %s="%s">\n' % (el, attr, url))
f.close()
elif ("input" == el and "value" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-url-value/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid value attribute: %s</title>\n' % desc)
f.write('<%s type=url %s="%s">\n' % (el, attr, url))
f.close()
elif ("link" == el):
f = open(os.path.join(ccdir, "html/elements/link/href/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid href: %s</title>\n' % desc)
f.write('<link href="%s" rel=help>\n' % url)
f.close()
elif ("source" == el or "track" == el):
f = open(os.path.join(ccdir, "html/elements/%s/%s/%s-novalid.html" % (el, attr, desc)), 'w')
f.write(template + '<title>invalid %s: %s</title>\n' % (attr, desc))
f.write('<video><%s %s="%s"></video>\n' % (el, attr, url))
f.close()
else:
f = open(os.path.join(ccdir, "html/elements/%s/%s/%s-novalid.html" % (el, attr, desc)), 'w')
f.write(template + '<title>invalid %s: %s</title>\n' % (attr, desc))
f.write('<%s %s="%s"></%s>\n' % (el, attr, url, el))
f.close()
for desc, url in invalid.items():
f = open(os.path.join(ccdir, "html/microdata/itemid/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid itemid: %s</title>\n' % desc)
f.close()
for desc, url in invalid_absolute.items():
f = open(os.path.join(ccdir, "html/microdata/itemtype/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid itemtype: %s</title>\n' % desc)
f.write('<div itemtype="%s" itemscope></div>\n' % url)
f.close()
f = open(os.path.join(ccdir, "html/elements/input/type-url-value/%s-novalid.html" % desc), 'w')
f.write(template + '<title>invalid value attribute: %s</title>\n' %desc)
f.write('<input type=url value="%s">\n' % url)
f.close()
def write_haswarn_files():
for el, attr in (pair.split() for pair in element_attribute_pairs):
for desc, url in warnings.items():
if ("area" == el):
f = open(os.path.join(ccdir, "html/elements/area/href/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<map name=foo><%s %s="%s" alt></map>\n' % (el, attr, url))
f.close()
elif ("base" == el or "embed" == el):
f = open(os.path.join(ccdir, "html/elements/%s/%s/%s-haswarn.html" % (el, attr, desc)), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s %s="%s">\n' % (el, attr, url))
f.close()
elif ("img" == el):
f = open(os.path.join(ccdir, "html/elements/img/src/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s %s="%s" alt>\n' % (el, attr, url))
f.close()
elif ("input" == el and "src" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-image-src/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s type=image alt="foo" %s="%s">\n' % (el, attr, url))
f.close()
elif ("input" == el and "formaction" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-submit-formaction/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s type=submit %s="%s">\n' % (el, attr, url))
f.close()
f = open(os.path.join(ccdir, "html/elements/input/type-image-formaction/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s type=image alt="foo" %s="%s">\n' % (el, attr, url))
f.close()
elif ("input" == el and "value" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-url-value/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s type=url %s="%s">\n' % (el, attr, url))
f.close()
elif ("link" == el):
f = open(os.path.join(ccdir, "html/elements/link/href/%s-haswarn.html" % desc), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<%s %s="%s" rel=help>\n' % (el, attr, url))
f.close()
elif ("source" == el or "track" == el):
f = open(os.path.join(ccdir, "html/elements/%s/%s/%s-haswarn.html" % (el, attr, desc)), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (attr, desc))
f.write('<video><%s %s="%s"></video>\n' % (el, attr, url))
f.close()
else:
f = open(os.path.join(ccdir, "html/elements/%s/%s/%s-haswarn.html" % (el, attr, desc)), 'w')
f.write(template + '<title>%s warning: %s</title>\n' % (url, desc))
f.write('<%s %s="%s"></%s>\n' % (el, attr, url, el))
f.close()
for desc, url in warnings.items():
f = open(os.path.join(ccdir, "html/microdata/itemtype-%s-haswarn.html" % desc ), 'w')
f.write(template + '<title>warning: %s</title>\n' % desc)
f.write('<div itemtype="%s" itemscope></div>\n' % url)
f.close()
f = open(os.path.join(ccdir, "html/microdata/itemid-%s-haswarn.html" % desc), 'w')
f.write(template + '<title>warning: %s</title>\n' % desc)
f.close()
def write_isvalid_files():
for el, attr in (pair.split() for pair in element_attribute_pairs):
if ("base" == el):
continue
if ("html" == el):
continue
elif ("input" == el and "value" == attr):
continue
elif ("input" == el and "formaction" == attr):
fs = open(os.path.join(ccdir, "html/elements/input/type-submit-formaction-isvalid.html"), 'w')
fs.write(template + '<title>valid formaction</title>\n')
fi = open(os.path.join(ccdir, "html/elements/input/type-image-formaction-isvalid.html"), 'w')
fi.write(template + '<title>valid formaction</title>\n')
elif ("input" == el and "src" == attr):
f = open(os.path.join(ccdir, "html/elements/input/type-image-src-isvalid.html"), 'w')
f.write(template + '<title>valid src</title>\n')
else:
f = open(os.path.join(ccdir, "html/elements/%s/%s-isvalid.html" % (el, attr)), 'w')
f.write(template + '<title>valid %s</title>\n' % attr)
for desc, url in valid.items():
if ("area" == el):
f.write('<map name=foo><%s %s="%s" alt></map><!-- %s -->\n' % (el, attr, url, desc))
elif ("embed" == el):
f.write('<%s %s="%s"><!-- %s -->\n' % (el, attr, url, desc))
elif ("img" == el):
f.write('<%s %s="%s" alt><!-- %s -->\n' % (el, attr, url, desc))
elif ("input" == el and "src" == attr):
f.write('<%s type=image alt="foo" %s="%s"><!-- %s -->\n' % (el, attr, url, desc))
elif ("input" == el and "formaction" == attr):
fs.write('<%s type=submit %s="%s"><!-- %s -->\n' % (el, attr, url, desc))
fi.write('<%s type=image alt="foo" %s="%s"><!-- %s -->\n' % (el, attr, url, desc))
elif ("link" == el):
f.write('<%s %s="%s" rel=help><!-- %s -->\n' % (el, attr, url, desc))
elif ("source" == el or "track" == el):
f.write('<video><%s %s="%s"></video><!-- %s -->\n' % (el, attr, url, desc))
else:
f.write('<%s %s="%s"></%s><!-- %s -->\n' % (el, attr, url, el, desc))
if ("input" == el and "formaction" == attr):
fs.close()
fi.close()
else:
if ("a" == el and "href" == attr):
f.write('<a href=""></a><!-- empty-href -->\n')
f.close()
for desc, url in valid.items():
f = open(os.path.join(ccdir, "html/elements/base/href/%s-isvalid.html" % desc), 'w')
f.write(template + '<title>valid href: %s</title>\n' % desc)
f.write('<base href="%s">\n' % url)
f.close()
f = open(os.path.join(ccdir, "html/elements/meta/refresh-isvalid.html"), 'w')
f.write(template + '<title>valid meta refresh</title>\n')
for desc, url in valid.items():
f.write('<meta http-equiv=refresh content="0; URL=%s"><!-- %s -->\n' % (url, desc))
f.close()
f = open(os.path.join(ccdir, "html/microdata/itemid-isvalid.html"), 'w')
f.write(template + '<title>valid itemid</title>\n')
for desc, url in valid.items():
f.close()
f = open(os.path.join(ccdir, "html/microdata/itemtype-isvalid.html"), 'w')
f.write(template + '<title>valid itemtype</title>\n')
for desc, url in valid_absolute.items():
f.write('<div itemtype="%s" itemscope></div><!-- %s -->\n' % (url, desc))
f.close()
f = open(os.path.join(ccdir, "html/elements/input/type-url-value-isvalid.html"), 'w')
f.write(template + '<title>valid value attribute</title>\n')
for desc, url in valid_absolute.items():
f.write('<input type=url value="%s"><!-- %s -->\n' % (url, desc))
f.close()
write_novalid_files()
write_haswarn_files()
write_isvalid_files()
# vim: ts=4:sw=4