UNPKG

4.26 kBJavaScriptView Raw
1const K = require('kcore');
2require('./KBSCNateSvg');
3require('./KNateXmlStatic');
4
5// -----------------------------------------------------------------------------
6// SVG Prototypes
7// -----------------------------------------------------------------------------
8
9K.NateSvgElemProto = Object.assign({}, K.NateXmlAbstractProto, K.BSC.NateSvgProto,
10{
11 init(parent, namespace)
12 {
13 K.NateXmlAbstractProto.init.call(this, parent, namespace);
14
15 this.isAttributeNamesCaseSensitive = true;
16 this.isTagNameCaseSensitive = true;
17 this.addSlashToOpenCloseAtOnceTag = true;
18
19 if (K.Object.isNotNull(parent))
20 {
21 parent.push(this);
22 }
23 },
24
25 _getDefaultNamespace()
26 {
27 return K.nateSvgStaticNamespace;
28 },
29
30 newSVGElement(tag, params) {
31 return this.tagOpenClose(tag, params);
32 }
33});
34
35K.NateSvgDocumentProto = Object.assign({}, K.NateSvgElemProto,
36{
37 init(parent, namespace)
38 {
39 K.NateSvgElemProto.init.call(this, parent, namespace);
40
41 this.set('width', 300);
42 this.set('height', 200);
43 },
44
45 render()
46 {
47 let rv = '<svg'
48 + ' version="1.1"'
49 + ' baseProfile="full"'
50 + ' width="' + this.tagParamsList['width'] + '"'
51 + ' height="' + this.tagParamsList['height'] + '"'
52 + ' xmlns="http://www.w3.org/2000/svg">';
53
54 rv += K.NateXmlAbstractProto.render.call(this);
55 rv += '</svg>';
56
57 return rv;
58 }
59});
60
61// -----------------------------------------------------------------------------
62// SVG namespace
63// -----------------------------------------------------------------------------
64
65K.nateSvgStaticNamespace =
66 K.nateXmlStaticNamespace.createExtension('SvgStatic', {nateProto: K.NateSvgElemProto});
67
68// -----------------------------------------------------------------------------
69// SVG tags
70// -----------------------------------------------------------------------------
71
72K.BSC.nateSvgCommonOpenCloseAtOnceTags.forEach((tagName) => {
73 K.nateSvgStaticNamespace.addCreateFunction(tagName, K.NateXmlAbstractProto.tagOpenCloseAtOnce);
74})
75
76K.BSC.nateSvgCommonTags.forEach((tagName) => {
77 K.nateSvgStaticNamespace.addCreateFunction(tagName, K.NateXmlAbstractProto.tagOpenClose);
78})
79
80// -----------------------------------------------------------------------------
81// SVG property setters
82// -----------------------------------------------------------------------------
83
84K.BSC.nateSvgAttributes.forEach((key) => {
85 K.nateSvgStaticNamespace.addSetter(key, K.NateXmlAbstractProto.commonAttributeSetter)
86})
87
88// Special case: overload default handler for xlink:href.
89// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href
90// "Note: SVG 2 removed the need for the xlink namespace, so instead of xlink:href you
91// should use href."
92// We emit href attribute instead of xlink:href to be consistent with what
93// does DOM do.
94K.nateSvgStaticNamespace.addSetter('xlink:href', (nNode, key, value) => {
95 K.NateXmlAbstractProto.commonAttributeSetter(nNode, 'href', value)
96})
97
98// Href version should works too.
99K.nateSvgStaticNamespace.addSetter('href', (nNode, key, value) => {
100 K.NateXmlAbstractProto.commonAttributeSetter(nNode, 'href', value)
101})
102
103K.nateSvgStaticNamespace.addSetter('textContent', (nNode, key, value) => {
104 const escapedValue = K.NateXmlAbstractProto.escapeXmlTags(value);
105 K.NateXmlAbstractProto.commonInnerContentSetter(nNode, 'textContent', escapedValue)
106})
107
108// This is not a standard, but Firefox and Chrome implement it in theirs DOMs,
109// so we keep this call for compatibility.
110K.nateSvgStaticNamespace.addSetter('innerHTML',
111 K.NateXmlAbstractProto.commonInnerContentSetter)
112
113// -----------------------------------------------------------------------------
114// SVG factory functions
115// -----------------------------------------------------------------------------
116
117K.NateSvgElem = K.nateSvgStaticNamespace.createFactoryFunction(K.NateSvgElemProto);
118K.NateSvgDocument = K.nateSvgStaticNamespace.createFactoryFunction(K.NateSvgDocumentProto);