/** * Create a Document * * \param doc Pointer to location to receive created document * \param daf The default action fetcher * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion. * * The returned document will already be referenced. */ dom_exception _dom_document_create(dom_events_default_action_fetcher daf, dom_document **doc) { dom_document *d; dom_exception err; /* Create document */ d = malloc(sizeof(dom_document)); if (d == NULL) return DOM_NO_MEM_ERR; /* Initialise the virtual table */ d->base.base.vtable = &document_vtable; d->base.vtable = &document_protect_vtable; /* Initialise base class -- the Document has no parent, so * destruction will be attempted as soon as its reference count * reaches zero. Documents own themselves (this simplifies the * rest of the code, as it doesn't need to special case Documents) */ err = _dom_document_initialise(d, daf); if (err != DOM_NO_ERR) { /* Clean up document */ free(d); return err; } *doc = d; return DOM_NO_ERR; }
/* Initialise a HTMLDocument */ dom_exception _dom_html_document_initialise(dom_html_document *doc, dom_events_default_action_fetcher daf, void *daf_ctx) { dom_exception error; int sidx; error = _dom_document_initialise(&doc->base, daf, daf_ctx); if (error != DOM_NO_ERR) return error; doc->title = NULL; doc->referrer = NULL; doc->domain = NULL; doc->url = NULL; doc->cookie = NULL; doc->body = NULL; doc->memoised = calloc(sizeof(dom_string *), hds_COUNT); if (doc->memoised == NULL) { error = DOM_NO_MEM_ERR; goto out; } #define HTML_DOCUMENT_STRINGS_ACTION(attr,str) \ error = dom_string_create_interned((const uint8_t *) #str, \ SLEN(#str), &doc->memoised[hds_##attr]); \ if (error != DOM_NO_ERR) { \ goto out; \ } #include "html_document_strings.h" #undef HTML_DOCUMENT_STRINGS_ACTION out: if (doc->memoised != NULL && error != DOM_NO_ERR) { for(sidx = 0; sidx < hds_COUNT; ++sidx) { if (doc->memoised[sidx] != NULL) { dom_string_unref(doc->memoised[sidx]); } } free(doc->memoised); doc->memoised = NULL; } return error; }