Skip to content

Add support for Microdata #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

* Add support for the clip-path presentation attribute
(#333 by Martin @MBodin Bodin)
* Add support for Microdata attributes
(#343 by toastal)
* Add support for <meta itemprop> to be in flow content
(#343 by toastal)

# 4.6.0

Expand Down
19 changes: 19 additions & 0 deletions lib/html_f.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ struct

let uri_attrib a s = Xml.uri_attrib a s

let uris_attrib a ss = Xml.uris_attrib a ss

let space_sep_attrib = Xml.space_sep_attrib

let comma_sep_attrib = Xml.comma_sep_attrib
Expand Down Expand Up @@ -500,6 +502,18 @@ struct

let a_aria name = space_sep_attrib ("aria-" ^ name)

(* Microdata *)

let a_itemid = uri_attrib "itemid"

let a_itemprop = string_attrib "itemprop"

let a_itemref = space_sep_attrib "itemref"

let a_itemscope = constant_attrib "itemscope"

let a_itemtype = uris_attrib "itemtype"

type 'a elt = Xml.elt

type ('a, 'b) nullary = ?a: (('a attrib) list) -> unit -> 'b elt
Expand Down Expand Up @@ -817,6 +831,9 @@ struct

let meta = terminal "meta"

let meta_itemprop itemprop ?(a = []) () =
Xml.leaf ~a: ((a_itemprop itemprop) :: a) "meta"

let style ?(a = []) elts = Xml.node ~a "style" elts

let link ~rel ~href ?(a = []) () =
Expand Down Expand Up @@ -858,6 +875,8 @@ struct

let uri_attrib a s = Xml.uri_attrib a s

let uris_attrib a ss = Xml.uris_attrib a ss

let space_sep_attrib = Xml.space_sep_attrib

let comma_sep_attrib = Xml.comma_sep_attrib
Expand Down
27 changes: 26 additions & 1 deletion lib/html_sigs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,24 @@ module type T = sig
@see <https://www.w3.org/TR/wai-aria-1.1/#state_prop_def> List of WAI-ARIA attributes
*)

(** {3 Microdata support} *)

(** {{: https://html.spec.whatwg.org/multipage/microdata.html} Microdata} is
a specification written by the W3C, defining a set of additional HTML
attributes that can be applied to elements to annotate content with
specific machine-readable labels using schemas.
*)

val a_itemid : Xml.uri wrap -> [> | `ItemID ] attrib

val a_itemprop : string wrap -> [> | `ItemProp ] attrib

val a_itemref : idrefs wrap -> [> | `ItemRef ] attrib

val a_itemscope : unit -> [> | `ItemScope ] attrib

val a_itemtype : Xml.uri list wrap -> [> | `ItemType ] attrib

(** {2:elements Elements} *)

val txt : string wrap -> [> | txt] elt
Expand Down Expand Up @@ -1079,6 +1097,10 @@ module type T = sig

val meta : ([< | meta_attrib], [> | meta]) nullary

val meta_itemprop :
string wrap ->
([< | meta_attrib], [> | meta_with_itemprop]) nullary

(** {3 Style Sheets} *)

val style :
Expand Down Expand Up @@ -1169,7 +1191,10 @@ module type T = sig
(** Same, for URI attribute *)
val uri_attrib : string -> uri wrap -> 'a attrib

(** Same, for a space separated list of values *)
(** Same, for a space-separated list of URI attributes *)
val uris_attrib : string -> uri list wrap -> 'a attrib

(** Same, for a space-separated list of values *)
val space_sep_attrib : string -> string list wrap -> 'a attrib

(** Same, for a comma separated list of values *)
Expand Down
31 changes: 27 additions & 4 deletions lib/html_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,19 @@ type aria =
| `Aria
]

(** {2 Microdata} *)

type microdata =
[
| `ItemID
| `ItemProp
| `ItemRef
| `ItemScope
| `ItemType
]

(** Common attributes *)
type common = [ | core | i18n | events | aria ]
type common = [ | core | i18n | events | aria | microdata ]

(** {1 Categories of HTML elements}

Expand Down Expand Up @@ -571,6 +582,7 @@ type core_phrasing =
| `Img | `Img_interactive
| `Picture
| `PCDATA
| `Meta_with_itemprop
]

type core_phrasing_without_noscript =
Expand Down Expand Up @@ -611,6 +623,7 @@ type core_phrasing_without_noscript =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
]
type core_phrasing_without_interactive =
[
Expand Down Expand Up @@ -646,6 +659,7 @@ type core_phrasing_without_interactive =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
]

type core_phrasing_without_media =
Expand Down Expand Up @@ -686,6 +700,7 @@ type core_phrasing_without_media =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
]

type phrasing_without_noscript =
Expand Down Expand Up @@ -722,9 +737,10 @@ type (+'a, +'b) between_phrasing_and_phrasing_without_interactive =
phrasing,
phrasing_without_media) transparent
> `Abbr `B `Bdo `Br `Canvas `Cite `Code `Command
`Datalist `Del `Dfn `Em `I `Img `Picture `Ins `Kbd `Map `Mark `Meter
`Noscript `Object `PCDATA `Progress `Q `Ruby `Samp `Script
`Small `Span `Strong `Sub `Sup `Svg `Template `Time `U `Var `Wbr ] as 'a)
`Datalist `Del `Dfn `Em `I `Img `Picture `Ins `Kbd `Map `Mark
`Meta_with_itemprop `Meter `Noscript `Object `PCDATA `Progress `Q `Ruby
`Samp `Script `Small `Span `Strong `Sub `Sup `Svg `Template `Time `U
`Var `Wbr ] as 'a)

(** Phrasing without the interactive markups *)
type phrasing_without_dfn =
Expand Down Expand Up @@ -761,6 +777,7 @@ type phrasing_without_dfn =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
| (phrasing_without_interactive, phrasing_without_noscript,
phrasing_without_dfn, phrasing_without_media) transparent
]
Expand Down Expand Up @@ -799,6 +816,7 @@ type phrasing_without_label =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
| (phrasing_without_interactive, phrasing_without_noscript,
phrasing_without_label, phrasing_without_media) transparent
]
Expand Down Expand Up @@ -840,6 +858,7 @@ type phrasing_without_progress =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
| (phrasing_without_interactive, phrasing_without_noscript,
phrasing_without_progress, phrasing_without_media) transparent
]
Expand Down Expand Up @@ -878,6 +897,7 @@ type phrasing_without_time =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
| (phrasing_without_interactive, phrasing_without_noscript,
phrasing_without_time, phrasing_without_media) transparent
]
Expand Down Expand Up @@ -919,6 +939,7 @@ type phrasing_without_meter =
| `B
| `Abbr
| `PCDATA
| `Meta_with_itemprop
| (phrasing_without_interactive, phrasing_without_noscript,
phrasing_without_meter, phrasing_without_media) transparent
]
Expand Down Expand Up @@ -2276,6 +2297,8 @@ type noscript_attrib = [ | common ]
(* NAME: meta, KIND: nullary, TYPE: [= common | `Http_equiv | `Name | `Content | `Charset ], [=`Meta], ARG: notag, ATTRIB: OUT: [=`Meta] *)
type meta = [ | `Meta ]

type meta_with_itemprop = [ | `Meta_with_itemprop ]

type meta_content = notag

type meta_content_fun = notag
Expand Down
1 change: 1 addition & 0 deletions syntax/reflect/reflect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ let rec to_attribute_parser lang name ~loc = function
| [[%type: shape]] ->
[%expr variant]

| [[%type: Xml.uri list]]
| [[%type: nmtokens]]
| [[%type: idrefs]]
| [[%type: charsets]]
Expand Down
20 changes: 20 additions & 0 deletions test/test_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,26 @@ let attribs = "ppx attribs", HtmlTests.make Html.[
[[%html "<div aria-hidden=true></div>"]],
[div ~a:[a_aria "hidden" ["true"]] []] ;

"microdata attributes",
[[%html "<dl itemscope itemtype='https://md.example.com/track https://md.example.com/lighting'><dt>Name</dt><dd itemprop='name'>Turnout Lantern Kit</dd><dt>Purpose<dd>For retrofitting 2 <span itemprop='track-type'>C</span> Track turnouts.<meta itemprop='scale' content='HO'></dl>"]],
[dl
~a: [
a_itemscope ();
a_itemtype ["https://md.example.com/track"; "https://md.example.com/lighting"];
]
[
dt [txt "Name"];
dd ~a: [a_itemprop "name"] [txt "Turnout Lantern Kit"];
dt [txt "Purpose"];
dd
[
txt "For retrofitting 2 ";
span ~a: [a_itemprop "track-type"] [txt "C"];
txt " Track turnouts.";
meta_itemprop "scale" ~a: [a_content "HO"] ();
]
]];

"touch events",
[[%html "<div ontouchstart='alert()'></div>"]],
[div ~a:[a_ontouchstart "alert()"] []] ;
Expand Down