Browse Source

added xml namespace extension

A Silk instance will use its xml namespace as namespace for new elements created through the DNU mechanism.
Example:
  svg := Silk newElement: 'svg' xmlns: 'http://www.w3.org/2000/svg'. "this creates a new svg element in its proper namespace"
  svg CIRCLE: {'cx' -> 60. 'cy' -> 25. 'r' -> 10}.   "This creates a svg circle, not a html circle"

The current behavior is not affected, this is just a new optional feature.
Christian Haider 4 years ago
parent
commit
b273123456
1 changed files with 41 additions and 5 deletions
  1. 41 5
      src/Silk.st

+ 41 - 5
src/Silk.st

@@ -51,13 +51,23 @@ Taken all this together, one can do pretty neat constructs:
 adds `<p id="mission">We are the champions.</p>` into `aSilk`
 and returns the Silk-wrapped `<p>` with insertion cursor at the end.!
 
+!Silk methodsFor: 'accessing'!
+
+namespace
+	"<String>
+	XML namespace for elements: html.
+	The default for all virtual Silk tag messages"
+	
+	^ self element namespaceURI
+! !
+
 !Silk methodsFor: 'writing'!
 
 doesNotUnderstand: aMessage
 	"`aSilk DIV` creates a div element and inserts it.
 	`aSilk DIV: anObject` creates a div element, inserts it
 	and puts contents in it"
-	(self class tryMakeDnuElement: aMessage)
+	(self class tryMakeDnuElement: aMessage in: self)
 		ifNil: [ ^ super doesNotUnderstand: aMessage ]
 		ifNotNil: [ :newElement | self << newElement. ^ newElement ]
 !
@@ -71,11 +81,37 @@ nextPut: anObject
 		ifNil: [ super nextPut: anObject ]
 ! !
 
+!Silk class methodsFor: 'accessing'!
+
+htmlNamespace
+	"<String>
+	XML namespace for HTML elements.
+	The default for all virtual Silk tag messages"
+	
+	^ 'http://www.w3.org/1999/xhtml'
+!
+
+namespace
+	"<String>
+	XML namespace for elements: html.
+	The default for all virtual Silk tag messages"
+	
+	^ self htmlNamespace
+! !
+
 !Silk class methodsFor: 'instance creation'!
 
-tryMakeDnuElement: aMessage
+tryMakeDnuElement: aMessage in: aSilk
 	"`DIV` creates a div element.
-	`DIV: anObject` creates a div element and puts contents in it"
+	`DIV: anObject` creates a div element and puts contents in it.
+	When aSilk is an instance and not the class Silk, 
+	and the instance has an xml namespace other than the default #html,
+	Then that namespace is used for the new element.
+	You can do:
+		svg := Silk newElement: 'svg' xmlns: 'http://www.w3.org/2000/svg'.
+		svg CIRCLE: {'cx' -> 60. 'cy' -> 25. 'r' -> 10}.
+	This creates a svg circle, not a html circle."
+	
 	| selector newElement useArg |
 	selector := aMessage selector.
 	selector asUppercase = selector
@@ -85,7 +121,7 @@ tryMakeDnuElement: aMessage
 		ifFalse: [ useArg := false ].
 	(selector includes: ':')
 		ifTrue: [ ^ nil ].
-	newElement := self newElement: selector asLowercase.
+	newElement := self newElement: selector asLowercase xmlns: aSilk namespace.
 	useArg ifTrue: [ newElement << aMessage arguments first ].
 	^ newElement
 ! !
@@ -95,7 +131,7 @@ tryMakeDnuElement: aMessage
 doesNotUnderstand: aMessage
 	"`Silk DIV` creates a div element.
 	`Silk DIV: anObject` creates a div element and puts contents in it"
-	^ (self tryMakeDnuElement: aMessage)
+	^ (self tryMakeDnuElement: aMessage in: self)
 		ifNil: [ super doesNotUnderstand: aMessage ]
 ! !