Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: markupbase.py
"""Shared support for scanning document type declarations in HTML and XHTML.
[0] Fix | Delete
[1] Fix | Delete
This module is used as a foundation for the HTMLParser and sgmllib
[2] Fix | Delete
modules (indirectly, for htmllib as well). It has no documented
[3] Fix | Delete
public API and should not be used directly.
[4] Fix | Delete
[5] Fix | Delete
"""
[6] Fix | Delete
[7] Fix | Delete
import re
[8] Fix | Delete
[9] Fix | Delete
_declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match
[10] Fix | Delete
_declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match
[11] Fix | Delete
_commentclose = re.compile(r'--\s*>')
[12] Fix | Delete
_markedsectionclose = re.compile(r']\s*]\s*>')
[13] Fix | Delete
[14] Fix | Delete
# An analysis of the MS-Word extensions is available at
[15] Fix | Delete
# http://www.planetpublish.com/xmlarena/xap/Thursday/WordtoXML.pdf
[16] Fix | Delete
[17] Fix | Delete
_msmarkedsectionclose = re.compile(r']\s*>')
[18] Fix | Delete
[19] Fix | Delete
del re
[20] Fix | Delete
[21] Fix | Delete
[22] Fix | Delete
class ParserBase:
[23] Fix | Delete
"""Parser base class which provides some common support methods used
[24] Fix | Delete
by the SGML/HTML and XHTML parsers."""
[25] Fix | Delete
[26] Fix | Delete
def __init__(self):
[27] Fix | Delete
if self.__class__ is ParserBase:
[28] Fix | Delete
raise RuntimeError(
[29] Fix | Delete
"markupbase.ParserBase must be subclassed")
[30] Fix | Delete
[31] Fix | Delete
def error(self, message):
[32] Fix | Delete
raise NotImplementedError(
[33] Fix | Delete
"subclasses of ParserBase must override error()")
[34] Fix | Delete
[35] Fix | Delete
def reset(self):
[36] Fix | Delete
self.lineno = 1
[37] Fix | Delete
self.offset = 0
[38] Fix | Delete
[39] Fix | Delete
def getpos(self):
[40] Fix | Delete
"""Return current line number and offset."""
[41] Fix | Delete
return self.lineno, self.offset
[42] Fix | Delete
[43] Fix | Delete
# Internal -- update line number and offset. This should be
[44] Fix | Delete
# called for each piece of data exactly once, in order -- in other
[45] Fix | Delete
# words the concatenation of all the input strings to this
[46] Fix | Delete
# function should be exactly the entire input.
[47] Fix | Delete
def updatepos(self, i, j):
[48] Fix | Delete
if i >= j:
[49] Fix | Delete
return j
[50] Fix | Delete
rawdata = self.rawdata
[51] Fix | Delete
nlines = rawdata.count("\n", i, j)
[52] Fix | Delete
if nlines:
[53] Fix | Delete
self.lineno = self.lineno + nlines
[54] Fix | Delete
pos = rawdata.rindex("\n", i, j) # Should not fail
[55] Fix | Delete
self.offset = j-(pos+1)
[56] Fix | Delete
else:
[57] Fix | Delete
self.offset = self.offset + j-i
[58] Fix | Delete
return j
[59] Fix | Delete
[60] Fix | Delete
_decl_otherchars = ''
[61] Fix | Delete
[62] Fix | Delete
# Internal -- parse declaration (for use by subclasses).
[63] Fix | Delete
def parse_declaration(self, i):
[64] Fix | Delete
# This is some sort of declaration; in "HTML as
[65] Fix | Delete
# deployed," this should only be the document type
[66] Fix | Delete
# declaration ("<!DOCTYPE html...>").
[67] Fix | Delete
# ISO 8879:1986, however, has more complex
[68] Fix | Delete
# declaration syntax for elements in <!...>, including:
[69] Fix | Delete
# --comment--
[70] Fix | Delete
# [marked section]
[71] Fix | Delete
# name in the following list: ENTITY, DOCTYPE, ELEMENT,
[72] Fix | Delete
# ATTLIST, NOTATION, SHORTREF, USEMAP,
[73] Fix | Delete
# LINKTYPE, LINK, IDLINK, USELINK, SYSTEM
[74] Fix | Delete
rawdata = self.rawdata
[75] Fix | Delete
j = i + 2
[76] Fix | Delete
assert rawdata[i:j] == "<!", "unexpected call to parse_declaration"
[77] Fix | Delete
if rawdata[j:j+1] == ">":
[78] Fix | Delete
# the empty comment <!>
[79] Fix | Delete
return j + 1
[80] Fix | Delete
if rawdata[j:j+1] in ("-", ""):
[81] Fix | Delete
# Start of comment followed by buffer boundary,
[82] Fix | Delete
# or just a buffer boundary.
[83] Fix | Delete
return -1
[84] Fix | Delete
# A simple, practical version could look like: ((name|stringlit) S*) + '>'
[85] Fix | Delete
n = len(rawdata)
[86] Fix | Delete
if rawdata[j:j+2] == '--': #comment
[87] Fix | Delete
# Locate --.*-- as the body of the comment
[88] Fix | Delete
return self.parse_comment(i)
[89] Fix | Delete
elif rawdata[j] == '[': #marked section
[90] Fix | Delete
# Locate [statusWord [...arbitrary SGML...]] as the body of the marked section
[91] Fix | Delete
# Where statusWord is one of TEMP, CDATA, IGNORE, INCLUDE, RCDATA
[92] Fix | Delete
# Note that this is extended by Microsoft Office "Save as Web" function
[93] Fix | Delete
# to include [if...] and [endif].
[94] Fix | Delete
return self.parse_marked_section(i)
[95] Fix | Delete
else: #all other declaration elements
[96] Fix | Delete
decltype, j = self._scan_name(j, i)
[97] Fix | Delete
if j < 0:
[98] Fix | Delete
return j
[99] Fix | Delete
if decltype == "doctype":
[100] Fix | Delete
self._decl_otherchars = ''
[101] Fix | Delete
while j < n:
[102] Fix | Delete
c = rawdata[j]
[103] Fix | Delete
if c == ">":
[104] Fix | Delete
# end of declaration syntax
[105] Fix | Delete
data = rawdata[i+2:j]
[106] Fix | Delete
if decltype == "doctype":
[107] Fix | Delete
self.handle_decl(data)
[108] Fix | Delete
else:
[109] Fix | Delete
# According to the HTML5 specs sections "8.2.4.44 Bogus
[110] Fix | Delete
# comment state" and "8.2.4.45 Markup declaration open
[111] Fix | Delete
# state", a comment token should be emitted.
[112] Fix | Delete
# Calling unknown_decl provides more flexibility though.
[113] Fix | Delete
self.unknown_decl(data)
[114] Fix | Delete
return j + 1
[115] Fix | Delete
if c in "\"'":
[116] Fix | Delete
m = _declstringlit_match(rawdata, j)
[117] Fix | Delete
if not m:
[118] Fix | Delete
return -1 # incomplete
[119] Fix | Delete
j = m.end()
[120] Fix | Delete
elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
[121] Fix | Delete
name, j = self._scan_name(j, i)
[122] Fix | Delete
elif c in self._decl_otherchars:
[123] Fix | Delete
j = j + 1
[124] Fix | Delete
elif c == "[":
[125] Fix | Delete
# this could be handled in a separate doctype parser
[126] Fix | Delete
if decltype == "doctype":
[127] Fix | Delete
j = self._parse_doctype_subset(j + 1, i)
[128] Fix | Delete
elif decltype in ("attlist", "linktype", "link", "element"):
[129] Fix | Delete
# must tolerate []'d groups in a content model in an element declaration
[130] Fix | Delete
# also in data attribute specifications of attlist declaration
[131] Fix | Delete
# also link type declaration subsets in linktype declarations
[132] Fix | Delete
# also link attribute specification lists in link declarations
[133] Fix | Delete
self.error("unsupported '[' char in %s declaration" % decltype)
[134] Fix | Delete
else:
[135] Fix | Delete
self.error("unexpected '[' char in declaration")
[136] Fix | Delete
else:
[137] Fix | Delete
self.error(
[138] Fix | Delete
"unexpected %r char in declaration" % rawdata[j])
[139] Fix | Delete
if j < 0:
[140] Fix | Delete
return j
[141] Fix | Delete
return -1 # incomplete
[142] Fix | Delete
[143] Fix | Delete
# Internal -- parse a marked section
[144] Fix | Delete
# Override this to handle MS-word extension syntax <![if word]>content<![endif]>
[145] Fix | Delete
def parse_marked_section(self, i, report=1):
[146] Fix | Delete
rawdata= self.rawdata
[147] Fix | Delete
assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
[148] Fix | Delete
sectName, j = self._scan_name( i+3, i )
[149] Fix | Delete
if j < 0:
[150] Fix | Delete
return j
[151] Fix | Delete
if sectName in ("temp", "cdata", "ignore", "include", "rcdata"):
[152] Fix | Delete
# look for standard ]]> ending
[153] Fix | Delete
match= _markedsectionclose.search(rawdata, i+3)
[154] Fix | Delete
elif sectName in ("if", "else", "endif"):
[155] Fix | Delete
# look for MS Office ]> ending
[156] Fix | Delete
match= _msmarkedsectionclose.search(rawdata, i+3)
[157] Fix | Delete
else:
[158] Fix | Delete
self.error('unknown status keyword %r in marked section' % rawdata[i+3:j])
[159] Fix | Delete
if not match:
[160] Fix | Delete
return -1
[161] Fix | Delete
if report:
[162] Fix | Delete
j = match.start(0)
[163] Fix | Delete
self.unknown_decl(rawdata[i+3: j])
[164] Fix | Delete
return match.end(0)
[165] Fix | Delete
[166] Fix | Delete
# Internal -- parse comment, return length or -1 if not terminated
[167] Fix | Delete
def parse_comment(self, i, report=1):
[168] Fix | Delete
rawdata = self.rawdata
[169] Fix | Delete
if rawdata[i:i+4] != '<!--':
[170] Fix | Delete
self.error('unexpected call to parse_comment()')
[171] Fix | Delete
match = _commentclose.search(rawdata, i+4)
[172] Fix | Delete
if not match:
[173] Fix | Delete
return -1
[174] Fix | Delete
if report:
[175] Fix | Delete
j = match.start(0)
[176] Fix | Delete
self.handle_comment(rawdata[i+4: j])
[177] Fix | Delete
return match.end(0)
[178] Fix | Delete
[179] Fix | Delete
# Internal -- scan past the internal subset in a <!DOCTYPE declaration,
[180] Fix | Delete
# returning the index just past any whitespace following the trailing ']'.
[181] Fix | Delete
def _parse_doctype_subset(self, i, declstartpos):
[182] Fix | Delete
rawdata = self.rawdata
[183] Fix | Delete
n = len(rawdata)
[184] Fix | Delete
j = i
[185] Fix | Delete
while j < n:
[186] Fix | Delete
c = rawdata[j]
[187] Fix | Delete
if c == "<":
[188] Fix | Delete
s = rawdata[j:j+2]
[189] Fix | Delete
if s == "<":
[190] Fix | Delete
# end of buffer; incomplete
[191] Fix | Delete
return -1
[192] Fix | Delete
if s != "<!":
[193] Fix | Delete
self.updatepos(declstartpos, j + 1)
[194] Fix | Delete
self.error("unexpected char in internal subset (in %r)" % s)
[195] Fix | Delete
if (j + 2) == n:
[196] Fix | Delete
# end of buffer; incomplete
[197] Fix | Delete
return -1
[198] Fix | Delete
if (j + 4) > n:
[199] Fix | Delete
# end of buffer; incomplete
[200] Fix | Delete
return -1
[201] Fix | Delete
if rawdata[j:j+4] == "<!--":
[202] Fix | Delete
j = self.parse_comment(j, report=0)
[203] Fix | Delete
if j < 0:
[204] Fix | Delete
return j
[205] Fix | Delete
continue
[206] Fix | Delete
name, j = self._scan_name(j + 2, declstartpos)
[207] Fix | Delete
if j == -1:
[208] Fix | Delete
return -1
[209] Fix | Delete
if name not in ("attlist", "element", "entity", "notation"):
[210] Fix | Delete
self.updatepos(declstartpos, j + 2)
[211] Fix | Delete
self.error(
[212] Fix | Delete
"unknown declaration %r in internal subset" % name)
[213] Fix | Delete
# handle the individual names
[214] Fix | Delete
meth = getattr(self, "_parse_doctype_" + name)
[215] Fix | Delete
j = meth(j, declstartpos)
[216] Fix | Delete
if j < 0:
[217] Fix | Delete
return j
[218] Fix | Delete
elif c == "%":
[219] Fix | Delete
# parameter entity reference
[220] Fix | Delete
if (j + 1) == n:
[221] Fix | Delete
# end of buffer; incomplete
[222] Fix | Delete
return -1
[223] Fix | Delete
s, j = self._scan_name(j + 1, declstartpos)
[224] Fix | Delete
if j < 0:
[225] Fix | Delete
return j
[226] Fix | Delete
if rawdata[j] == ";":
[227] Fix | Delete
j = j + 1
[228] Fix | Delete
elif c == "]":
[229] Fix | Delete
j = j + 1
[230] Fix | Delete
while j < n and rawdata[j].isspace():
[231] Fix | Delete
j = j + 1
[232] Fix | Delete
if j < n:
[233] Fix | Delete
if rawdata[j] == ">":
[234] Fix | Delete
return j
[235] Fix | Delete
self.updatepos(declstartpos, j)
[236] Fix | Delete
self.error("unexpected char after internal subset")
[237] Fix | Delete
else:
[238] Fix | Delete
return -1
[239] Fix | Delete
elif c.isspace():
[240] Fix | Delete
j = j + 1
[241] Fix | Delete
else:
[242] Fix | Delete
self.updatepos(declstartpos, j)
[243] Fix | Delete
self.error("unexpected char %r in internal subset" % c)
[244] Fix | Delete
# end of buffer reached
[245] Fix | Delete
return -1
[246] Fix | Delete
[247] Fix | Delete
# Internal -- scan past <!ELEMENT declarations
[248] Fix | Delete
def _parse_doctype_element(self, i, declstartpos):
[249] Fix | Delete
name, j = self._scan_name(i, declstartpos)
[250] Fix | Delete
if j == -1:
[251] Fix | Delete
return -1
[252] Fix | Delete
# style content model; just skip until '>'
[253] Fix | Delete
rawdata = self.rawdata
[254] Fix | Delete
if '>' in rawdata[j:]:
[255] Fix | Delete
return rawdata.find(">", j) + 1
[256] Fix | Delete
return -1
[257] Fix | Delete
[258] Fix | Delete
# Internal -- scan past <!ATTLIST declarations
[259] Fix | Delete
def _parse_doctype_attlist(self, i, declstartpos):
[260] Fix | Delete
rawdata = self.rawdata
[261] Fix | Delete
name, j = self._scan_name(i, declstartpos)
[262] Fix | Delete
c = rawdata[j:j+1]
[263] Fix | Delete
if c == "":
[264] Fix | Delete
return -1
[265] Fix | Delete
if c == ">":
[266] Fix | Delete
return j + 1
[267] Fix | Delete
while 1:
[268] Fix | Delete
# scan a series of attribute descriptions; simplified:
[269] Fix | Delete
# name type [value] [#constraint]
[270] Fix | Delete
name, j = self._scan_name(j, declstartpos)
[271] Fix | Delete
if j < 0:
[272] Fix | Delete
return j
[273] Fix | Delete
c = rawdata[j:j+1]
[274] Fix | Delete
if c == "":
[275] Fix | Delete
return -1
[276] Fix | Delete
if c == "(":
[277] Fix | Delete
# an enumerated type; look for ')'
[278] Fix | Delete
if ")" in rawdata[j:]:
[279] Fix | Delete
j = rawdata.find(")", j) + 1
[280] Fix | Delete
else:
[281] Fix | Delete
return -1
[282] Fix | Delete
while rawdata[j:j+1].isspace():
[283] Fix | Delete
j = j + 1
[284] Fix | Delete
if not rawdata[j:]:
[285] Fix | Delete
# end of buffer, incomplete
[286] Fix | Delete
return -1
[287] Fix | Delete
else:
[288] Fix | Delete
name, j = self._scan_name(j, declstartpos)
[289] Fix | Delete
c = rawdata[j:j+1]
[290] Fix | Delete
if not c:
[291] Fix | Delete
return -1
[292] Fix | Delete
if c in "'\"":
[293] Fix | Delete
m = _declstringlit_match(rawdata, j)
[294] Fix | Delete
if m:
[295] Fix | Delete
j = m.end()
[296] Fix | Delete
else:
[297] Fix | Delete
return -1
[298] Fix | Delete
c = rawdata[j:j+1]
[299] Fix | Delete
if not c:
[300] Fix | Delete
return -1
[301] Fix | Delete
if c == "#":
[302] Fix | Delete
if rawdata[j:] == "#":
[303] Fix | Delete
# end of buffer
[304] Fix | Delete
return -1
[305] Fix | Delete
name, j = self._scan_name(j + 1, declstartpos)
[306] Fix | Delete
if j < 0:
[307] Fix | Delete
return j
[308] Fix | Delete
c = rawdata[j:j+1]
[309] Fix | Delete
if not c:
[310] Fix | Delete
return -1
[311] Fix | Delete
if c == '>':
[312] Fix | Delete
# all done
[313] Fix | Delete
return j + 1
[314] Fix | Delete
[315] Fix | Delete
# Internal -- scan past <!NOTATION declarations
[316] Fix | Delete
def _parse_doctype_notation(self, i, declstartpos):
[317] Fix | Delete
name, j = self._scan_name(i, declstartpos)
[318] Fix | Delete
if j < 0:
[319] Fix | Delete
return j
[320] Fix | Delete
rawdata = self.rawdata
[321] Fix | Delete
while 1:
[322] Fix | Delete
c = rawdata[j:j+1]
[323] Fix | Delete
if not c:
[324] Fix | Delete
# end of buffer; incomplete
[325] Fix | Delete
return -1
[326] Fix | Delete
if c == '>':
[327] Fix | Delete
return j + 1
[328] Fix | Delete
if c in "'\"":
[329] Fix | Delete
m = _declstringlit_match(rawdata, j)
[330] Fix | Delete
if not m:
[331] Fix | Delete
return -1
[332] Fix | Delete
j = m.end()
[333] Fix | Delete
else:
[334] Fix | Delete
name, j = self._scan_name(j, declstartpos)
[335] Fix | Delete
if j < 0:
[336] Fix | Delete
return j
[337] Fix | Delete
[338] Fix | Delete
# Internal -- scan past <!ENTITY declarations
[339] Fix | Delete
def _parse_doctype_entity(self, i, declstartpos):
[340] Fix | Delete
rawdata = self.rawdata
[341] Fix | Delete
if rawdata[i:i+1] == "%":
[342] Fix | Delete
j = i + 1
[343] Fix | Delete
while 1:
[344] Fix | Delete
c = rawdata[j:j+1]
[345] Fix | Delete
if not c:
[346] Fix | Delete
return -1
[347] Fix | Delete
if c.isspace():
[348] Fix | Delete
j = j + 1
[349] Fix | Delete
else:
[350] Fix | Delete
break
[351] Fix | Delete
else:
[352] Fix | Delete
j = i
[353] Fix | Delete
name, j = self._scan_name(j, declstartpos)
[354] Fix | Delete
if j < 0:
[355] Fix | Delete
return j
[356] Fix | Delete
while 1:
[357] Fix | Delete
c = self.rawdata[j:j+1]
[358] Fix | Delete
if not c:
[359] Fix | Delete
return -1
[360] Fix | Delete
if c in "'\"":
[361] Fix | Delete
m = _declstringlit_match(rawdata, j)
[362] Fix | Delete
if m:
[363] Fix | Delete
j = m.end()
[364] Fix | Delete
else:
[365] Fix | Delete
return -1 # incomplete
[366] Fix | Delete
elif c == ">":
[367] Fix | Delete
return j + 1
[368] Fix | Delete
else:
[369] Fix | Delete
name, j = self._scan_name(j, declstartpos)
[370] Fix | Delete
if j < 0:
[371] Fix | Delete
return j
[372] Fix | Delete
[373] Fix | Delete
# Internal -- scan a name token and the new position and the token, or
[374] Fix | Delete
# return -1 if we've reached the end of the buffer.
[375] Fix | Delete
def _scan_name(self, i, declstartpos):
[376] Fix | Delete
rawdata = self.rawdata
[377] Fix | Delete
n = len(rawdata)
[378] Fix | Delete
if i == n:
[379] Fix | Delete
return None, -1
[380] Fix | Delete
m = _declname_match(rawdata, i)
[381] Fix | Delete
if m:
[382] Fix | Delete
s = m.group()
[383] Fix | Delete
name = s.strip()
[384] Fix | Delete
if (i + len(s)) == n:
[385] Fix | Delete
return None, -1 # end of buffer
[386] Fix | Delete
return name.lower(), m.end()
[387] Fix | Delete
else:
[388] Fix | Delete
self.updatepos(declstartpos, i)
[389] Fix | Delete
self.error("expected name token at %r"
[390] Fix | Delete
% rawdata[declstartpos:declstartpos+20])
[391] Fix | Delete
[392] Fix | Delete
# To be overridden -- handlers for unknown objects
[393] Fix | Delete
def unknown_decl(self, data):
[394] Fix | Delete
pass
[395] Fix | Delete
[396] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function