Skip to content

Commit 8d04b30

Browse files
committed
new behavior for <self-closed/> tags
- if an empty tag is the very last ‘character’ on a line, it will be extended to contain the \n that follows it (so “<a/>\n…” is equivalent to “<a>\n</a>…”) - this ends up being useful if you want to style an otherwise ‘blank’ line (e.g., by setting its leading/spacing for an arbitrary vertical-skip)
1 parent 7ac3522 commit 8d04b30

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

plotdevice/util/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,32 @@ def _enter(self, name, attrs):
370370

371371
def _chars(self, data):
372372
selector = tuple([e.tag for e in self.stack])
373+
374+
# handle special case where a self-closed tag precedes a '\n'
375+
if hasattr(self, '_crlf'):
376+
if data == "\n":
377+
selector = selector + (self._crlf.tag,)
378+
del self._crlf
379+
373380
self.regions[selector].append(tuple([self.cursor-self._offset, len(data)]))
374381
self.cursor += len(data)
375382
self.body.append(data)
376383
self.log(data)
377384

378385
def _leave(self, name):
379386
node = self.stack.pop()._replace(end=self.cursor)
387+
388+
# hang onto line-ending self-closed tags so they can be applied to the next '\n' in _chars
389+
if node.start==node.end:
390+
at = self._expat.CurrentByteIndex
391+
if self._xml[at-2:at]=='/>' and self._xml[at:at+1]=="\n":
392+
node = node._replace(end=node.start+1)
393+
self._crlf = node
394+
380395
self.nodes[name].append(node)
381396
self.log(u'</%s>'%(name), indent=-1)
397+
398+
# if we've exited the root node, clean up the parsed elements
382399
if name == INTERNAL:
383400
del self.nodes[INTERNAL]
384401
self.nodes = {tag:ordered(elts, 'start') for tag,elts in self.nodes.items()}

0 commit comments

Comments
 (0)