| 1 |
|
| #ifndef FRX_EDIT_H |
| 2 |
|
| #define FRX_EDIT_H |
| 3 |
|
| |
| 4 |
|
| /* The mutable tree: building documents, editing them, moving subtrees. |
| 5 |
|
| * |
| 6 |
|
| * Every operation keeps the two promises the read-only tree made. Pointers |
| 7 |
|
| * stay stable: a node is never moved or freed by an edit; detaching |
| 8 |
|
| * unlinks it and it remains a valid node of its document, so a Perl Node |
| 9 |
|
| * holding it holds the document as before. Growth is by replacement |
| 10 |
|
| * block: attributes and declarations are arena blocks with a capacity, |
| 11 |
|
| * and adding past it allocates a larger block, copies, and orphans the |
| 12 |
|
| * old one until the document is freed (frx_tree.h says so at the top). |
| 13 |
|
| * |
| 14 |
|
| * Every string an edit writes is checked as XML Chars under the |
| 15 |
|
| * document's version and every name as a Name or a QName, at the edit, |
| 16 |
|
| * so an unwritable document cannot be built; the writer's own checks |
| 17 |
|
| * (frx_write.h) are a belt over these braces. A comment may not hold |
| 18 |
|
| * `--`, a PI may not hold `?>` or be targeted at `xml`. |
| 19 |
|
| * |
| 20 |
|
| * Namespaces on attach. When a subtree is appended or inserted, its |
| 21 |
|
| * scope chain is re-rooted onto the new parent's, then every element |
| 22 |
|
| * and attribute in it is checked against what is now in scope: a prefix |
| 23 |
|
| * unbound there, or bound to a different URI than the node carries, gets |
| 24 |
|
| * a declaration on that node, and an unprefixed element whose namespace |
| 25 |
|
| * is not the default in force gets one for the default (xmlns="" when it |
| 26 |
|
| * has no namespace). Every declaration goes on the node whose name would |
| 27 |
|
| * otherwise change, so it is scoped to exactly what it fixes and reaches |
| 28 |
|
| * no name elsewhere. A node's URI is always known: `ns` is resolved at |
| 29 |
|
| * parse or given at creation, and a prefix without a namespace is |
| 30 |
|
| * refused at creation. |
| 31 |
|
| * |
| 32 |
|
| * Cross-document moves are copies: import deep-copies a subtree into this |
| 33 |
|
| * arena, with the bindings in scope at its source declared on the copy's |
| 34 |
|
| * root so it means the same wherever it is attached; the source is |
| 35 |
|
| * untouched. A node from another document passed to any other edit is |
| 36 |
|
| * refused at the Perl boundary, because its strings live in the other |
| 37 |
|
| * arena and would dangle. |
| 38 |
|
| * |
| 39 |
|
| * The ID index stays a sorted array. The first edit that touches it |
| 40 |
|
| * copies the arena's array into a malloc'd one the document frees; |
| 41 |
|
| * set_attr on an indexed name refuses a duplicate with the tree |
| 42 |
|
| * unchanged and inserts in place, remove_attr and detach delete, and an |
| 43 |
|
| * attached subtree's indexed attributes are checked before the link. |
| 44 |
|
| * Every structural edit sets doc->renumber; frx_doc_renumber (frx_tree.h) |
| 45 |
|
| * recomputes `order` in one walk when a consumer of document order asks. |
| 46 |
|
| * |
| 47 |
|
| * Nothing recurses (rule 2): subtree walks use frx_walk_next and an |
| 48 |
|
| * explicit stack for the import. |
| 49 |
|
| * |
| 50 |
|
| * Needs frx_tree.h, frx_ns.h, frx_utf8.h, frx_parse.h (frx_walk_next). */ |
| 51 |
|
| |
| 52 |
|
| /* the refusal as an expression, for `return FRX_EDIT_FAIL(...)`, and as a |
| 53 |
|
| * statement, for the functions that answer with a NULL node: the comma |
| 54 |
|
| * form's discarded value is an error under the MinGW proof's -Werror */ |
| 55 |
|
| #define FRX_EDIT_FAIL(e, off, what) (frx_err_set((e), FRX_E_SYNTAX, (off), (what)), 0) |
| 56 |
|
| #define FRX_EDIT_SET(e, off, what) frx_err_set((e), FRX_E_SYNTAX, (off), (what)) |
| 57 |
|
| |
| 58 |
|
| static int |
| 59 |
181 |
| frx_edit_v11(const frx_doc *d) |
| 60 |
|
| { |
| 61 |
181 |
| return frx_str_eq(&d->version, "1.1", 3); |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| /* every code point valid UTF-8 and a Char under the version */ |
| 65 |
|
| static int |
| 66 |
181 |
| frx_edit_check_chars(const frx_doc *d, const char *p, size_t n, frx_err *e, const char *what) |
| 67 |
|
| { |
| 68 |
181 |
| size_t i = 0; |
| 69 |
181 |
| int v11 = frx_edit_v11(d); |
| 70 |
3299 |
▶ | while (i < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { |
3121 |
178 |
3299 |
|
| 71 |
|
| unsigned long cp; |
| 72 |
|
| size_t k; |
| 73 |
3121 |
| unsigned char c = (unsigned char)p[i]; |
| 74 |
3121 |
▶ | if (c < 0x80) { cp = c; k = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { cp = c; k = 1; } |
3117 |
4 |
3121 |
|
| 75 |
|
| else { |
| 76 |
4 |
| k = frx_utf8_decode((const unsigned char *)p + i, n - i, &cp); |
| 77 |
5 |
▶ | if (!k) return FRX_EDIT_FAIL(e, i, "not UTF-8"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return FRX_EDIT_FAIL(e, i, "not UTF-8"); |
2 |
2 |
4 |
|
| 78 |
|
| } |
| 79 |
3119 |
▶ | if (!(v11 ? frx_is_char11(cp) : frx_is_char(cp))) return FRX_EDIT_FAIL(e, i, what); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(v11 ? frx_is_char11(cp) : frx_is_char(cp))) return FRX_EDIT_FAIL(e, i, what); |
3 |
3116 |
3119 |
| if (!(v11 ? frx_is_char11(cp) : frx_is_char(cp))) return FRX_EDIT_FAIL(e, i, what); |
1 |
3118 |
3119 |
|
| 80 |
3118 |
| i += k; |
| 81 |
|
| } |
| 82 |
178 |
| return 1; |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
|
| /* a Name (qname 0: no colon; qname 1: at most one, a part on each side) */ |
| 86 |
|
| static int |
| 87 |
374 |
| frx_edit_check_name(const char *p, size_t n, int qname, frx_err *e, const char *what) |
| 88 |
|
| { |
| 89 |
374 |
| size_t i = 0, colons = 0; |
| 90 |
374 |
| int first = 1; |
| 91 |
374 |
▶ | if (!n) return FRX_EDIT_FAIL(e, 0, what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return FRX_EDIT_FAIL(e, 0, what); |
0 |
374 |
374 |
|
| 92 |
1608 |
▶ | while (i < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { |
1237 |
371 |
1608 |
|
| 93 |
|
| unsigned long cp; |
| 94 |
|
| size_t k; |
| 95 |
1237 |
| unsigned char c = (unsigned char)p[i]; |
| 96 |
1237 |
▶ | if (c < 0x80) { cp = c; k = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { cp = c; k = 1; } |
1237 |
0 |
1237 |
|
| 97 |
|
| else { |
| 98 |
0 |
| k = frx_utf8_decode((const unsigned char *)p + i, n - i, &cp); |
| 99 |
3 |
▶ | if (!k) return FRX_EDIT_FAIL(e, i, what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return FRX_EDIT_FAIL(e, i, what); |
0 |
0 |
0 |
|
| 100 |
|
| } |
| 101 |
1237 |
▶ | if (cp == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp == ':') { |
1 |
1236 |
1237 |
|
| 102 |
1 |
▶ | if (!qname || first || colons) return FRX_EDIT_FAIL(e, i, what); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!qname || first || colons) return FRX_EDIT_FAIL(e, i, what); |
0 |
1 |
1 |
| if (!qname || first || colons) return FRX_EDIT_FAIL(e, i, what); |
0 |
0 |
0 |
| if (!qname || first || colons) return FRX_EDIT_FAIL(e, i, what); |
0 |
0 |
0 |
|
| 103 |
0 |
| colons++; |
| 104 |
0 |
| first = 1; |
| 105 |
0 |
| i++; |
| 106 |
0 |
| continue; |
| 107 |
|
| } |
| 108 |
1236 |
▶ | if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) return FRX_EDIT_FAIL(e, i, what); |
Branch: 2 branches
| Branch | True | False | Total |
| if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) return FRX_EDIT_FAIL(e, i, what); |
374 |
862 |
1236 |
| if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) return FRX_EDIT_FAIL(e, i, what); |
2 |
1234 |
1236 |
|
| 109 |
1234 |
| first = 0; |
| 110 |
1234 |
| i += k; |
| 111 |
|
| } |
| 112 |
371 |
▶ | if (first) return FRX_EDIT_FAIL(e, n, what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (first) return FRX_EDIT_FAIL(e, n, what); |
0 |
371 |
371 |
|
| 113 |
371 |
| return 1; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| static int |
| 117 |
1368 |
| frx_edit_dup(frx_doc *d, const char *p, size_t n, frx_str *out) |
| 118 |
|
| { |
| 119 |
1368 |
▶ | *out = frx_arena_strndup(&d->arena, p ? p : "", n); |
Branch: 1 branch
| Branch | True | False | Total |
| *out = frx_arena_strndup(&d->arena, p ? p : "", n); |
1368 |
0 |
1368 |
|
| 120 |
1368 |
| return out->p != NULL; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
|
| /* ---- creation --------------------------------------------------------- */ |
| 124 |
|
| |
| 125 |
|
| /* an empty document with its document node; version11 selects XML 1.1 */ |
| 126 |
|
| static frx_doc * |
| 127 |
11 |
| frx_doc_new_empty(int version11) |
| 128 |
|
| { |
| 129 |
11 |
| frx_doc *d = frx_doc_new(); |
| 130 |
11 |
▶ | if (!d) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d) return NULL; |
0 |
11 |
11 |
|
| 131 |
11 |
| d->profile = FRX_PROFILE_FULL; |
| 132 |
11 |
| d->max_depth = 0; |
| 133 |
11 |
▶ | if (version11) { d->version.p = "1.1"; d->version.len = 3; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (version11) { d->version.p = "1.1"; d->version.len = 3; } |
2 |
9 |
11 |
|
| 134 |
11 |
| d->document = frx_node_new(d, FRX_DOCUMENT, 0); |
| 135 |
11 |
▶ | if (!d->document) { frx_doc_free(d); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d->document) { frx_doc_free(d); return NULL; } |
0 |
11 |
11 |
|
| 136 |
11 |
| return d; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| /* the qualified name prefix:local, or local, in the arena; prefix and |
| 140 |
|
| * local point into it */ |
| 141 |
|
| static int |
| 142 |
183 |
| frx_edit_qname(frx_doc *d, const char *prefix, size_t plen, const char *local, size_t llen, |
| 143 |
|
| frx_str *qname, frx_str *pfx, frx_str *loc) |
| 144 |
|
| { |
| 145 |
183 |
| char *p = (char *)frx_arena_alloc(&d->arena, plen + llen + 2); |
| 146 |
183 |
▶ | if (!p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return 0; |
0 |
183 |
183 |
|
| 147 |
183 |
▶ | if (plen) { memcpy(p, prefix, plen); p[plen] = ':'; memcpy(p + plen + 1, local, llen); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (plen) { memcpy(p, prefix, plen); p[plen] = ':'; memcpy(p + plen + 1, local, llen); } |
156 |
27 |
183 |
|
| 148 |
27 |
| else memcpy(p, local, llen); |
| 149 |
183 |
▶ | p[plen ? plen + 1 + llen : llen] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| p[plen ? plen + 1 + llen : llen] = '\0'; |
156 |
27 |
183 |
|
| 150 |
183 |
▶ | qname->p = p; qname->len = plen ? plen + 1 + llen : llen; |
Branch: 1 branch
| Branch | True | False | Total |
| qname->p = p; qname->len = plen ? plen + 1 + llen : llen; |
156 |
27 |
183 |
|
| 151 |
183 |
| frx_ns_split(qname, pfx, loc); |
| 152 |
183 |
| return 1; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* the name rules an element or attribute shares */ |
| 156 |
|
| static int |
| 157 |
194 |
| frx_edit_check_named(const char *ns, size_t nslen, const char *prefix, size_t plen, |
| 158 |
|
| const char *local, size_t llen, frx_err *e) |
| 159 |
|
| { |
| 160 |
194 |
▶ | if (!frx_edit_check_name(local, llen, 0, e, "the local name is not a name")) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_name(local, llen, 0, e, "the local name is not a name")) return 0; |
3 |
191 |
194 |
|
| 161 |
191 |
▶ | if (plen && !frx_edit_check_name(prefix, plen, 0, e, "the prefix is not a name")) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen && !frx_edit_check_name(prefix, plen, 0, e, "the prefix is not a name")) return 0; |
158 |
33 |
191 |
| if (plen && !frx_edit_check_name(prefix, plen, 0, e, "the prefix is not a name")) return 0; |
0 |
158 |
158 |
|
| 162 |
191 |
▶ | if (plen == 5 && memcmp(prefix, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, 0, "the xmlns prefix is not a name for elements or attributes; use declare_ns"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen == 5 && memcmp(prefix, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, 0, "the xmlns prefix is not a name for elements or attributes; use declare_ns"); |
1 |
190 |
191 |
| if (plen == 5 && memcmp(prefix, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, 0, "the xmlns prefix is not a name for elements or attributes; use declare_ns"); |
1 |
0 |
1 |
|
| 163 |
190 |
▶ | if (plen && !nslen) return FRX_EDIT_FAIL(e, 0, "a prefix needs a namespace name"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen && !nslen) return FRX_EDIT_FAIL(e, 0, "a prefix needs a namespace name"); |
157 |
33 |
190 |
| if (plen && !nslen) return FRX_EDIT_FAIL(e, 0, "a prefix needs a namespace name"); |
1 |
156 |
157 |
|
| 164 |
189 |
▶ | if (plen == 3 && memcmp(prefix, "xml", 3) == 0 && !(nslen == sizeof FRX_XML_NS - 1 && memcmp(ns, FRX_XML_NS, nslen) == 0)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0 && !(nslen == sizeof FRX_XML_NS - 1 && memcmp(ns, FRX_XML_NS, nslen) == 0)) |
139 |
50 |
189 |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0 && !(nslen == sizeof FRX_XML_NS - 1 && memcmp(ns, FRX_XML_NS, nslen) == 0)) |
139 |
0 |
139 |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0 && !(nslen == sizeof FRX_XML_NS - 1 && memcmp(ns, FRX_XML_NS, nslen) == 0)) |
139 |
0 |
139 |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0 && !(nslen == sizeof FRX_XML_NS - 1 && memcmp(ns, FRX_XML_NS, nslen) == 0)) |
0 |
139 |
139 |
|
| 165 |
0 |
| return FRX_EDIT_FAIL(e, 0, "the xml prefix is bound to its own namespace and no other"); |
| 166 |
189 |
▶ | if (nslen && !frx_ns_is_absolute(ns, nslen)) return FRX_EDIT_FAIL(e, 0, "a relative namespace URI is refused"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (nslen && !frx_ns_is_absolute(ns, nslen)) return FRX_EDIT_FAIL(e, 0, "a relative namespace URI is refused"); |
161 |
28 |
189 |
| if (nslen && !frx_ns_is_absolute(ns, nslen)) return FRX_EDIT_FAIL(e, 0, "a relative namespace URI is refused"); |
1 |
160 |
161 |
|
| 167 |
188 |
▶ | if (nslen == sizeof FRX_XMLNS_NS - 1 && memcmp(ns, FRX_XMLNS_NS, nslen) == 0) return FRX_EDIT_FAIL(e, 0, "the xmlns namespace name cannot be bound"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (nslen == sizeof FRX_XMLNS_NS - 1 && memcmp(ns, FRX_XMLNS_NS, nslen) == 0) return FRX_EDIT_FAIL(e, 0, "the xmlns namespace name cannot be bound"); |
0 |
188 |
188 |
| if (nslen == sizeof FRX_XMLNS_NS - 1 && memcmp(ns, FRX_XMLNS_NS, nslen) == 0) return FRX_EDIT_FAIL(e, 0, "the xmlns namespace name cannot be bound"); |
0 |
0 |
0 |
|
| 168 |
188 |
| return 1; |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
|
| static frx_node * |
| 172 |
38 |
| frx_new_element(frx_doc *d, const char *ns, size_t nslen, const char *prefix, size_t plen, |
| 173 |
|
| const char *local, size_t llen, frx_err *e) |
| 174 |
|
| { |
| 175 |
|
| frx_node *n; |
| 176 |
38 |
▶ | if (!frx_edit_check_named(ns, nslen, prefix, plen, local, llen, e)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_named(ns, nslen, prefix, plen, local, llen, e)) return NULL; |
6 |
32 |
38 |
|
| 177 |
32 |
| n = frx_node_new(d, FRX_ELEMENT, 0); |
| 178 |
32 |
▶ | if (!n) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
32 |
32 |
|
| 179 |
32 |
▶ | if (!frx_edit_qname(d, prefix, plen, local, llen, &n->qname, &n->prefix, &n->local) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_qname(d, prefix, plen, local, llen, &n->qname, &n->prefix, &n->local) |
32 |
0 |
32 |
|
| 180 |
32 |
▶ | || !frx_edit_dup(d, ns, nslen, &n->ns)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| || !frx_edit_dup(d, ns, nslen, &n->ns)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
32 |
32 |
|
| 181 |
32 |
| d->renumber = 1; |
| 182 |
32 |
| return n; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| static frx_node * |
| 186 |
18 |
| frx_new_text(frx_doc *d, const char *s, size_t n, frx_err *e) |
| 187 |
|
| { |
| 188 |
|
| frx_node *node; |
| 189 |
18 |
▶ | if (!frx_edit_check_chars(d, s, n, e, "text holds a character that is not an XML character")) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_chars(d, s, n, e, "text holds a character that is not an XML character")) return NULL; |
3 |
15 |
18 |
|
| 190 |
15 |
| node = frx_node_new(d, FRX_TEXT, 0); |
| 191 |
15 |
▶ | if (!node || !frx_edit_dup(d, s, n, &node->value)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!node || !frx_edit_dup(d, s, n, &node->value)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
15 |
0 |
15 |
| if (!node || !frx_edit_dup(d, s, n, &node->value)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
15 |
15 |
|
| 192 |
15 |
| d->renumber = 1; |
| 193 |
15 |
| return node; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| static int |
| 197 |
5 |
| frx_edit_check_comment(const frx_doc *d, const char *s, size_t n, frx_err *e) |
| 198 |
|
| { |
| 199 |
|
| size_t i; |
| 200 |
5 |
▶ | if (!frx_edit_check_chars(d, s, n, e, "a comment holds a character that is not an XML character")) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_chars(d, s, n, e, "a comment holds a character that is not an XML character")) return 0; |
0 |
5 |
5 |
|
| 201 |
18 |
▶ | for (i = 0; i + 1 < n; i++) if (s[i] == '-' && s[i + 1] == '-') return FRX_EDIT_FAIL(e, i, "-- is not allowed inside a comment"); |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i++) if (s[i] == '-' && s[i + 1] == '-') return FRX_EDIT_FAIL(e, i, "-- is not allowed inside a comment"); |
1 |
13 |
14 |
| for (i = 0; i + 1 < n; i++) if (s[i] == '-' && s[i + 1] == '-') return FRX_EDIT_FAIL(e, i, "-- is not allowed inside a comment"); |
1 |
0 |
1 |
| for (i = 0; i + 1 < n; i++) if (s[i] == '-' && s[i + 1] == '-') return FRX_EDIT_FAIL(e, i, "-- is not allowed inside a comment"); |
14 |
4 |
18 |
|
| 202 |
4 |
▶ | if (n && s[n - 1] == '-') return FRX_EDIT_FAIL(e, n - 1, "a comment cannot end in -"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && s[n - 1] == '-') return FRX_EDIT_FAIL(e, n - 1, "a comment cannot end in -"); |
4 |
0 |
4 |
| if (n && s[n - 1] == '-') return FRX_EDIT_FAIL(e, n - 1, "a comment cannot end in -"); |
0 |
4 |
4 |
|
| 203 |
4 |
| return 1; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
|
| static frx_node * |
| 207 |
3 |
| frx_new_comment(frx_doc *d, const char *s, size_t n, frx_err *e) |
| 208 |
|
| { |
| 209 |
|
| frx_node *node; |
| 210 |
3 |
▶ | if (!frx_edit_check_comment(d, s, n, e)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_comment(d, s, n, e)) return NULL; |
0 |
3 |
3 |
|
| 211 |
3 |
| node = frx_node_new(d, FRX_COMMENT, 0); |
| 212 |
3 |
▶ | if (!node || !frx_edit_dup(d, s, n, &node->value)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!node || !frx_edit_dup(d, s, n, &node->value)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
3 |
0 |
3 |
| if (!node || !frx_edit_dup(d, s, n, &node->value)) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
3 |
3 |
|
| 213 |
3 |
| d->renumber = 1; |
| 214 |
3 |
| return node; |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
|
| static int |
| 218 |
5 |
| frx_edit_check_pi(const frx_doc *d, const char *target, size_t tlen, const char *data, size_t dlen, frx_err *e) |
| 219 |
|
| { |
| 220 |
|
| frx_str t; |
| 221 |
|
| size_t i; |
| 222 |
5 |
▶ | if (!frx_edit_check_name(target, tlen, 0, e, "the target is not a name")) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_name(target, tlen, 0, e, "the target is not a name")) return 0; |
0 |
5 |
5 |
|
| 223 |
5 |
| t.p = target; t.len = tlen; |
| 224 |
5 |
▶ | if (frx_lex_target_is_xml(&t)) return FRX_EDIT_FAIL(e, 0, "the xml target is reserved"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_lex_target_is_xml(&t)) return FRX_EDIT_FAIL(e, 0, "the xml target is reserved"); |
1 |
4 |
5 |
|
| 225 |
4 |
▶ | if (!frx_edit_check_chars(d, data, dlen, e, "the data holds a character that is not an XML character")) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_chars(d, data, dlen, e, "the data holds a character that is not an XML character")) return 0; |
0 |
4 |
4 |
|
| 226 |
8 |
▶ | for (i = 0; i + 1 < dlen; i++) if (data[i] == '?' && data[i + 1] == '>') return FRX_EDIT_FAIL(e, i, "?> is not allowed inside a processing instruction"); |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i + 1 < dlen; i++) if (data[i] == '?' && data[i + 1] == '>') return FRX_EDIT_FAIL(e, i, "?> is not allowed inside a processing instruction"); |
1 |
4 |
5 |
| for (i = 0; i + 1 < dlen; i++) if (data[i] == '?' && data[i + 1] == '>') return FRX_EDIT_FAIL(e, i, "?> is not allowed inside a processing instruction"); |
1 |
0 |
1 |
| for (i = 0; i + 1 < dlen; i++) if (data[i] == '?' && data[i + 1] == '>') return FRX_EDIT_FAIL(e, i, "?> is not allowed inside a processing instruction"); |
5 |
3 |
8 |
|
| 227 |
3 |
| return 1; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| static frx_node * |
| 231 |
4 |
| frx_new_pi(frx_doc *d, const char *target, size_t tlen, const char *data, size_t dlen, frx_err *e) |
| 232 |
|
| { |
| 233 |
|
| frx_node *node; |
| 234 |
4 |
▶ | if (!frx_edit_check_pi(d, target, tlen, data, dlen, e)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_pi(d, target, tlen, data, dlen, e)) return NULL; |
1 |
3 |
4 |
|
| 235 |
3 |
| node = frx_node_new(d, FRX_PI, 0); |
| 236 |
3 |
▶ | if (!node || !frx_edit_dup(d, target, tlen, &node->local) || !frx_edit_dup(d, data, dlen, &node->value)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!node || !frx_edit_dup(d, target, tlen, &node->local) || !frx_edit_dup(d, data, dlen, &node->value)) { |
3 |
0 |
3 |
| if (!node || !frx_edit_dup(d, target, tlen, &node->local) || !frx_edit_dup(d, data, dlen, &node->value)) { |
3 |
0 |
3 |
| if (!node || !frx_edit_dup(d, target, tlen, &node->local) || !frx_edit_dup(d, data, dlen, &node->value)) { |
0 |
3 |
3 |
|
| 237 |
0 |
| FRX_EDIT_SET(e, 0, "out of memory"); |
| 238 |
0 |
| return NULL; |
| 239 |
|
| } |
| 240 |
3 |
| node->qname = node->local; |
| 241 |
3 |
| d->renumber = 1; |
| 242 |
3 |
| return node; |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| /* ---- the ID index ------------------------------------------------------ */ |
| 246 |
|
| |
| 247 |
|
| static int |
| 248 |
664 |
| frx_edit_is_id_attr(const frx_doc *d, const frx_str *local) |
| 249 |
|
| { |
| 250 |
|
| int i; |
| 251 |
1264 |
▶ | for (i = 0; i < d->n_id_attrs; i++) if (frx_str_eq2(&d->id_attrs[i], local)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < d->n_id_attrs; i++) if (frx_str_eq2(&d->id_attrs[i], local)) return 1; |
14 |
600 |
614 |
| for (i = 0; i < d->n_id_attrs; i++) if (frx_str_eq2(&d->id_attrs[i], local)) return 1; |
614 |
650 |
1264 |
|
| 252 |
650 |
| return 0; |
| 253 |
|
| } |
| 254 |
|
| |
| 255 |
|
| /* the index as a malloc'd array the document owns, once */ |
| 256 |
|
| static int |
| 257 |
10 |
| frx_edit_ids_mut(frx_doc *d) |
| 258 |
|
| { |
| 259 |
10 |
▶ | if (d->ids_mut) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->ids_mut) return 1; |
9 |
1 |
10 |
|
| 260 |
1 |
▶ | d->cap_ids_mut = d->n_ids ? d->n_ids * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| d->cap_ids_mut = d->n_ids ? d->n_ids * 2 : 8; |
1 |
0 |
1 |
|
| 261 |
1 |
| d->ids_mut = (frx_idx *)malloc((size_t)d->cap_ids_mut * sizeof *d->ids_mut); |
| 262 |
1 |
▶ | if (!d->ids_mut) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d->ids_mut) return 0; |
0 |
1 |
1 |
|
| 263 |
1 |
▶ | if (d->n_ids) memcpy(d->ids_mut, d->ids, (size_t)d->n_ids * sizeof *d->ids_mut); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->n_ids) memcpy(d->ids_mut, d->ids, (size_t)d->n_ids * sizeof *d->ids_mut); |
1 |
0 |
1 |
|
| 264 |
1 |
| d->ids = d->ids_mut; |
| 265 |
1 |
| return 1; |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
|
| /* the index slot for (attr, value): *found says a match is there */ |
| 269 |
|
| static int |
| 270 |
18 |
| frx_edit_ids_find(const frx_doc *d, const frx_str *attr, const frx_str *value, int *found) |
| 271 |
|
| { |
| 272 |
18 |
| int lo = 0, hi = d->n_ids; |
| 273 |
|
| frx_idx key; |
| 274 |
18 |
| key.attr = *attr; key.value = *value; key.node = NULL; key.offset = 0; |
| 275 |
38 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
20 |
18 |
38 |
|
| 276 |
20 |
| int mid = (lo + hi) / 2; |
| 277 |
20 |
| int c = frx_idx_key_cmp(&d->ids[mid], &key); |
| 278 |
20 |
▶ | if (c < 0) lo = mid + 1; else hi = mid; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0) lo = mid + 1; else hi = mid; |
8 |
12 |
20 |
|
| 279 |
|
| } |
| 280 |
18 |
▶ | *found = lo < d->n_ids && frx_idx_key_cmp(&d->ids[lo], &key) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| *found = lo < d->n_ids && frx_idx_key_cmp(&d->ids[lo], &key) == 0; |
10 |
8 |
18 |
| *found = lo < d->n_ids && frx_idx_key_cmp(&d->ids[lo], &key) == 0; |
7 |
3 |
10 |
|
| 281 |
18 |
| return lo; |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
|
| static int |
| 285 |
5 |
| frx_edit_ids_insert(frx_doc *d, const frx_attr *a, const frx_node *n) |
| 286 |
|
| { |
| 287 |
|
| int found, at; |
| 288 |
5 |
▶ | if (!frx_edit_ids_mut(d)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_ids_mut(d)) return 0; |
0 |
5 |
5 |
|
| 289 |
5 |
| at = frx_edit_ids_find(d, &a->local, &a->value, &found); |
| 290 |
5 |
▶ | if (found) return 0; /* the caller checked; a duplicate here is a bug */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) return 0; /* the caller checked; a duplicate here is a bug */ |
0 |
5 |
5 |
|
| 291 |
5 |
▶ | if (d->n_ids == d->cap_ids_mut) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->n_ids == d->cap_ids_mut) { |
0 |
5 |
5 |
|
| 292 |
0 |
| frx_idx *ni = (frx_idx *)realloc(d->ids_mut, (size_t)d->cap_ids_mut * 2 * sizeof *ni); |
| 293 |
0 |
▶ | if (!ni) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ni) return 0; |
0 |
0 |
0 |
|
| 294 |
0 |
| d->ids_mut = ni; d->cap_ids_mut *= 2; d->ids = ni; |
| 295 |
|
| } |
| 296 |
5 |
| memmove(d->ids_mut + at + 1, d->ids_mut + at, (size_t)(d->n_ids - at) * sizeof *d->ids_mut); |
| 297 |
5 |
| d->ids_mut[at].attr = a->local; d->ids_mut[at].value = a->value; d->ids_mut[at].node = n; d->ids_mut[at].offset = a->offset; |
| 298 |
5 |
| d->n_ids++; |
| 299 |
5 |
| return 1; |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| static int |
| 303 |
5 |
| frx_edit_ids_remove(frx_doc *d, const frx_str *attr, const frx_str *value, const frx_node *n) |
| 304 |
|
| { |
| 305 |
|
| int found, at; |
| 306 |
5 |
▶ | if (!d->n_ids) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d->n_ids) return 1; |
0 |
5 |
5 |
|
| 307 |
5 |
| at = frx_edit_ids_find(d, attr, value, &found); |
| 308 |
5 |
▶ | if (!found || d->ids[at].node != n) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!found || d->ids[at].node != n) return 1; |
5 |
0 |
5 |
| if (!found || d->ids[at].node != n) return 1; |
0 |
5 |
5 |
|
| 309 |
5 |
▶ | if (!frx_edit_ids_mut(d)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_ids_mut(d)) return 0; |
0 |
5 |
5 |
|
| 310 |
5 |
| memmove(d->ids_mut + at, d->ids_mut + at + 1, (size_t)(d->n_ids - at - 1) * sizeof *d->ids_mut); |
| 311 |
5 |
| d->n_ids--; |
| 312 |
5 |
| return 1; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
|
| /* the indexed attributes of a subtree, out of or into the index; a |
| 316 |
|
| * duplicate on the way in is refused before anything is linked */ |
| 317 |
|
| static int |
| 318 |
346 |
| frx_edit_ids_subtree(frx_doc *d, frx_node *root, int insert, frx_err *e) |
| 319 |
|
| { |
| 320 |
346 |
| frx_node *n = root; |
| 321 |
346 |
▶ | if (!d->n_id_attrs) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d->n_id_attrs) return 1; |
143 |
203 |
346 |
|
| 322 |
408 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
206 |
202 |
408 |
|
| 323 |
206 |
▶ | if (n->kind == FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->kind == FRX_ELEMENT) { |
206 |
0 |
206 |
|
| 324 |
|
| int i; |
| 325 |
511 |
▶ | for (i = 0; i < n->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) { |
306 |
205 |
511 |
|
| 326 |
306 |
| const frx_attr *a = &n->attrs[i]; |
| 327 |
306 |
▶ | if (!frx_edit_is_id_attr(d, &a->local)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_is_id_attr(d, &a->local)) continue; |
300 |
6 |
306 |
|
| 328 |
6 |
▶ | if (insert) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (insert) { |
4 |
2 |
6 |
|
| 329 |
|
| int found; |
| 330 |
4 |
| frx_edit_ids_find(d, &a->local, &a->value, &found); |
| 331 |
4 |
▶ | if (found) return FRX_EDIT_FAIL(e, a->offset, "the attached subtree carries an ID value the document already has"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) return FRX_EDIT_FAIL(e, a->offset, "the attached subtree carries an ID value the document already has"); |
1 |
3 |
4 |
|
| 332 |
2 |
▶ | } else if (!frx_edit_ids_remove(d, &a->local, &a->value, n)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!frx_edit_ids_remove(d, &a->local, &a->value, n)) { |
0 |
2 |
2 |
|
| 333 |
0 |
| return FRX_EDIT_FAIL(e, a->offset, "out of memory"); |
| 334 |
|
| } |
| 335 |
|
| } |
| 336 |
|
| } |
| 337 |
205 |
| n = (frx_node *)frx_walk_next(n, root); |
| 338 |
|
| } |
| 339 |
202 |
▶ | if (insert) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (insert) { |
101 |
101 |
202 |
|
| 340 |
203 |
▶ | for (n = root; n; n = (frx_node *)frx_walk_next(n, root)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = root; n; n = (frx_node *)frx_walk_next(n, root)) { |
102 |
101 |
203 |
|
| 341 |
102 |
▶ | if (n->kind != FRX_ELEMENT) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->kind != FRX_ELEMENT) continue; |
0 |
102 |
102 |
|
| 342 |
|
| { |
| 343 |
|
| int i; |
| 344 |
304 |
▶ | for (i = 0; i < n->n_attrs; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) |
202 |
102 |
304 |
|
| 345 |
202 |
▶ | if (frx_edit_is_id_attr(d, &n->attrs[i].local) && !frx_edit_ids_insert(d, &n->attrs[i], n)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_edit_is_id_attr(d, &n->attrs[i].local) && !frx_edit_ids_insert(d, &n->attrs[i], n)) |
2 |
200 |
202 |
| if (frx_edit_is_id_attr(d, &n->attrs[i].local) && !frx_edit_ids_insert(d, &n->attrs[i], n)) |
0 |
2 |
2 |
|
| 346 |
0 |
| return FRX_EDIT_FAIL(e, n->attrs[i].offset, "out of memory"); |
| 347 |
|
| } |
| 348 |
|
| } |
| 349 |
|
| } |
| 350 |
202 |
| return 1; |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| /* ---- namespaces ---------------------------------------------------------- */ |
| 354 |
|
| |
| 355 |
|
| /* the scope an element's descendants inherit from it */ |
| 356 |
|
| static int |
| 357 |
188 |
| frx_edit_owns_scope(const frx_node *el) |
| 358 |
|
| { |
| 359 |
188 |
▶ | return el->scope && el->n_decls && el->scope->decls == el->decls; |
Branch: 3 branches
| Branch | True | False | Total |
| return el->scope && el->n_decls && el->scope->decls == el->decls; |
43 |
145 |
188 |
| return el->scope && el->n_decls && el->scope->decls == el->decls; |
41 |
2 |
43 |
| return el->scope && el->n_decls && el->scope->decls == el->decls; |
36 |
5 |
41 |
|
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
|
| /* the subtree at root sees new_inherit where it saw old_inherit: nodes |
| 363 |
|
| * sharing it, and scope records chained onto it */ |
| 364 |
|
| static void |
| 365 |
185 |
| frx_edit_reroot(frx_node *root, const frx_scope *old_inherit, const frx_scope *new_inherit) |
| 366 |
|
| { |
| 367 |
185 |
| frx_node *n = root; |
| 368 |
185 |
▶ | if (old_inherit == new_inherit) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (old_inherit == new_inherit) return; |
19 |
166 |
185 |
|
| 369 |
371 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
205 |
166 |
371 |
|
| 370 |
205 |
▶ | if (n->kind == FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->kind == FRX_ELEMENT) { |
201 |
4 |
205 |
|
| 371 |
201 |
▶ | if (n->scope == old_inherit) n->scope = new_inherit; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->scope == old_inherit) n->scope = new_inherit; |
136 |
65 |
201 |
|
| 372 |
65 |
▶ | else if (n->scope && n->scope->up == old_inherit) ((frx_scope *)n->scope)->up = new_inherit; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n->scope && n->scope->up == old_inherit) ((frx_scope *)n->scope)->up = new_inherit; |
65 |
0 |
65 |
| else if (n->scope && n->scope->up == old_inherit) ((frx_scope *)n->scope)->up = new_inherit; |
32 |
33 |
65 |
|
| 373 |
|
| } |
| 374 |
205 |
| n = (frx_node *)frx_walk_next(n, root); |
| 375 |
|
| } |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
|
| /* a declaration on el: added, or its URI replaced; el's own scope record |
| 379 |
|
| * is created on the first, and its descendants re-rooted onto it */ |
| 380 |
|
| static int |
| 381 |
21 |
| frx_declare_ns(frx_doc *d, frx_node *el, const char *prefix, size_t plen, const char *uri, size_t ulen, frx_err *e) |
| 382 |
|
| { |
| 383 |
|
| int i; |
| 384 |
|
| frx_str uri_s; |
| 385 |
21 |
▶ | if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element declares a namespace"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element declares a namespace"); |
0 |
21 |
21 |
|
| 386 |
21 |
▶ | if (plen && !frx_edit_check_name(prefix, plen, 0, e, "the prefix is not a name")) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen && !frx_edit_check_name(prefix, plen, 0, e, "the prefix is not a name")) return 0; |
17 |
4 |
21 |
| if (plen && !frx_edit_check_name(prefix, plen, 0, e, "the prefix is not a name")) return 0; |
0 |
17 |
17 |
|
| 387 |
21 |
▶ | if (plen == 5 && memcmp(prefix, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "the xmlns prefix cannot be declared"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen == 5 && memcmp(prefix, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "the xmlns prefix cannot be declared"); |
1 |
20 |
21 |
| if (plen == 5 && memcmp(prefix, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "the xmlns prefix cannot be declared"); |
1 |
0 |
1 |
|
| 388 |
20 |
▶ | if (ulen == sizeof FRX_XMLNS_NS - 1 && memcmp(uri, FRX_XMLNS_NS, ulen) == 0) return FRX_EDIT_FAIL(e, el->offset, "the xmlns namespace name cannot be bound"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ulen == sizeof FRX_XMLNS_NS - 1 && memcmp(uri, FRX_XMLNS_NS, ulen) == 0) return FRX_EDIT_FAIL(e, el->offset, "the xmlns namespace name cannot be bound"); |
0 |
20 |
20 |
| if (ulen == sizeof FRX_XMLNS_NS - 1 && memcmp(uri, FRX_XMLNS_NS, ulen) == 0) return FRX_EDIT_FAIL(e, el->offset, "the xmlns namespace name cannot be bound"); |
0 |
0 |
0 |
|
| 389 |
20 |
▶ | if (plen == 3 && memcmp(prefix, "xml", 3) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0) { |
2 |
18 |
20 |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0) { |
2 |
0 |
2 |
|
| 390 |
2 |
▶ | if (!(ulen == sizeof FRX_XML_NS - 1 && memcmp(uri, FRX_XML_NS, ulen) == 0)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(ulen == sizeof FRX_XML_NS - 1 && memcmp(uri, FRX_XML_NS, ulen) == 0)) |
1 |
1 |
2 |
| if (!(ulen == sizeof FRX_XML_NS - 1 && memcmp(uri, FRX_XML_NS, ulen) == 0)) |
0 |
1 |
1 |
|
| 391 |
1 |
| return FRX_EDIT_FAIL(e, el->offset, "the xml prefix is bound to its own namespace and no other"); |
| 392 |
1 |
| return 1; /* accepted, and dropped, as at parse */ |
| 393 |
|
| } |
| 394 |
18 |
▶ | if (ulen == sizeof FRX_XML_NS - 1 && memcmp(uri, FRX_XML_NS, ulen) == 0) return FRX_EDIT_FAIL(e, el->offset, "only the xml prefix may be bound to the xml namespace"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ulen == sizeof FRX_XML_NS - 1 && memcmp(uri, FRX_XML_NS, ulen) == 0) return FRX_EDIT_FAIL(e, el->offset, "only the xml prefix may be bound to the xml namespace"); |
0 |
18 |
18 |
| if (ulen == sizeof FRX_XML_NS - 1 && memcmp(uri, FRX_XML_NS, ulen) == 0) return FRX_EDIT_FAIL(e, el->offset, "only the xml prefix may be bound to the xml namespace"); |
0 |
0 |
0 |
|
| 395 |
18 |
▶ | if (plen && !ulen) return FRX_EDIT_FAIL(e, el->offset, "a prefix cannot be undeclared (that is Namespaces 1.1)"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen && !ulen) return FRX_EDIT_FAIL(e, el->offset, "a prefix cannot be undeclared (that is Namespaces 1.1)"); |
14 |
4 |
18 |
| if (plen && !ulen) return FRX_EDIT_FAIL(e, el->offset, "a prefix cannot be undeclared (that is Namespaces 1.1)"); |
1 |
13 |
14 |
|
| 396 |
17 |
▶ | if (ulen && !frx_ns_is_absolute(uri, ulen)) return FRX_EDIT_FAIL(e, el->offset, "a relative namespace URI is refused"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ulen && !frx_ns_is_absolute(uri, ulen)) return FRX_EDIT_FAIL(e, el->offset, "a relative namespace URI is refused"); |
16 |
1 |
17 |
| if (ulen && !frx_ns_is_absolute(uri, ulen)) return FRX_EDIT_FAIL(e, el->offset, "a relative namespace URI is refused"); |
1 |
15 |
16 |
|
| 397 |
16 |
▶ | if (!frx_edit_dup(d, uri, ulen, &uri_s)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, uri, ulen, &uri_s)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
16 |
16 |
|
| 398 |
|
| |
| 399 |
19 |
▶ | for (i = 0; i < el->n_decls; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < el->n_decls; i++) { |
3 |
16 |
19 |
|
| 400 |
3 |
▶ | if (frx_str_eq(&el->decls[i].prefix, prefix, plen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(&el->decls[i].prefix, prefix, plen)) { |
0 |
3 |
3 |
|
| 401 |
0 |
| ((frx_nsdecl *)el->decls)[i].uri = uri_s; |
| 402 |
0 |
| return 1; |
| 403 |
|
| } |
| 404 |
|
| } |
| 405 |
16 |
▶ | if (el->n_decls == (el->cap_decls ? el->cap_decls : el->n_decls)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (el->n_decls == (el->cap_decls ? el->cap_decls : el->n_decls)) { |
3 |
13 |
16 |
| if (el->n_decls == (el->cap_decls ? el->cap_decls : el->n_decls)) { |
0 |
3 |
3 |
|
| 406 |
13 |
▶ | int ncap = el->n_decls ? el->n_decls * 2 : 4; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = el->n_decls ? el->n_decls * 2 : 4; |
0 |
13 |
13 |
|
| 407 |
13 |
| frx_nsdecl *nd = (frx_nsdecl *)frx_arena_alloc(&d->arena, (size_t)ncap * sizeof *nd); |
| 408 |
13 |
▶ | if (!nd) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nd) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
13 |
13 |
|
| 409 |
13 |
▶ | if (el->n_decls) memcpy(nd, el->decls, (size_t)el->n_decls * sizeof *nd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->n_decls) memcpy(nd, el->decls, (size_t)el->n_decls * sizeof *nd); |
0 |
13 |
13 |
|
| 410 |
13 |
| el->decls = nd; |
| 411 |
13 |
| el->cap_decls = ncap; |
| 412 |
|
| } |
| 413 |
|
| { |
| 414 |
16 |
| frx_nsdecl *nd = (frx_nsdecl *)el->decls; |
| 415 |
16 |
▶ | if (!frx_edit_dup(d, prefix, plen, &nd[el->n_decls].prefix)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, prefix, plen, &nd[el->n_decls].prefix)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
16 |
16 |
|
| 416 |
16 |
| nd[el->n_decls].uri = uri_s; |
| 417 |
16 |
| el->n_decls++; |
| 418 |
|
| } |
| 419 |
16 |
▶ | if (frx_edit_owns_scope(el) || (el->scope && el->n_decls > 1 && el->scope->decls != el->decls && el->scope->up != NULL && 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_edit_owns_scope(el) || (el->scope && el->n_decls > 1 && el->scope->decls != el->decls && el->scope->up != NULL && 0)) { |
3 |
13 |
16 |
|
| 420 |
3 |
| frx_scope *s = (frx_scope *)el->scope; |
| 421 |
3 |
| s->decls = el->decls; |
| 422 |
3 |
| s->n_decls = el->n_decls; |
| 423 |
13 |
▶ | } else if (el->scope && el->n_decls > 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (el->scope && el->n_decls > 1) { |
5 |
8 |
13 |
| } else if (el->scope && el->n_decls > 1) { |
0 |
5 |
5 |
|
| 424 |
|
| /* the record exists but its block was replaced above */ |
| 425 |
0 |
| frx_scope *s = (frx_scope *)el->scope; |
| 426 |
0 |
| s->decls = el->decls; |
| 427 |
0 |
| s->n_decls = el->n_decls; |
| 428 |
|
| } else { |
| 429 |
|
| /* the first declaration: el gets its own record over what it |
| 430 |
|
| * inherited, and its descendants come with it */ |
| 431 |
13 |
| frx_scope *s = (frx_scope *)frx_arena_alloc(&d->arena, sizeof *s); |
| 432 |
13 |
| const frx_scope *inherited = el->scope; |
| 433 |
13 |
▶ | if (!s) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
13 |
13 |
|
| 434 |
13 |
| s->up = inherited; |
| 435 |
13 |
| s->decls = el->decls; |
| 436 |
13 |
| s->n_decls = el->n_decls; |
| 437 |
13 |
| frx_edit_reroot(el, inherited, s); |
| 438 |
13 |
| el->scope = s; |
| 439 |
|
| } |
| 440 |
16 |
| return 1; |
| 441 |
|
| } |
| 442 |
|
| |
| 443 |
|
| /* After a subtree is attached: every name in it must still mean what it |
| 444 |
|
| * meant. A declaration goes on the node whose name would otherwise |
| 445 |
|
| * change, never on the subtree's root, so it is scoped to exactly what it |
| 446 |
|
| * fixes and cannot reach a name anywhere else; that is what lets an |
| 447 |
|
| * unprefixed element declare the default namespace here (the plan's |
| 448 |
|
| * generated prefix was the price of declaring on a shared ancestor, and |
| 449 |
|
| * nothing declares on one). An element that ends up needing no namespace |
| 450 |
|
| * under a default gets xmlns="" the same way. */ |
| 451 |
|
| static int |
| 452 |
174 |
| frx_edit_fixup(frx_doc *d, frx_node *root, frx_err *e) |
| 453 |
|
| { |
| 454 |
174 |
| frx_node *n = root; |
| 455 |
395 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
221 |
174 |
395 |
|
| 456 |
221 |
▶ | if (n->kind == FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->kind == FRX_ELEMENT) { |
211 |
10 |
221 |
|
| 457 |
|
| const frx_str *bound; |
| 458 |
|
| int i; |
| 459 |
211 |
▶ | if (n->prefix.len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->prefix.len) { |
51 |
160 |
211 |
|
| 460 |
51 |
▶ | if (!frx_str_eq(&n->prefix, "xml", 3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_str_eq(&n->prefix, "xml", 3)) { |
51 |
0 |
51 |
|
| 461 |
51 |
| bound = frx_ns_lookup(n->scope, n->prefix.p, n->prefix.len); |
| 462 |
51 |
▶ | if (!bound || !frx_str_eq2(bound, &n->ns)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bound || !frx_str_eq2(bound, &n->ns)) |
45 |
6 |
51 |
| if (!bound || !frx_str_eq2(bound, &n->ns)) |
1 |
44 |
45 |
|
| 463 |
7 |
▶ | if (!frx_declare_ns(d, n, n->prefix.p, n->prefix.len, n->ns.p, n->ns.len, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_declare_ns(d, n, n->prefix.p, n->prefix.len, n->ns.p, n->ns.len, e)) return 0; |
0 |
7 |
7 |
|
| 464 |
|
| } |
| 465 |
|
| } else { |
| 466 |
160 |
| bound = frx_ns_lookup(n->scope, "", 0); |
| 467 |
160 |
▶ | if (!bound || !frx_str_eq2(bound, &n->ns)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bound || !frx_str_eq2(bound, &n->ns)) |
160 |
0 |
160 |
| if (!bound || !frx_str_eq2(bound, &n->ns)) |
3 |
157 |
160 |
|
| 468 |
3 |
▶ | if (!frx_declare_ns(d, n, "", 0, n->ns.p, n->ns.len, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_declare_ns(d, n, "", 0, n->ns.p, n->ns.len, e)) return 0; |
0 |
3 |
3 |
|
| 469 |
|
| } |
| 470 |
485 |
▶ | for (i = 0; i < n->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) { |
274 |
211 |
485 |
|
| 471 |
274 |
| const frx_attr *a = &n->attrs[i]; |
| 472 |
274 |
▶ | if (!a->prefix.len || frx_str_eq(&a->prefix, "xml", 3)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a->prefix.len || frx_str_eq(&a->prefix, "xml", 3)) continue; |
140 |
134 |
274 |
| if (!a->prefix.len || frx_str_eq(&a->prefix, "xml", 3)) continue; |
140 |
0 |
140 |
|
| 473 |
0 |
| bound = frx_ns_lookup(n->scope, a->prefix.p, a->prefix.len); |
| 474 |
0 |
▶ | if (!bound || !frx_str_eq2(bound, &a->ns)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bound || !frx_str_eq2(bound, &a->ns)) |
0 |
0 |
0 |
| if (!bound || !frx_str_eq2(bound, &a->ns)) |
0 |
0 |
0 |
|
| 475 |
0 |
▶ | if (!frx_declare_ns(d, n, a->prefix.p, a->prefix.len, a->ns.p, a->ns.len, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_declare_ns(d, n, a->prefix.p, a->prefix.len, a->ns.p, a->ns.len, e)) return 0; |
0 |
0 |
0 |
|
| 476 |
|
| } |
| 477 |
|
| } |
| 478 |
221 |
| n = (frx_node *)frx_walk_next(n, root); |
| 479 |
|
| } |
| 480 |
174 |
| return 1; |
| 481 |
|
| } |
| 482 |
|
| |
| 483 |
|
| /* ---- structure ---------------------------------------------------------- */ |
| 484 |
|
| |
| 485 |
|
| static int |
| 486 |
193 |
| frx_edit_is_ancestor(const frx_node *maybe, const frx_node *of) |
| 487 |
|
| { |
| 488 |
688 |
▶ | for (; of; of = of->parent) if (of == maybe) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (; of; of = of->parent) if (of == maybe) return 1; |
1 |
495 |
496 |
| for (; of; of = of->parent) if (of == maybe) return 1; |
496 |
192 |
688 |
|
| 489 |
192 |
| return 0; |
| 490 |
|
| } |
| 491 |
|
| |
| 492 |
|
| /* what an attach needs true of parent and child */ |
| 493 |
|
| static int |
| 494 |
196 |
| frx_edit_can_attach(frx_doc *d, frx_node *parent, frx_node *child, frx_err *e) |
| 495 |
|
| { |
| 496 |
196 |
▶ | if (child->parent) return FRX_EDIT_FAIL(e, child->offset, "the node has a parent; detach it first"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (child->parent) return FRX_EDIT_FAIL(e, child->offset, "the node has a parent; detach it first"); |
2 |
194 |
196 |
|
| 497 |
194 |
▶ | if (child->kind == FRX_DOCUMENT) return FRX_EDIT_FAIL(e, child->offset, "a document node cannot be a child"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (child->kind == FRX_DOCUMENT) return FRX_EDIT_FAIL(e, child->offset, "a document node cannot be a child"); |
0 |
194 |
194 |
|
| 498 |
194 |
▶ | if (parent->kind == FRX_DOCUMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parent->kind == FRX_DOCUMENT) { |
12 |
182 |
194 |
|
| 499 |
12 |
▶ | if (child->kind == FRX_TEXT) return FRX_EDIT_FAIL(e, child->offset, "text is not allowed outside the root element"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (child->kind == FRX_TEXT) return FRX_EDIT_FAIL(e, child->offset, "text is not allowed outside the root element"); |
1 |
11 |
12 |
|
| 500 |
11 |
▶ | if (child->kind == FRX_ELEMENT && d->root) return FRX_EDIT_FAIL(e, child->offset, "a document has one root element"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (child->kind == FRX_ELEMENT && d->root) return FRX_EDIT_FAIL(e, child->offset, "a document has one root element"); |
9 |
2 |
11 |
| if (child->kind == FRX_ELEMENT && d->root) return FRX_EDIT_FAIL(e, child->offset, "a document has one root element"); |
1 |
8 |
9 |
|
| 501 |
182 |
▶ | } else if (parent->kind != FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (parent->kind != FRX_ELEMENT) { |
1 |
181 |
182 |
|
| 502 |
1 |
| return FRX_EDIT_FAIL(e, parent->offset, "only an element or the document node has children"); |
| 503 |
|
| } |
| 504 |
191 |
▶ | if (frx_edit_is_ancestor(child, parent)) return FRX_EDIT_FAIL(e, child->offset, "a node cannot be put under itself"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_edit_is_ancestor(child, parent)) return FRX_EDIT_FAIL(e, child->offset, "a node cannot be put under itself"); |
0 |
191 |
191 |
|
| 505 |
191 |
| return 1; |
| 506 |
|
| } |
| 507 |
|
| |
| 508 |
|
| /* the scope a detached subtree's root inherits: what its chain ends at */ |
| 509 |
|
| static const frx_scope * |
| 510 |
190 |
| frx_edit_inherited(const frx_node *child) |
| 511 |
|
| { |
| 512 |
190 |
▶ | if (child->kind != FRX_ELEMENT) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (child->kind != FRX_ELEMENT) return NULL; |
18 |
172 |
190 |
|
| 513 |
172 |
▶ | return frx_edit_owns_scope(child) ? child->scope->up : child->scope; |
Branch: 1 branch
| Branch | True | False | Total |
| return frx_edit_owns_scope(child) ? child->scope->up : child->scope; |
33 |
139 |
172 |
|
| 514 |
|
| } |
| 515 |
|
| |
| 516 |
|
| static int |
| 517 |
196 |
| frx_edit_attach(frx_doc *d, frx_node *parent, frx_node *child, frx_node *before, frx_err *e) |
| 518 |
|
| { |
| 519 |
|
| const frx_scope *old_inherit, *new_inherit; |
| 520 |
196 |
▶ | if (!frx_edit_can_attach(d, parent, child, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_can_attach(d, parent, child, e)) return 0; |
5 |
191 |
196 |
|
| 521 |
191 |
▶ | if (!frx_edit_ids_subtree(d, child, 1, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_ids_subtree(d, child, 1, e)) return 0; |
1 |
190 |
191 |
|
| 522 |
190 |
| old_inherit = frx_edit_inherited(child); |
| 523 |
190 |
▶ | new_inherit = parent->kind == FRX_ELEMENT ? parent->scope : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| new_inherit = parent->kind == FRX_ELEMENT ? parent->scope : NULL; |
180 |
10 |
190 |
|
| 524 |
190 |
▶ | if (before) frx_node_insert_before(before, child); |
Branch: 1 branch
| Branch | True | False | Total |
| if (before) frx_node_insert_before(before, child); |
148 |
42 |
190 |
|
| 525 |
42 |
| else frx_node_append(parent, child); |
| 526 |
190 |
▶ | if (child->kind == FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (child->kind == FRX_ELEMENT) { |
172 |
18 |
190 |
|
| 527 |
172 |
| frx_edit_reroot(child, old_inherit, new_inherit); |
| 528 |
172 |
▶ | if (!frx_edit_fixup(d, child, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_fixup(d, child, e)) return 0; |
0 |
172 |
172 |
|
| 529 |
|
| } |
| 530 |
190 |
▶ | if (parent->kind == FRX_DOCUMENT && child->kind == FRX_ELEMENT) d->root = child; |
Branch: 2 branches
| Branch | True | False | Total |
| if (parent->kind == FRX_DOCUMENT && child->kind == FRX_ELEMENT) d->root = child; |
10 |
180 |
190 |
| if (parent->kind == FRX_DOCUMENT && child->kind == FRX_ELEMENT) d->root = child; |
8 |
2 |
10 |
|
| 531 |
190 |
| d->renumber = 1; |
| 532 |
190 |
| return 1; |
| 533 |
|
| } |
| 534 |
|
| |
| 535 |
|
| static int |
| 536 |
48 |
| frx_append_child(frx_doc *d, frx_node *parent, frx_node *child, frx_err *e) |
| 537 |
|
| { |
| 538 |
48 |
| return frx_edit_attach(d, parent, child, NULL, e); |
| 539 |
|
| } |
| 540 |
|
| |
| 541 |
|
| static int |
| 542 |
149 |
| frx_insert_before(frx_doc *d, frx_node *ref, frx_node *child, frx_err *e) |
| 543 |
|
| { |
| 544 |
149 |
▶ | if (!ref->parent) return FRX_EDIT_FAIL(e, ref->offset, "the reference node has no parent"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ref->parent) return FRX_EDIT_FAIL(e, ref->offset, "the reference node has no parent"); |
1 |
148 |
149 |
|
| 545 |
148 |
| return frx_edit_attach(d, ref->parent, child, ref, e); |
| 546 |
|
| } |
| 547 |
|
| |
| 548 |
|
| /* out of the tree; the node stays a node of its document */ |
| 549 |
|
| static int |
| 550 |
157 |
| frx_remove(frx_doc *d, frx_node *n, frx_err *e) |
| 551 |
|
| { |
| 552 |
157 |
▶ | if (n->kind == FRX_DOCUMENT) return FRX_EDIT_FAIL(e, 0, "the document node cannot be detached"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->kind == FRX_DOCUMENT) return FRX_EDIT_FAIL(e, 0, "the document node cannot be detached"); |
1 |
156 |
157 |
|
| 553 |
156 |
▶ | if (!n->parent) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n->parent) return 1; |
1 |
155 |
156 |
|
| 554 |
155 |
▶ | if (!frx_edit_ids_subtree(d, n, 0, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_ids_subtree(d, n, 0, e)) return 0; |
0 |
155 |
155 |
|
| 555 |
155 |
▶ | if (d->root == n) d->root = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->root == n) d->root = NULL; |
0 |
155 |
155 |
|
| 556 |
155 |
| frx_node_unlink(n); |
| 557 |
155 |
| d->renumber = 1; |
| 558 |
155 |
| return 1; |
| 559 |
|
| } |
| 560 |
|
| |
| 561 |
|
| /* ---- attributes and content ---------------------------------------------- */ |
| 562 |
|
| |
| 563 |
|
| static int |
| 564 |
154 |
| frx_set_attr(frx_doc *d, frx_node *el, const char *ns, size_t nslen, const char *prefix, size_t plen, |
| 565 |
|
| const char *local, size_t llen, const char *value, size_t vlen, frx_err *e) |
| 566 |
|
| { |
| 567 |
|
| frx_str v, loc, nss; |
| 568 |
154 |
| frx_attr *a = NULL; |
| 569 |
|
| int i, indexed; |
| 570 |
154 |
▶ | if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element has attributes"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element has attributes"); |
0 |
154 |
154 |
|
| 571 |
154 |
▶ | if (!frx_edit_check_named(ns, nslen, prefix, plen, local, llen, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_named(ns, nslen, prefix, plen, local, llen, e)) return 0; |
0 |
154 |
154 |
|
| 572 |
154 |
▶ | if (nslen && !plen) return FRX_EDIT_FAIL(e, el->offset, "an attribute with a namespace needs a prefix"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (nslen && !plen) return FRX_EDIT_FAIL(e, el->offset, "an attribute with a namespace needs a prefix"); |
141 |
13 |
154 |
| if (nslen && !plen) return FRX_EDIT_FAIL(e, el->offset, "an attribute with a namespace needs a prefix"); |
1 |
140 |
141 |
|
| 573 |
153 |
▶ | if (!plen && llen == 5 && memcmp(local, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "xmlns is a declaration; use declare_ns"); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!plen && llen == 5 && memcmp(local, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "xmlns is a declaration; use declare_ns"); |
13 |
140 |
153 |
| if (!plen && llen == 5 && memcmp(local, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "xmlns is a declaration; use declare_ns"); |
1 |
12 |
13 |
| if (!plen && llen == 5 && memcmp(local, "xmlns", 5) == 0) return FRX_EDIT_FAIL(e, el->offset, "xmlns is a declaration; use declare_ns"); |
1 |
0 |
1 |
|
| 574 |
152 |
▶ | if (!frx_edit_check_chars(d, value, vlen, e, "the value holds a character that is not an XML character")) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_chars(d, value, vlen, e, "the value holds a character that is not an XML character")) return 0; |
0 |
152 |
152 |
|
| 575 |
152 |
| loc.p = local; loc.len = llen; |
| 576 |
152 |
| nss.p = ns; nss.len = nslen; |
| 577 |
152 |
| indexed = frx_edit_is_id_attr(d, &loc); |
| 578 |
152 |
▶ | if (!frx_edit_dup(d, value, vlen, &v)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, value, vlen, &v)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
152 |
152 |
|
| 579 |
|
| |
| 580 |
257 |
▶ | for (i = 0; i < el->n_attrs; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < el->n_attrs; i++) |
107 |
150 |
257 |
|
| 581 |
107 |
▶ | if (frx_str_eq2(&el->attrs[i].local, &loc) && frx_str_eq2(&el->attrs[i].ns, &nss)) { a = (frx_attr *)&el->attrs[i]; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_str_eq2(&el->attrs[i].local, &loc) && frx_str_eq2(&el->attrs[i].ns, &nss)) { a = (frx_attr *)&el->attrs[i]; break; } |
2 |
105 |
107 |
| if (frx_str_eq2(&el->attrs[i].local, &loc) && frx_str_eq2(&el->attrs[i].ns, &nss)) { a = (frx_attr *)&el->attrs[i]; break; } |
2 |
0 |
2 |
|
| 582 |
|
| |
| 583 |
152 |
▶ | if (indexed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (indexed) { |
4 |
148 |
152 |
|
| 584 |
4 |
| int found, at = frx_edit_ids_find(d, &loc, &v, &found); |
| 585 |
4 |
▶ | if (found && d->ids[at].node != el) |
Branch: 2 branches
| Branch | True | False | Total |
| if (found && d->ids[at].node != el) |
1 |
3 |
4 |
| if (found && d->ids[at].node != el) |
1 |
0 |
1 |
|
| 586 |
1 |
| return FRX_EDIT_FAIL(e, el->offset, "another element carries that ID value already"); |
| 587 |
3 |
▶ | if (a && !frx_edit_ids_remove(d, &a->local, &a->value, el)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (a && !frx_edit_ids_remove(d, &a->local, &a->value, el)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
1 |
2 |
3 |
| if (a && !frx_edit_ids_remove(d, &a->local, &a->value, el)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
1 |
1 |
|
| 588 |
|
| } |
| 589 |
151 |
▶ | if (a) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a) { |
2 |
149 |
151 |
|
| 590 |
2 |
| a->value = v; |
| 591 |
|
| } else { |
| 592 |
149 |
▶ | if (el->n_attrs == (el->cap_attrs ? el->cap_attrs : el->n_attrs)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (el->n_attrs == (el->cap_attrs ? el->cap_attrs : el->n_attrs)) { |
3 |
146 |
149 |
| if (el->n_attrs == (el->cap_attrs ? el->cap_attrs : el->n_attrs)) { |
0 |
3 |
3 |
|
| 593 |
146 |
▶ | int ncap = el->n_attrs ? el->n_attrs * 2 : 4; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = el->n_attrs ? el->n_attrs * 2 : 4; |
102 |
44 |
146 |
|
| 594 |
146 |
| frx_attr *na = (frx_attr *)frx_arena_alloc(&d->arena, (size_t)ncap * sizeof *na); |
| 595 |
146 |
▶ | if (!na) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!na) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
146 |
146 |
|
| 596 |
146 |
▶ | if (el->n_attrs) memcpy(na, el->attrs, (size_t)el->n_attrs * sizeof *na); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->n_attrs) memcpy(na, el->attrs, (size_t)el->n_attrs * sizeof *na); |
102 |
44 |
146 |
|
| 597 |
146 |
| el->attrs = na; |
| 598 |
146 |
| el->cap_attrs = ncap; |
| 599 |
|
| } |
| 600 |
149 |
| a = (frx_attr *)&el->attrs[el->n_attrs]; |
| 601 |
149 |
| memset(a, 0, sizeof *a); |
| 602 |
149 |
▶ | if (!frx_edit_qname(d, prefix, plen, local, llen, &a->qname, &a->prefix, &a->local) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_qname(d, prefix, plen, local, llen, &a->qname, &a->prefix, &a->local) |
149 |
0 |
149 |
|
| 603 |
149 |
▶ | || !frx_edit_dup(d, ns, nslen, &a->ns)) |
Branch: 1 branch
| Branch | True | False | Total |
| || !frx_edit_dup(d, ns, nslen, &a->ns)) |
0 |
149 |
149 |
|
| 604 |
0 |
| return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
| 605 |
149 |
| a->value = v; |
| 606 |
149 |
| a->offset = el->offset; |
| 607 |
149 |
| el->n_attrs++; |
| 608 |
|
| } |
| 609 |
151 |
▶ | if (indexed && !frx_edit_ids_insert(d, a, el)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (indexed && !frx_edit_ids_insert(d, a, el)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
3 |
148 |
151 |
| if (indexed && !frx_edit_ids_insert(d, a, el)) return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
0 |
3 |
3 |
|
| 610 |
|
| /* the prefix must mean the namespace here */ |
| 611 |
151 |
▶ | if (plen && !frx_str_eq(&a->prefix, "xml", 3)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen && !frx_str_eq(&a->prefix, "xml", 3)) { |
140 |
11 |
151 |
| if (plen && !frx_str_eq(&a->prefix, "xml", 3)) { |
1 |
139 |
140 |
|
| 612 |
1 |
| const frx_str *bound = frx_ns_lookup(el->scope, a->prefix.p, a->prefix.len); |
| 613 |
1 |
▶ | if (!bound || !frx_str_eq2(bound, &a->ns)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bound || !frx_str_eq2(bound, &a->ns)) |
0 |
1 |
1 |
| if (!bound || !frx_str_eq2(bound, &a->ns)) |
0 |
0 |
0 |
|
| 614 |
1 |
▶ | if (!frx_declare_ns(d, el, prefix, plen, ns, nslen, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_declare_ns(d, el, prefix, plen, ns, nslen, e)) return 0; |
0 |
1 |
1 |
|
| 615 |
|
| } |
| 616 |
151 |
| return 1; |
| 617 |
|
| } |
| 618 |
|
| |
| 619 |
|
| static int |
| 620 |
5 |
| frx_remove_attr(frx_doc *d, frx_node *el, const char *ns, size_t nslen, const char *local, size_t llen, frx_err *e) |
| 621 |
|
| { |
| 622 |
|
| int i; |
| 623 |
5 |
▶ | if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element has attributes"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element has attributes"); |
0 |
5 |
5 |
|
| 624 |
7 |
▶ | for (i = 0; i < el->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < el->n_attrs; i++) { |
6 |
1 |
7 |
|
| 625 |
6 |
| frx_attr *a = (frx_attr *)&el->attrs[i]; |
| 626 |
6 |
▶ | if (!frx_str_eq(&a->local, local, llen) || !frx_str_eq(&a->ns, ns, nslen)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_str_eq(&a->local, local, llen) || !frx_str_eq(&a->ns, ns, nslen)) continue; |
4 |
2 |
6 |
| if (!frx_str_eq(&a->local, local, llen) || !frx_str_eq(&a->ns, ns, nslen)) continue; |
0 |
4 |
4 |
|
| 627 |
4 |
▶ | if (frx_edit_is_id_attr(d, &a->local) && !frx_edit_ids_remove(d, &a->local, &a->value, el)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (frx_edit_is_id_attr(d, &a->local) && !frx_edit_ids_remove(d, &a->local, &a->value, el)) |
2 |
2 |
4 |
| if (frx_edit_is_id_attr(d, &a->local) && !frx_edit_ids_remove(d, &a->local, &a->value, el)) |
0 |
2 |
2 |
|
| 628 |
0 |
| return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
| 629 |
4 |
| memmove(a, a + 1, (size_t)(el->n_attrs - i - 1) * sizeof *a); |
| 630 |
4 |
| el->n_attrs--; |
| 631 |
4 |
▶ | if (!el->cap_attrs) el->cap_attrs = el->n_attrs + 1; /* the block is one longer than n now */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!el->cap_attrs) el->cap_attrs = el->n_attrs + 1; /* the block is one longer than n now */ |
1 |
3 |
4 |
|
| 632 |
4 |
| return 1; |
| 633 |
|
| } |
| 634 |
1 |
| return 1; |
| 635 |
|
| } |
| 636 |
|
| |
| 637 |
|
| /* a text node's value; an element's content replaced by one text node */ |
| 638 |
|
| static int |
| 639 |
15 |
| frx_set_text(frx_doc *d, frx_node *n, const char *s, size_t len, frx_err *e) |
| 640 |
|
| { |
| 641 |
15 |
| switch (n->kind) { |
| 642 |
2 |
| case FRX_TEXT: { |
| 643 |
|
| frx_str v; |
| 644 |
2 |
▶ | if (!frx_edit_check_chars(d, s, len, e, "text holds a character that is not an XML character")) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_chars(d, s, len, e, "text holds a character that is not an XML character")) return 0; |
0 |
2 |
2 |
|
| 645 |
2 |
▶ | if (!frx_edit_dup(d, s, len, &v)) return FRX_EDIT_FAIL(e, n->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, s, len, &v)) return FRX_EDIT_FAIL(e, n->offset, "out of memory"); |
0 |
2 |
2 |
|
| 646 |
2 |
| n->value = v; n->cdata_spans = NULL; n->n_cdata_spans = 0; |
| 647 |
2 |
| return 1; |
| 648 |
|
| } |
| 649 |
2 |
| case FRX_COMMENT: { |
| 650 |
|
| frx_str v; |
| 651 |
2 |
▶ | if (!frx_edit_check_comment(d, s, len, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_comment(d, s, len, e)) return 0; |
1 |
1 |
2 |
|
| 652 |
1 |
▶ | if (!frx_edit_dup(d, s, len, &v)) return FRX_EDIT_FAIL(e, n->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, s, len, &v)) return FRX_EDIT_FAIL(e, n->offset, "out of memory"); |
0 |
1 |
1 |
|
| 653 |
1 |
| n->value = v; |
| 654 |
1 |
| return 1; |
| 655 |
|
| } |
| 656 |
1 |
| case FRX_PI: { |
| 657 |
|
| frx_str v; |
| 658 |
1 |
▶ | if (!frx_edit_check_pi(d, n->local.p, n->local.len, s, len, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_pi(d, n->local.p, n->local.len, s, len, e)) return 0; |
1 |
0 |
1 |
|
| 659 |
0 |
▶ | if (!frx_edit_dup(d, s, len, &v)) return FRX_EDIT_FAIL(e, n->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, s, len, &v)) return FRX_EDIT_FAIL(e, n->offset, "out of memory"); |
0 |
0 |
0 |
|
| 660 |
0 |
| n->value = v; |
| 661 |
0 |
| return 1; |
| 662 |
|
| } |
| 663 |
10 |
| case FRX_ELEMENT: { |
| 664 |
10 |
| frx_node *t = frx_new_text(d, s, len, e); |
| 665 |
10 |
▶ | if (!t) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return 0; |
1 |
9 |
10 |
|
| 666 |
10 |
▶ | while (n->first_child) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n->first_child) { |
1 |
9 |
10 |
|
| 667 |
1 |
| frx_node *c = n->first_child; |
| 668 |
1 |
▶ | if (!frx_remove(d, c, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_remove(d, c, e)) return 0; |
0 |
1 |
1 |
|
| 669 |
|
| } |
| 670 |
9 |
| return frx_append_child(d, n, t, e); |
| 671 |
|
| } |
| 672 |
0 |
| default: |
| 673 |
0 |
| return FRX_EDIT_FAIL(e, n->offset, "the document node has no text to set"); |
| 674 |
|
| } |
| 675 |
|
| } |
| 676 |
|
| |
| 677 |
|
| static int |
| 678 |
2 |
| frx_set_name(frx_doc *d, frx_node *el, const char *ns, size_t nslen, const char *prefix, size_t plen, |
| 679 |
|
| const char *local, size_t llen, frx_err *e) |
| 680 |
|
| { |
| 681 |
|
| frx_str q, p, l, nss; |
| 682 |
2 |
▶ | if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element is renamed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->kind != FRX_ELEMENT) return FRX_EDIT_FAIL(e, el->offset, "only an element is renamed"); |
0 |
2 |
2 |
|
| 683 |
2 |
▶ | if (!frx_edit_check_named(ns, nslen, prefix, plen, local, llen, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_check_named(ns, nslen, prefix, plen, local, llen, e)) return 0; |
0 |
2 |
2 |
|
| 684 |
2 |
▶ | if (!frx_edit_qname(d, prefix, plen, local, llen, &q, &p, &l) || !frx_edit_dup(d, ns, nslen, &nss)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_edit_qname(d, prefix, plen, local, llen, &q, &p, &l) || !frx_edit_dup(d, ns, nslen, &nss)) |
2 |
0 |
2 |
| if (!frx_edit_qname(d, prefix, plen, local, llen, &q, &p, &l) || !frx_edit_dup(d, ns, nslen, &nss)) |
0 |
2 |
2 |
|
| 685 |
0 |
| return FRX_EDIT_FAIL(e, el->offset, "out of memory"); |
| 686 |
2 |
| el->qname = q; el->prefix = p; el->local = l; el->ns = nss; |
| 687 |
2 |
▶ | if (el->parent) return frx_edit_fixup(d, el, e); /* attached: the prefix must mean this here */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (el->parent) return frx_edit_fixup(d, el, e); /* attached: the prefix must mean this here */ |
2 |
0 |
2 |
|
| 688 |
0 |
| return 1; |
| 689 |
|
| } |
| 690 |
|
| |
| 691 |
|
| /* ---- import --------------------------------------------------------------- */ |
| 692 |
|
| |
| 693 |
|
| typedef struct frx_import_frame { const frx_node *src; frx_node *dst; } frx_import_frame; |
| 694 |
|
| |
| 695 |
|
| /* one node of another document copied into d, without its children */ |
| 696 |
|
| static frx_node * |
| 697 |
178 |
| frx_import_one(frx_doc *d, const frx_node *src, frx_err *e) |
| 698 |
|
| { |
| 699 |
178 |
| frx_node *n = frx_node_new(d, src->kind, 0); |
| 700 |
178 |
▶ | if (!n) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
178 |
178 |
|
| 701 |
178 |
| switch (src->kind) { |
| 702 |
174 |
| case FRX_ELEMENT: { |
| 703 |
|
| int i; |
| 704 |
174 |
▶ | if (!frx_edit_dup(d, src->qname.p, src->qname.len, &n->qname) || !frx_edit_dup(d, src->ns.p, src->ns.len, &n->ns)) goto nomem; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_edit_dup(d, src->qname.p, src->qname.len, &n->qname) || !frx_edit_dup(d, src->ns.p, src->ns.len, &n->ns)) goto nomem; |
174 |
0 |
174 |
| if (!frx_edit_dup(d, src->qname.p, src->qname.len, &n->qname) || !frx_edit_dup(d, src->ns.p, src->ns.len, &n->ns)) goto nomem; |
0 |
174 |
174 |
|
| 705 |
174 |
| frx_ns_split(&n->qname, &n->prefix, &n->local); |
| 706 |
174 |
▶ | if (src->n_attrs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->n_attrs) { |
134 |
40 |
174 |
|
| 707 |
134 |
| frx_attr *attrs = (frx_attr *)frx_arena_alloc(&d->arena, (size_t)src->n_attrs * sizeof *attrs); |
| 708 |
134 |
▶ | if (!attrs) goto nomem; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!attrs) goto nomem; |
0 |
134 |
134 |
|
| 709 |
268 |
▶ | for (i = 0; i < src->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < src->n_attrs; i++) { |
134 |
134 |
268 |
|
| 710 |
134 |
| attrs[i] = src->attrs[i]; |
| 711 |
134 |
| attrs[i].offset = 0; |
| 712 |
134 |
▶ | if (!frx_edit_dup(d, src->attrs[i].qname.p, src->attrs[i].qname.len, &attrs[i].qname) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, src->attrs[i].qname.p, src->attrs[i].qname.len, &attrs[i].qname) |
134 |
0 |
134 |
|
| 713 |
134 |
▶ | || !frx_edit_dup(d, src->attrs[i].value.p, src->attrs[i].value.len, &attrs[i].value) |
Branch: 1 branch
| Branch | True | False | Total |
| || !frx_edit_dup(d, src->attrs[i].value.p, src->attrs[i].value.len, &attrs[i].value) |
134 |
0 |
134 |
|
| 714 |
134 |
▶ | || !frx_edit_dup(d, src->attrs[i].ns.p, src->attrs[i].ns.len, &attrs[i].ns)) goto nomem; |
Branch: 1 branch
| Branch | True | False | Total |
| || !frx_edit_dup(d, src->attrs[i].ns.p, src->attrs[i].ns.len, &attrs[i].ns)) goto nomem; |
0 |
134 |
134 |
|
| 715 |
134 |
| frx_ns_split(&attrs[i].qname, &attrs[i].prefix, &attrs[i].local); |
| 716 |
|
| } |
| 717 |
134 |
| n->attrs = attrs; n->n_attrs = src->n_attrs; |
| 718 |
|
| } |
| 719 |
174 |
▶ | if (src->n_decls) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->n_decls) { |
30 |
144 |
174 |
|
| 720 |
30 |
| frx_nsdecl *decls = (frx_nsdecl *)frx_arena_alloc(&d->arena, (size_t)src->n_decls * sizeof *decls); |
| 721 |
30 |
▶ | if (!decls) goto nomem; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!decls) goto nomem; |
0 |
30 |
30 |
|
| 722 |
60 |
▶ | for (i = 0; i < src->n_decls; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < src->n_decls; i++) |
30 |
30 |
60 |
|
| 723 |
30 |
▶ | if (!frx_edit_dup(d, src->decls[i].prefix.p, src->decls[i].prefix.len, &decls[i].prefix) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, src->decls[i].prefix.p, src->decls[i].prefix.len, &decls[i].prefix) |
30 |
0 |
30 |
|
| 724 |
30 |
▶ | || !frx_edit_dup(d, src->decls[i].uri.p, src->decls[i].uri.len, &decls[i].uri)) goto nomem; |
Branch: 1 branch
| Branch | True | False | Total |
| || !frx_edit_dup(d, src->decls[i].uri.p, src->decls[i].uri.len, &decls[i].uri)) goto nomem; |
0 |
30 |
30 |
|
| 725 |
30 |
| n->decls = decls; n->n_decls = src->n_decls; |
| 726 |
|
| } |
| 727 |
174 |
| return n; |
| 728 |
|
| } |
| 729 |
0 |
| case FRX_PI: |
| 730 |
0 |
▶ | if (!frx_edit_dup(d, src->local.p, src->local.len, &n->local)) goto nomem; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, src->local.p, src->local.len, &n->local)) goto nomem; |
0 |
0 |
0 |
|
| 731 |
0 |
| n->qname = n->local; |
| 732 |
|
| /* fall through */ |
| 733 |
4 |
| default: |
| 734 |
4 |
▶ | if (!frx_edit_dup(d, src->value.p, src->value.len, &n->value)) goto nomem; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_edit_dup(d, src->value.p, src->value.len, &n->value)) goto nomem; |
0 |
4 |
4 |
|
| 735 |
4 |
| return n; |
| 736 |
|
| } |
| 737 |
0 |
| nomem: |
| 738 |
0 |
| FRX_EDIT_SET(e, 0, "out of memory"); |
| 739 |
0 |
| return NULL; |
| 740 |
|
| } |
| 741 |
|
| |
| 742 |
|
| /* the copy's root declares every binding in scope at the source that it |
| 743 |
|
| * does not already declare, so the copy means the same anywhere */ |
| 744 |
|
| static int |
| 745 |
140 |
| frx_import_bindings(frx_doc *d, const frx_node *src, frx_node *dst, frx_err *e) |
| 746 |
|
| { |
| 747 |
|
| const frx_scope *s; |
| 748 |
|
| int i; |
| 749 |
172 |
▶ | for (s = src->scope; s; s = s->up) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = src->scope; s; s = s->up) { |
32 |
140 |
172 |
|
| 750 |
65 |
▶ | for (i = 0; i < s->n_decls; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < s->n_decls; i++) { |
33 |
32 |
65 |
|
| 751 |
33 |
| const frx_nsdecl *dec = &s->decls[i]; |
| 752 |
33 |
| int j, have = 0; |
| 753 |
64 |
▶ | for (j = 0; j < dst->n_decls; j++) if (frx_str_eq2(&dst->decls[j].prefix, &dec->prefix)) have = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < dst->n_decls; j++) if (frx_str_eq2(&dst->decls[j].prefix, &dec->prefix)) have = 1; |
30 |
1 |
31 |
| for (j = 0; j < dst->n_decls; j++) if (frx_str_eq2(&dst->decls[j].prefix, &dec->prefix)) have = 1; |
31 |
33 |
64 |
|
| 754 |
33 |
▶ | if (have) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (have) continue; |
30 |
3 |
33 |
|
| 755 |
3 |
▶ | if (!dec->prefix.len && !dec->uri.len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!dec->prefix.len && !dec->uri.len) { |
1 |
2 |
3 |
| if (!dec->prefix.len && !dec->uri.len) { |
0 |
1 |
1 |
|
| 756 |
|
| /* the default undeclared: only worth saying if the copy needs it */ |
| 757 |
0 |
| continue; |
| 758 |
|
| } |
| 759 |
3 |
▶ | if (!frx_declare_ns(d, dst, dec->prefix.p, dec->prefix.len, dec->uri.p, dec->uri.len, e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_declare_ns(d, dst, dec->prefix.p, dec->prefix.len, dec->uri.p, dec->uri.len, e)) return 0; |
0 |
3 |
3 |
|
| 760 |
|
| } |
| 761 |
|
| } |
| 762 |
140 |
| return 1; |
| 763 |
|
| } |
| 764 |
|
| |
| 765 |
|
| /* a detached deep copy of src, a node of another document, into d */ |
| 766 |
|
| static frx_node * |
| 767 |
141 |
| frx_import(frx_doc *d, const frx_node *src, frx_err *e) |
| 768 |
|
| { |
| 769 |
141 |
| frx_import_frame *stack = NULL; |
| 770 |
141 |
| int sp = 0, cap = 0; |
| 771 |
|
| frx_node *root, *cur_dst; |
| 772 |
|
| const frx_node *cur; |
| 773 |
|
| |
| 774 |
141 |
▶ | if (src->kind == FRX_DOCUMENT) { FRX_EDIT_SET(e, 0, "the document node cannot be imported; import its root"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->kind == FRX_DOCUMENT) { FRX_EDIT_SET(e, 0, "the document node cannot be imported; import its root"); return NULL; } |
1 |
140 |
141 |
|
| 775 |
140 |
| root = frx_import_one(d, src, e); |
| 776 |
140 |
▶ | if (!root) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!root) return NULL; |
0 |
140 |
140 |
|
| 777 |
140 |
▶ | if (src->kind == FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->kind == FRX_ELEMENT) { |
140 |
0 |
140 |
|
| 778 |
|
| /* the copy's own declarations become its scope; then the rest of |
| 779 |
|
| * what was in scope at the source */ |
| 780 |
140 |
▶ | if (root->n_decls) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (root->n_decls) { |
30 |
110 |
140 |
|
| 781 |
30 |
| frx_scope *s = (frx_scope *)frx_arena_alloc(&d->arena, sizeof *s); |
| 782 |
30 |
▶ | if (!s) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) { FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
30 |
30 |
|
| 783 |
30 |
| s->up = NULL; s->decls = root->decls; s->n_decls = root->n_decls; |
| 784 |
30 |
| root->scope = s; |
| 785 |
|
| } |
| 786 |
140 |
▶ | if (!frx_import_bindings(d, src, root, e)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_import_bindings(d, src, root, e)) return NULL; |
0 |
140 |
140 |
|
| 787 |
|
| } |
| 788 |
|
| |
| 789 |
140 |
| cur = src->first_child; |
| 790 |
140 |
| cur_dst = root; |
| 791 |
142 |
▶ | while (cur) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (cur) { |
38 |
104 |
142 |
|
| 792 |
38 |
| frx_node *copy = frx_import_one(d, cur, e); |
| 793 |
38 |
▶ | if (!copy) { free(stack); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy) { free(stack); return NULL; } |
0 |
38 |
38 |
|
| 794 |
38 |
▶ | if (copy->kind == FRX_ELEMENT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (copy->kind == FRX_ELEMENT) { |
34 |
4 |
38 |
|
| 795 |
34 |
▶ | if (copy->n_decls) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (copy->n_decls) { |
0 |
34 |
34 |
|
| 796 |
0 |
| frx_scope *s = (frx_scope *)frx_arena_alloc(&d->arena, sizeof *s); |
| 797 |
0 |
▶ | if (!s) { free(stack); FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) { free(stack); FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
0 |
0 |
|
| 798 |
0 |
| s->up = cur_dst->scope; s->decls = copy->decls; s->n_decls = copy->n_decls; |
| 799 |
0 |
| copy->scope = s; |
| 800 |
|
| } else { |
| 801 |
34 |
| copy->scope = cur_dst->scope; |
| 802 |
|
| } |
| 803 |
|
| } |
| 804 |
38 |
| frx_node_append(cur_dst, copy); |
| 805 |
38 |
▶ | if (cur->first_child) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cur->first_child) { |
2 |
36 |
38 |
|
| 806 |
2 |
▶ | if (sp == cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp == cap) { |
1 |
1 |
2 |
|
| 807 |
1 |
▶ | int ncap = cap ? cap * 2 : 16; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = cap ? cap * 2 : 16; |
0 |
1 |
1 |
|
| 808 |
1 |
| frx_import_frame *ns = (frx_import_frame *)realloc(stack, (size_t)ncap * sizeof *ns); |
| 809 |
1 |
▶ | if (!ns) { free(stack); FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ns) { free(stack); FRX_EDIT_SET(e, 0, "out of memory"); return NULL; } |
0 |
1 |
1 |
|
| 810 |
1 |
| stack = ns; cap = ncap; |
| 811 |
|
| } |
| 812 |
2 |
| stack[sp].src = cur; stack[sp].dst = cur_dst; sp++; |
| 813 |
2 |
| cur_dst = copy; |
| 814 |
2 |
| cur = cur->first_child; |
| 815 |
2 |
| continue; |
| 816 |
|
| } |
| 817 |
38 |
▶ | while (!cur->next && sp) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (!cur->next && sp) { |
38 |
0 |
38 |
| while (!cur->next && sp) { |
2 |
36 |
38 |
|
| 818 |
2 |
| sp--; |
| 819 |
2 |
| cur = stack[sp].src; |
| 820 |
2 |
| cur_dst = stack[sp].dst; |
| 821 |
|
| } |
| 822 |
36 |
| cur = cur->next; |
| 823 |
36 |
▶ | if (!cur && !sp) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cur && !sp) break; |
36 |
0 |
36 |
| if (!cur && !sp) break; |
36 |
0 |
36 |
|
| 824 |
|
| } |
| 825 |
140 |
| free(stack); |
| 826 |
140 |
| d->renumber = 1; |
| 827 |
140 |
| return root; |
| 828 |
|
| } |
| 829 |
|
| |
| 830 |
|
| #endif /* FRX_EDIT_H */ |