| 1 |
|
| #ifndef FRX_XMLATTR_H |
| 2 |
|
| #define FRX_XMLATTR_H |
| 3 |
|
| |
| 4 |
|
| /* The four attributes the XML family gives meaning to without a DTD: |
| 5 |
|
| * xml:base, xml:lang, xml:space, and xml:id. |
| 6 |
|
| * |
| 7 |
|
| * xml:id is the parser's business and is handled where the ID index is |
| 8 |
|
| * built (frx_sink.h): under the full profile it is an ID attribute in |
| 9 |
|
| * every document without being named in id_attrs, its value must be an |
| 10 |
|
| * NCName, and a second element with the same value is refused exactly as |
| 11 |
|
| * a duplicate in id_attrs is. Under strict it is an ordinary attribute, |
| 12 |
|
| * as in 0.01. |
| 13 |
|
| * |
| 14 |
|
| * The other three are read on request, by walking ancestors, because the |
| 15 |
|
| * tree is mutable and a cached answer would go stale (decision 2). They |
| 16 |
|
| * are here rather than in frx_uri.h, which the plan named, because |
| 17 |
|
| * frx_uri.h knows nothing of nodes: it is included before frx_tree.h so |
| 18 |
|
| * that the canonicaliser can use its join. This file is the node-shaped |
| 19 |
|
| * face of the same rules. |
| 20 |
|
| * |
| 21 |
|
| * base_uri is XML Base section 3: the base of an element is its own |
| 22 |
|
| * xml:base resolved against the base of its parent, and the base of the |
| 23 |
|
| * document is the URI it was read from, which the caller supplies as the |
| 24 |
|
| * `base` option and the plugin fills in from the file's path. The join |
| 25 |
|
| * is frx_uri.h's plain RFC 3986 one, not the Canonical XML 1.1 variant, |
| 26 |
|
| * so a document's base and a signature's xml:base cannot drift into each |
| 27 |
|
| * other. Nothing is cached. |
| 28 |
|
| * |
| 29 |
|
| * lang is xml:lang from the nearest ancestor-or-self that carries one, |
| 30 |
|
| * the empty string when none does and when the nearest says "", which |
| 31 |
|
| * section 2.12 gives as the way to say no language. space is |
| 32 |
|
| * xml:space from the nearest ancestor-or-self carrying it: "preserve" or |
| 33 |
|
| * "default", the latter when nothing says otherwise. |
| 34 |
|
| * |
| 35 |
|
| * Nothing recurses: each walk is a loop up the parent chain, and the |
| 36 |
|
| * base walk collects the chain into a small array first so that it can |
| 37 |
|
| * be joined from the outside in. |
| 38 |
|
| * |
| 39 |
|
| * Needs frx_tree.h, frx_uri.h, frx_buf.h. */ |
| 40 |
|
| |
| 41 |
|
| /* the value of an xml: attribute on n, or NULL */ |
| 42 |
|
| static const frx_str * |
| 43 |
1163 |
| frx_xml_attr(const frx_node *n, const char *local) |
| 44 |
|
| { |
| 45 |
|
| int i; |
| 46 |
1163 |
▶ | if (n->kind != FRX_ELEMENT) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->kind != FRX_ELEMENT) return NULL; |
312 |
851 |
1163 |
|
| 47 |
1785 |
▶ | for (i = 0; i < n->n_attrs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) { |
1177 |
608 |
1785 |
|
| 48 |
1177 |
| const frx_attr *a = &n->attrs[i]; |
| 49 |
1177 |
▶ | if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1) |
245 |
932 |
1177 |
|
| 50 |
245 |
▶ | && frx_str_eq(&a->local, local, strlen(local))) |
Branch: 1 branch
| Branch | True | False | Total |
| && frx_str_eq(&a->local, local, strlen(local))) |
243 |
2 |
245 |
|
| 51 |
243 |
| return &a->value; |
| 52 |
|
| } |
| 53 |
608 |
| return NULL; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| /* the nearest xml:lang at or above n; "" when none, and "" when the |
| 57 |
|
| * nearest is empty, which section 2.12 uses to say "no language" */ |
| 58 |
|
| static frx_str |
| 59 |
7 |
| frx_node_lang(const frx_node *n) |
| 60 |
|
| { |
| 61 |
12 |
▶ | for (; n; n = n->parent) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; n; n = n->parent) { |
11 |
1 |
12 |
|
| 62 |
11 |
| const frx_str *v = frx_xml_attr(n, "lang"); |
| 63 |
11 |
▶ | if (v) return *v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) return *v; |
6 |
5 |
11 |
|
| 64 |
|
| } |
| 65 |
1 |
| return FRX_EMPTY_STR; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| /* "preserve" when the nearest xml:space says so, "default" otherwise */ |
| 69 |
|
| static const char * |
| 70 |
6 |
| frx_node_space(const frx_node *n) |
| 71 |
|
| { |
| 72 |
10 |
▶ | for (; n; n = n->parent) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; n; n = n->parent) { |
9 |
1 |
10 |
|
| 73 |
9 |
| const frx_str *v = frx_xml_attr(n, "space"); |
| 74 |
9 |
▶ | if (v) return frx_str_eq(v, "preserve", 8) ? "preserve" : "default"; |
Branch: 2 branches
| Branch | True | False | Total |
| if (v) return frx_str_eq(v, "preserve", 8) ? "preserve" : "default"; |
5 |
4 |
9 |
| if (v) return frx_str_eq(v, "preserve", 8) ? "preserve" : "default"; |
2 |
3 |
5 |
|
| 75 |
|
| } |
| 76 |
1 |
| return "default"; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* The base URI of n into out: the document's base, then every xml:base |
| 80 |
|
| * from the outermost ancestor inwards, each joined onto what came |
| 81 |
|
| * before. 0 on allocation failure. */ |
| 82 |
|
| static int |
| 83 |
310 |
| frx_node_base_uri(const frx_node *n, const frx_doc *d, frx_buf *out) |
| 84 |
|
| { |
| 85 |
310 |
| const frx_str **chain = NULL; |
| 86 |
310 |
| int cap = 0, cnt = 0, i, ok = 1; |
| 87 |
|
| const frx_node *c; |
| 88 |
|
| frx_buf acc; |
| 89 |
|
| |
| 90 |
1453 |
▶ | for (c = n; c; c = c->parent) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = n; c; c = c->parent) { |
1143 |
310 |
1453 |
|
| 91 |
1143 |
| const frx_str *v = frx_xml_attr(c, "base"); |
| 92 |
1143 |
▶ | if (!v) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) continue; |
911 |
232 |
1143 |
|
| 93 |
232 |
▶ | if (cnt == cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnt == cap) { |
69 |
163 |
232 |
|
| 94 |
69 |
▶ | int ncap = cap ? cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = cap ? cap * 2 : 8; |
6 |
63 |
69 |
|
| 95 |
69 |
| const frx_str **nc = (const frx_str **)realloc(chain, (size_t)ncap * sizeof *nc); |
| 96 |
69 |
▶ | if (!nc) { free(chain); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nc) { free(chain); return 0; } |
0 |
69 |
69 |
|
| 97 |
69 |
| chain = nc; cap = ncap; |
| 98 |
|
| } |
| 99 |
232 |
| chain[cnt++] = v; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
310 |
| frx_buf_init(&acc); |
| 103 |
310 |
| frx_buf_append_n(&acc, d->base_uri.p, d->base_uri.len); |
| 104 |
542 |
▶ | for (i = cnt - 1; ok && i >= 0; i--) { /* outermost first */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = cnt - 1; ok && i >= 0; i--) { /* outermost first */ |
542 |
0 |
542 |
| for (i = cnt - 1; ok && i >= 0; i--) { /* outermost first */ |
232 |
310 |
542 |
|
| 105 |
|
| frx_buf joined; |
| 106 |
232 |
| frx_buf_init(&joined); |
| 107 |
232 |
▶ | ok = frx_uri_join(&joined, acc.p ? acc.p : "", acc.len, chain[i]->p, chain[i]->len, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| ok = frx_uri_join(&joined, acc.p ? acc.p : "", acc.len, chain[i]->p, chain[i]->len, 0); |
232 |
0 |
232 |
|
| 108 |
232 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
232 |
0 |
232 |
|
| 109 |
232 |
| acc.len = 0; |
| 110 |
232 |
▶ | frx_buf_append_n(&acc, joined.p ? joined.p : "", joined.len); |
Branch: 1 branch
| Branch | True | False | Total |
| frx_buf_append_n(&acc, joined.p ? joined.p : "", joined.len); |
232 |
0 |
232 |
|
| 111 |
232 |
| ok = !acc.failed; |
| 112 |
|
| } |
| 113 |
232 |
| frx_buf_free(&joined); |
| 114 |
|
| } |
| 115 |
310 |
| free(chain); |
| 116 |
310 |
| out->len = 0; |
| 117 |
310 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
310 |
0 |
310 |
|
| 118 |
310 |
▶ | frx_buf_append_n(out, acc.p ? acc.p : "", acc.len); |
Branch: 1 branch
| Branch | True | False | Total |
| frx_buf_append_n(out, acc.p ? acc.p : "", acc.len); |
310 |
0 |
310 |
|
| 119 |
310 |
| ok = !out->failed; |
| 120 |
|
| } |
| 121 |
310 |
| frx_buf_free(&acc); |
| 122 |
310 |
| return ok; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
|
| #endif /* FRX_XMLATTR_H */ |