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