Skip to content

Commit

Permalink
Fix #184: set ws-replace tag name depending on parent tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Jul 24, 2018
1 parent cf8eb94 commit a0e9952
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions WebSharper.UI.Templating.Runtime/Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ type Runtime private () =
]
static let defaultTemplateWrappers = ("""<div style="display:none" {0}="{1}">""", "</div>")

static let holeTagName parent =
match parent with
| Some "select" -> "option"
| Some "fieldset" -> "legend"
| Some "map" -> "area"
| Some "object" -> "param"
| Some "table" -> "tbody"
| Some "tbody" -> "tr"
| Some "colgroup" -> "col"
| Some "tr" -> "td"
| _ -> "div"

static member RunTemplate (fillWith: seq<TemplateHole>): Doc =
failwith "Template.Bind() can only be called from the client side."

Expand Down Expand Up @@ -363,35 +375,36 @@ type Runtime private () =
ctx.Writer.Write(HtmlTextWriter.SelfClosingTagEnd)
else
ctx.Writer.Write(HtmlTextWriter.TagRightChar)
Array.iter (writeNode false plain) children
Array.iter (writeNode (Some tag) plain) children
if tag = "body" && Option.isNone name && Option.isSome inlineBaseName then
ctx.Templates |> Seq.iter (fun (KeyValue(k, v)) ->
match k.NameAsOption with
| Some templateName -> writeWrappedTemplate templateName v ctx
| None -> ()
)
ctx.Writer.WriteEndTag(tag)
and writeNode isRoot plain = function
and writeNode parent plain = function
| Node.Element (tag, _, attrs, children) ->
writeElement isRoot plain tag attrs None children
writeElement (Option.isNone parent) plain tag attrs None children
| Node.Input (tag, holeName, attrs, children) ->
let doPlain() = writeElement isRoot plain tag attrs (Some holeName) children
let doPlain() = writeElement (Option.isNone parent) plain tag attrs (Some holeName) children
if plain then doPlain() else
let wsVar, attrs =
match ctx.FillWith.TryGetValue holeName with
| true, TemplateHole.EventQ (_, _, _) ->
None, Array.append [|Attr.Event("input", holeName)|] attrs
| _ ->
Some holeName, attrs
writeElement isRoot plain tag attrs wsVar children
writeElement (Option.isNone parent) plain tag attrs wsVar children
| Node.Text text ->
writeStringParts text ctx.Writer
| Node.DocHole holeName ->
let doPlain() =
ctx.Writer.WriteBeginTag("div")
let tagName = holeTagName parent
ctx.Writer.WriteBeginTag(tagName)
ctx.Writer.WriteAttribute(ReplaceAttr, holeName)
ctx.Writer.Write(HtmlTextWriter.TagRightChar)
ctx.Writer.WriteEndTag("div")
ctx.Writer.WriteEndTag(tagName)
if plain then doPlain() else
match holeName with
| "scripts" | "styles" | "meta" when Option.isSome ctx.Resources ->
Expand Down Expand Up @@ -427,7 +440,7 @@ type Runtime private () =
writeElement false true k [||] None v
textHole |> Option.iter ctx.Writer.WriteEncodedText
ctx.Writer.WriteEndTag(tagName)
Array.iter (writeNode true plain) template.Value
Array.iter (writeNode None plain) template.Value
let templates = ref None
let getTemplates (ctx: Web.Context) =
match !templates with
Expand Down

0 comments on commit a0e9952

Please sign in to comment.