I am abstract class providing methods to build xhtml. My most commonly used subclass is WAHtmlRenderer.
ProtoObjectObjectWAAbstractHtmlBuilder
| attributeBuffer |
| HtmlCharacters |
| attributeBuffer |
|---|
|
| HtmlCharacters |
|---|
|
|
| accessing |
|---|
| document |
self subclassResponsibility |
| basic markup |
|---|
| attributeAt: aString put: anObject |
self attributes at: aString put: anObject |
| attributes |
^ attributeBuffer ifNil: [attributeBuffer _ WAHtmlAttributes new] |
| attributes: aCollection |
aCollection associationsDo: [:x | self attributeAt: x key put: x value] |
| closeTag: aString |
self document closeTag: aString |
| encodeCharacter: aCharacter |
self html: ('&#', aCharacter asInteger asString, ';').
|
| encodeText: anObject |
"deprecated" self text: anObject |
| html: aString |
self document text: aString. |
| labelFor: idString do: aBlock |
self attributes at: 'for' put: idString. self tag: 'label' do: aBlock |
| openTag: aString |
self document openTag: aString attributes: self attributes. attributeBuffer _ nil. |
| render: anObject |
anObject renderOn: self |
| shouldCloseTag: aString |
^ #(#hr #br #input #img #link) |
| tag: aString |
self openTag: aString. self closeTag: aString. |
| tag: aString do: anObject |
self openTag: aString. self render: anObject. self closeTag: aString. |
| text: anObject |
self html: (self class encode: anObject) |
| css |
|---|
| cssClass: aString |
self attributeAt: 'class' put: aString |
| cssId: aString |
self attributeAt: 'id' put: aString |
| divClass: aString with: anObject |
self cssClass: aString; div: anObject |
| divNamed: aString with: anObject |
self cssId: aString; div: anObject |
| spanClass: aString with: anObject |
self cssClass: aString; span: anObject |
| spanNamed: aString with: anObject |
self cssId: aString; span: anObject |
| style: aString |
self attributeAt: 'style' put: aString |
| dnu |
|---|
| doesNotUnderstand: aMessage |
| argCount | argCount _ aMessage arguments size. argCount = 0 ifTrue: [^ self tag: aMessage selector]. argCount = 1 ifTrue: [^ self tag: aMessage selector allButLast do: aMessage argument]. ^ super doesNotUnderstand: aMessage |
| forms |
|---|
| buttonForUrl: urlString withText: labelString |
self buttonForUrl: urlString withText: labelString data: #() |
| buttonForUrl: urlString withText: labelString data: assocCollection |
self formWithMethod: 'GET' action: urlString do: [assocCollection do: [:each | self inputWithType: 'hidden' named: each key asString value: each value]. self submitButtonWithText: labelString] |
| buttonWithType: typeString named: nameString value: valueString do: aBlock |
nameString ifNotNil: [self attributes at: 'name' put: nameString; at: 'id' ifAbsentPut: nameString]. valueString ifNotNil: [self attributes value: valueString asString]. self attributes type: typeString. self tag: 'button' do: aBlock. |
| fieldsetWithLegend: aString do: aBlock |
self tag: 'fieldset' do: [ self tag: 'legend' do: aString. aBlock value. ] |
| inputWithType: type |
self inputWithType: type named: nil |
| inputWithType: type named: name |
self inputWithType: type named: name value: nil. |
| inputWithType: type named: name value: value |
name ifNotNil: [self attributes at: 'name' put: name]. value ifNotNil: [self attributes value: value asString]. self attributes type: type. self tag: 'input'. |
| submitButtonWithText: aString |
self attributes value: aString. self inputWithType: 'submit' |
| head |
|---|
| scriptWithUrl: urlString |
self attributes at: 'type' put: 'text/javascript'; at: 'src' put: urlString. self tag: 'script'. |
| html |
|---|
| acronym: titleString do: aBlock |
self attributes at: 'title' put: titleString. self tag: 'acronym' do: aBlock |
| anchorWithMailto: emailString |
self anchorWithUrl: 'mailto:', emailString do: emailString |
| anchorWithMailto: emailString subject: subjectString |
self anchorWithUrl: 'mailto:', emailString, '?subject=', subjectString do: emailString |
| anchorWithName: aString |
self attributes name: aString. self tag: #a. |
| anchorWithUrl: urlString do: aBlock |
self attributes href: urlString. self tag: 'a' do: aBlock. |
| anchorWithUrl: urlString title: titleString do: aBlock |
self attributes title: titleString. self anchorWithUrl: urlString do: aBlock. |
| bold: aBlock |
self tag: 'b' do: aBlock. |
| break |
self tag: 'br' |
| div: aBlock |
self tag: 'div' do: aBlock. |
| emphasis: aBlock |
self tag: 'em' do: aBlock. |
| formWithAction: actionUrl do: aBlock |
self formWithMethod: 'post' action: actionUrl do: aBlock |
| formWithMethod: methodString action: actionUrl do: aBlock |
self attributes method: methodString; action: actionUrl. self tag: 'form' do: aBlock |
| heading: aString |
self heading: aString level: 1 |
| heading: anObject level: aNumber |
self tag: 'h', aNumber asString do: anObject |
| horizontalRule |
self tag: 'hr' |
| image: urlString |
self image: urlString altText: '' |
| image: urlString altText: altString |
self attributeAt: 'alt' put: altString. self attributeAt: 'src' put: urlString. self tag: #img. |
| image: urlString width: width height: height |
self image: urlString width: width height: height altText: '' |
| image: urlString width: width height: height altText: altString |
self attributeAt: 'width' put: width. self attributeAt: 'height' put: height. self attributeAt: 'border' put: 0. self image: urlString altText: altString |
| italic: aBlock |
self tag: 'i' do: aBlock. |
| list: aCollection |
self list: aCollection do: [:x | self text: x] |
| list: aCollection do: aBlock |
self unorderedList: [aCollection do: [:item | self listItem: [aBlock value: item]]] |
| listItem: aBlock |
self tag: #li do: aBlock |
| orderedList: aBlock |
self tag: 'ol' do: aBlock |
| orderedList: aCollection do: aBlock |
self orderedList: [aCollection do: [:item | self listItem: [aBlock value: item]]] |
| paragraph |
self tag: 'p' |
| paragraph: aBlock |
self tag: 'p' do: aBlock |
| preformatted: anObject |
self tag: 'pre' do: anObject |
| script: aString |
self tag: 'script' do: [self html: aString] |
| space |
self html: ' ' |
| space: aNumber |
aNumber timesRepeat: [self space] |
| span: aBlock |
self tag: 'span' do: aBlock. |
| submitButton |
self inputWithType: 'submit' |
| underline: aBlock |
self tag: 'u' do: aBlock. |
| unorderedList: aBlock |
self tag: 'ul' do: aBlock |
| javascript |
|---|
| anchorWithPopupUrl: urlString extent: aPoint do: anObject |
self anchorWithPopupUrl: urlString name: '''''' extent: aPoint toggles: #(menubar resizable scrollbars) do: anObject |
| anchorWithPopupUrl: urlString extent: aPoint text: aString |
self anchorWithPopupUrl: urlString extent: aPoint do: aString |
| anchorWithPopupUrl: urlString name: nameString extent: aPoint toggles: aCollection do: anObject |
self attributes at: 'onClick' put: (self popupStringWithUrl: urlString name: nameString extent: aPoint toggles: aCollection), '; return false'. self anchorWithUrl: urlString do: anObject. |
| popupStringWithUrl: urlString name: nameString extent: aPoint toggles: aCollection |
^ String streamContents:
[:stream |
stream
nextPutAll: 'window.open(';
nextPut: $';
nextPutAll: urlString;
nextPut: $';
nextPut: $,;
nextPut: $';
nextPutAll: nameString;
nextPut: $';
nextPut:$,;
nextPut: $';
nextPutAll: 'width=';
nextPutAll: aPoint x asString;
nextPutAll: ',height=';
nextPutAll: aPoint y asString;
nextPut: $,;
nextPutAll: (self toggleStringFor: aCollection);
nextPut: $';
nextPutAll: ')']
|
| toggleStringFor: aCollection |
^ String streamContents: [:stream | aCollection do: [:ea | stream nextPutAll: ea asString, '=yes'] separatedBy: [stream nextPutAll: ',']] |
| model |
|---|
| labelForSelector: aSymbol |
^ aSymbol asCapitalizedPhrase |
| tables |
|---|
| layoutTable: aBlock |
self attributes border: 0; cellspacing: 0; cellpadding: 0. self table: aBlock |
| layoutTableOfWidth: width do: aBlock |
self attributes width: width. self layoutTable: aBlock |
| spacerRow |
self tableRowWith: [self space] |
| table: aBlock |
self tag: 'table' do: aBlock |
| tableData: aBlock |
self tag: 'td' do: aBlock |
| tableData: aBlock span: aNumber |
aNumber > 0 ifTrue: [self attributes at: 'colspan' put: aNumber. self tableData: aBlock] |
| tableHeading: aBlock |
self tag: 'th' do: aBlock |
| tableHeadings: aCollection |
self tableRow: [aCollection do: [:ea | self tableHeading: ea]] |
| tableRow: aBlock |
self tag: 'tr' do: aBlock |
| tableRowWith: aBlock |
self tableRow: [ self tableData: aBlock ] |
| tableRowWith: aBlock span: aNumber |
self tableRow: [ self tableData: aBlock span: aNumber] |
| tableRowWith: aBlock with: anotherBlock |
self tableRow: [ self tableData: aBlock; tableData: anotherBlock] |
| tableRowWith: x with: y with: z |
self tableRow: [ self tableData: x; tableData: y; tableData: z] |
| tableRowWithLabel: anObject column: aBlock |
self tableRow: [ self cssClass: 'label'. self tableData: anObject. self tableData: aBlock] |
| tableRowWithLabel: anObject column: aBlock column: anotherBlock |
self tableRow: [ self cssClass: 'label'. self tableData: anObject. self tableData: aBlock; tableData: anotherBlock] |
| as yet unclassified |
|---|
| encode: anObject |
| charOrString | ^ String streamContents: [:s | anObject asString do: [:char | charOrString _ HtmlCharacters at: char asInteger + 1. charOrString isString ifTrue: [s nextPutAll: charOrString] ifFalse: [s nextPut: charOrString]]] |
| initialize |
"WAHtmlBuilder initialize"
HtmlCharacters _ Array new: 256.
0 to: 255 do: [:ea | HtmlCharacters at: ea + 1 put: ea asCharacter].
#($" 'quot' $< 'lt' $& 'amp' $> 'gt') pairsDo:
[:c :s | HtmlCharacters at: (c asInteger + 1) put: ('&',s,';') ]
|