You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
8.0 KiB

  1. 'use strict';
  2. const Tokenizer = require('../tokenizer');
  3. const HTML = require('./html');
  4. //Aliases
  5. const $ = HTML.TAG_NAMES;
  6. const NS = HTML.NAMESPACES;
  7. const ATTRS = HTML.ATTRS;
  8. //MIME types
  9. const MIME_TYPES = {
  10. TEXT_HTML: 'text/html',
  11. APPLICATION_XML: 'application/xhtml+xml'
  12. };
  13. //Attributes
  14. const DEFINITION_URL_ATTR = 'definitionurl';
  15. const ADJUSTED_DEFINITION_URL_ATTR = 'definitionURL';
  16. const SVG_ATTRS_ADJUSTMENT_MAP = {
  17. attributename: 'attributeName',
  18. attributetype: 'attributeType',
  19. basefrequency: 'baseFrequency',
  20. baseprofile: 'baseProfile',
  21. calcmode: 'calcMode',
  22. clippathunits: 'clipPathUnits',
  23. diffuseconstant: 'diffuseConstant',
  24. edgemode: 'edgeMode',
  25. filterunits: 'filterUnits',
  26. glyphref: 'glyphRef',
  27. gradienttransform: 'gradientTransform',
  28. gradientunits: 'gradientUnits',
  29. kernelmatrix: 'kernelMatrix',
  30. kernelunitlength: 'kernelUnitLength',
  31. keypoints: 'keyPoints',
  32. keysplines: 'keySplines',
  33. keytimes: 'keyTimes',
  34. lengthadjust: 'lengthAdjust',
  35. limitingconeangle: 'limitingConeAngle',
  36. markerheight: 'markerHeight',
  37. markerunits: 'markerUnits',
  38. markerwidth: 'markerWidth',
  39. maskcontentunits: 'maskContentUnits',
  40. maskunits: 'maskUnits',
  41. numoctaves: 'numOctaves',
  42. pathlength: 'pathLength',
  43. patterncontentunits: 'patternContentUnits',
  44. patterntransform: 'patternTransform',
  45. patternunits: 'patternUnits',
  46. pointsatx: 'pointsAtX',
  47. pointsaty: 'pointsAtY',
  48. pointsatz: 'pointsAtZ',
  49. preservealpha: 'preserveAlpha',
  50. preserveaspectratio: 'preserveAspectRatio',
  51. primitiveunits: 'primitiveUnits',
  52. refx: 'refX',
  53. refy: 'refY',
  54. repeatcount: 'repeatCount',
  55. repeatdur: 'repeatDur',
  56. requiredextensions: 'requiredExtensions',
  57. requiredfeatures: 'requiredFeatures',
  58. specularconstant: 'specularConstant',
  59. specularexponent: 'specularExponent',
  60. spreadmethod: 'spreadMethod',
  61. startoffset: 'startOffset',
  62. stddeviation: 'stdDeviation',
  63. stitchtiles: 'stitchTiles',
  64. surfacescale: 'surfaceScale',
  65. systemlanguage: 'systemLanguage',
  66. tablevalues: 'tableValues',
  67. targetx: 'targetX',
  68. targety: 'targetY',
  69. textlength: 'textLength',
  70. viewbox: 'viewBox',
  71. viewtarget: 'viewTarget',
  72. xchannelselector: 'xChannelSelector',
  73. ychannelselector: 'yChannelSelector',
  74. zoomandpan: 'zoomAndPan'
  75. };
  76. const XML_ATTRS_ADJUSTMENT_MAP = {
  77. 'xlink:actuate': { prefix: 'xlink', name: 'actuate', namespace: NS.XLINK },
  78. 'xlink:arcrole': { prefix: 'xlink', name: 'arcrole', namespace: NS.XLINK },
  79. 'xlink:href': { prefix: 'xlink', name: 'href', namespace: NS.XLINK },
  80. 'xlink:role': { prefix: 'xlink', name: 'role', namespace: NS.XLINK },
  81. 'xlink:show': { prefix: 'xlink', name: 'show', namespace: NS.XLINK },
  82. 'xlink:title': { prefix: 'xlink', name: 'title', namespace: NS.XLINK },
  83. 'xlink:type': { prefix: 'xlink', name: 'type', namespace: NS.XLINK },
  84. 'xml:base': { prefix: 'xml', name: 'base', namespace: NS.XML },
  85. 'xml:lang': { prefix: 'xml', name: 'lang', namespace: NS.XML },
  86. 'xml:space': { prefix: 'xml', name: 'space', namespace: NS.XML },
  87. xmlns: { prefix: '', name: 'xmlns', namespace: NS.XMLNS },
  88. 'xmlns:xlink': { prefix: 'xmlns', name: 'xlink', namespace: NS.XMLNS }
  89. };
  90. //SVG tag names adjustment map
  91. const SVG_TAG_NAMES_ADJUSTMENT_MAP = (exports.SVG_TAG_NAMES_ADJUSTMENT_MAP = {
  92. altglyph: 'altGlyph',
  93. altglyphdef: 'altGlyphDef',
  94. altglyphitem: 'altGlyphItem',
  95. animatecolor: 'animateColor',
  96. animatemotion: 'animateMotion',
  97. animatetransform: 'animateTransform',
  98. clippath: 'clipPath',
  99. feblend: 'feBlend',
  100. fecolormatrix: 'feColorMatrix',
  101. fecomponenttransfer: 'feComponentTransfer',
  102. fecomposite: 'feComposite',
  103. feconvolvematrix: 'feConvolveMatrix',
  104. fediffuselighting: 'feDiffuseLighting',
  105. fedisplacementmap: 'feDisplacementMap',
  106. fedistantlight: 'feDistantLight',
  107. feflood: 'feFlood',
  108. fefunca: 'feFuncA',
  109. fefuncb: 'feFuncB',
  110. fefuncg: 'feFuncG',
  111. fefuncr: 'feFuncR',
  112. fegaussianblur: 'feGaussianBlur',
  113. feimage: 'feImage',
  114. femerge: 'feMerge',
  115. femergenode: 'feMergeNode',
  116. femorphology: 'feMorphology',
  117. feoffset: 'feOffset',
  118. fepointlight: 'fePointLight',
  119. fespecularlighting: 'feSpecularLighting',
  120. fespotlight: 'feSpotLight',
  121. fetile: 'feTile',
  122. feturbulence: 'feTurbulence',
  123. foreignobject: 'foreignObject',
  124. glyphref: 'glyphRef',
  125. lineargradient: 'linearGradient',
  126. radialgradient: 'radialGradient',
  127. textpath: 'textPath'
  128. });
  129. //Tags that causes exit from foreign content
  130. const EXITS_FOREIGN_CONTENT = {
  131. [$.B]: true,
  132. [$.BIG]: true,
  133. [$.BLOCKQUOTE]: true,
  134. [$.BODY]: true,
  135. [$.BR]: true,
  136. [$.CENTER]: true,
  137. [$.CODE]: true,
  138. [$.DD]: true,
  139. [$.DIV]: true,
  140. [$.DL]: true,
  141. [$.DT]: true,
  142. [$.EM]: true,
  143. [$.EMBED]: true,
  144. [$.H1]: true,
  145. [$.H2]: true,
  146. [$.H3]: true,
  147. [$.H4]: true,
  148. [$.H5]: true,
  149. [$.H6]: true,
  150. [$.HEAD]: true,
  151. [$.HR]: true,
  152. [$.I]: true,
  153. [$.IMG]: true,
  154. [$.LI]: true,
  155. [$.LISTING]: true,
  156. [$.MENU]: true,
  157. [$.META]: true,
  158. [$.NOBR]: true,
  159. [$.OL]: true,
  160. [$.P]: true,
  161. [$.PRE]: true,
  162. [$.RUBY]: true,
  163. [$.S]: true,
  164. [$.SMALL]: true,
  165. [$.SPAN]: true,
  166. [$.STRONG]: true,
  167. [$.STRIKE]: true,
  168. [$.SUB]: true,
  169. [$.SUP]: true,
  170. [$.TABLE]: true,
  171. [$.TT]: true,
  172. [$.U]: true,
  173. [$.UL]: true,
  174. [$.VAR]: true
  175. };
  176. //Check exit from foreign content
  177. exports.causesExit = function(startTagToken) {
  178. const tn = startTagToken.tagName;
  179. const isFontWithAttrs =
  180. tn === $.FONT &&
  181. (Tokenizer.getTokenAttr(startTagToken, ATTRS.COLOR) !== null ||
  182. Tokenizer.getTokenAttr(startTagToken, ATTRS.SIZE) !== null ||
  183. Tokenizer.getTokenAttr(startTagToken, ATTRS.FACE) !== null);
  184. return isFontWithAttrs ? true : EXITS_FOREIGN_CONTENT[tn];
  185. };
  186. //Token adjustments
  187. exports.adjustTokenMathMLAttrs = function(token) {
  188. for (let i = 0; i < token.attrs.length; i++) {
  189. if (token.attrs[i].name === DEFINITION_URL_ATTR) {
  190. token.attrs[i].name = ADJUSTED_DEFINITION_URL_ATTR;
  191. break;
  192. }
  193. }
  194. };
  195. exports.adjustTokenSVGAttrs = function(token) {
  196. for (let i = 0; i < token.attrs.length; i++) {
  197. const adjustedAttrName = SVG_ATTRS_ADJUSTMENT_MAP[token.attrs[i].name];
  198. if (adjustedAttrName) {
  199. token.attrs[i].name = adjustedAttrName;
  200. }
  201. }
  202. };
  203. exports.adjustTokenXMLAttrs = function(token) {
  204. for (let i = 0; i < token.attrs.length; i++) {
  205. const adjustedAttrEntry = XML_ATTRS_ADJUSTMENT_MAP[token.attrs[i].name];
  206. if (adjustedAttrEntry) {
  207. token.attrs[i].prefix = adjustedAttrEntry.prefix;
  208. token.attrs[i].name = adjustedAttrEntry.name;
  209. token.attrs[i].namespace = adjustedAttrEntry.namespace;
  210. }
  211. }
  212. };
  213. exports.adjustTokenSVGTagName = function(token) {
  214. const adjustedTagName = SVG_TAG_NAMES_ADJUSTMENT_MAP[token.tagName];
  215. if (adjustedTagName) {
  216. token.tagName = adjustedTagName;
  217. }
  218. };
  219. //Integration points
  220. function isMathMLTextIntegrationPoint(tn, ns) {
  221. return ns === NS.MATHML && (tn === $.MI || tn === $.MO || tn === $.MN || tn === $.MS || tn === $.MTEXT);
  222. }
  223. function isHtmlIntegrationPoint(tn, ns, attrs) {
  224. if (ns === NS.MATHML && tn === $.ANNOTATION_XML) {
  225. for (let i = 0; i < attrs.length; i++) {
  226. if (attrs[i].name === ATTRS.ENCODING) {
  227. const value = attrs[i].value.toLowerCase();
  228. return value === MIME_TYPES.TEXT_HTML || value === MIME_TYPES.APPLICATION_XML;
  229. }
  230. }
  231. }
  232. return ns === NS.SVG && (tn === $.FOREIGN_OBJECT || tn === $.DESC || tn === $.TITLE);
  233. }
  234. exports.isIntegrationPoint = function(tn, ns, attrs, foreignNS) {
  235. if ((!foreignNS || foreignNS === NS.HTML) && isHtmlIntegrationPoint(tn, ns, attrs)) {
  236. return true;
  237. }
  238. if ((!foreignNS || foreignNS === NS.MATHML) && isMathMLTextIntegrationPoint(tn, ns)) {
  239. return true;
  240. }
  241. return false;
  242. };