Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file attributes.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(* TyXML
* http://www.ocsigen.org/tyxml
* Copyright (C) 2016 Anton Bachin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02111-1307, USA.
*)letparseloc(ns,element_name)attributes=letlanguage,(moduleReflected)=Namespace.reflectlocnsin(* For prefix ["prefix"] and attribute names ["prefix-foo"], evaluates to
[Some "foo"], otherwise evaluates to [None].
Used to parse user-data attributes (prefixed by "data-") and ARIA
attributes (prefixed by "aria-").
*)letparse_prefixedprefixname=letlength=String.lengthprefixinletis_prefixed=trylanguage=Html&&String.subname0length=prefixwithInvalid_argument_->falseinifnotis_prefixedthenNoneelseSome(String.subnamelength(String.lengthname-length))in(* Applied to each attribute. Accumulates individually labeled attributes,
such as img/src, in "labeled," and attributes passed in ~a in "regular." *)letparse_attribute(labeled,regular)((_,local_name),value)=(* Convert the markup name of the attribute to a TyXML name without regard
to renamed attributes such as "a_input_max." Renaming will be accounted
for later. *)lettyxml_name=Tyxml_name.attriblocal_nameinlettest_labeled(e,a,_)=e=element_name&&a=local_nameinlettest_blacklisted(a,_,_)=a=tyxml_nameinlettest_renamed(_,a,es)=a=local_name&&List.memelement_nameesinletunknown()=Common.errorloc"Unknown attribute in %s element: %s"(Common.langlanguage)local_namein(* Check whether this attribute is individually labeled. Parse its argument
and accumulate the attribute if so. *)matchCommon.findtest_labeledReflected.labeled_attributeswith|Some(_,label,parser)->lete=matchparserlanguageloclocal_namevaluewith|None->Common.errorloc"Internal error: labeled attribute %s without an argument"label|Somee->ein(Common.Label.labelledlabel,e)::labeled,regular|None->(* The attribute is not individually labeled, so it is passed in ~a.
First, check if the default TyXML name of this attribute collides with
the TyXML name of a renamed attribute. For example, if the language is
HTML, and this attribute has markup name "input-max" (which is
invalid), then its default TyXML name will be "a_input_max", which is a
*valid* value in TyXML. We want to avoid mapping "input-max" to
"a_input_max", because "input-max" is invalid, and because
"a_input_max" maps to "max" instead. *)ifList.existstest_blacklistedReflected.renamed_attributesthenunknown()elseletparse_prefixed_attributetagtyxml_name=letparser=tryList.assoctyxml_nameReflected.attribute_parserswithNot_found->Common.errorloc"Internal error: no parser for %s"tyxml_nameinletidentifier=Common.make~loclanguagetyxml_nameinlettag=Common.stringloctaginlete=matchparserlanguageloclocal_namevaluewith|Somee'->[%expr[%eidentifier][%etag][%ee']][@metalocloc]|None->Common.errorloc"Internal error: no expression for %s"tyxml_nameinlabeled,e::regularin(* Check if this is a "data-foo" or "aria-foo" attribute. Parse the
attribute value, and accumulate it in the list of attributes passed
in ~a. *)matchparse_prefixed"data-"local_name,parse_prefixed"aria-"local_namewith|Sometag,_->parse_prefixed_attributetag"a_user_data"|_,Sometag->parse_prefixed_attributetag"a_aria"|None,None->lettyxml_name=matchCommon.findtest_renamedReflected.renamed_attributeswith|Some(name,_,_)->name|None->tyxml_nameinletparser=tryList.assoctyxml_nameReflected.attribute_parserswithNot_found->unknown()inletidentifier=Common.make~loclanguagetyxml_nameinlete=matchparserlanguageloclocal_namevaluewith|None->identifier|Somee'->[%expr[%eidentifier][%ee']][@metalocloc]inlabeled,e::regularinletlabeled,regular=List.fold_leftparse_attribute([],[])attributesin(* If there are any attributes to pass in ~a, assemble them into a parse tree
for a list, and prefix that with the ~a label. *)ifregular=[]thenList.revlabeledelseletregular=Common.Label.labelled"a",Common.listloc(List.revregular)inList.rev(regular::labeled)