mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 14:55:24 +00:00
@@ -2014,7 +2014,7 @@ proc parseHtml*(s: Stream, filename: string,
|
||||
## Parses the XML from stream `s` and returns a ``XmlNode``. Every
|
||||
## occurred parsing error is added to the `errors` sequence.
|
||||
var x: XmlParser
|
||||
open(x, s, filename, {reportComments, reportWhitespace})
|
||||
open(x, s, filename, {reportComments, reportWhitespace, allowUnquotedAttribs})
|
||||
next(x)
|
||||
# skip the DOCTYPE:
|
||||
if x.kind == xmlSpecial: next(x)
|
||||
|
||||
@@ -180,6 +180,7 @@ type
|
||||
errEqExpected, ## ``=`` expected
|
||||
errQuoteExpected, ## ``"`` or ``'`` expected
|
||||
errEndOfCommentExpected ## ``-->`` expected
|
||||
errAttributeValueExpected ## non-empty attribute value expected
|
||||
|
||||
ParserState = enum
|
||||
stateStart, stateNormal, stateAttr, stateEmptyElementTag, stateError
|
||||
@@ -187,6 +188,7 @@ type
|
||||
XmlParseOption* = enum ## options for the XML parser
|
||||
reportWhitespace, ## report whitespace
|
||||
reportComments ## report comments
|
||||
allowUnquotedAttribs ## allow unquoted attribute values (for HTML)
|
||||
|
||||
XmlParser* = object of BaseLexer ## the parser object.
|
||||
a, b, c: string
|
||||
@@ -207,7 +209,8 @@ const
|
||||
"'>' expected",
|
||||
"'=' expected",
|
||||
"'\"' or \"'\" expected",
|
||||
"'-->' expected"
|
||||
"'-->' expected",
|
||||
"attribute value expected"
|
||||
]
|
||||
|
||||
proc open*(my: var XmlParser, input: Stream, filename: string,
|
||||
@@ -669,6 +672,21 @@ proc parseAttribute(my: var XmlParser) =
|
||||
pendingSpace = false
|
||||
add(my.b, buf[pos])
|
||||
inc(pos)
|
||||
elif allowUnquotedAttribs in my.options:
|
||||
const disallowedChars = {'"', '\'', '`', '=', '<', '>', ' ',
|
||||
'\0', '\t', '\L', '\F', '\f'}
|
||||
let startPos = pos
|
||||
while (let c = buf[pos]; c notin disallowedChars):
|
||||
if c == '&':
|
||||
my.bufpos = pos
|
||||
parseEntity(my, my.b)
|
||||
my.kind = xmlAttribute # parseEntity overwrites my.kind!
|
||||
pos = my.bufpos
|
||||
else:
|
||||
add(my.b, c)
|
||||
inc(pos)
|
||||
if pos == startPos:
|
||||
markError(my, errAttributeValueExpected)
|
||||
else:
|
||||
markError(my, errQuoteExpected)
|
||||
# error corrections: guess what was meant
|
||||
|
||||
Reference in New Issue
Block a user