| File: | content/handlers/html/html.c |
| Warning: | line 323, column 29 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | |||
| 2 | * Copyright 2007 James Bursa <bursa@users.sourceforge.net> | |||
| 3 | * Copyright 2010 Michael Drake <tlsa@netsurf-browser.org> | |||
| 4 | * | |||
| 5 | * This file is part of NetSurf, http://www.netsurf-browser.org/ | |||
| 6 | * | |||
| 7 | * NetSurf is free software; you can redistribute it and/or modify | |||
| 8 | * it under the terms of the GNU General Public License as published by | |||
| 9 | * the Free Software Foundation; version 2 of the License. | |||
| 10 | * | |||
| 11 | * NetSurf is distributed in the hope that it will be useful, | |||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| 14 | * GNU General Public License for more details. | |||
| 15 | * | |||
| 16 | * You should have received a copy of the GNU General Public License | |||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 18 | */ | |||
| 19 | ||||
| 20 | /** | |||
| 21 | * \file | |||
| 22 | * Implementation of HTML content handling. | |||
| 23 | */ | |||
| 24 | ||||
| 25 | #include <assert.h> | |||
| 26 | #include <stdint.h> | |||
| 27 | #include <string.h> | |||
| 28 | #include <strings.h> | |||
| 29 | #include <stdlib.h> | |||
| 30 | #include <nsutils/time.h> | |||
| 31 | ||||
| 32 | #include "utils/utils.h" | |||
| 33 | #include "utils/config.h" | |||
| 34 | #include "utils/corestrings.h" | |||
| 35 | #include "utils/http.h" | |||
| 36 | #include "utils/libdom.h" | |||
| 37 | #include "utils/log.h" | |||
| 38 | #include "utils/messages.h" | |||
| 39 | #include "utils/talloc.h" | |||
| 40 | #include "utils/utf8.h" | |||
| 41 | #include "utils/nsoption.h" | |||
| 42 | #include "utils/string.h" | |||
| 43 | #include "utils/ascii.h" | |||
| 44 | #include "netsurf/content.h" | |||
| 45 | #include "netsurf/browser_window.h" | |||
| 46 | #include "netsurf/utf8.h" | |||
| 47 | #include "netsurf/keypress.h" | |||
| 48 | #include "netsurf/layout.h" | |||
| 49 | #include "netsurf/misc.h" | |||
| 50 | #include "content/hlcache.h" | |||
| 51 | #include "content/content_factory.h" | |||
| 52 | #include "content/textsearch.h" | |||
| 53 | #include "desktop/selection.h" | |||
| 54 | #include "desktop/scrollbar.h" | |||
| 55 | #include "desktop/textarea.h" | |||
| 56 | #include "netsurf/bitmap.h" | |||
| 57 | #include "javascript/js.h" | |||
| 58 | #include "desktop/gui_internal.h" | |||
| 59 | ||||
| 60 | #include "html/html.h" | |||
| 61 | #include "html/private.h" | |||
| 62 | #include "html/dom_event.h" | |||
| 63 | #include "html/css.h" | |||
| 64 | #include "html/object.h" | |||
| 65 | #include "html/html_save.h" | |||
| 66 | #include "html/interaction.h" | |||
| 67 | #include "html/box.h" | |||
| 68 | #include "html/box_construct.h" | |||
| 69 | #include "html/box_inspect.h" | |||
| 70 | #include "html/form_internal.h" | |||
| 71 | #include "html/imagemap.h" | |||
| 72 | #include "html/layout.h" | |||
| 73 | #include "html/textselection.h" | |||
| 74 | ||||
| 75 | #define CHUNK4096 4096 | |||
| 76 | ||||
| 77 | /* Change these to 1 to cause a dump to stderr of the frameset or box | |||
| 78 | * when the trees have been built. | |||
| 79 | */ | |||
| 80 | #define ALWAYS_DUMP_FRAMESET0 0 | |||
| 81 | #define ALWAYS_DUMP_BOX0 0 | |||
| 82 | ||||
| 83 | static const char *html_types[] = { | |||
| 84 | "application/xhtml+xml", | |||
| 85 | "text/html" | |||
| 86 | }; | |||
| 87 | ||||
| 88 | /** | |||
| 89 | * Fire an event at the DOM | |||
| 90 | * | |||
| 91 | * Helper that swallows DOM errors. | |||
| 92 | * | |||
| 93 | * \param[in] event the event to fire at the DOM | |||
| 94 | * \param[in] target the event target | |||
| 95 | * \return true on success | |||
| 96 | */ | |||
| 97 | static bool_Bool fire_dom_event(dom_event *event, dom_node *target) | |||
| 98 | { | |||
| 99 | dom_exception exc; | |||
| 100 | bool_Bool result; | |||
| 101 | ||||
| 102 | exc = dom_event_target_dispatch_event(target, event, &result)dom_event_target_dispatch_event((dom_event_target *) (target) , (struct dom_event *) (event), (_Bool *) (&result)); | |||
| 103 | if (exc != DOM_NO_ERR) { | |||
| 104 | return false0; | |||
| 105 | } | |||
| 106 | ||||
| 107 | return result; | |||
| 108 | } | |||
| 109 | ||||
| 110 | /* Exported interface, see html_internal.h */ | |||
| 111 | bool_Bool fire_generic_dom_event(dom_string *type, dom_node *target, | |||
| 112 | bool_Bool bubbles, bool_Bool cancelable) | |||
| 113 | { | |||
| 114 | dom_exception exc; | |||
| 115 | dom_event *evt; | |||
| 116 | bool_Bool result; | |||
| 117 | ||||
| 118 | exc = dom_event_create(&evt)_dom_event_create((dom_event **) (&evt)); | |||
| 119 | if (exc != DOM_NO_ERR) return false0; | |||
| 120 | exc = dom_event_init(evt, type, bubbles, cancelable)_dom_event_init((dom_event *) (evt), (dom_string *) (type), ( _Bool) (bubbles), (_Bool) (cancelable)); | |||
| 121 | if (exc != DOM_NO_ERR) { | |||
| 122 | dom_event_unref(evt)_dom_event_unref((dom_event *) (evt)); | |||
| 123 | return false0; | |||
| 124 | } | |||
| 125 | NSLOG(netsurf, INFO, "Dispatching '%*s' against %p",do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 126 , }; nslog__log(&_nslog_ctx, "Dispatching '%*s' against %p" , (int)dom_string_length(type), dom_string_data(type), target ); } } while(0) | |||
| 126 | (int)dom_string_length(type), dom_string_data(type), target)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 126 , }; nslog__log(&_nslog_ctx, "Dispatching '%*s' against %p" , (int)dom_string_length(type), dom_string_data(type), target ); } } while(0); | |||
| 127 | result = fire_dom_event(evt, target); | |||
| 128 | dom_event_unref(evt)_dom_event_unref((dom_event *) (evt)); | |||
| 129 | return result; | |||
| 130 | } | |||
| 131 | ||||
| 132 | /* Exported interface, see html_internal.h */ | |||
| 133 | bool_Bool fire_dom_keyboard_event(dom_string *type, dom_node *target, | |||
| 134 | bool_Bool bubbles, bool_Bool cancelable, uint32_t key) | |||
| 135 | { | |||
| 136 | bool_Bool is_special = key <= 0x001F || (0x007F <= key && key <= 0x009F); | |||
| 137 | dom_string *dom_key = NULL((void*)0); | |||
| 138 | dom_keyboard_event *evt; | |||
| 139 | dom_exception exc; | |||
| 140 | bool_Bool result; | |||
| 141 | ||||
| 142 | if (is_special) { | |||
| 143 | switch (key) { | |||
| 144 | case NS_KEY_ESCAPE: | |||
| 145 | dom_key = dom_string_ref(corestring_dom_Escape); | |||
| 146 | break; | |||
| 147 | case NS_KEY_LEFT: | |||
| 148 | dom_key = dom_string_ref(corestring_dom_ArrowLeft); | |||
| 149 | break; | |||
| 150 | case NS_KEY_RIGHT: | |||
| 151 | dom_key = dom_string_ref(corestring_dom_ArrowRight); | |||
| 152 | break; | |||
| 153 | case NS_KEY_UP: | |||
| 154 | dom_key = dom_string_ref(corestring_dom_ArrowUp); | |||
| 155 | break; | |||
| 156 | case NS_KEY_DOWN: | |||
| 157 | dom_key = dom_string_ref(corestring_dom_ArrowDown); | |||
| 158 | break; | |||
| 159 | case NS_KEY_PAGE_UP: | |||
| 160 | dom_key = dom_string_ref(corestring_dom_PageUp); | |||
| 161 | break; | |||
| 162 | case NS_KEY_PAGE_DOWN: | |||
| 163 | dom_key = dom_string_ref(corestring_dom_PageDown); | |||
| 164 | break; | |||
| 165 | case NS_KEY_TEXT_START: | |||
| 166 | dom_key = dom_string_ref(corestring_dom_Home); | |||
| 167 | break; | |||
| 168 | case NS_KEY_TEXT_END: | |||
| 169 | dom_key = dom_string_ref(corestring_dom_End); | |||
| 170 | break; | |||
| 171 | default: | |||
| 172 | dom_key = NULL((void*)0); | |||
| 173 | break; | |||
| 174 | } | |||
| 175 | } else { | |||
| 176 | char utf8[6]; | |||
| 177 | size_t length = utf8_from_ucs4(key, utf8); | |||
| 178 | utf8[length] = '\0'; | |||
| 179 | ||||
| 180 | exc = dom_string_create((const uint8_t *)utf8, strlen(utf8), | |||
| 181 | &dom_key); | |||
| 182 | if (exc != DOM_NO_ERR) { | |||
| 183 | return exc; | |||
| 184 | } | |||
| 185 | } | |||
| 186 | ||||
| 187 | exc = dom_keyboard_event_create(&evt)_dom_keyboard_event_create((dom_keyboard_event **) (&evt) ); | |||
| 188 | if (exc != DOM_NO_ERR) { | |||
| 189 | dom_string_unref(dom_key); | |||
| 190 | return false0; | |||
| 191 | } | |||
| 192 | ||||
| 193 | exc = dom_keyboard_event_init(evt, type, bubbles, cancelable, NULL,_dom_keyboard_event_init((dom_keyboard_event *) (evt), (dom_string *) (type), (_Bool) (bubbles), (_Bool) (cancelable), (struct dom_abstract_view *) (((void*)0)), (dom_string *) (dom_key), (dom_string *) (( (void*)0)), (dom_key_location) (DOM_KEY_LOCATION_STANDARD), ( _Bool) (0), (_Bool) (0), (_Bool) (0), (_Bool) (0), (_Bool) (0 ), (_Bool) (0)) | |||
| 194 | dom_key, NULL, DOM_KEY_LOCATION_STANDARD, false,_dom_keyboard_event_init((dom_keyboard_event *) (evt), (dom_string *) (type), (_Bool) (bubbles), (_Bool) (cancelable), (struct dom_abstract_view *) (((void*)0)), (dom_string *) (dom_key), (dom_string *) (( (void*)0)), (dom_key_location) (DOM_KEY_LOCATION_STANDARD), ( _Bool) (0), (_Bool) (0), (_Bool) (0), (_Bool) (0), (_Bool) (0 ), (_Bool) (0)) | |||
| 195 | false, false, false, false, false)_dom_keyboard_event_init((dom_keyboard_event *) (evt), (dom_string *) (type), (_Bool) (bubbles), (_Bool) (cancelable), (struct dom_abstract_view *) (((void*)0)), (dom_string *) (dom_key), (dom_string *) (( (void*)0)), (dom_key_location) (DOM_KEY_LOCATION_STANDARD), ( _Bool) (0), (_Bool) (0), (_Bool) (0), (_Bool) (0), (_Bool) (0 ), (_Bool) (0)); | |||
| 196 | dom_string_unref(dom_key); | |||
| 197 | if (exc != DOM_NO_ERR) { | |||
| 198 | dom_event_unref(evt)_dom_event_unref((dom_event *) (evt)); | |||
| 199 | return false0; | |||
| 200 | } | |||
| 201 | ||||
| 202 | NSLOG(netsurf, INFO, "Dispatching '%*s' against %p",do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 203 , }; nslog__log(&_nslog_ctx, "Dispatching '%*s' against %p" , (int)dom_string_length(type), dom_string_data(type), target ); } } while(0) | |||
| 203 | (int)dom_string_length(type), dom_string_data(type), target)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 203 , }; nslog__log(&_nslog_ctx, "Dispatching '%*s' against %p" , (int)dom_string_length(type), dom_string_data(type), target ); } } while(0); | |||
| 204 | ||||
| 205 | result = fire_dom_event((dom_event *) evt, target); | |||
| 206 | dom_event_unref(evt)_dom_event_unref((dom_event *) (evt)); | |||
| 207 | return result; | |||
| 208 | } | |||
| 209 | ||||
| 210 | /** | |||
| 211 | * Perform post-box-creation conversion of a document | |||
| 212 | * | |||
| 213 | * \param c HTML content to complete conversion of | |||
| 214 | * \param success Whether box tree construction was successful | |||
| 215 | */ | |||
| 216 | static void html_box_convert_done(html_content *c, bool_Bool success) | |||
| 217 | { | |||
| 218 | nserror err; | |||
| 219 | dom_exception exc; /* returned by libdom functions */ | |||
| 220 | dom_node *html; | |||
| 221 | ||||
| 222 | NSLOG(netsurf, INFO, "DOM to box conversion complete (content %p)", c)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 222 , }; nslog__log(&_nslog_ctx, "DOM to box conversion complete (content %p)" , c); } } while(0); | |||
| 223 | ||||
| 224 | c->box_conversion_context = NULL((void*)0); | |||
| 225 | ||||
| 226 | /* Clean up and report error if unsuccessful or aborted */ | |||
| 227 | if ((success == false0) || (c->aborted)) { | |||
| 228 | html_object_free_objects(c); | |||
| 229 | ||||
| 230 | if (success == false0) { | |||
| 231 | content_broadcast_error(&c->base, NSERROR_BOX_CONVERT, NULL((void*)0)); | |||
| 232 | } else { | |||
| 233 | content_broadcast_error(&c->base, NSERROR_STOPPED, NULL((void*)0)); | |||
| 234 | } | |||
| 235 | ||||
| 236 | content_set_error(&c->base); | |||
| 237 | return; | |||
| 238 | } | |||
| 239 | ||||
| 240 | ||||
| 241 | #if ALWAYS_DUMP_BOX0 | |||
| 242 | box_dump(stderrstderr, c->layout->children, 0, true1); | |||
| 243 | #endif | |||
| 244 | #if ALWAYS_DUMP_FRAMESET0 | |||
| 245 | if (c->frameset) | |||
| 246 | html_dump_frameset(c->frameset, 0); | |||
| 247 | #endif | |||
| 248 | ||||
| 249 | exc = dom_document_get_document_element(c->document, (void *) &html)dom_document_get_document_element((dom_document *) (c->document ), (struct dom_element **) ((void *) &html)); | |||
| 250 | if ((exc != DOM_NO_ERR) || (html == NULL((void*)0))) { | |||
| 251 | /** @todo should this call html_object_free_objects(c); | |||
| 252 | * like the other error paths | |||
| 253 | */ | |||
| 254 | NSLOG(netsurf, INFO, "error retrieving html element from dom")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 254 , }; nslog__log(&_nslog_ctx, "error retrieving html element from dom" ); } } while(0); | |||
| 255 | content_broadcast_error(&c->base, NSERROR_DOM, NULL((void*)0)); | |||
| 256 | content_set_error(&c->base); | |||
| 257 | return; | |||
| 258 | } | |||
| 259 | ||||
| 260 | /* extract image maps - can't do this sensibly in dom_to_box */ | |||
| 261 | err = imagemap_extract(c); | |||
| 262 | if (err != NSERROR_OK) { | |||
| 263 | NSLOG(netsurf, INFO, "imagemap extraction failed")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 263 , }; nslog__log(&_nslog_ctx, "imagemap extraction failed" ); } } while(0); | |||
| 264 | html_object_free_objects(c); | |||
| 265 | content_broadcast_error(&c->base, err, NULL((void*)0)); | |||
| 266 | content_set_error(&c->base); | |||
| 267 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 268 | return; | |||
| 269 | } | |||
| 270 | /*imagemap_dump(c);*/ | |||
| 271 | ||||
| 272 | /* Destroy the parser binding */ | |||
| 273 | dom_hubbub_parser_destroy(c->parser); | |||
| 274 | c->parser = NULL((void*)0); | |||
| 275 | ||||
| 276 | content_set_ready(&c->base); | |||
| 277 | ||||
| 278 | html_proceed_to_done(c); | |||
| 279 | ||||
| 280 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 281 | } | |||
| 282 | ||||
| 283 | /* Documented in html_internal.h */ | |||
| 284 | nserror | |||
| 285 | html_proceed_to_done(html_content *html) | |||
| 286 | { | |||
| 287 | switch (content__get_status(&html->base)) { | |||
| 288 | case CONTENT_STATUS_READY: | |||
| 289 | if (html->base.active == 0) { | |||
| 290 | content_set_done(&html->base); | |||
| 291 | return NSERROR_OK; | |||
| 292 | } | |||
| 293 | break; | |||
| 294 | case CONTENT_STATUS_DONE: | |||
| 295 | /* fallthrough */ | |||
| 296 | case CONTENT_STATUS_LOADING: | |||
| 297 | return NSERROR_OK; | |||
| 298 | default: | |||
| 299 | NSLOG(netsurf, ERROR, "Content status unexpectedly not LOADING/READY/DONE")do { if (NSLOG_LEVEL_ERROR >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_ERROR, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 299 , }; nslog__log(&_nslog_ctx, "Content status unexpectedly not LOADING/READY/DONE" ); } } while(0); | |||
| 300 | break; | |||
| 301 | } | |||
| 302 | return NSERROR_UNKNOWN; | |||
| 303 | } | |||
| 304 | ||||
| 305 | ||||
| 306 | static void html_get_dimensions(html_content *htmlc) | |||
| 307 | { | |||
| 308 | css_fixed device_dpi = nscss_screen_dpi; | |||
| 309 | unsigned f_size; | |||
| 310 | unsigned f_min; | |||
| 311 | unsigned w; | |||
| 312 | unsigned h; | |||
| 313 | union content_msg_data msg_data = { | |||
| 314 | .getdims = { | |||
| 315 | .viewport_width = &w, | |||
| 316 | .viewport_height = &h, | |||
| 317 | }, | |||
| 318 | }; | |||
| 319 | ||||
| 320 | content_broadcast(&htmlc->base, CONTENT_MSG_GETDIMS, &msg_data); | |||
| 321 | ||||
| 322 | ||||
| 323 | w = css_unit_device2css_px(INTTOFIX(w)(css_int_to_fixed(w)), device_dpi); | |||
| ||||
| 324 | h = css_unit_device2css_px(INTTOFIX(h)(css_int_to_fixed(h)), device_dpi); | |||
| 325 | ||||
| 326 | htmlc->media.width = w; | |||
| 327 | htmlc->media.height = h; | |||
| 328 | htmlc->unit_len_ctx.viewport_width = w; | |||
| 329 | htmlc->unit_len_ctx.viewport_height = h; | |||
| 330 | htmlc->unit_len_ctx.device_dpi = device_dpi; | |||
| 331 | ||||
| 332 | /** \todo Change nsoption font sizes to px. */ | |||
| 333 | f_size = FDIV(FMUL(F_96, FDIV(INTTOFIX(nsoption_int(font_size)), F_10)), F_72)(css_divide_fixed(((css_multiply_fixed((0x00018000), ((css_divide_fixed (((css_int_to_fixed((nsoptions[NSOPTION_font_size].value.i))) ), (0x00002800))))))), (0x00012000))); | |||
| 334 | f_min = FDIV(FMUL(F_96, FDIV(INTTOFIX(nsoption_int(font_min_size)), F_10)), F_72)(css_divide_fixed(((css_multiply_fixed((0x00018000), ((css_divide_fixed (((css_int_to_fixed((nsoptions[NSOPTION_font_min_size].value. i)))), (0x00002800))))))), (0x00012000))); | |||
| 335 | ||||
| 336 | htmlc->unit_len_ctx.font_size_default = f_size; | |||
| 337 | htmlc->unit_len_ctx.font_size_minimum = f_min; | |||
| 338 | } | |||
| 339 | ||||
| 340 | /* exported function documented in html/html_internal.h */ | |||
| 341 | void html_finish_conversion(html_content *htmlc) | |||
| 342 | { | |||
| 343 | union content_msg_data msg_data; | |||
| 344 | dom_exception exc; /* returned by libdom functions */ | |||
| 345 | dom_node *html; | |||
| 346 | nserror error; | |||
| 347 | ||||
| 348 | /* Bail out if we've been aborted */ | |||
| 349 | if (htmlc->aborted) { | |||
| 350 | content_broadcast_error(&htmlc->base, NSERROR_STOPPED, NULL((void*)0)); | |||
| 351 | content_set_error(&htmlc->base); | |||
| 352 | return; | |||
| 353 | } | |||
| 354 | ||||
| 355 | /* If we already have a selection context, then we have already | |||
| 356 | * "finished" conversion. We can get here twice if e.g. some JS | |||
| 357 | * adds a new stylesheet, and the stylesheet gets added after | |||
| 358 | * the HTML content is initially finished. | |||
| 359 | * | |||
| 360 | * If we didn't do this, the HTML content would try to rebuild the | |||
| 361 | * box tree for the html content when this new stylesheet is ready. | |||
| 362 | * NetSurf has no concept of dynamically changing documents, so this | |||
| 363 | * would break badly. | |||
| 364 | */ | |||
| 365 | if (htmlc->select_ctx != NULL((void*)0)) { | |||
| 366 | NSLOG(netsurf, INFO,do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 367 , }; nslog__log(&_nslog_ctx, "Ignoring style change: NS layout is static." ); } } while(0) | |||
| 367 | "Ignoring style change: NS layout is static.")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 367 , }; nslog__log(&_nslog_ctx, "Ignoring style change: NS layout is static." ); } } while(0); | |||
| 368 | return; | |||
| 369 | } | |||
| 370 | ||||
| 371 | /* create new css selection context */ | |||
| 372 | error = html_css_new_selection_context(htmlc, &htmlc->select_ctx); | |||
| 373 | if (error != NSERROR_OK) { | |||
| 374 | content_broadcast_error(&htmlc->base, error, NULL((void*)0)); | |||
| 375 | content_set_error(&htmlc->base); | |||
| 376 | return; | |||
| 377 | } | |||
| 378 | ||||
| 379 | ||||
| 380 | /* fire a simple event named load at the Document's Window | |||
| 381 | * object, but with its target set to the Document object (and | |||
| 382 | * the currentTarget set to the Window object) | |||
| 383 | */ | |||
| 384 | if (htmlc->jsthread != NULL((void*)0)) { | |||
| 385 | js_fire_event(htmlc->jsthread, "load", htmlc->document, NULL((void*)0)); | |||
| 386 | } | |||
| 387 | ||||
| 388 | /* convert dom tree to box tree */ | |||
| 389 | NSLOG(netsurf, INFO, "DOM to box (%p)", htmlc)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 389 , }; nslog__log(&_nslog_ctx, "DOM to box (%p)", htmlc); } } while(0); | |||
| 390 | content_set_status(&htmlc->base, messages_get("Processing")); | |||
| 391 | msg_data.explicit_status_text = NULL((void*)0); | |||
| 392 | content_broadcast(&htmlc->base, CONTENT_MSG_STATUS, &msg_data); | |||
| 393 | ||||
| 394 | exc = dom_document_get_document_element(htmlc->document, (void *) &html)dom_document_get_document_element((dom_document *) (htmlc-> document), (struct dom_element **) ((void *) &html)); | |||
| 395 | if ((exc != DOM_NO_ERR) || (html == NULL((void*)0))) { | |||
| 396 | NSLOG(netsurf, INFO, "error retrieving html element from dom")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 396 , }; nslog__log(&_nslog_ctx, "error retrieving html element from dom" ); } } while(0); | |||
| 397 | content_broadcast_error(&htmlc->base, NSERROR_DOM, NULL((void*)0)); | |||
| 398 | content_set_error(&htmlc->base); | |||
| 399 | return; | |||
| 400 | } | |||
| 401 | ||||
| 402 | html_get_dimensions(htmlc); | |||
| 403 | ||||
| 404 | error = dom_to_box(html, htmlc, html_box_convert_done, &htmlc->box_conversion_context); | |||
| 405 | if (error != NSERROR_OK) { | |||
| 406 | NSLOG(netsurf, INFO, "box conversion failed")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 406 , }; nslog__log(&_nslog_ctx, "box conversion failed"); } } while(0); | |||
| 407 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 408 | html_object_free_objects(htmlc); | |||
| 409 | content_broadcast_error(&htmlc->base, error, NULL((void*)0)); | |||
| 410 | content_set_error(&htmlc->base); | |||
| 411 | return; | |||
| 412 | } | |||
| 413 | ||||
| 414 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 415 | } | |||
| 416 | ||||
| 417 | ||||
| 418 | static void | |||
| 419 | html_document_user_data_handler(dom_node_operation operation, | |||
| 420 | dom_string *key, void *data, | |||
| 421 | struct dom_node *src, | |||
| 422 | struct dom_node *dst) | |||
| 423 | { | |||
| 424 | if (dom_string_isequal(corestring_dom___ns_key_html_content_data, | |||
| 425 | key) == false0 || data == NULL((void*)0)) { | |||
| 426 | return; | |||
| 427 | } | |||
| 428 | ||||
| 429 | switch (operation) { | |||
| 430 | case DOM_NODE_CLONED: | |||
| 431 | NSLOG(netsurf, INFO, "Cloned")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 431 , }; nslog__log(&_nslog_ctx, "Cloned"); } } while(0); | |||
| 432 | break; | |||
| 433 | case DOM_NODE_RENAMED: | |||
| 434 | NSLOG(netsurf, INFO, "Renamed")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 434 , }; nslog__log(&_nslog_ctx, "Renamed"); } } while(0); | |||
| 435 | break; | |||
| 436 | case DOM_NODE_IMPORTED: | |||
| 437 | NSLOG(netsurf, INFO, "imported")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 437 , }; nslog__log(&_nslog_ctx, "imported"); } } while(0); | |||
| 438 | break; | |||
| 439 | case DOM_NODE_ADOPTED: | |||
| 440 | NSLOG(netsurf, INFO, "Adopted")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 440 , }; nslog__log(&_nslog_ctx, "Adopted"); } } while(0); | |||
| 441 | break; | |||
| 442 | case DOM_NODE_DELETED: | |||
| 443 | /* This is the only path I expect */ | |||
| 444 | break; | |||
| 445 | default: | |||
| 446 | NSLOG(netsurf, INFO, "User data operation not handled.")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 446 , }; nslog__log(&_nslog_ctx, "User data operation not handled." ); } } while(0); | |||
| 447 | assert(0)((0) ? (void) (0) : __assert_fail ("0", "content/handlers/html/html.c" , 447, __extension__ __PRETTY_FUNCTION__)); | |||
| 448 | } | |||
| 449 | } | |||
| 450 | ||||
| 451 | ||||
| 452 | static nserror | |||
| 453 | html_create_html_data(html_content *c, const http_parameter *params) | |||
| 454 | { | |||
| 455 | lwc_string *charset; | |||
| 456 | nserror nerror; | |||
| 457 | dom_hubbub_parser_params parse_params; | |||
| 458 | dom_hubbub_error error; | |||
| 459 | dom_exception err; | |||
| 460 | void *old_node_data; | |||
| 461 | const char *prefer_color_mode = (nsoption_bool(prefer_dark_mode)(nsoptions[NSOPTION_prefer_dark_mode].value.b)) ? | |||
| 462 | "dark" : "light"; | |||
| 463 | ||||
| 464 | c->parser = NULL((void*)0); | |||
| 465 | c->parse_completed = false0; | |||
| 466 | c->conversion_begun = false0; | |||
| 467 | c->document = NULL((void*)0); | |||
| 468 | c->quirks = DOM_DOCUMENT_QUIRKS_MODE_NONE; | |||
| 469 | c->encoding = NULL((void*)0); | |||
| 470 | c->base_url = nsurl_ref(content_get_url(&c->base)); | |||
| 471 | c->base_target = NULL((void*)0); | |||
| 472 | c->aborted = false0; | |||
| 473 | c->refresh = false0; | |||
| 474 | c->reflowing = false0; | |||
| 475 | c->title = NULL((void*)0); | |||
| 476 | c->bctx = NULL((void*)0); | |||
| 477 | c->layout = NULL((void*)0); | |||
| 478 | c->background_colour = NS_TRANSPARENT0x01000000; | |||
| 479 | c->stylesheet_count = 0; | |||
| 480 | c->stylesheets = NULL((void*)0); | |||
| 481 | c->select_ctx = NULL((void*)0); | |||
| 482 | c->media.type = CSS_MEDIA_SCREEN; | |||
| 483 | c->universal = NULL((void*)0); | |||
| 484 | c->num_objects = 0; | |||
| 485 | c->object_list = NULL((void*)0); | |||
| 486 | c->forms = NULL((void*)0); | |||
| 487 | c->imagemaps = NULL((void*)0); | |||
| 488 | c->bw = NULL((void*)0); | |||
| 489 | c->frameset = NULL((void*)0); | |||
| 490 | c->iframe = NULL((void*)0); | |||
| 491 | c->page = NULL((void*)0); | |||
| 492 | c->font_func = guit->layout; | |||
| 493 | c->drag_type = HTML_DRAG_NONE; | |||
| 494 | c->drag_owner.no_owner = true1; | |||
| 495 | c->selection_type = HTML_SELECTION_NONE; | |||
| 496 | c->selection_owner.none = true1; | |||
| 497 | c->focus_type = HTML_FOCUS_SELF; | |||
| 498 | c->focus_owner.self = true1; | |||
| 499 | c->scripts_count = 0; | |||
| 500 | c->scripts = NULL((void*)0); | |||
| 501 | c->jsthread = NULL((void*)0); | |||
| 502 | ||||
| 503 | c->enable_scripting = nsoption_bool(enable_javascript)(nsoptions[NSOPTION_enable_javascript].value.b); | |||
| 504 | c->base.active = 1; /* The html content itself is active */ | |||
| 505 | ||||
| 506 | if (lwc_intern_string("*", SLEN("*")(sizeof(("*")) - 1), &c->universal) != lwc_error_ok) { | |||
| 507 | return NSERROR_NOMEM; | |||
| 508 | } | |||
| 509 | ||||
| 510 | if (lwc_intern_string(prefer_color_mode, strlen(prefer_color_mode), | |||
| 511 | &c->media.prefers_color_scheme) != lwc_error_ok) { | |||
| 512 | lwc_string_unref(c->universal){ lwc_string *__lwc_s = (c->universal); ((__lwc_s != ((void *)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL", "content/handlers/html/html.c" , 512, __extension__ __PRETTY_FUNCTION__)); __lwc_s->refcnt --; if ((__lwc_s->refcnt == 0) || ((__lwc_s->refcnt == 1 ) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy (__lwc_s); }; | |||
| 513 | c->universal = NULL((void*)0); | |||
| 514 | return NSERROR_NOMEM; | |||
| 515 | } | |||
| 516 | ||||
| 517 | c->sel = selection_create((struct content *)c); | |||
| 518 | ||||
| 519 | nerror = http_parameter_list_find_item(params, corestring_lwc_charset, &charset); | |||
| 520 | if (nerror == NSERROR_OK) { | |||
| 521 | c->encoding = strdup(lwc_string_data(charset)({((charset != ((void*)0)) ? (void) (0) : __assert_fail ("charset != NULL" , "content/handlers/html/html.c", 521, __extension__ __PRETTY_FUNCTION__ )); (const char *)((charset)+1);})); | |||
| 522 | ||||
| 523 | lwc_string_unref(charset){ lwc_string *__lwc_s = (charset); ((__lwc_s != ((void*)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL", "content/handlers/html/html.c" , 523, __extension__ __PRETTY_FUNCTION__)); __lwc_s->refcnt --; if ((__lwc_s->refcnt == 0) || ((__lwc_s->refcnt == 1 ) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy (__lwc_s); }; | |||
| 524 | ||||
| 525 | if (c->encoding == NULL((void*)0)) { | |||
| 526 | lwc_string_unref(c->universal){ lwc_string *__lwc_s = (c->universal); ((__lwc_s != ((void *)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL", "content/handlers/html/html.c" , 526, __extension__ __PRETTY_FUNCTION__)); __lwc_s->refcnt --; if ((__lwc_s->refcnt == 0) || ((__lwc_s->refcnt == 1 ) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy (__lwc_s); }; | |||
| 527 | c->universal = NULL((void*)0); | |||
| 528 | lwc_string_unref(c->media.prefers_color_scheme){ lwc_string *__lwc_s = (c->media.prefers_color_scheme); ( (__lwc_s != ((void*)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL" , "content/handlers/html/html.c", 528, __extension__ __PRETTY_FUNCTION__ )); __lwc_s->refcnt--; if ((__lwc_s->refcnt == 0) || (( __lwc_s->refcnt == 1) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy(__lwc_s); }; | |||
| 529 | c->media.prefers_color_scheme = NULL((void*)0); | |||
| 530 | return NSERROR_NOMEM; | |||
| 531 | ||||
| 532 | } | |||
| 533 | c->encoding_source = DOM_HUBBUB_ENCODING_SOURCE_HEADER; | |||
| 534 | } | |||
| 535 | ||||
| 536 | /* Create the parser binding */ | |||
| 537 | parse_params.enc = c->encoding; | |||
| 538 | parse_params.fix_enc = true1; | |||
| 539 | parse_params.enable_script = c->enable_scripting; | |||
| 540 | parse_params.msg = NULL((void*)0); | |||
| 541 | parse_params.script = html_process_script; | |||
| 542 | parse_params.ctx = c; | |||
| 543 | parse_params.daf = html_dom_event_fetcher; | |||
| 544 | ||||
| 545 | error = dom_hubbub_parser_create(&parse_params, | |||
| 546 | &c->parser, | |||
| 547 | &c->document); | |||
| 548 | if ((error != DOM_HUBBUB_OK) && (c->encoding != NULL((void*)0))) { | |||
| 549 | /* Ok, we don't support the declared encoding. Bailing out | |||
| 550 | * isn't exactly user-friendly, so fall back to autodetect */ | |||
| 551 | free(c->encoding); | |||
| 552 | c->encoding = NULL((void*)0); | |||
| 553 | ||||
| 554 | parse_params.enc = c->encoding; | |||
| 555 | ||||
| 556 | error = dom_hubbub_parser_create(&parse_params, | |||
| 557 | &c->parser, | |||
| 558 | &c->document); | |||
| 559 | } | |||
| 560 | if (error != DOM_HUBBUB_OK) { | |||
| 561 | nsurl_unref(c->base_url); | |||
| 562 | c->base_url = NULL((void*)0); | |||
| 563 | ||||
| 564 | lwc_string_unref(c->universal){ lwc_string *__lwc_s = (c->universal); ((__lwc_s != ((void *)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL", "content/handlers/html/html.c" , 564, __extension__ __PRETTY_FUNCTION__)); __lwc_s->refcnt --; if ((__lwc_s->refcnt == 0) || ((__lwc_s->refcnt == 1 ) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy (__lwc_s); }; | |||
| 565 | c->universal = NULL((void*)0); | |||
| 566 | lwc_string_unref(c->media.prefers_color_scheme){ lwc_string *__lwc_s = (c->media.prefers_color_scheme); ( (__lwc_s != ((void*)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL" , "content/handlers/html/html.c", 566, __extension__ __PRETTY_FUNCTION__ )); __lwc_s->refcnt--; if ((__lwc_s->refcnt == 0) || (( __lwc_s->refcnt == 1) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy(__lwc_s); }; | |||
| 567 | c->media.prefers_color_scheme = NULL((void*)0); | |||
| 568 | ||||
| 569 | return libdom_hubbub_error_to_nserror(error); | |||
| 570 | } | |||
| 571 | ||||
| 572 | err = dom_node_set_user_data(c->document,dom_node_set_user_data( (dom_node *) (c->document), (corestring_dom___ns_key_html_content_data ), (void *) (c), (dom_user_data_handler) html_document_user_data_handler , (void **) ((void *) &old_node_data)) | |||
| 573 | corestring_dom___ns_key_html_content_data,dom_node_set_user_data( (dom_node *) (c->document), (corestring_dom___ns_key_html_content_data ), (void *) (c), (dom_user_data_handler) html_document_user_data_handler , (void **) ((void *) &old_node_data)) | |||
| 574 | c, html_document_user_data_handler,dom_node_set_user_data( (dom_node *) (c->document), (corestring_dom___ns_key_html_content_data ), (void *) (c), (dom_user_data_handler) html_document_user_data_handler , (void **) ((void *) &old_node_data)) | |||
| 575 | (void *) &old_node_data)dom_node_set_user_data( (dom_node *) (c->document), (corestring_dom___ns_key_html_content_data ), (void *) (c), (dom_user_data_handler) html_document_user_data_handler , (void **) ((void *) &old_node_data)); | |||
| 576 | if (err != DOM_NO_ERR) { | |||
| 577 | dom_hubbub_parser_destroy(c->parser); | |||
| 578 | c->parser = NULL((void*)0); | |||
| 579 | nsurl_unref(c->base_url); | |||
| 580 | c->base_url = NULL((void*)0); | |||
| 581 | ||||
| 582 | lwc_string_unref(c->universal){ lwc_string *__lwc_s = (c->universal); ((__lwc_s != ((void *)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL", "content/handlers/html/html.c" , 582, __extension__ __PRETTY_FUNCTION__)); __lwc_s->refcnt --; if ((__lwc_s->refcnt == 0) || ((__lwc_s->refcnt == 1 ) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy (__lwc_s); }; | |||
| 583 | c->universal = NULL((void*)0); | |||
| 584 | lwc_string_unref(c->media.prefers_color_scheme){ lwc_string *__lwc_s = (c->media.prefers_color_scheme); ( (__lwc_s != ((void*)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL" , "content/handlers/html/html.c", 584, __extension__ __PRETTY_FUNCTION__ )); __lwc_s->refcnt--; if ((__lwc_s->refcnt == 0) || (( __lwc_s->refcnt == 1) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy(__lwc_s); }; | |||
| 585 | c->media.prefers_color_scheme = NULL((void*)0); | |||
| 586 | ||||
| 587 | NSLOG(netsurf, INFO, "Unable to set user data.")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 587 , }; nslog__log(&_nslog_ctx, "Unable to set user data."); } } while(0); | |||
| 588 | return NSERROR_DOM; | |||
| 589 | } | |||
| 590 | ||||
| 591 | assert(old_node_data == NULL)((old_node_data == ((void*)0)) ? (void) (0) : __assert_fail ( "old_node_data == NULL", "content/handlers/html/html.c", 591, __extension__ __PRETTY_FUNCTION__)); | |||
| 592 | ||||
| 593 | return NSERROR_OK; | |||
| 594 | ||||
| 595 | } | |||
| 596 | ||||
| 597 | /** | |||
| 598 | * Create a CONTENT_HTML. | |||
| 599 | * | |||
| 600 | * The content_html_data structure is initialized and the HTML parser is | |||
| 601 | * created. | |||
| 602 | */ | |||
| 603 | ||||
| 604 | static nserror | |||
| 605 | html_create(const content_handler *handler, | |||
| 606 | lwc_string *imime_type, | |||
| 607 | const http_parameter *params, | |||
| 608 | llcache_handle *llcache, | |||
| 609 | const char *fallback_charset, | |||
| 610 | bool_Bool quirks, | |||
| 611 | struct content **c) | |||
| 612 | { | |||
| 613 | html_content *html; | |||
| 614 | nserror error; | |||
| 615 | ||||
| 616 | html = calloc(1, sizeof(html_content)); | |||
| 617 | if (html == NULL((void*)0)) | |||
| 618 | return NSERROR_NOMEM; | |||
| 619 | ||||
| 620 | error = content__init(&html->base, handler, imime_type, params, | |||
| 621 | llcache, fallback_charset, quirks); | |||
| 622 | if (error != NSERROR_OK) { | |||
| 623 | free(html); | |||
| 624 | return error; | |||
| 625 | } | |||
| 626 | ||||
| 627 | error = html_create_html_data(html, params); | |||
| 628 | if (error != NSERROR_OK) { | |||
| 629 | content_broadcast_error(&html->base, error, NULL((void*)0)); | |||
| 630 | free(html); | |||
| 631 | return error; | |||
| 632 | } | |||
| 633 | ||||
| 634 | error = html_css_new_stylesheets(html); | |||
| 635 | if (error != NSERROR_OK) { | |||
| 636 | content_broadcast_error(&html->base, error, NULL((void*)0)); | |||
| 637 | free(html); | |||
| 638 | return error; | |||
| 639 | } | |||
| 640 | ||||
| 641 | *c = (struct content *) html; | |||
| 642 | ||||
| 643 | return NSERROR_OK; | |||
| 644 | } | |||
| 645 | ||||
| 646 | ||||
| 647 | ||||
| 648 | static nserror | |||
| 649 | html_process_encoding_change(struct content *c, | |||
| 650 | const char *data, | |||
| 651 | unsigned int size) | |||
| 652 | { | |||
| 653 | html_content *html = (html_content *) c; | |||
| 654 | dom_hubbub_parser_params parse_params; | |||
| 655 | dom_hubbub_error error; | |||
| 656 | const char *encoding; | |||
| 657 | const uint8_t *source_data; | |||
| 658 | size_t source_size; | |||
| 659 | ||||
| 660 | /* Retrieve new encoding */ | |||
| 661 | encoding = dom_hubbub_parser_get_encoding(html->parser, | |||
| 662 | &html->encoding_source); | |||
| 663 | if (encoding == NULL((void*)0)) { | |||
| 664 | return NSERROR_NOMEM; | |||
| 665 | } | |||
| 666 | ||||
| 667 | if (html->encoding != NULL((void*)0)) { | |||
| 668 | free(html->encoding); | |||
| 669 | html->encoding = NULL((void*)0); | |||
| 670 | } | |||
| 671 | ||||
| 672 | html->encoding = strdup(encoding); | |||
| 673 | if (html->encoding == NULL((void*)0)) { | |||
| 674 | return NSERROR_NOMEM; | |||
| 675 | } | |||
| 676 | ||||
| 677 | /* Destroy binding */ | |||
| 678 | dom_hubbub_parser_destroy(html->parser); | |||
| 679 | html->parser = NULL((void*)0); | |||
| 680 | ||||
| 681 | if (html->document != NULL((void*)0)) { | |||
| 682 | dom_node_unref(html->document)dom_node_unref((dom_node *) (html->document)); | |||
| 683 | } | |||
| 684 | ||||
| 685 | parse_params.enc = html->encoding; | |||
| 686 | parse_params.fix_enc = true1; | |||
| 687 | parse_params.enable_script = html->enable_scripting; | |||
| 688 | parse_params.msg = NULL((void*)0); | |||
| 689 | parse_params.script = html_process_script; | |||
| 690 | parse_params.ctx = html; | |||
| 691 | parse_params.daf = html_dom_event_fetcher; | |||
| 692 | ||||
| 693 | /* Create new binding, using the new encoding */ | |||
| 694 | error = dom_hubbub_parser_create(&parse_params, | |||
| 695 | &html->parser, | |||
| 696 | &html->document); | |||
| 697 | if (error != DOM_HUBBUB_OK) { | |||
| 698 | /* Ok, we don't support the declared encoding. Bailing out | |||
| 699 | * isn't exactly user-friendly, so fall back to Windows-1252 */ | |||
| 700 | free(html->encoding); | |||
| 701 | html->encoding = strdup("Windows-1252"); | |||
| 702 | if (html->encoding == NULL((void*)0)) { | |||
| 703 | return NSERROR_NOMEM; | |||
| 704 | } | |||
| 705 | parse_params.enc = html->encoding; | |||
| 706 | ||||
| 707 | error = dom_hubbub_parser_create(&parse_params, | |||
| 708 | &html->parser, | |||
| 709 | &html->document); | |||
| 710 | ||||
| 711 | if (error != DOM_HUBBUB_OK) { | |||
| 712 | return libdom_hubbub_error_to_nserror(error); | |||
| 713 | } | |||
| 714 | ||||
| 715 | } | |||
| 716 | ||||
| 717 | source_data = content__get_source_data(c, &source_size); | |||
| 718 | ||||
| 719 | /* Reprocess all the data. This is safe because | |||
| 720 | * the encoding is now specified at parser start which means | |||
| 721 | * it cannot be changed again. | |||
| 722 | */ | |||
| 723 | error = dom_hubbub_parser_parse_chunk(html->parser, | |||
| 724 | source_data, | |||
| 725 | source_size); | |||
| 726 | ||||
| 727 | return libdom_hubbub_error_to_nserror(error); | |||
| 728 | } | |||
| 729 | ||||
| 730 | ||||
| 731 | /** | |||
| 732 | * Process data for CONTENT_HTML. | |||
| 733 | */ | |||
| 734 | ||||
| 735 | static bool_Bool | |||
| 736 | html_process_data(struct content *c, const char *data, unsigned int size) | |||
| 737 | { | |||
| 738 | html_content *html = (html_content *) c; | |||
| 739 | dom_hubbub_error dom_ret; | |||
| 740 | nserror err = NSERROR_OK; /* assume its all going to be ok */ | |||
| 741 | ||||
| 742 | dom_ret = dom_hubbub_parser_parse_chunk(html->parser, | |||
| 743 | (const uint8_t *) data, | |||
| 744 | size); | |||
| 745 | ||||
| 746 | err = libdom_hubbub_error_to_nserror(dom_ret); | |||
| 747 | ||||
| 748 | /* deal with encoding change */ | |||
| 749 | if (err == NSERROR_ENCODING_CHANGE) { | |||
| 750 | err = html_process_encoding_change(c, data, size); | |||
| 751 | } | |||
| 752 | ||||
| 753 | /* broadcast the error if necessary */ | |||
| 754 | if (err != NSERROR_OK) { | |||
| 755 | content_broadcast_error(c, err, NULL((void*)0)); | |||
| 756 | return false0; | |||
| 757 | } | |||
| 758 | ||||
| 759 | return true1; | |||
| 760 | } | |||
| 761 | ||||
| 762 | ||||
| 763 | /** | |||
| 764 | * Convert a CONTENT_HTML for display. | |||
| 765 | * | |||
| 766 | * The following steps are carried out in order: | |||
| 767 | * | |||
| 768 | * - parsing to an XML tree is completed | |||
| 769 | * - stylesheets are fetched | |||
| 770 | * - the XML tree is converted to a box tree and object fetches are started | |||
| 771 | * | |||
| 772 | * On exit, the content status will be either CONTENT_STATUS_DONE if the | |||
| 773 | * document is completely loaded or CONTENT_STATUS_READY if objects are still | |||
| 774 | * being fetched. | |||
| 775 | */ | |||
| 776 | ||||
| 777 | static bool_Bool html_convert(struct content *c) | |||
| ||||
| 778 | { | |||
| 779 | html_content *htmlc = (html_content *) c; | |||
| 780 | dom_exception exc; /* returned by libdom functions */ | |||
| 781 | ||||
| 782 | /* The quirk check and associated stylesheet fetch is "safe" | |||
| 783 | * once the root node has been inserted into the document | |||
| 784 | * which must have happened by this point in the parse. | |||
| 785 | * | |||
| 786 | * faliure to retrive the quirk mode or to start the | |||
| 787 | * stylesheet fetch is non fatal as this "only" affects the | |||
| 788 | * render and it would annoy the user to fail the entire | |||
| 789 | * render for want of a quirks stylesheet. | |||
| 790 | */ | |||
| 791 | exc = dom_document_get_quirks_mode(htmlc->document, &htmlc->quirks)dom_document_get_quirks_mode((dom_document *) (htmlc->document ), (&htmlc->quirks)); | |||
| 792 | if (exc == DOM_NO_ERR) { | |||
| 793 | html_css_quirks_stylesheets(htmlc); | |||
| 794 | NSLOG(netsurf, INFO, "quirks set to %d", htmlc->quirks)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 794 , }; nslog__log(&_nslog_ctx, "quirks set to %d", htmlc-> quirks); } } while(0); | |||
| 795 | } | |||
| 796 | ||||
| 797 | htmlc->base.active--; /* the html fetch is no longer active */ | |||
| 798 | NSLOG(netsurf, INFO, "%d fetches active (%p)", htmlc->base.active, c)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 798 , }; nslog__log(&_nslog_ctx, "%d fetches active (%p)", htmlc ->base.active, c); } } while(0); | |||
| 799 | ||||
| 800 | /* The parse cannot be completed here because it may be paused | |||
| 801 | * untill all the resources being fetched have completed. | |||
| 802 | */ | |||
| 803 | ||||
| 804 | /* if there are no active fetches in progress no scripts are | |||
| 805 | * being fetched or they completed already. | |||
| 806 | */ | |||
| 807 | if (html_can_begin_conversion(htmlc)) { | |||
| 808 | return html_begin_conversion(htmlc); | |||
| 809 | } | |||
| 810 | return true1; | |||
| 811 | } | |||
| 812 | ||||
| 813 | /* Exported interface documented in html_internal.h */ | |||
| 814 | bool_Bool html_can_begin_conversion(html_content *htmlc) | |||
| 815 | { | |||
| 816 | unsigned int i; | |||
| 817 | ||||
| 818 | /* Cannot begin conversion if we're still fetching stuff */ | |||
| 819 | if (htmlc->base.active != 0) | |||
| 820 | return false0; | |||
| 821 | ||||
| 822 | for (i = 0; i != htmlc->stylesheet_count; i++) { | |||
| 823 | /* Cannot begin conversion if the stylesheets are modified */ | |||
| 824 | if (htmlc->stylesheets[i].modified) | |||
| 825 | return false0; | |||
| 826 | } | |||
| 827 | ||||
| 828 | /* All is good, begin */ | |||
| 829 | return true1; | |||
| 830 | } | |||
| 831 | ||||
| 832 | bool_Bool | |||
| 833 | html_begin_conversion(html_content *htmlc) | |||
| 834 | { | |||
| 835 | dom_node *html; | |||
| 836 | nserror ns_error; | |||
| 837 | struct form *f; | |||
| 838 | dom_exception exc; /* returned by libdom functions */ | |||
| 839 | dom_string *node_name = NULL((void*)0); | |||
| 840 | dom_hubbub_error error; | |||
| 841 | ||||
| 842 | /* The act of completing the parse can result in additional data | |||
| 843 | * being flushed through the parser. This may result in new style or | |||
| 844 | * script nodes, upon which the conversion depends. Thus, once we | |||
| 845 | * have completed the parse, we must check again to see if we can | |||
| 846 | * begin the conversion. If we can't, we must stop and wait for the | |||
| 847 | * new styles/scripts to be processed. Once they have been processed, | |||
| 848 | * we will be called again to begin the conversion for real. Thus, | |||
| 849 | * we must also ensure that we don't attempt to complete the parse | |||
| 850 | * multiple times, so store a flag to indicate that parsing is | |||
| 851 | * complete to avoid repeating the completion pointlessly. | |||
| 852 | */ | |||
| 853 | if (htmlc->parse_completed == false0) { | |||
| 854 | NSLOG(netsurf, INFO, "Completing parse (%p)", htmlc)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 854 , }; nslog__log(&_nslog_ctx, "Completing parse (%p)", htmlc ); } } while(0); | |||
| 855 | /* complete parsing */ | |||
| 856 | error = dom_hubbub_parser_completed(htmlc->parser); | |||
| 857 | if (error == DOM_HUBBUB_HUBBUB_ERR_PAUSED && htmlc->base.active > 0) { | |||
| 858 | /* The act of completing the parse failed because we've | |||
| 859 | * encountered a sync script which needs to run | |||
| 860 | */ | |||
| 861 | NSLOG(netsurf, INFO, "Completing parse brought synchronous JS to light, cannot complete yet")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 861 , }; nslog__log(&_nslog_ctx, "Completing parse brought synchronous JS to light, cannot complete yet" ); } } while(0); | |||
| 862 | return true1; | |||
| 863 | } | |||
| 864 | if (error != DOM_HUBBUB_OK) { | |||
| 865 | NSLOG(netsurf, INFO, "Parsing failed")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 865 , }; nslog__log(&_nslog_ctx, "Parsing failed"); } } while (0); | |||
| 866 | ||||
| 867 | content_broadcast_error(&htmlc->base, | |||
| 868 | libdom_hubbub_error_to_nserror(error), | |||
| 869 | NULL((void*)0)); | |||
| 870 | ||||
| 871 | return false0; | |||
| 872 | } | |||
| 873 | htmlc->parse_completed = true1; | |||
| 874 | } | |||
| 875 | ||||
| 876 | if (html_can_begin_conversion(htmlc) == false0) { | |||
| 877 | NSLOG(netsurf, INFO, "Can't begin conversion (%p)", htmlc)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 877 , }; nslog__log(&_nslog_ctx, "Can't begin conversion (%p)" , htmlc); } } while(0); | |||
| 878 | /* We can't proceed (see commentary above) */ | |||
| 879 | return true1; | |||
| 880 | } | |||
| 881 | ||||
| 882 | /* Give up processing if we've been aborted */ | |||
| 883 | if (htmlc->aborted) { | |||
| 884 | NSLOG(netsurf, INFO, "Conversion aborted (%p) (active: %u)",do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 885 , }; nslog__log(&_nslog_ctx, "Conversion aborted (%p) (active: %u)" , htmlc, htmlc->base.active); } } while(0) | |||
| 885 | htmlc, htmlc->base.active)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 885 , }; nslog__log(&_nslog_ctx, "Conversion aborted (%p) (active: %u)" , htmlc, htmlc->base.active); } } while(0); | |||
| 886 | content_set_error(&htmlc->base); | |||
| 887 | content_broadcast_error(&htmlc->base, NSERROR_STOPPED, NULL((void*)0)); | |||
| 888 | return false0; | |||
| 889 | } | |||
| 890 | ||||
| 891 | /* Conversion begins proper at this point */ | |||
| 892 | htmlc->conversion_begun = true1; | |||
| 893 | ||||
| 894 | /* complete script execution, including deferred scripts */ | |||
| 895 | html_script_exec(htmlc, true1); | |||
| 896 | ||||
| 897 | /* fire a simple event that bubbles named DOMContentLoaded at | |||
| 898 | * the Document. | |||
| 899 | */ | |||
| 900 | ||||
| 901 | /* get encoding */ | |||
| 902 | if (htmlc->encoding == NULL((void*)0)) { | |||
| 903 | const char *encoding; | |||
| 904 | ||||
| 905 | encoding = dom_hubbub_parser_get_encoding(htmlc->parser, | |||
| 906 | &htmlc->encoding_source); | |||
| 907 | if (encoding == NULL((void*)0)) { | |||
| 908 | content_broadcast_error(&htmlc->base, | |||
| 909 | NSERROR_NOMEM, | |||
| 910 | NULL((void*)0)); | |||
| 911 | return false0; | |||
| 912 | } | |||
| 913 | ||||
| 914 | htmlc->encoding = strdup(encoding); | |||
| 915 | if (htmlc->encoding == NULL((void*)0)) { | |||
| 916 | content_broadcast_error(&htmlc->base, | |||
| 917 | NSERROR_NOMEM, | |||
| 918 | NULL((void*)0)); | |||
| 919 | return false0; | |||
| 920 | } | |||
| 921 | } | |||
| 922 | ||||
| 923 | /* locate root element and ensure it is html */ | |||
| 924 | exc = dom_document_get_document_element(htmlc->document, (void *) &html)dom_document_get_document_element((dom_document *) (htmlc-> document), (struct dom_element **) ((void *) &html)); | |||
| 925 | if ((exc != DOM_NO_ERR) || (html == NULL((void*)0))) { | |||
| 926 | NSLOG(netsurf, INFO, "error retrieving html element from dom")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 926 , }; nslog__log(&_nslog_ctx, "error retrieving html element from dom" ); } } while(0); | |||
| 927 | content_broadcast_error(&htmlc->base, NSERROR_DOM, NULL((void*)0)); | |||
| 928 | return false0; | |||
| 929 | } | |||
| 930 | ||||
| 931 | exc = dom_node_get_node_name(html, &node_name)dom_node_get_node_name((dom_node *) (html), (&node_name)); | |||
| 932 | if ((exc != DOM_NO_ERR) || | |||
| 933 | (node_name == NULL((void*)0)) || | |||
| 934 | (!dom_string_caseless_lwc_isequal(node_name, | |||
| 935 | corestring_lwc_html))) { | |||
| 936 | NSLOG(netsurf, INFO, "root element not html")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 936 , }; nslog__log(&_nslog_ctx, "root element not html"); } } while(0); | |||
| 937 | content_broadcast_error(&htmlc->base, NSERROR_DOM, NULL((void*)0)); | |||
| 938 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 939 | return false0; | |||
| 940 | } | |||
| 941 | dom_string_unref(node_name); | |||
| 942 | ||||
| 943 | /* Retrieve forms from parser */ | |||
| 944 | htmlc->forms = html_forms_get_forms(htmlc->encoding, | |||
| 945 | (dom_html_document *) htmlc->document); | |||
| 946 | for (f = htmlc->forms; f != NULL((void*)0); f = f->prev) { | |||
| 947 | nsurl *action; | |||
| 948 | ||||
| 949 | /* Make all actions absolute */ | |||
| 950 | if (f->action == NULL((void*)0) || f->action[0] == '\0') { | |||
| 951 | /* HTML5 4.10.22.3 step 9 */ | |||
| 952 | nsurl *doc_addr = content_get_url(&htmlc->base); | |||
| 953 | ns_error = nsurl_join(htmlc->base_url, | |||
| 954 | nsurl_access(doc_addr), | |||
| 955 | &action); | |||
| 956 | } else { | |||
| 957 | ns_error = nsurl_join(htmlc->base_url, | |||
| 958 | f->action, | |||
| 959 | &action); | |||
| 960 | } | |||
| 961 | ||||
| 962 | if (ns_error != NSERROR_OK) { | |||
| 963 | content_broadcast_error(&htmlc->base, ns_error, NULL((void*)0)); | |||
| 964 | ||||
| 965 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 966 | return false0; | |||
| 967 | } | |||
| 968 | ||||
| 969 | free(f->action); | |||
| 970 | f->action = strdup(nsurl_access(action)); | |||
| 971 | nsurl_unref(action); | |||
| 972 | if (f->action == NULL((void*)0)) { | |||
| 973 | content_broadcast_error(&htmlc->base, | |||
| 974 | NSERROR_NOMEM, | |||
| 975 | NULL((void*)0)); | |||
| 976 | ||||
| 977 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 978 | return false0; | |||
| 979 | } | |||
| 980 | ||||
| 981 | /* Ensure each form has a document encoding */ | |||
| 982 | if (f->document_charset == NULL((void*)0)) { | |||
| 983 | f->document_charset = strdup(htmlc->encoding); | |||
| 984 | if (f->document_charset == NULL((void*)0)) { | |||
| 985 | content_broadcast_error(&htmlc->base, | |||
| 986 | NSERROR_NOMEM, | |||
| 987 | NULL((void*)0)); | |||
| 988 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 989 | return false0; | |||
| 990 | } | |||
| 991 | } | |||
| 992 | } | |||
| 993 | ||||
| 994 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 995 | ||||
| 996 | if (htmlc->base.active == 0) { | |||
| 997 | html_finish_conversion(htmlc); | |||
| 998 | } | |||
| 999 | ||||
| 1000 | return true1; | |||
| 1001 | } | |||
| 1002 | ||||
| 1003 | ||||
| 1004 | /** | |||
| 1005 | * Stop loading a CONTENT_HTML. | |||
| 1006 | * | |||
| 1007 | * called when the content is aborted. This must clean up any state | |||
| 1008 | * created during the fetch. | |||
| 1009 | */ | |||
| 1010 | ||||
| 1011 | static void html_stop(struct content *c) | |||
| 1012 | { | |||
| 1013 | html_content *htmlc = (html_content *) c; | |||
| 1014 | ||||
| 1015 | switch (c->status) { | |||
| 1016 | case CONTENT_STATUS_LOADING: | |||
| 1017 | /* Still loading; simply flag that we've been aborted | |||
| 1018 | * html_convert/html_finish_conversion will do the rest */ | |||
| 1019 | htmlc->aborted = true1; | |||
| 1020 | if (htmlc->jsthread != NULL((void*)0)) { | |||
| 1021 | /* Close the JS thread to cancel out any callbacks */ | |||
| 1022 | js_closethread(htmlc->jsthread); | |||
| 1023 | } | |||
| 1024 | break; | |||
| 1025 | ||||
| 1026 | case CONTENT_STATUS_READY: | |||
| 1027 | html_object_abort_objects(htmlc); | |||
| 1028 | ||||
| 1029 | /* If there are no further active fetches and we're still | |||
| 1030 | * in the READY state, transition to the DONE state. */ | |||
| 1031 | if (c->status == CONTENT_STATUS_READY && c->active == 0) { | |||
| 1032 | content_set_done(c); | |||
| 1033 | } | |||
| 1034 | ||||
| 1035 | break; | |||
| 1036 | ||||
| 1037 | case CONTENT_STATUS_DONE: | |||
| 1038 | /* Nothing to do */ | |||
| 1039 | break; | |||
| 1040 | ||||
| 1041 | default: | |||
| 1042 | NSLOG(netsurf, INFO, "Unexpected status %d (%p)", c->status,do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1043 , }; nslog__log(&_nslog_ctx, "Unexpected status %d (%p)", c->status, c); } } while(0) | |||
| 1043 | c)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1043 , }; nslog__log(&_nslog_ctx, "Unexpected status %d (%p)", c->status, c); } } while(0); | |||
| 1044 | assert(0)((0) ? (void) (0) : __assert_fail ("0", "content/handlers/html/html.c" , 1044, __extension__ __PRETTY_FUNCTION__)); | |||
| 1045 | } | |||
| 1046 | } | |||
| 1047 | ||||
| 1048 | ||||
| 1049 | /** | |||
| 1050 | * Reformat a CONTENT_HTML to a new width. | |||
| 1051 | */ | |||
| 1052 | ||||
| 1053 | static void html_reformat(struct content *c, int width, int height) | |||
| 1054 | { | |||
| 1055 | html_content *htmlc = (html_content *) c; | |||
| 1056 | struct box *layout; | |||
| 1057 | uint64_t ms_before; | |||
| 1058 | uint64_t ms_after; | |||
| 1059 | uint64_t ms_interval; | |||
| 1060 | ||||
| 1061 | nsu_getmonotonic_ms(&ms_before); | |||
| 1062 | ||||
| 1063 | htmlc->reflowing = true1; | |||
| 1064 | ||||
| 1065 | htmlc->unit_len_ctx.viewport_width = css_unit_device2css_px( | |||
| 1066 | INTTOFIX(width)(css_int_to_fixed(width)), htmlc->unit_len_ctx.device_dpi); | |||
| 1067 | htmlc->unit_len_ctx.viewport_height = css_unit_device2css_px( | |||
| 1068 | INTTOFIX(height)(css_int_to_fixed(height)), htmlc->unit_len_ctx.device_dpi); | |||
| 1069 | htmlc->unit_len_ctx.root_style = htmlc->layout->style; | |||
| 1070 | ||||
| 1071 | layout_document(htmlc, width, height); | |||
| 1072 | layout = htmlc->layout; | |||
| 1073 | ||||
| 1074 | /* width and height are at least margin box of document */ | |||
| 1075 | c->width = layout->x + layout->padding[LEFT] + layout->width + | |||
| 1076 | layout->padding[RIGHT] + layout->border[RIGHT].width + | |||
| 1077 | layout->margin[RIGHT]; | |||
| 1078 | c->height = layout->y + layout->padding[TOP] + layout->height + | |||
| 1079 | layout->padding[BOTTOM] + layout->border[BOTTOM].width + | |||
| 1080 | layout->margin[BOTTOM]; | |||
| 1081 | ||||
| 1082 | /* if boxes overflow right or bottom edge, expand to contain it */ | |||
| 1083 | if (c->width < layout->x + layout->descendant_x1) | |||
| 1084 | c->width = layout->x + layout->descendant_x1; | |||
| 1085 | if (c->height < layout->y + layout->descendant_y1) | |||
| 1086 | c->height = layout->y + layout->descendant_y1; | |||
| 1087 | ||||
| 1088 | selection_reinit(htmlc->sel); | |||
| 1089 | ||||
| 1090 | htmlc->reflowing = false0; | |||
| 1091 | htmlc->had_initial_layout = true1; | |||
| 1092 | ||||
| 1093 | /* calculate next reflow time at three times what it took to reflow */ | |||
| 1094 | nsu_getmonotonic_ms(&ms_after); | |||
| 1095 | ||||
| 1096 | ms_interval = (ms_after - ms_before) * 3; | |||
| 1097 | if (ms_interval < (nsoption_uint(min_reflow_period)(nsoptions[NSOPTION_min_reflow_period].value.u) * 10)) { | |||
| 1098 | ms_interval = nsoption_uint(min_reflow_period)(nsoptions[NSOPTION_min_reflow_period].value.u) * 10; | |||
| 1099 | } | |||
| 1100 | c->reformat_time = ms_after + ms_interval; | |||
| 1101 | } | |||
| 1102 | ||||
| 1103 | ||||
| 1104 | /** | |||
| 1105 | * Redraw a box. | |||
| 1106 | * | |||
| 1107 | * \param h content containing the box, of type CONTENT_HTML | |||
| 1108 | * \param box box to redraw | |||
| 1109 | */ | |||
| 1110 | ||||
| 1111 | void html_redraw_a_box(hlcache_handle *h, struct box *box) | |||
| 1112 | { | |||
| 1113 | int x, y; | |||
| 1114 | ||||
| 1115 | box_coords(box, &x, &y); | |||
| 1116 | ||||
| 1117 | content_request_redraw(h, x, y, | |||
| 1118 | box->padding[LEFT] + box->width + box->padding[RIGHT], | |||
| 1119 | box->padding[TOP] + box->height + box->padding[BOTTOM]); | |||
| 1120 | } | |||
| 1121 | ||||
| 1122 | ||||
| 1123 | /** | |||
| 1124 | * Redraw a box. | |||
| 1125 | * | |||
| 1126 | * \param html content containing the box, of type CONTENT_HTML | |||
| 1127 | * \param box box to redraw. | |||
| 1128 | */ | |||
| 1129 | ||||
| 1130 | void html__redraw_a_box(struct html_content *html, struct box *box) | |||
| 1131 | { | |||
| 1132 | int x, y; | |||
| 1133 | ||||
| 1134 | box_coords(box, &x, &y); | |||
| 1135 | ||||
| 1136 | content__request_redraw((struct content *)html, x, y, | |||
| 1137 | box->padding[LEFT] + box->width + box->padding[RIGHT], | |||
| 1138 | box->padding[TOP] + box->height + box->padding[BOTTOM]); | |||
| 1139 | } | |||
| 1140 | ||||
| 1141 | static void html_destroy_frameset(struct content_html_frames *frameset) | |||
| 1142 | { | |||
| 1143 | int i; | |||
| 1144 | ||||
| 1145 | if (frameset->name) { | |||
| 1146 | talloc_free(frameset->name); | |||
| 1147 | frameset->name = NULL((void*)0); | |||
| 1148 | } | |||
| 1149 | if (frameset->url) { | |||
| 1150 | talloc_free(frameset->url); | |||
| 1151 | frameset->url = NULL((void*)0); | |||
| 1152 | } | |||
| 1153 | if (frameset->children) { | |||
| 1154 | for (i = 0; i < (frameset->rows * frameset->cols); i++) { | |||
| 1155 | if (frameset->children[i].name) { | |||
| 1156 | talloc_free(frameset->children[i].name); | |||
| 1157 | frameset->children[i].name = NULL((void*)0); | |||
| 1158 | } | |||
| 1159 | if (frameset->children[i].url) { | |||
| 1160 | nsurl_unref(frameset->children[i].url); | |||
| 1161 | frameset->children[i].url = NULL((void*)0); | |||
| 1162 | } | |||
| 1163 | if (frameset->children[i].children) | |||
| 1164 | html_destroy_frameset(&frameset->children[i]); | |||
| 1165 | } | |||
| 1166 | talloc_free(frameset->children); | |||
| 1167 | frameset->children = NULL((void*)0); | |||
| 1168 | } | |||
| 1169 | } | |||
| 1170 | ||||
| 1171 | static void html_destroy_iframe(struct content_html_iframe *iframe) | |||
| 1172 | { | |||
| 1173 | struct content_html_iframe *next; | |||
| 1174 | next = iframe; | |||
| 1175 | while ((iframe = next) != NULL((void*)0)) { | |||
| 1176 | next = iframe->next; | |||
| 1177 | if (iframe->name) | |||
| 1178 | talloc_free(iframe->name); | |||
| 1179 | if (iframe->url) { | |||
| 1180 | nsurl_unref(iframe->url); | |||
| 1181 | iframe->url = NULL((void*)0); | |||
| 1182 | } | |||
| 1183 | talloc_free(iframe); | |||
| 1184 | } | |||
| 1185 | } | |||
| 1186 | ||||
| 1187 | ||||
| 1188 | static void html_free_layout(html_content *htmlc) | |||
| 1189 | { | |||
| 1190 | if (htmlc->bctx != NULL((void*)0)) { | |||
| 1191 | /* freeing talloc context should let the entire box | |||
| 1192 | * set be destroyed | |||
| 1193 | */ | |||
| 1194 | talloc_free(htmlc->bctx); | |||
| 1195 | } | |||
| 1196 | } | |||
| 1197 | ||||
| 1198 | /** | |||
| 1199 | * Destroy a CONTENT_HTML and free all resources it owns. | |||
| 1200 | */ | |||
| 1201 | ||||
| 1202 | static void html_destroy(struct content *c) | |||
| 1203 | { | |||
| 1204 | html_content *html = (html_content *) c; | |||
| 1205 | struct form *f, *g; | |||
| 1206 | ||||
| 1207 | NSLOG(netsurf, INFO, "content %p", c)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1207 , }; nslog__log(&_nslog_ctx, "content %p", c); } } while( 0); | |||
| 1208 | ||||
| 1209 | /* If we're still converting a layout, cancel it */ | |||
| 1210 | if (html->box_conversion_context != NULL((void*)0)) { | |||
| 1211 | if (cancel_dom_to_box(html->box_conversion_context) != NSERROR_OK) { | |||
| 1212 | NSLOG(netsurf, CRITICAL, "WARNING, Unable to cancel conversion context, browser may crash")do { if (NSLOG_LEVEL_CRITICAL >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_CRITICAL, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 1212, }; nslog__log(&_nslog_ctx , "WARNING, Unable to cancel conversion context, browser may crash" ); } } while(0); | |||
| 1213 | } | |||
| 1214 | } | |||
| 1215 | ||||
| 1216 | selection_destroy(html->sel); | |||
| 1217 | ||||
| 1218 | /* Destroy forms */ | |||
| 1219 | for (f = html->forms; f != NULL((void*)0); f = g) { | |||
| 1220 | g = f->prev; | |||
| 1221 | ||||
| 1222 | form_free(f); | |||
| 1223 | } | |||
| 1224 | ||||
| 1225 | imagemap_destroy(html); | |||
| 1226 | ||||
| 1227 | if (c->refresh) | |||
| 1228 | nsurl_unref(c->refresh); | |||
| 1229 | ||||
| 1230 | if (html->base_url) | |||
| 1231 | nsurl_unref(html->base_url); | |||
| 1232 | ||||
| 1233 | /* At this point we can be moderately confident the JS is offline | |||
| 1234 | * so we destroy the JS thread. | |||
| 1235 | */ | |||
| 1236 | if (html->jsthread != NULL((void*)0)) { | |||
| 1237 | js_destroythread(html->jsthread); | |||
| 1238 | html->jsthread = NULL((void*)0); | |||
| 1239 | } | |||
| 1240 | ||||
| 1241 | if (html->parser != NULL((void*)0)) { | |||
| 1242 | dom_hubbub_parser_destroy(html->parser); | |||
| 1243 | html->parser = NULL((void*)0); | |||
| 1244 | } | |||
| 1245 | ||||
| 1246 | if (html->document != NULL((void*)0)) { | |||
| 1247 | dom_node_unref(html->document)dom_node_unref((dom_node *) (html->document)); | |||
| 1248 | html->document = NULL((void*)0); | |||
| 1249 | } | |||
| 1250 | ||||
| 1251 | if (html->title != NULL((void*)0)) { | |||
| 1252 | dom_node_unref(html->title)dom_node_unref((dom_node *) (html->title)); | |||
| 1253 | html->title = NULL((void*)0); | |||
| 1254 | } | |||
| 1255 | ||||
| 1256 | /* Free encoding */ | |||
| 1257 | if (html->encoding != NULL((void*)0)) { | |||
| 1258 | free(html->encoding); | |||
| 1259 | html->encoding = NULL((void*)0); | |||
| 1260 | } | |||
| 1261 | ||||
| 1262 | /* Free base target */ | |||
| 1263 | if (html->base_target != NULL((void*)0)) { | |||
| 1264 | free(html->base_target); | |||
| 1265 | html->base_target = NULL((void*)0); | |||
| 1266 | } | |||
| 1267 | ||||
| 1268 | /* Free frameset */ | |||
| 1269 | if (html->frameset != NULL((void*)0)) { | |||
| 1270 | html_destroy_frameset(html->frameset); | |||
| 1271 | talloc_free(html->frameset); | |||
| 1272 | html->frameset = NULL((void*)0); | |||
| 1273 | } | |||
| 1274 | ||||
| 1275 | /* Free iframes */ | |||
| 1276 | if (html->iframe != NULL((void*)0)) { | |||
| 1277 | html_destroy_iframe(html->iframe); | |||
| 1278 | html->iframe = NULL((void*)0); | |||
| 1279 | } | |||
| 1280 | ||||
| 1281 | /* Destroy selection context */ | |||
| 1282 | if (html->select_ctx != NULL((void*)0)) { | |||
| 1283 | css_select_ctx_destroy(html->select_ctx); | |||
| 1284 | html->select_ctx = NULL((void*)0); | |||
| 1285 | } | |||
| 1286 | ||||
| 1287 | if (html->universal != NULL((void*)0)) { | |||
| 1288 | lwc_string_unref(html->universal){ lwc_string *__lwc_s = (html->universal); ((__lwc_s != (( void*)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL", "content/handlers/html/html.c" , 1288, __extension__ __PRETTY_FUNCTION__)); __lwc_s->refcnt --; if ((__lwc_s->refcnt == 0) || ((__lwc_s->refcnt == 1 ) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy (__lwc_s); }; | |||
| 1289 | html->universal = NULL((void*)0); | |||
| 1290 | } | |||
| 1291 | ||||
| 1292 | if (html->media.prefers_color_scheme != NULL((void*)0)) { | |||
| 1293 | lwc_string_unref(html->media.prefers_color_scheme){ lwc_string *__lwc_s = (html->media.prefers_color_scheme) ; ((__lwc_s != ((void*)0)) ? (void) (0) : __assert_fail ("__lwc_s != NULL" , "content/handlers/html/html.c", 1293, __extension__ __PRETTY_FUNCTION__ )); __lwc_s->refcnt--; if ((__lwc_s->refcnt == 0) || (( __lwc_s->refcnt == 1) && (__lwc_s->insensitive == __lwc_s))) lwc_string_destroy(__lwc_s); }; | |||
| 1294 | html->media.prefers_color_scheme = NULL((void*)0); | |||
| 1295 | } | |||
| 1296 | ||||
| 1297 | /* Free stylesheets */ | |||
| 1298 | html_css_free_stylesheets(html); | |||
| 1299 | ||||
| 1300 | /* Free scripts */ | |||
| 1301 | html_script_free(html); | |||
| 1302 | ||||
| 1303 | /* Free objects */ | |||
| 1304 | html_object_free_objects(html); | |||
| 1305 | ||||
| 1306 | /* free layout */ | |||
| 1307 | html_free_layout(html); | |||
| 1308 | } | |||
| 1309 | ||||
| 1310 | ||||
| 1311 | static nserror html_clone(const struct content *old, struct content **newc) | |||
| 1312 | { | |||
| 1313 | /** \todo Clone HTML specifics */ | |||
| 1314 | ||||
| 1315 | /* In the meantime, we should never be called, as HTML contents | |||
| 1316 | * cannot be shared and we're not intending to fix printing's | |||
| 1317 | * cloning of documents. */ | |||
| 1318 | assert(0 && "html_clone should never be called")((0 && "html_clone should never be called") ? (void) ( 0) : __assert_fail ("0 && \"html_clone should never be called\"" , "content/handlers/html/html.c", 1318, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1319 | ||||
| 1320 | return true1; | |||
| 1321 | } | |||
| 1322 | ||||
| 1323 | ||||
| 1324 | /** | |||
| 1325 | * Handle a window containing a CONTENT_HTML being opened. | |||
| 1326 | */ | |||
| 1327 | ||||
| 1328 | static nserror | |||
| 1329 | html_open(struct content *c, | |||
| 1330 | struct browser_window *bw, | |||
| 1331 | struct content *page, | |||
| 1332 | struct object_params *params) | |||
| 1333 | { | |||
| 1334 | html_content *html = (html_content *) c; | |||
| 1335 | ||||
| 1336 | html->bw = bw; | |||
| 1337 | html->page = (html_content *) page; | |||
| 1338 | ||||
| 1339 | html->drag_type = HTML_DRAG_NONE; | |||
| 1340 | html->drag_owner.no_owner = true1; | |||
| 1341 | ||||
| 1342 | /* text selection */ | |||
| 1343 | selection_init(html->sel); | |||
| 1344 | html->selection_type = HTML_SELECTION_NONE; | |||
| 1345 | html->selection_owner.none = true1; | |||
| 1346 | ||||
| 1347 | html_object_open_objects(html, bw); | |||
| 1348 | ||||
| 1349 | return NSERROR_OK; | |||
| 1350 | } | |||
| 1351 | ||||
| 1352 | ||||
| 1353 | /** | |||
| 1354 | * Handle a window containing a CONTENT_HTML being closed. | |||
| 1355 | */ | |||
| 1356 | ||||
| 1357 | static nserror html_close(struct content *c) | |||
| 1358 | { | |||
| 1359 | html_content *htmlc = (html_content *) c; | |||
| 1360 | nserror ret = NSERROR_OK; | |||
| 1361 | ||||
| 1362 | selection_clear(htmlc->sel, false0); | |||
| 1363 | ||||
| 1364 | /* clear the html content reference to the browser window */ | |||
| 1365 | htmlc->bw = NULL((void*)0); | |||
| 1366 | ||||
| 1367 | /* remove all object references from the html content */ | |||
| 1368 | html_object_close_objects(htmlc); | |||
| 1369 | ||||
| 1370 | if (htmlc->jsthread != NULL((void*)0)) { | |||
| 1371 | /* Close, but do not destroy (yet) the JS thread */ | |||
| 1372 | ret = js_closethread(htmlc->jsthread); | |||
| 1373 | } | |||
| 1374 | ||||
| 1375 | return ret; | |||
| 1376 | } | |||
| 1377 | ||||
| 1378 | ||||
| 1379 | /** | |||
| 1380 | * Return an HTML content's selection context | |||
| 1381 | */ | |||
| 1382 | ||||
| 1383 | static void html_clear_selection(struct content *c) | |||
| 1384 | { | |||
| 1385 | html_content *html = (html_content *) c; | |||
| 1386 | ||||
| 1387 | switch (html->selection_type) { | |||
| 1388 | case HTML_SELECTION_NONE: | |||
| 1389 | /* Nothing to do */ | |||
| 1390 | assert(html->selection_owner.none == true)((html->selection_owner.none == 1) ? (void) (0) : __assert_fail ("html->selection_owner.none == true", "content/handlers/html/html.c" , 1390, __extension__ __PRETTY_FUNCTION__)); | |||
| 1391 | break; | |||
| 1392 | case HTML_SELECTION_TEXTAREA: | |||
| 1393 | textarea_clear_selection(html->selection_owner.textarea-> | |||
| 1394 | gadget->data.text.ta); | |||
| 1395 | break; | |||
| 1396 | case HTML_SELECTION_SELF: | |||
| 1397 | assert(html->selection_owner.none == false)((html->selection_owner.none == 0) ? (void) (0) : __assert_fail ("html->selection_owner.none == false", "content/handlers/html/html.c" , 1397, __extension__ __PRETTY_FUNCTION__)); | |||
| 1398 | selection_clear(html->sel, true1); | |||
| 1399 | break; | |||
| 1400 | case HTML_SELECTION_CONTENT: | |||
| 1401 | content_clear_selection(html->selection_owner.content->object); | |||
| 1402 | break; | |||
| 1403 | default: | |||
| 1404 | break; | |||
| 1405 | } | |||
| 1406 | ||||
| 1407 | /* There is no selection now. */ | |||
| 1408 | html->selection_type = HTML_SELECTION_NONE; | |||
| 1409 | html->selection_owner.none = true1; | |||
| 1410 | } | |||
| 1411 | ||||
| 1412 | ||||
| 1413 | /** | |||
| 1414 | * Return an HTML content's selection context | |||
| 1415 | */ | |||
| 1416 | ||||
| 1417 | static char *html_get_selection(struct content *c) | |||
| 1418 | { | |||
| 1419 | html_content *html = (html_content *) c; | |||
| 1420 | ||||
| 1421 | switch (html->selection_type) { | |||
| 1422 | case HTML_SELECTION_TEXTAREA: | |||
| 1423 | return textarea_get_selection(html->selection_owner.textarea-> | |||
| 1424 | gadget->data.text.ta); | |||
| 1425 | case HTML_SELECTION_SELF: | |||
| 1426 | assert(html->selection_owner.none == false)((html->selection_owner.none == 0) ? (void) (0) : __assert_fail ("html->selection_owner.none == false", "content/handlers/html/html.c" , 1426, __extension__ __PRETTY_FUNCTION__)); | |||
| 1427 | return selection_get_copy(html->sel); | |||
| 1428 | case HTML_SELECTION_CONTENT: | |||
| 1429 | return content_get_selection( | |||
| 1430 | html->selection_owner.content->object); | |||
| 1431 | case HTML_SELECTION_NONE: | |||
| 1432 | /* Nothing to do */ | |||
| 1433 | assert(html->selection_owner.none == true)((html->selection_owner.none == 1) ? (void) (0) : __assert_fail ("html->selection_owner.none == true", "content/handlers/html/html.c" , 1433, __extension__ __PRETTY_FUNCTION__)); | |||
| 1434 | break; | |||
| 1435 | default: | |||
| 1436 | break; | |||
| 1437 | } | |||
| 1438 | ||||
| 1439 | return NULL((void*)0); | |||
| 1440 | } | |||
| 1441 | ||||
| 1442 | ||||
| 1443 | /** | |||
| 1444 | * Get access to any content, link URLs and objects (images) currently | |||
| 1445 | * at the given (x, y) coordinates. | |||
| 1446 | * | |||
| 1447 | * \param[in] c html content to look inside | |||
| 1448 | * \param[in] x x-coordinate of point of interest | |||
| 1449 | * \param[in] y y-coordinate of point of interest | |||
| 1450 | * \param[out] data Positional features struct to be updated with any | |||
| 1451 | * relevent content, or set to NULL if none. | |||
| 1452 | * \return NSERROR_OK on success else appropriate error code. | |||
| 1453 | */ | |||
| 1454 | static nserror | |||
| 1455 | html_get_contextual_content(struct content *c, int x, int y, | |||
| 1456 | struct browser_window_features *data) | |||
| 1457 | { | |||
| 1458 | html_content *html = (html_content *) c; | |||
| 1459 | ||||
| 1460 | struct box *box = html->layout; | |||
| 1461 | struct box *next; | |||
| 1462 | int box_x = 0, box_y = 0; | |||
| 1463 | ||||
| 1464 | while ((next = box_at_point(&html->unit_len_ctx, box, x, y, | |||
| 1465 | &box_x, &box_y)) != NULL((void*)0)) { | |||
| 1466 | box = next; | |||
| 1467 | ||||
| 1468 | /* hidden boxes are ignored */ | |||
| 1469 | if ((box->style != NULL((void*)0)) && | |||
| 1470 | css_computed_visibility(box->style) == CSS_VISIBILITY_HIDDEN) { | |||
| 1471 | continue; | |||
| 1472 | } | |||
| 1473 | ||||
| 1474 | if (box->iframe) { | |||
| 1475 | float scale = browser_window_get_scale(box->iframe); | |||
| 1476 | browser_window_get_features(box->iframe, | |||
| 1477 | (x - box_x) * scale, | |||
| 1478 | (y - box_y) * scale, | |||
| 1479 | data); | |||
| 1480 | } | |||
| 1481 | ||||
| 1482 | if (box->object) | |||
| 1483 | content_get_contextual_content(box->object, | |||
| 1484 | x - box_x, y - box_y, data); | |||
| 1485 | ||||
| 1486 | if (box->object) | |||
| 1487 | data->object = box->object; | |||
| 1488 | ||||
| 1489 | if (box->href) { | |||
| 1490 | data->link = box->href; | |||
| 1491 | data->link_title = box->text; | |||
| 1492 | data->link_title_length = box->length; | |||
| 1493 | } | |||
| 1494 | ||||
| 1495 | if (box->usemap) { | |||
| 1496 | const char *target = NULL((void*)0); | |||
| 1497 | nsurl *url = imagemap_get(html, box->usemap, box_x, | |||
| 1498 | box_y, x, y, &target); | |||
| 1499 | /* Box might have imagemap, but no actual link area | |||
| 1500 | * at point */ | |||
| 1501 | if (url != NULL((void*)0)) | |||
| 1502 | data->link = url; | |||
| 1503 | } | |||
| 1504 | if (box->gadget) { | |||
| 1505 | switch (box->gadget->type) { | |||
| 1506 | case GADGET_TEXTBOX: | |||
| 1507 | case GADGET_TEXTAREA: | |||
| 1508 | case GADGET_PASSWORD: | |||
| 1509 | data->form_features = CTX_FORM_TEXT; | |||
| 1510 | break; | |||
| 1511 | ||||
| 1512 | case GADGET_FILE: | |||
| 1513 | data->form_features = CTX_FORM_FILE; | |||
| 1514 | break; | |||
| 1515 | ||||
| 1516 | default: | |||
| 1517 | data->form_features = CTX_FORM_NONE; | |||
| 1518 | break; | |||
| 1519 | } | |||
| 1520 | } | |||
| 1521 | } | |||
| 1522 | return NSERROR_OK; | |||
| 1523 | } | |||
| 1524 | ||||
| 1525 | ||||
| 1526 | /** | |||
| 1527 | * Scroll deepest thing within the content which can be scrolled at given point | |||
| 1528 | * | |||
| 1529 | * \param c html content to look inside | |||
| 1530 | * \param x x-coordinate of point of interest | |||
| 1531 | * \param y y-coordinate of point of interest | |||
| 1532 | * \param scrx number of px try to scroll something in x direction | |||
| 1533 | * \param scry number of px try to scroll something in y direction | |||
| 1534 | * \return true iff scroll was consumed by something in the content | |||
| 1535 | */ | |||
| 1536 | static bool_Bool | |||
| 1537 | html_scroll_at_point(struct content *c, int x, int y, int scrx, int scry) | |||
| 1538 | { | |||
| 1539 | html_content *html = (html_content *) c; | |||
| 1540 | ||||
| 1541 | struct box *box = html->layout; | |||
| 1542 | struct box *next; | |||
| 1543 | int box_x = 0, box_y = 0; | |||
| 1544 | bool_Bool handled_scroll = false0; | |||
| 1545 | ||||
| 1546 | /* TODO: invert order; visit deepest box first */ | |||
| 1547 | ||||
| 1548 | while ((next = box_at_point(&html->unit_len_ctx, box, x, y, | |||
| 1549 | &box_x, &box_y)) != NULL((void*)0)) { | |||
| 1550 | box = next; | |||
| 1551 | ||||
| 1552 | if (box->style && css_computed_visibility(box->style) == | |||
| 1553 | CSS_VISIBILITY_HIDDEN) | |||
| 1554 | continue; | |||
| 1555 | ||||
| 1556 | /* Pass into iframe */ | |||
| 1557 | if (box->iframe) { | |||
| 1558 | float scale = browser_window_get_scale(box->iframe); | |||
| 1559 | ||||
| 1560 | if (browser_window_scroll_at_point(box->iframe, | |||
| 1561 | (x - box_x) * scale, | |||
| 1562 | (y - box_y) * scale, | |||
| 1563 | scrx, scry) == true1) | |||
| 1564 | return true1; | |||
| 1565 | } | |||
| 1566 | ||||
| 1567 | /* Pass into textarea widget */ | |||
| 1568 | if (box->gadget && (box->gadget->type == GADGET_TEXTAREA || | |||
| 1569 | box->gadget->type == GADGET_PASSWORD || | |||
| 1570 | box->gadget->type == GADGET_TEXTBOX) && | |||
| 1571 | textarea_scroll(box->gadget->data.text.ta, | |||
| 1572 | scrx, scry) == true1) | |||
| 1573 | return true1; | |||
| 1574 | ||||
| 1575 | /* Pass into object */ | |||
| 1576 | if (box->object != NULL((void*)0) && content_scroll_at_point( | |||
| 1577 | box->object, x - box_x, y - box_y, | |||
| 1578 | scrx, scry) == true1) | |||
| 1579 | return true1; | |||
| 1580 | ||||
| 1581 | /* Handle box scrollbars */ | |||
| 1582 | if (box->scroll_y && scrollbar_scroll(box->scroll_y, scry)) | |||
| 1583 | handled_scroll = true1; | |||
| 1584 | ||||
| 1585 | if (box->scroll_x && scrollbar_scroll(box->scroll_x, scrx)) | |||
| 1586 | handled_scroll = true1; | |||
| 1587 | ||||
| 1588 | if (handled_scroll == true1) | |||
| 1589 | return true1; | |||
| 1590 | } | |||
| 1591 | ||||
| 1592 | return false0; | |||
| 1593 | } | |||
| 1594 | ||||
| 1595 | /** Helper for file gadgets to store their filename unencoded on the | |||
| 1596 | * dom node associated with the gadget. | |||
| 1597 | * | |||
| 1598 | * \todo Get rid of this crap eventually | |||
| 1599 | */ | |||
| 1600 | static void html__dom_user_data_handler(dom_node_operation operation, | |||
| 1601 | dom_string *key, void *_data, struct dom_node *src, | |||
| 1602 | struct dom_node *dst) | |||
| 1603 | { | |||
| 1604 | char *oldfile; | |||
| 1605 | char *data = (char *)_data; | |||
| 1606 | ||||
| 1607 | if (!dom_string_isequal(corestring_dom___ns_key_file_name_node_data, | |||
| 1608 | key) || data == NULL((void*)0)) { | |||
| 1609 | return; | |||
| 1610 | } | |||
| 1611 | ||||
| 1612 | switch (operation) { | |||
| 1613 | case DOM_NODE_CLONED: | |||
| 1614 | if (dom_node_set_user_data(dst,dom_node_set_user_data( (dom_node *) (dst), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(data)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) | |||
| 1615 | corestring_dom___ns_key_file_name_node_data,dom_node_set_user_data( (dom_node *) (dst), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(data)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) | |||
| 1616 | strdup(data), html__dom_user_data_handler,dom_node_set_user_data( (dom_node *) (dst), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(data)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) | |||
| 1617 | &oldfile)dom_node_set_user_data( (dom_node *) (dst), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(data)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) == DOM_NO_ERR) { | |||
| 1618 | if (oldfile != NULL((void*)0)) | |||
| 1619 | free(oldfile); | |||
| 1620 | } | |||
| 1621 | break; | |||
| 1622 | ||||
| 1623 | case DOM_NODE_RENAMED: | |||
| 1624 | case DOM_NODE_IMPORTED: | |||
| 1625 | case DOM_NODE_ADOPTED: | |||
| 1626 | break; | |||
| 1627 | ||||
| 1628 | case DOM_NODE_DELETED: | |||
| 1629 | free(data); | |||
| 1630 | break; | |||
| 1631 | default: | |||
| 1632 | NSLOG(netsurf, INFO, "User data operation not handled.")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1632 , }; nslog__log(&_nslog_ctx, "User data operation not handled." ); } } while(0); | |||
| 1633 | assert(0)((0) ? (void) (0) : __assert_fail ("0", "content/handlers/html/html.c" , 1633, __extension__ __PRETTY_FUNCTION__)); | |||
| 1634 | } | |||
| 1635 | } | |||
| 1636 | ||||
| 1637 | static void html__set_file_gadget_filename(struct content *c, | |||
| 1638 | struct form_control *gadget, const char *fn) | |||
| 1639 | { | |||
| 1640 | nserror ret; | |||
| 1641 | char *utf8_fn, *oldfile = NULL((void*)0); | |||
| 1642 | html_content *html = (html_content *)c; | |||
| 1643 | struct box *file_box = gadget->box; | |||
| 1644 | ||||
| 1645 | ret = guit->utf8->local_to_utf8(fn, 0, &utf8_fn); | |||
| 1646 | if (ret != NSERROR_OK) { | |||
| 1647 | assert(ret != NSERROR_BAD_ENCODING)((ret != NSERROR_BAD_ENCODING) ? (void) (0) : __assert_fail ( "ret != NSERROR_BAD_ENCODING", "content/handlers/html/html.c" , 1647, __extension__ __PRETTY_FUNCTION__)); | |||
| 1648 | NSLOG(netsurf, INFO,do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1649 , }; nslog__log(&_nslog_ctx, "utf8 to local encoding conversion failed" ); } } while(0) | |||
| 1649 | "utf8 to local encoding conversion failed")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1649 , }; nslog__log(&_nslog_ctx, "utf8 to local encoding conversion failed" ); } } while(0); | |||
| 1650 | /* Load was for us - just no memory */ | |||
| 1651 | return; | |||
| 1652 | } | |||
| 1653 | ||||
| 1654 | form_gadget_update_value(gadget, utf8_fn); | |||
| 1655 | ||||
| 1656 | /* corestring_dom___ns_key_file_name_node_data */ | |||
| 1657 | if (dom_node_set_user_data((dom_node *)file_box->gadget->node,dom_node_set_user_data( (dom_node *) ((dom_node *)file_box-> gadget->node), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(fn)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) | |||
| 1658 | corestring_dom___ns_key_file_name_node_data,dom_node_set_user_data( (dom_node *) ((dom_node *)file_box-> gadget->node), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(fn)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) | |||
| 1659 | strdup(fn), html__dom_user_data_handler,dom_node_set_user_data( (dom_node *) ((dom_node *)file_box-> gadget->node), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(fn)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) | |||
| 1660 | &oldfile)dom_node_set_user_data( (dom_node *) ((dom_node *)file_box-> gadget->node), (corestring_dom___ns_key_file_name_node_data ), (void *) (strdup(fn)), (dom_user_data_handler) html__dom_user_data_handler , (void **) (&oldfile)) == DOM_NO_ERR) { | |||
| 1661 | if (oldfile != NULL((void*)0)) | |||
| 1662 | free(oldfile); | |||
| 1663 | } | |||
| 1664 | ||||
| 1665 | /* Redraw box. */ | |||
| 1666 | html__redraw_a_box(html, file_box); | |||
| 1667 | } | |||
| 1668 | ||||
| 1669 | void html_set_file_gadget_filename(struct hlcache_handle *hl, | |||
| 1670 | struct form_control *gadget, const char *fn) | |||
| 1671 | { | |||
| 1672 | return html__set_file_gadget_filename(hlcache_handle_get_content(hl), | |||
| 1673 | gadget, fn); | |||
| 1674 | } | |||
| 1675 | ||||
| 1676 | /** | |||
| 1677 | * Drop a file onto a content at a particular point, or determine if a file | |||
| 1678 | * may be dropped onto the content at given point. | |||
| 1679 | * | |||
| 1680 | * \param c html content to look inside | |||
| 1681 | * \param x x-coordinate of point of interest | |||
| 1682 | * \param y y-coordinate of point of interest | |||
| 1683 | * \param file path to file to be dropped, or NULL to know if drop allowed | |||
| 1684 | * \return true iff file drop has been handled, or if drop possible (NULL file) | |||
| 1685 | */ | |||
| 1686 | static bool_Bool html_drop_file_at_point(struct content *c, int x, int y, char *file) | |||
| 1687 | { | |||
| 1688 | html_content *html = (html_content *) c; | |||
| 1689 | ||||
| 1690 | struct box *box = html->layout; | |||
| 1691 | struct box *next; | |||
| 1692 | struct box *file_box = NULL((void*)0); | |||
| 1693 | struct box *text_box = NULL((void*)0); | |||
| 1694 | int box_x = 0, box_y = 0; | |||
| 1695 | ||||
| 1696 | /* Scan box tree for boxes that can handle drop */ | |||
| 1697 | while ((next = box_at_point(&html->unit_len_ctx, box, x, y, | |||
| 1698 | &box_x, &box_y)) != NULL((void*)0)) { | |||
| 1699 | box = next; | |||
| 1700 | ||||
| 1701 | if (box->style && | |||
| 1702 | css_computed_visibility(box->style) == CSS_VISIBILITY_HIDDEN) | |||
| 1703 | continue; | |||
| 1704 | ||||
| 1705 | if (box->iframe) { | |||
| 1706 | float scale = browser_window_get_scale(box->iframe); | |||
| 1707 | return browser_window_drop_file_at_point( | |||
| 1708 | box->iframe, | |||
| 1709 | (x - box_x) * scale, | |||
| 1710 | (y - box_y) * scale, | |||
| 1711 | file); | |||
| 1712 | } | |||
| 1713 | ||||
| 1714 | if (box->object && | |||
| 1715 | content_drop_file_at_point(box->object, | |||
| 1716 | x - box_x, y - box_y, file) == true1) | |||
| 1717 | return true1; | |||
| 1718 | ||||
| 1719 | if (box->gadget) { | |||
| 1720 | switch (box->gadget->type) { | |||
| 1721 | case GADGET_FILE: | |||
| 1722 | file_box = box; | |||
| 1723 | break; | |||
| 1724 | ||||
| 1725 | case GADGET_TEXTBOX: | |||
| 1726 | case GADGET_TEXTAREA: | |||
| 1727 | case GADGET_PASSWORD: | |||
| 1728 | text_box = box; | |||
| 1729 | break; | |||
| 1730 | ||||
| 1731 | default: /* appease compiler */ | |||
| 1732 | break; | |||
| 1733 | } | |||
| 1734 | } | |||
| 1735 | } | |||
| 1736 | ||||
| 1737 | if (!file_box && !text_box) | |||
| 1738 | /* No box capable of handling drop */ | |||
| 1739 | return false0; | |||
| 1740 | ||||
| 1741 | if (file == NULL((void*)0)) | |||
| 1742 | /* There is a box capable of handling drop here */ | |||
| 1743 | return true1; | |||
| 1744 | ||||
| 1745 | /* Handle the drop */ | |||
| 1746 | if (file_box) { | |||
| 1747 | /* File dropped on file input */ | |||
| 1748 | html__set_file_gadget_filename(c, file_box->gadget, file); | |||
| 1749 | ||||
| 1750 | } else { | |||
| 1751 | /* File dropped on text input */ | |||
| 1752 | ||||
| 1753 | size_t file_len; | |||
| 1754 | FILE *fp = NULL((void*)0); | |||
| 1755 | char *buffer; | |||
| 1756 | char *utf8_buff; | |||
| 1757 | nserror ret; | |||
| 1758 | unsigned int size; | |||
| 1759 | int bx, by; | |||
| 1760 | ||||
| 1761 | /* Open file */ | |||
| 1762 | fp = fopen(file, "rb"); | |||
| 1763 | if (fp == NULL((void*)0)) { | |||
| 1764 | /* Couldn't open file, but drop was for us */ | |||
| 1765 | return true1; | |||
| 1766 | } | |||
| 1767 | ||||
| 1768 | /* Get filesize */ | |||
| 1769 | fseek(fp, 0, SEEK_END2); | |||
| 1770 | file_len = ftell(fp); | |||
| 1771 | fseek(fp, 0, SEEK_SET0); | |||
| 1772 | ||||
| 1773 | if ((long)file_len == -1) { | |||
| 1774 | /* unable to get file length, but drop was for us */ | |||
| 1775 | fclose(fp); | |||
| 1776 | return true1; | |||
| 1777 | } | |||
| 1778 | ||||
| 1779 | /* Allocate buffer for file data */ | |||
| 1780 | buffer = malloc(file_len + 1); | |||
| 1781 | if (buffer == NULL((void*)0)) { | |||
| 1782 | /* No memory, but drop was for us */ | |||
| 1783 | fclose(fp); | |||
| 1784 | return true1; | |||
| 1785 | } | |||
| 1786 | ||||
| 1787 | /* Stick file into buffer */ | |||
| 1788 | if (file_len != fread(buffer, 1, file_len, fp)) { | |||
| 1789 | /* Failed, but drop was for us */ | |||
| 1790 | free(buffer); | |||
| 1791 | fclose(fp); | |||
| 1792 | return true1; | |||
| 1793 | } | |||
| 1794 | ||||
| 1795 | /* Done with file */ | |||
| 1796 | fclose(fp); | |||
| 1797 | ||||
| 1798 | /* Ensure buffer's string termination */ | |||
| 1799 | buffer[file_len] = '\0'; | |||
| 1800 | ||||
| 1801 | /* TODO: Sniff for text? */ | |||
| 1802 | ||||
| 1803 | /* Convert to UTF-8 */ | |||
| 1804 | ret = guit->utf8->local_to_utf8(buffer, file_len, &utf8_buff); | |||
| 1805 | if (ret != NSERROR_OK) { | |||
| 1806 | /* bad encoding shouldn't happen */ | |||
| 1807 | NSLOG(netsurf, ERROR,do { if (NSLOG_LEVEL_ERROR >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_ERROR, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1809 , }; nslog__log(&_nslog_ctx, "local to utf8 encoding failed (%s)" , messages_get_errorcode(ret)); } } while(0) | |||
| 1808 | "local to utf8 encoding failed (%s)",do { if (NSLOG_LEVEL_ERROR >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_ERROR, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1809 , }; nslog__log(&_nslog_ctx, "local to utf8 encoding failed (%s)" , messages_get_errorcode(ret)); } } while(0) | |||
| 1809 | messages_get_errorcode(ret))do { if (NSLOG_LEVEL_ERROR >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_ERROR, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1809 , }; nslog__log(&_nslog_ctx, "local to utf8 encoding failed (%s)" , messages_get_errorcode(ret)); } } while(0); | |||
| 1810 | assert(ret != NSERROR_BAD_ENCODING)((ret != NSERROR_BAD_ENCODING) ? (void) (0) : __assert_fail ( "ret != NSERROR_BAD_ENCODING", "content/handlers/html/html.c" , 1810, __extension__ __PRETTY_FUNCTION__)); | |||
| 1811 | free(buffer); | |||
| 1812 | return true1; | |||
| 1813 | } | |||
| 1814 | ||||
| 1815 | /* Done with buffer */ | |||
| 1816 | free(buffer); | |||
| 1817 | ||||
| 1818 | /* Get new length */ | |||
| 1819 | size = strlen(utf8_buff); | |||
| 1820 | ||||
| 1821 | /* Simulate a click over the input box, to place caret */ | |||
| 1822 | box_coords(text_box, &bx, &by); | |||
| 1823 | textarea_mouse_action(text_box->gadget->data.text.ta, | |||
| 1824 | BROWSER_MOUSE_PRESS_1, x - bx, y - by); | |||
| 1825 | ||||
| 1826 | /* Paste the file as text */ | |||
| 1827 | textarea_drop_text(text_box->gadget->data.text.ta, | |||
| 1828 | utf8_buff, size); | |||
| 1829 | ||||
| 1830 | free(utf8_buff); | |||
| 1831 | } | |||
| 1832 | ||||
| 1833 | return true1; | |||
| 1834 | } | |||
| 1835 | ||||
| 1836 | ||||
| 1837 | /** | |||
| 1838 | * set debug status. | |||
| 1839 | * | |||
| 1840 | * \param c The content to debug | |||
| 1841 | * \param op The debug operation type | |||
| 1842 | */ | |||
| 1843 | static nserror | |||
| 1844 | html_debug(struct content *c, enum content_debug op) | |||
| 1845 | { | |||
| 1846 | html_redraw_debug = !html_redraw_debug; | |||
| 1847 | ||||
| 1848 | return NSERROR_OK; | |||
| 1849 | } | |||
| 1850 | ||||
| 1851 | ||||
| 1852 | /** | |||
| 1853 | * Dump debug info concerning the html_content | |||
| 1854 | * | |||
| 1855 | * \param c The content to debug | |||
| 1856 | * \param f The file to dump to | |||
| 1857 | * \param op The debug dump type | |||
| 1858 | */ | |||
| 1859 | static nserror | |||
| 1860 | html_debug_dump(struct content *c, FILE *f, enum content_debug op) | |||
| 1861 | { | |||
| 1862 | html_content *htmlc = (html_content *)c; | |||
| 1863 | dom_node *html; | |||
| 1864 | dom_exception exc; /* returned by libdom functions */ | |||
| 1865 | nserror ret; | |||
| 1866 | ||||
| 1867 | assert(htmlc != NULL)((htmlc != ((void*)0)) ? (void) (0) : __assert_fail ("htmlc != NULL" , "content/handlers/html/html.c", 1867, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1868 | ||||
| 1869 | if (op == CONTENT_DEBUG_RENDER) { | |||
| 1870 | assert(htmlc->layout != NULL)((htmlc->layout != ((void*)0)) ? (void) (0) : __assert_fail ("htmlc->layout != NULL", "content/handlers/html/html.c", 1870, __extension__ __PRETTY_FUNCTION__)); | |||
| 1871 | box_dump(f, htmlc->layout, 0, true1); | |||
| 1872 | ret = NSERROR_OK; | |||
| 1873 | } else { | |||
| 1874 | if (htmlc->document == NULL((void*)0)) { | |||
| 1875 | NSLOG(netsurf, INFO, "No document to dump")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1875 , }; nslog__log(&_nslog_ctx, "No document to dump"); } } while (0); | |||
| 1876 | return NSERROR_DOM; | |||
| 1877 | } | |||
| 1878 | ||||
| 1879 | exc = dom_document_get_document_element(htmlc->document, (void *) &html)dom_document_get_document_element((dom_document *) (htmlc-> document), (struct dom_element **) ((void *) &html)); | |||
| 1880 | if ((exc != DOM_NO_ERR) || (html == NULL((void*)0))) { | |||
| 1881 | NSLOG(netsurf, INFO, "Unable to obtain root node")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1881 , }; nslog__log(&_nslog_ctx, "Unable to obtain root node" ); } } while(0); | |||
| 1882 | return NSERROR_DOM; | |||
| 1883 | } | |||
| 1884 | ||||
| 1885 | ret = libdom_dump_structure(html, f, 0); | |||
| 1886 | ||||
| 1887 | NSLOG(netsurf, INFO, "DOM structure dump returning %d", ret)do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "content/handlers/html/html.c", sizeof("content/handlers/html/html.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 1887 , }; nslog__log(&_nslog_ctx, "DOM structure dump returning %d" , ret); } } while(0); | |||
| 1888 | ||||
| 1889 | dom_node_unref(html)dom_node_unref((dom_node *) (html)); | |||
| 1890 | } | |||
| 1891 | ||||
| 1892 | return ret; | |||
| 1893 | } | |||
| 1894 | ||||
| 1895 | ||||
| 1896 | #if ALWAYS_DUMP_FRAMESET0 | |||
| 1897 | /** | |||
| 1898 | * Print a frameset tree to stderr. | |||
| 1899 | */ | |||
| 1900 | ||||
| 1901 | static void | |||
| 1902 | html_dump_frameset(struct content_html_frames *frame, unsigned int depth) | |||
| 1903 | { | |||
| 1904 | unsigned int i; | |||
| 1905 | int row, col, index; | |||
| 1906 | const char *unit[] = {"px", "%", "*"}; | |||
| 1907 | const char *scrolling[] = {"auto", "yes", "no"}; | |||
| 1908 | ||||
| 1909 | assert(frame)((frame) ? (void) (0) : __assert_fail ("frame", "content/handlers/html/html.c" , 1909, __extension__ __PRETTY_FUNCTION__)); | |||
| 1910 | ||||
| 1911 | fprintf(stderrstderr, "%p ", frame); | |||
| 1912 | ||||
| 1913 | fprintf(stderrstderr, "(%i %i) ", frame->rows, frame->cols); | |||
| 1914 | ||||
| 1915 | fprintf(stderrstderr, "w%g%s ", frame->width.value, unit[frame->width.unit]); | |||
| 1916 | fprintf(stderrstderr, "h%g%s ", frame->height.value,unit[frame->height.unit]); | |||
| 1917 | fprintf(stderrstderr, "(margin w%i h%i) ", | |||
| 1918 | frame->margin_width, frame->margin_height); | |||
| 1919 | ||||
| 1920 | if (frame->name) | |||
| 1921 | fprintf(stderrstderr, "'%s' ", frame->name); | |||
| 1922 | if (frame->url) | |||
| 1923 | fprintf(stderrstderr, "<%s> ", frame->url); | |||
| 1924 | ||||
| 1925 | if (frame->no_resize) | |||
| 1926 | fprintf(stderrstderr, "noresize "); | |||
| 1927 | fprintf(stderrstderr, "(scrolling %s) ", scrolling[frame->scrolling]); | |||
| 1928 | if (frame->border) | |||
| 1929 | fprintf(stderrstderr, "border %x ", | |||
| 1930 | (unsigned int) frame->border_colour); | |||
| 1931 | ||||
| 1932 | fprintf(stderrstderr, "\n"); | |||
| 1933 | ||||
| 1934 | if (frame->children) { | |||
| 1935 | for (row = 0; row != frame->rows; row++) { | |||
| 1936 | for (col = 0; col != frame->cols; col++) { | |||
| 1937 | for (i = 0; i != depth; i++) | |||
| 1938 | fprintf(stderrstderr, " "); | |||
| 1939 | fprintf(stderrstderr, "(%i %i): ", row, col); | |||
| 1940 | index = (row * frame->cols) + col; | |||
| 1941 | html_dump_frameset(&frame->children[index], | |||
| 1942 | depth + 1); | |||
| 1943 | } | |||
| 1944 | } | |||
| 1945 | } | |||
| 1946 | } | |||
| 1947 | ||||
| 1948 | #endif | |||
| 1949 | ||||
| 1950 | /** | |||
| 1951 | * Retrieve HTML document tree | |||
| 1952 | * | |||
| 1953 | * \param h HTML content to retrieve document tree from | |||
| 1954 | * \return Pointer to document tree | |||
| 1955 | */ | |||
| 1956 | dom_document *html_get_document(hlcache_handle *h) | |||
| 1957 | { | |||
| 1958 | html_content *c = (html_content *) hlcache_handle_get_content(h); | |||
| 1959 | ||||
| 1960 | assert(c != NULL)((c != ((void*)0)) ? (void) (0) : __assert_fail ("c != NULL", "content/handlers/html/html.c", 1960, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1961 | ||||
| 1962 | return c->document; | |||
| 1963 | } | |||
| 1964 | ||||
| 1965 | /** | |||
| 1966 | * Retrieve box tree | |||
| 1967 | * | |||
| 1968 | * \param h HTML content to retrieve tree from | |||
| 1969 | * \return Pointer to box tree | |||
| 1970 | * | |||
| 1971 | * \todo This API must die, as must all use of the box tree outside of | |||
| 1972 | * HTML content handler | |||
| 1973 | */ | |||
| 1974 | struct box *html_get_box_tree(hlcache_handle *h) | |||
| 1975 | { | |||
| 1976 | html_content *c = (html_content *) hlcache_handle_get_content(h); | |||
| 1977 | ||||
| 1978 | assert(c != NULL)((c != ((void*)0)) ? (void) (0) : __assert_fail ("c != NULL", "content/handlers/html/html.c", 1978, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1979 | ||||
| 1980 | return c->layout; | |||
| 1981 | } | |||
| 1982 | ||||
| 1983 | /** | |||
| 1984 | * Retrieve the charset of an HTML document | |||
| 1985 | * | |||
| 1986 | * \param c Content to retrieve charset from | |||
| 1987 | * \param op The content encoding operation to perform. | |||
| 1988 | * \return Pointer to charset, or NULL | |||
| 1989 | */ | |||
| 1990 | static const char *html_encoding(const struct content *c, enum content_encoding_type op) | |||
| 1991 | { | |||
| 1992 | html_content *html = (html_content *) c; | |||
| 1993 | static char enc_token[10] = "Encoding0"; | |||
| 1994 | ||||
| 1995 | assert(html != NULL)((html != ((void*)0)) ? (void) (0) : __assert_fail ("html != NULL" , "content/handlers/html/html.c", 1995, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1996 | ||||
| 1997 | if (op == CONTENT_ENCODING_SOURCE) { | |||
| 1998 | enc_token[8] = '0' + html->encoding_source; | |||
| 1999 | return messages_get(enc_token); | |||
| 2000 | } | |||
| 2001 | ||||
| 2002 | return html->encoding; | |||
| 2003 | } | |||
| 2004 | ||||
| 2005 | ||||
| 2006 | /** | |||
| 2007 | * Retrieve framesets used in an HTML document | |||
| 2008 | * | |||
| 2009 | * \param h Content to inspect | |||
| 2010 | * \return Pointer to framesets, or NULL if none | |||
| 2011 | */ | |||
| 2012 | struct content_html_frames *html_get_frameset(hlcache_handle *h) | |||
| 2013 | { | |||
| 2014 | html_content *c = (html_content *) hlcache_handle_get_content(h); | |||
| 2015 | ||||
| 2016 | assert(c != NULL)((c != ((void*)0)) ? (void) (0) : __assert_fail ("c != NULL", "content/handlers/html/html.c", 2016, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2017 | ||||
| 2018 | return c->frameset; | |||
| 2019 | } | |||
| 2020 | ||||
| 2021 | /** | |||
| 2022 | * Retrieve iframes used in an HTML document | |||
| 2023 | * | |||
| 2024 | * \param h Content to inspect | |||
| 2025 | * \return Pointer to iframes, or NULL if none | |||
| 2026 | */ | |||
| 2027 | struct content_html_iframe *html_get_iframe(hlcache_handle *h) | |||
| 2028 | { | |||
| 2029 | html_content *c = (html_content *) hlcache_handle_get_content(h); | |||
| 2030 | ||||
| 2031 | assert(c != NULL)((c != ((void*)0)) ? (void) (0) : __assert_fail ("c != NULL", "content/handlers/html/html.c", 2031, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2032 | ||||
| 2033 | return c->iframe; | |||
| 2034 | } | |||
| 2035 | ||||
| 2036 | /** | |||
| 2037 | * Retrieve an HTML content's base URL | |||
| 2038 | * | |||
| 2039 | * \param h Content to retrieve base target from | |||
| 2040 | * \return Pointer to URL | |||
| 2041 | */ | |||
| 2042 | nsurl *html_get_base_url(hlcache_handle *h) | |||
| 2043 | { | |||
| 2044 | html_content *c = (html_content *) hlcache_handle_get_content(h); | |||
| 2045 | ||||
| 2046 | assert(c != NULL)((c != ((void*)0)) ? (void) (0) : __assert_fail ("c != NULL", "content/handlers/html/html.c", 2046, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2047 | ||||
| 2048 | return c->base_url; | |||
| 2049 | } | |||
| 2050 | ||||
| 2051 | /** | |||
| 2052 | * Retrieve an HTML content's base target | |||
| 2053 | * | |||
| 2054 | * \param h Content to retrieve base target from | |||
| 2055 | * \return Pointer to target, or NULL if none | |||
| 2056 | */ | |||
| 2057 | const char *html_get_base_target(hlcache_handle *h) | |||
| 2058 | { | |||
| 2059 | html_content *c = (html_content *) hlcache_handle_get_content(h); | |||
| 2060 | ||||
| 2061 | assert(c != NULL)((c != ((void*)0)) ? (void) (0) : __assert_fail ("c != NULL", "content/handlers/html/html.c", 2061, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2062 | ||||
| 2063 | return c->base_target; | |||
| 2064 | } | |||
| 2065 | ||||
| 2066 | ||||
| 2067 | /** | |||
| 2068 | * Retrieve layout coordinates of box with given id | |||
| 2069 | * | |||
| 2070 | * \param h HTML document to search | |||
| 2071 | * \param frag_id String containing an element id | |||
| 2072 | * \param x Updated to global x coord iff id found | |||
| 2073 | * \param y Updated to global y coord iff id found | |||
| 2074 | * \return true iff id found | |||
| 2075 | */ | |||
| 2076 | bool_Bool html_get_id_offset(hlcache_handle *h, lwc_string *frag_id, int *x, int *y) | |||
| 2077 | { | |||
| 2078 | struct box *pos; | |||
| 2079 | struct box *layout; | |||
| 2080 | ||||
| 2081 | if (content_get_type(h) != CONTENT_HTML) | |||
| 2082 | return false0; | |||
| 2083 | ||||
| 2084 | layout = html_get_box_tree(h); | |||
| 2085 | ||||
| 2086 | if ((pos = box_find_by_id(layout, frag_id)) != 0) { | |||
| 2087 | box_coords(pos, x, y); | |||
| 2088 | return true1; | |||
| 2089 | } | |||
| 2090 | return false0; | |||
| 2091 | } | |||
| 2092 | ||||
| 2093 | bool_Bool html_exec(struct content *c, const char *src, size_t srclen) | |||
| 2094 | { | |||
| 2095 | html_content *htmlc = (html_content *)c; | |||
| 2096 | bool_Bool result = false0; | |||
| 2097 | dom_exception err; | |||
| 2098 | dom_html_body_element *body_node; | |||
| 2099 | dom_string *dom_src; | |||
| 2100 | dom_text *text_node; | |||
| 2101 | dom_node *spare_node; | |||
| 2102 | dom_html_script_element *script_node; | |||
| 2103 | ||||
| 2104 | if (htmlc->document == NULL((void*)0)) { | |||
| 2105 | NSLOG(netsurf, DEEPDEBUG, "Unable to exec, no document")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2105, }; nslog__log(&_nslog_ctx , "Unable to exec, no document"); } } while(0); | |||
| 2106 | goto out_no_string; | |||
| 2107 | } | |||
| 2108 | ||||
| 2109 | err = dom_string_create((const uint8_t *)src, srclen, &dom_src); | |||
| 2110 | if (err != DOM_NO_ERR) { | |||
| 2111 | NSLOG(netsurf, DEEPDEBUG, "Unable to exec, could not create string")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2111, }; nslog__log(&_nslog_ctx , "Unable to exec, could not create string"); } } while(0); | |||
| 2112 | goto out_no_string; | |||
| 2113 | } | |||
| 2114 | ||||
| 2115 | err = dom_html_document_get_body(htmlc->document, &body_node)dom_html_document_get_body((dom_html_document *) (htmlc->document ), (struct dom_html_element **) (&body_node)); | |||
| 2116 | if (err != DOM_NO_ERR) { | |||
| 2117 | NSLOG(netsurf, DEEPDEBUG, "Unable to retrieve body element")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2117, }; nslog__log(&_nslog_ctx , "Unable to retrieve body element"); } } while(0); | |||
| 2118 | goto out_no_body; | |||
| 2119 | } | |||
| 2120 | ||||
| 2121 | err = dom_document_create_text_node(htmlc->document, dom_src, &text_node)dom_document_create_text_node((dom_document *) (htmlc->document ), (dom_src), (struct dom_text **) (&text_node)); | |||
| 2122 | if (err != DOM_NO_ERR) { | |||
| 2123 | NSLOG(netsurf, DEEPDEBUG, "Unable to exec, could not create text node")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2123, }; nslog__log(&_nslog_ctx , "Unable to exec, could not create text node"); } } while(0); | |||
| 2124 | goto out_no_text_node; | |||
| 2125 | } | |||
| 2126 | ||||
| 2127 | err = dom_document_create_element(htmlc->document, corestring_dom_SCRIPT, &script_node)dom_document_create_element( (dom_document *) (htmlc->document ), (corestring_dom_SCRIPT), (struct dom_element **) (&script_node )); | |||
| 2128 | if (err != DOM_NO_ERR) { | |||
| 2129 | NSLOG(netsurf, DEEPDEBUG, "Unable to exec, could not create script node")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2129, }; nslog__log(&_nslog_ctx , "Unable to exec, could not create script node"); } } while( 0); | |||
| 2130 | goto out_no_script_node; | |||
| 2131 | } | |||
| 2132 | ||||
| 2133 | err = dom_node_append_child(script_node, text_node, &spare_node)dom_node_append_child( (dom_node *) (script_node), (dom_node * ) (text_node), (dom_node **) (&spare_node)); | |||
| 2134 | if (err != DOM_NO_ERR) { | |||
| 2135 | NSLOG(netsurf, DEEPDEBUG, "Unable to exec, could not insert code node into script node")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2135, }; nslog__log(&_nslog_ctx , "Unable to exec, could not insert code node into script node" ); } } while(0); | |||
| 2136 | goto out_unparented; | |||
| 2137 | } | |||
| 2138 | dom_node_unref(spare_node)dom_node_unref((dom_node *) (spare_node)); /* We do not need the spare ref at all */ | |||
| 2139 | ||||
| 2140 | err = dom_node_append_child(body_node, script_node, &spare_node)dom_node_append_child( (dom_node *) (body_node), (dom_node *) (script_node), (dom_node **) (&spare_node)); | |||
| 2141 | if (err != DOM_NO_ERR) { | |||
| 2142 | NSLOG(netsurf, DEEPDEBUG, "Unable to exec, could not insert script node into document body")do { if (NSLOG_LEVEL_DEEPDEBUG >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_DEEPDEBUG, "content/handlers/html/html.c", sizeof ("content/handlers/html/html.c") - 1, __PRETTY_FUNCTION__, sizeof (__PRETTY_FUNCTION__) - 1, 2142, }; nslog__log(&_nslog_ctx , "Unable to exec, could not insert script node into document body" ); } } while(0); | |||
| 2143 | goto out_unparented; | |||
| 2144 | } | |||
| 2145 | dom_node_unref(spare_node)dom_node_unref((dom_node *) (spare_node)); /* Again no need for the spare ref */ | |||
| 2146 | ||||
| 2147 | /* We successfully inserted the node into the DOM */ | |||
| 2148 | ||||
| 2149 | result = true1; | |||
| 2150 | ||||
| 2151 | /* Now we unwind, starting by removing the script from wherever it | |||
| 2152 | * ended up parented | |||
| 2153 | */ | |||
| 2154 | ||||
| 2155 | err = dom_node_get_parent_node(script_node, &spare_node)dom_node_get_parent_node( (dom_node *) (script_node), (dom_node **) (&spare_node)); | |||
| 2156 | if (err == DOM_NO_ERR && spare_node != NULL((void*)0)) { | |||
| 2157 | dom_node *second_spare; | |||
| 2158 | err = dom_node_remove_child(spare_node, script_node, &second_spare)dom_node_remove_child( (dom_node *) (spare_node), (dom_node * ) (script_node), (dom_node **) (&second_spare)); | |||
| 2159 | if (err == DOM_NO_ERR) { | |||
| 2160 | dom_node_unref(second_spare)dom_node_unref((dom_node *) (second_spare)); | |||
| 2161 | } | |||
| 2162 | dom_node_unref(spare_node)dom_node_unref((dom_node *) (spare_node)); | |||
| 2163 | } | |||
| 2164 | ||||
| 2165 | out_unparented: | |||
| 2166 | dom_node_unref(script_node)dom_node_unref((dom_node *) (script_node)); | |||
| 2167 | out_no_script_node: | |||
| 2168 | dom_node_unref(text_node)dom_node_unref((dom_node *) (text_node)); | |||
| 2169 | out_no_text_node: | |||
| 2170 | dom_node_unref(body_node)dom_node_unref((dom_node *) (body_node)); | |||
| 2171 | out_no_body: | |||
| 2172 | dom_string_unref(dom_src); | |||
| 2173 | out_no_string: | |||
| 2174 | return result; | |||
| 2175 | } | |||
| 2176 | ||||
| 2177 | /* See \ref content_saw_insecure_objects */ | |||
| 2178 | static bool_Bool | |||
| 2179 | html_saw_insecure_objects(struct content *c) | |||
| 2180 | { | |||
| 2181 | html_content *htmlc = (html_content *)c; | |||
| 2182 | struct content_html_object *obj = htmlc->object_list; | |||
| 2183 | ||||
| 2184 | /* Check through the object list */ | |||
| 2185 | while (obj != NULL((void*)0)) { | |||
| 2186 | if (obj->content != NULL((void*)0)) { | |||
| 2187 | if (content_saw_insecure_objects(obj->content)) | |||
| 2188 | return true1; | |||
| 2189 | } | |||
| 2190 | obj = obj->next; | |||
| 2191 | } | |||
| 2192 | ||||
| 2193 | /* Now check the script list */ | |||
| 2194 | if (html_saw_insecure_scripts(htmlc)) { | |||
| 2195 | return true1; | |||
| 2196 | } | |||
| 2197 | ||||
| 2198 | /* Now check stylesheets */ | |||
| 2199 | if (html_css_saw_insecure_stylesheets(htmlc)) { | |||
| 2200 | return true1; | |||
| 2201 | } | |||
| 2202 | ||||
| 2203 | return false0; | |||
| 2204 | } | |||
| 2205 | ||||
| 2206 | /** | |||
| 2207 | * Compute the type of a content | |||
| 2208 | * | |||
| 2209 | * \return CONTENT_HTML | |||
| 2210 | */ | |||
| 2211 | static content_type html_content_type(void) | |||
| 2212 | { | |||
| 2213 | return CONTENT_HTML; | |||
| 2214 | } | |||
| 2215 | ||||
| 2216 | ||||
| 2217 | static void html_fini(void) | |||
| 2218 | { | |||
| 2219 | html_css_fini(); | |||
| 2220 | } | |||
| 2221 | ||||
| 2222 | /** | |||
| 2223 | * Finds all occurrences of a given string in an html box | |||
| 2224 | * | |||
| 2225 | * \param pattern the string pattern to search for | |||
| 2226 | * \param p_len pattern length | |||
| 2227 | * \param cur pointer to the current box | |||
| 2228 | * \param case_sens whether to perform a case sensitive search | |||
| 2229 | * \param context The search context to add the entry to. | |||
| 2230 | * \return true on success, false on memory allocation failure | |||
| 2231 | */ | |||
| 2232 | static nserror | |||
| 2233 | find_occurrences_html_box(const char *pattern, | |||
| 2234 | int p_len, | |||
| 2235 | struct box *cur, | |||
| 2236 | bool_Bool case_sens, | |||
| 2237 | struct textsearch_context *context) | |||
| 2238 | { | |||
| 2239 | struct box *a; | |||
| 2240 | nserror res = NSERROR_OK; | |||
| 2241 | ||||
| 2242 | /* ignore this box, if there's no visible text */ | |||
| 2243 | if (!cur->object && cur->text) { | |||
| 2244 | const char *text = cur->text; | |||
| 2245 | unsigned length = cur->length; | |||
| 2246 | ||||
| 2247 | while (length > 0) { | |||
| 2248 | unsigned match_length; | |||
| 2249 | unsigned match_offset; | |||
| 2250 | const char *new_text; | |||
| 2251 | const char *pos; | |||
| 2252 | ||||
| 2253 | pos = content_textsearch_find_pattern(text, | |||
| 2254 | length, | |||
| 2255 | pattern, | |||
| 2256 | p_len, | |||
| 2257 | case_sens, | |||
| 2258 | &match_length); | |||
| 2259 | if (!pos) | |||
| 2260 | break; | |||
| 2261 | ||||
| 2262 | /* found string in box => add to list */ | |||
| 2263 | match_offset = pos - cur->text; | |||
| 2264 | ||||
| 2265 | res = content_textsearch_add_match(context, | |||
| 2266 | cur->byte_offset + match_offset, | |||
| 2267 | cur->byte_offset + match_offset + match_length, | |||
| 2268 | cur, | |||
| 2269 | cur); | |||
| 2270 | if (res != NSERROR_OK) { | |||
| 2271 | return res; | |||
| 2272 | } | |||
| 2273 | ||||
| 2274 | new_text = pos + match_length; | |||
| 2275 | length -= (new_text - text); | |||
| 2276 | text = new_text; | |||
| 2277 | } | |||
| 2278 | } | |||
| 2279 | ||||
| 2280 | /* and recurse */ | |||
| 2281 | for (a = cur->children; a; a = a->next) { | |||
| 2282 | res = find_occurrences_html_box(pattern, | |||
| 2283 | p_len, | |||
| 2284 | a, | |||
| 2285 | case_sens, | |||
| 2286 | context); | |||
| 2287 | if (res != NSERROR_OK) { | |||
| 2288 | return res; | |||
| 2289 | } | |||
| 2290 | } | |||
| 2291 | ||||
| 2292 | return res; | |||
| 2293 | } | |||
| 2294 | ||||
| 2295 | /** | |||
| 2296 | * Finds all occurrences of a given string in the html box tree | |||
| 2297 | * | |||
| 2298 | * \param pattern the string pattern to search for | |||
| 2299 | * \param p_len pattern length | |||
| 2300 | * \param c The content to search | |||
| 2301 | * \param csens whether to perform a case sensitive search | |||
| 2302 | * \param context The search context to add the entry to. | |||
| 2303 | * \return true on success, false on memory allocation failure | |||
| 2304 | */ | |||
| 2305 | static nserror | |||
| 2306 | html_textsearch_find(struct content *c, | |||
| 2307 | struct textsearch_context *context, | |||
| 2308 | const char *pattern, | |||
| 2309 | int p_len, | |||
| 2310 | bool_Bool csens) | |||
| 2311 | { | |||
| 2312 | html_content *html = (html_content *)c; | |||
| 2313 | ||||
| 2314 | if (html->layout == NULL((void*)0)) { | |||
| 2315 | return NSERROR_INVALID; | |||
| 2316 | } | |||
| 2317 | ||||
| 2318 | return find_occurrences_html_box(pattern, | |||
| 2319 | p_len, | |||
| 2320 | html->layout, | |||
| 2321 | csens, | |||
| 2322 | context); | |||
| 2323 | } | |||
| 2324 | ||||
| 2325 | ||||
| 2326 | static nserror | |||
| 2327 | html_textsearch_bounds(struct content *c, | |||
| 2328 | unsigned start_idx, | |||
| 2329 | unsigned end_idx, | |||
| 2330 | struct box *start_box, | |||
| 2331 | struct box *end_box, | |||
| 2332 | struct rect *bounds) | |||
| 2333 | { | |||
| 2334 | /* get box position and jump to it */ | |||
| 2335 | box_coords(start_box, &bounds->x0, &bounds->y0); | |||
| 2336 | /* \todo: move x0 in by correct idx */ | |||
| 2337 | box_coords(end_box, &bounds->x1, &bounds->y1); | |||
| 2338 | /* \todo: move x1 in by correct idx */ | |||
| 2339 | bounds->x1 += end_box->width; | |||
| 2340 | bounds->y1 += end_box->height; | |||
| 2341 | ||||
| 2342 | return NSERROR_OK; | |||
| 2343 | } | |||
| 2344 | ||||
| 2345 | ||||
| 2346 | /** | |||
| 2347 | * HTML content handler function table | |||
| 2348 | */ | |||
| 2349 | static const content_handler html_content_handler = { | |||
| 2350 | .fini = html_fini, | |||
| 2351 | .create = html_create, | |||
| 2352 | .process_data = html_process_data, | |||
| 2353 | .data_complete = html_convert, | |||
| 2354 | .reformat = html_reformat, | |||
| 2355 | .destroy = html_destroy, | |||
| 2356 | .stop = html_stop, | |||
| 2357 | .mouse_track = html_mouse_track, | |||
| 2358 | .mouse_action = html_mouse_action, | |||
| 2359 | .keypress = html_keypress, | |||
| 2360 | .redraw = html_redraw, | |||
| 2361 | .open = html_open, | |||
| 2362 | .close = html_close, | |||
| 2363 | .get_selection = html_get_selection, | |||
| 2364 | .clear_selection = html_clear_selection, | |||
| 2365 | .get_contextual_content = html_get_contextual_content, | |||
| 2366 | .scroll_at_point = html_scroll_at_point, | |||
| 2367 | .drop_file_at_point = html_drop_file_at_point, | |||
| 2368 | .debug_dump = html_debug_dump, | |||
| 2369 | .debug = html_debug, | |||
| 2370 | .clone = html_clone, | |||
| 2371 | .get_encoding = html_encoding, | |||
| 2372 | .type = html_content_type, | |||
| 2373 | .exec = html_exec, | |||
| 2374 | .saw_insecure_objects = html_saw_insecure_objects, | |||
| 2375 | .textsearch_find = html_textsearch_find, | |||
| 2376 | .textsearch_bounds = html_textsearch_bounds, | |||
| 2377 | .textselection_redraw = html_textselection_redraw, | |||
| 2378 | .textselection_copy = html_textselection_copy, | |||
| 2379 | .textselection_get_end = html_textselection_get_end, | |||
| 2380 | .no_share = true1, | |||
| 2381 | }; | |||
| 2382 | ||||
| 2383 | ||||
| 2384 | /* exported function documented in html/html.h */ | |||
| 2385 | nserror html_init(void) | |||
| 2386 | { | |||
| 2387 | uint32_t i; | |||
| 2388 | nserror error; | |||
| 2389 | ||||
| 2390 | error = html_css_init(); | |||
| 2391 | if (error != NSERROR_OK) | |||
| 2392 | goto error; | |||
| 2393 | ||||
| 2394 | for (i = 0; i < NOF_ELEMENTS(html_types)(sizeof(html_types)/sizeof(*(html_types))); i++) { | |||
| 2395 | error = content_factory_register_handler(html_types[i], | |||
| 2396 | &html_content_handler); | |||
| 2397 | if (error != NSERROR_OK) | |||
| 2398 | goto error; | |||
| 2399 | } | |||
| 2400 | ||||
| 2401 | return NSERROR_OK; | |||
| 2402 | ||||
| 2403 | error: | |||
| 2404 | html_fini(); | |||
| 2405 | ||||
| 2406 | return error; | |||
| 2407 | } |