From fd8a530259173ac6bcd38a7134e41427e5949d1e Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 21 Jul 2013 11:14:49 -0400 Subject: [PATCH] Implement Tokenizer_handle_single_only_tag_end(), Tokenizer_handle_single_tag_end() --- mwparserfromhell/parser/tokenizer.c | 66 +++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/mwparserfromhell/parser/tokenizer.c b/mwparserfromhell/parser/tokenizer.c index 37c5639..b1c92f8 100644 --- a/mwparserfromhell/parser/tokenizer.c +++ b/mwparserfromhell/parser/tokenizer.c @@ -1760,7 +1760,34 @@ Tokenizer_handle_invalid_tag_start(Tokenizer* self) static PyObject* Tokenizer_handle_single_only_tag_end(Tokenizer* self) { - return NULL; + PyObject *top, *padding, *kwargs, *token; + + top = PyObject_CallMethod(self->topstack->stack, "pop", NULL); + if (!top) + return NULL; + padding = PyObject_GetAttrString(top, "padding"); + Py_DECREF(top); + if (!padding) + return NULL; + kwargs = PyDict_New(); + if (!kwargs) { + Py_DECREF(padding); + return NULL; + } + PyDict_SetItemString(kwargs, "padding", padding); + PyDict_SetItemString(kwargs, "implicit", Py_True); + Py_DECREF(padding); + token = PyObject_Call(TagCloseSelfclose, NOARGS, kwargs); + Py_DECREF(kwargs); + if (!token) + return NULL; + if (Tokenizer_emit(self, token)) { + Py_DECREF(token); + return NULL; + } + Py_DECREF(token); + self->head--; // Offset displacement done by handle_tag_close_open + return Tokenizer_pop(self); } /* @@ -1769,7 +1796,42 @@ Tokenizer_handle_single_only_tag_end(Tokenizer* self) static PyObject* Tokenizer_handle_single_tag_end(Tokenizer* self) { - return NULL; + PyObject *token = 0, *padding, *kwargs; + Py_ssize_t len, index; + int is_instance; + + len = PyList_GET_SIZE(self->topstack->stack); + for (index = 0; index < len; index++) { + token = PyList_GET_ITEM(self->topstack->stack, index); + is_instance = PyObject_IsInstance(token, TagCloseOpen); + if (is_instance == -1) + return NULL; + else if (is_instance == 1) + break; + } + if (!token) + return NULL; + padding = PyObject_GetAttrString(token, "padding"); + if (!padding) + return NULL; + kwargs = PyDict_New(); + if (!kwargs) { + Py_DECREF(padding); + return NULL; + } + PyDict_SetItemString(kwargs, "padding", padding); + PyDict_SetItemString(kwargs, "implicit", Py_True); + Py_DECREF(padding); + token = PyObject_Call(TagCloseSelfclose, NOARGS, kwargs); + Py_DECREF(kwargs); + if (!token) + return NULL; + if (PyList_SetItem(self->topstack->stack, index, token)) { + Py_DECREF(token); + return NULL; + } + Py_DECREF(token); + return Tokenizer_pop(self); } /*