tag.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. #include "gumbo.h"
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <strings.h> // For strcasecmp.
  20. // NOTE(jdtang): Keep this in sync with the GumboTag enum in the header.
  21. // TODO(jdtang): Investigate whether there're efficiency benefits to putting the
  22. // most common tag names first, or to putting them in alphabetical order and
  23. // using a binary search.
  24. const char* kGumboTagNames[] = {
  25. "html",
  26. "head",
  27. "title",
  28. "base",
  29. "link",
  30. "meta",
  31. "style",
  32. "script",
  33. "noscript",
  34. "template",
  35. "body",
  36. "article",
  37. "section",
  38. "nav",
  39. "aside",
  40. "h1",
  41. "h2",
  42. "h3",
  43. "h4",
  44. "h5",
  45. "h6",
  46. "hgroup",
  47. "header",
  48. "footer",
  49. "address",
  50. "p",
  51. "hr",
  52. "pre",
  53. "blockquote",
  54. "ol",
  55. "ul",
  56. "li",
  57. "dl",
  58. "dt",
  59. "dd",
  60. "figure",
  61. "figcaption",
  62. "main",
  63. "div",
  64. "a",
  65. "em",
  66. "strong",
  67. "small",
  68. "s",
  69. "cite",
  70. "q",
  71. "dfn",
  72. "abbr",
  73. "data",
  74. "time",
  75. "code",
  76. "var",
  77. "samp",
  78. "kbd",
  79. "sub",
  80. "sup",
  81. "i",
  82. "b",
  83. "u",
  84. "mark",
  85. "ruby",
  86. "rt",
  87. "rp",
  88. "bdi",
  89. "bdo",
  90. "span",
  91. "br",
  92. "wbr",
  93. "ins",
  94. "del",
  95. "image",
  96. "img",
  97. "iframe",
  98. "embed",
  99. "object",
  100. "param",
  101. "video",
  102. "audio",
  103. "source",
  104. "track",
  105. "canvas",
  106. "map",
  107. "area",
  108. "math",
  109. "mi",
  110. "mo",
  111. "mn",
  112. "ms",
  113. "mtext",
  114. "mglyph",
  115. "malignmark",
  116. "annotation-xml",
  117. "svg",
  118. "foreignobject",
  119. "desc",
  120. "table",
  121. "caption",
  122. "colgroup",
  123. "col",
  124. "tbody",
  125. "thead",
  126. "tfoot",
  127. "tr",
  128. "td",
  129. "th",
  130. "form",
  131. "fieldset",
  132. "legend",
  133. "label",
  134. "input",
  135. "button",
  136. "select",
  137. "datalist",
  138. "optgroup",
  139. "option",
  140. "textarea",
  141. "keygen",
  142. "output",
  143. "progress",
  144. "meter",
  145. "details",
  146. "summary",
  147. "menu",
  148. "menuitem",
  149. "applet",
  150. "acronym",
  151. "bgsound",
  152. "dir",
  153. "frame",
  154. "frameset",
  155. "noframes",
  156. "isindex",
  157. "listing",
  158. "xmp",
  159. "nextid",
  160. "noembed",
  161. "plaintext",
  162. "rb",
  163. "strike",
  164. "basefont",
  165. "big",
  166. "blink",
  167. "center",
  168. "font",
  169. "marquee",
  170. "multicol",
  171. "nobr",
  172. "spacer",
  173. "tt",
  174. "", // TAG_UNKNOWN
  175. "", // TAG_LAST
  176. };
  177. const char* gumbo_normalized_tagname(GumboTag tag) {
  178. assert(tag <= GUMBO_TAG_LAST);
  179. return kGumboTagNames[tag];
  180. }
  181. // TODO(jdtang): Add test for this.
  182. void gumbo_tag_from_original_text(GumboStringPiece* text) {
  183. if (text->data == NULL) {
  184. return;
  185. }
  186. assert(text->length >= 2);
  187. assert(text->data[0] == '<');
  188. assert(text->data[text->length - 1] == '>');
  189. if (text->data[1] == '/') {
  190. // End tag.
  191. assert(text->length >= 3);
  192. text->data += 2; // Move past </
  193. text->length -= 3;
  194. } else {
  195. // Start tag.
  196. text->data += 1; // Move past <
  197. text->length -= 2;
  198. // strnchr is apparently not a standard C library function, so I loop
  199. // explicitly looking for whitespace or other illegal tag characters.
  200. for (const char* c = text->data; c != text->data + text->length; ++c) {
  201. if (isspace(*c) || *c == '/') {
  202. text->length = c - text->data;
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. GumboTag gumbo_tag_enum(const char* tagname) {
  209. for (int i = 0; i < GUMBO_TAG_LAST; ++i) {
  210. // TODO(jdtang): strcasecmp is non-portable, so if we want to support
  211. // non-GCC compilers, we'll need some #ifdef magic. This source already has
  212. // pretty significant issues with MSVC6 anyway.
  213. if (strcasecmp(tagname, kGumboTagNames[i]) == 0) {
  214. return i;
  215. }
  216. }
  217. return GUMBO_TAG_UNKNOWN;
  218. }