1 /*
2  * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /**
20  * \file
21  * Private data for text/html content.
22  */
23 
24 #ifndef NETSURF_HTML_PRIVATE_H
25 #define NETSURF_HTML_PRIVATE_H
26 
27 #include <dom/bindings/hubbub/parser.h>
28 
29 #include "netsurf/types.h"
30 #include "content/content_protected.h"
31 #include "content/handlers/css/utils.h"
32 
33 
34 struct gui_layout_table;
35 struct scrollbar_msg_data;
36 struct content_redraw_data;
37 struct selection;
38 
39 typedef enum {
40 	HTML_DRAG_NONE,			/** No drag */
41 	HTML_DRAG_SELECTION,		/** Own; Text selection */
42 	HTML_DRAG_SCROLLBAR,		/** Not own; drag in scrollbar widget */
43 	HTML_DRAG_TEXTAREA_SELECTION,	/** Not own; drag in textarea widget */
44 	HTML_DRAG_TEXTAREA_SCROLLBAR,	/** Not own; drag in textarea widget */
45 	HTML_DRAG_CONTENT_SELECTION,	/** Not own; drag in child content */
46 	HTML_DRAG_CONTENT_SCROLL	/** Not own; drag in child content */
47 } html_drag_type;
48 
49 /**
50  * For drags we don't own
51  */
52 union html_drag_owner {
53 	bool no_owner;
54 	struct box *content;
55 	struct scrollbar *scrollbar;
56 	struct box *textarea;
57 };
58 
59 typedef enum {
60 	HTML_SELECTION_NONE,		/** No selection */
61 	HTML_SELECTION_TEXTAREA,	/** Selection in one of our textareas */
62 	HTML_SELECTION_SELF,		/** Selection in this html content */
63 	HTML_SELECTION_CONTENT		/** Selection in child content */
64 } html_selection_type;
65 
66 /**
67  * For getting at selections in this content or things in this content
68  */
69 union html_selection_owner {
70 	bool none;
71 	struct box *textarea;
72 	struct box *content;
73 };
74 
75 typedef enum {
76 	HTML_FOCUS_SELF,		/**< Focus is our own */
77 	HTML_FOCUS_CONTENT,		/**< Focus belongs to child content */
78 	HTML_FOCUS_TEXTAREA		/**< Focus belongs to textarea */
79 } html_focus_type;
80 
81 /**
82  * For directing input
83  */
84 union html_focus_owner {
85 	bool self;
86 	struct box *textarea;
87 	struct box *content;
88 };
89 
90 /**
91  * Data specific to CONTENT_HTML.
92  */
93 typedef struct html_content {
94 	struct content base;
95 
96 	dom_hubbub_parser *parser; /**< Parser object handle */
97 	bool parse_completed; /**< Whether the parse has been completed */
98 	bool conversion_begun; /**< Whether or not the conversion has begun */
99 
100 	/** Document tree */
101 	dom_document *document;
102 	/** Quirkyness of document */
103 	dom_document_quirks_mode quirks;
104 
105 	/** Encoding of source, NULL if unknown. */
106 	char *encoding;
107 	/** Source of encoding information. */
108 	dom_hubbub_encoding_source encoding_source;
109 
110 	/** Base URL (may be a copy of content->url). */
111 	struct nsurl *base_url;
112 	/** Base target */
113 	char *base_target;
114 
115 	/** CSS length conversion context for document. */
116 	nscss_len_ctx len_ctx;
117 
118 	/** Content has been aborted in the LOADING state */
119 	bool aborted;
120 
121 	/** Whether a meta refresh has been handled */
122 	bool refresh;
123 
124 	/** Whether a layout (reflow) is in progress */
125 	bool reflowing;
126 
127 	/** Whether an initial layout has been done */
128 	bool had_initial_layout;
129 
130 	/** Whether scripts are enabled for this content */
131 	bool enable_scripting;
132 
133 	/* Title element node */
134 	dom_node *title;
135 
136 	/** A talloc context purely for the render box tree */
137 	int *bctx;
138 	/** A context pointer for the box conversion, NULL if no conversion
139 	 * is in progress.
140 	 */
141 	void *box_conversion_context;
142 	/** Box tree, or NULL. */
143 	struct box *layout;
144 	/** Document background colour. */
145 	colour background_colour;
146 
147 	/** Font callback table */
148 	const struct gui_layout_table *font_func;
149 
150 	/** Number of entries in scripts */
151 	unsigned int scripts_count;
152 	/** Scripts */
153 	struct html_script *scripts;
154 	/** javascript thread in use */
155 	struct jsthread *jsthread;
156 
157 	/** Number of entries in stylesheet_content. */
158 	unsigned int stylesheet_count;
159 	/** Stylesheets. Each may be NULL. */
160 	struct html_stylesheet *stylesheets;
161 	/**< Style selection context */
162 	css_select_ctx *select_ctx;
163 	/**< Style selection media specification */
164 	css_media media;
165 	/**< Universal selector */
166 	lwc_string *universal;
167 
168 	/** Number of entries in object_list. */
169 	unsigned int num_objects;
170 	/** List of objects. */
171 	struct content_html_object *object_list;
172 	/** Forms, in reverse order to document. */
173 	struct form *forms;
174 	/** Hash table of imagemaps. */
175 	struct imagemap **imagemaps;
176 
177 	/** Browser window containing this document, or NULL if not open. */
178 	struct browser_window *bw;
179 
180 	/** Frameset information */
181 	struct content_html_frames *frameset;
182 
183 	/** Inline frame information */
184 	struct content_html_iframe *iframe;
185 
186 	/** Content of type CONTENT_HTML containing this, or NULL if not an
187 	 * object within a page. */
188 	struct html_content *page;
189 
190 	/** Current drag type */
191 	html_drag_type drag_type;
192 	/** Widget capturing all mouse events */
193 	union html_drag_owner drag_owner;
194 
195 	/** Current selection state */
196 	html_selection_type selection_type;
197 	/** Current selection owner */
198 	union html_selection_owner selection_owner;
199 
200 	/** Current input focus target type */
201 	html_focus_type focus_type;
202 	/** Current input focus target */
203 	union html_focus_owner focus_owner;
204 
205 	/** HTML content's own text selection object */
206 	struct selection *sel;
207 
208 	/**
209 	 * Open core-handled form SELECT menu, or NULL if none
210 	 *  currently open.
211 	 */
212 	struct form_control *visible_select_menu;
213 
214 } html_content;
215 
216 /**
217  * Render padding and margin box outlines in html_redraw().
218  */
219 extern bool html_redraw_debug;
220 
221 
222 /* in html/html.c */
223 
224 /**
225  * redraw a box
226  *
227  * \param htmlc HTML content
228  * \param box The box to redraw.
229  */
230 void html__redraw_a_box(html_content *htmlc, struct box *box);
231 
232 
233 /**
234  * Complete conversion of an HTML document
235  *
236  * \param htmlc Content to convert
237  */
238 void html_finish_conversion(html_content *htmlc);
239 
240 
241 /**
242  * Test if an HTML content conversion can begin
243  *
244  * \param htmlc		html content to test
245  * \return true iff the html content conversion can begin
246  */
247 bool html_can_begin_conversion(html_content *htmlc);
248 
249 
250 /**
251  * Begin conversion of an HTML document
252  *
253  * \param htmlc Content to convert
254  */
255 bool html_begin_conversion(html_content *htmlc);
256 
257 
258 /**
259  * execute some text as a script element
260  */
261 bool html_exec(struct content *c, const char *src, size_t srclen);
262 
263 
264 /**
265  * Attempt script execution for defer and async scripts
266  *
267  * execute scripts using algorithm found in:
268  * http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#the-script-element
269  *
270  * \param htmlc html content.
271  * \param allow_defer allow deferred execution, if not, only async scripts.
272  * \return NSERROR_OK error code.
273  */
274 nserror html_script_exec(html_content *htmlc, bool allow_defer);
275 
276 
277 /**
278  * Free all script resources and references for a html content.
279  *
280  * \param htmlc html content.
281  * \return NSERROR_OK or error code.
282  */
283 nserror html_script_free(html_content *htmlc);
284 
285 
286 /**
287  * Check if any of the scripts loaded were insecure
288  */
289 bool html_saw_insecure_scripts(html_content *htmlc);
290 
291 
292 /**
293  * Complete the HTML content state machine *iff* all scripts are finished
294  */
295 nserror html_proceed_to_done(html_content *html);
296 
297 
298 /* in html/redraw.c */
299 bool html_redraw(struct content *c, struct content_redraw_data *data,
300 		const struct rect *clip, const struct redraw_context *ctx);
301 
302 
303 /* in html/redraw_border.c */
304 bool html_redraw_borders(struct box *box, int x_parent, int y_parent,
305 		int p_width, int p_height, const struct rect *clip, float scale,
306 		const struct redraw_context *ctx);
307 
308 
309 bool html_redraw_inline_borders(struct box *box, struct rect b,
310 		const struct rect *clip, float scale, bool first, bool last,
311 		const struct redraw_context *ctx);
312 
313 
314 /* in html/script.c */
315 dom_hubbub_error html_process_script(void *ctx, dom_node *node);
316 
317 
318 /* in html/forms.c */
319 struct form *html_forms_get_forms(const char *docenc, dom_html_document *doc);
320 struct form_control *html_forms_get_control_for_node(struct form *forms,
321 		dom_node *node);
322 
323 
324 /* in html/css_fetcher.c */
325 /**
326  * Register the fetcher for the pseudo x-ns-css scheme.
327  *
328  * \return NSERROR_OK on successful registration or error code on failure.
329  */
330 nserror html_css_fetcher_register(void);
331 nserror html_css_fetcher_add_item(dom_string *data, struct nsurl *base_url,
332 		uint32_t *key);
333 
334 
335 /* Events */
336 /**
337  * Construct an event and fire it at the DOM
338  *
339  */
340 bool fire_generic_dom_event(dom_string *type, dom_node *target,
341 		    bool bubbles, bool cancelable);
342 
343 /**
344  * Construct a keyboard event and fire it at the DOM
345  */
346 bool fire_dom_keyboard_event(dom_string *type, dom_node *target,
347 		bool bubbles, bool cancelable, uint32_t key);
348 
349 /* Useful dom_string pointers */
350 struct dom_string;
351 
352 extern struct dom_string *html_dom_string_map;
353 extern struct dom_string *html_dom_string_id;
354 extern struct dom_string *html_dom_string_name;
355 extern struct dom_string *html_dom_string_area;
356 extern struct dom_string *html_dom_string_a;
357 extern struct dom_string *html_dom_string_nohref;
358 extern struct dom_string *html_dom_string_href;
359 extern struct dom_string *html_dom_string_target;
360 extern struct dom_string *html_dom_string_shape;
361 extern struct dom_string *html_dom_string_default;
362 extern struct dom_string *html_dom_string_rect;
363 extern struct dom_string *html_dom_string_rectangle;
364 extern struct dom_string *html_dom_string_coords;
365 extern struct dom_string *html_dom_string_circle;
366 extern struct dom_string *html_dom_string_poly;
367 extern struct dom_string *html_dom_string_polygon;
368 extern struct dom_string *html_dom_string_text_javascript;
369 extern struct dom_string *html_dom_string_type;
370 extern struct dom_string *html_dom_string_src;
371 
372 #endif
373