1 /*
2  * test-stubs.c - this file is part of XMLSnippets, a Geany plugin
3  *
4  * Copyright 2010 Eugene Arshinov <earshinov(at)gmail(dot)com>
5  *
6  * This program 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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifdef TEST
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #include "test-stubs.h"
29 #include <ctype.h>
30 
31 
32 GHashTable *completions;
33 
test_stubs_init(void)34 void test_stubs_init(void)
35 {
36   completions = g_hash_table_new(g_str_hash, g_str_equal);
37 }
38 
test_stubs_finalize(void)39 void test_stubs_finalize(void)
40 {
41   g_hash_table_destroy(completions);
42 }
43 
editor_find_snippet(GeanyEditor * editor,const gchar * snippet_name)44 const gchar *editor_find_snippet(GeanyEditor *editor, const gchar *snippet_name)
45 {
46   return (const gchar *)g_hash_table_lookup(completions, snippet_name);
47 }
48 
49 
50 /** Searches backward through @a size bytes looking for a '<'.
51  * @param sel .
52  * @param size .
53  * @return pointer to '<' of the found opening tag within @a sel, or @c NULL if no opening tag was found.
54  */
utils_find_open_xml_tag_pos(const gchar sel[],gint size)55 const gchar *utils_find_open_xml_tag_pos(const gchar sel[], gint size)
56 {
57 	/* stolen from anjuta and modified */
58 	const gchar *begin, *cur;
59 
60 	if (G_UNLIKELY(size < 3))
61 	{	/* Smallest tag is "<p>" which is 3 characters */
62 		return NULL;
63 	}
64 	begin = &sel[0];
65 	cur = &sel[size - 1];
66 
67 	/* Skip to the character before the closing brace */
68 	while (cur > begin)
69 	{
70 		if (*cur == '>')
71 			break;
72 		--cur;
73 	}
74 	--cur;
75 	/* skip whitespace */
76 	while (cur > begin && isspace(*cur))
77 		cur--;
78 	if (*cur == '/')
79 		return NULL; /* we found a short tag which doesn't need to be closed */
80 	while (cur > begin)
81 	{
82 		if (*cur == '<')
83 			break;
84 		/* exit immediately if such non-valid XML/HTML is detected, e.g. "<script>if a >" */
85 		else if (*cur == '>')
86 			break;
87 		--cur;
88 	}
89 
90 	/* if the found tag is an opening, not a closing tag or empty <> */
91 	if (*cur == '<' && *(cur + 1) != '/' && *(cur + 1) != '>')
92 		return cur;
93 
94 	return NULL;
95 }
96 
97 #endif
98