Defining a "macro" in HTML? - html

In LaTeX, we can define a macro so that we don't have to keep typing the same thing all the time.
For my website, which is HTML + CSS, I'm constantly repeating a custom "separator":  / .
Can I define a macro so that it expands to this string?
Surprised to have found nothing online about this. I guess it's because "macro" means a different thing in HTML?

You might use SGML entities, but it is not a standard compliant solution and it only works when serving the file as XHTML:
<?xml version="1.0"?>
<!DOCTYPE html [
<!ENTITY name "John">
<!ENTITY surname "Doe">
<!ENTITY fullname "&name; &surname;">
]>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test</title></head>
<body>Hello, my name is &fullname;.</body>
</html>
As already noticed in other comments, use something else like JavaScript or XSLT.

Related

How can I put a bullet in a iso-8859-15 HTML document in a pre tag?

I want to put a bullet in a pre tag, without using any special html characters.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<pre>
bullet here!
</pre>
</body>
</html>
I have tried pasting various bullets in but they all show up as question marks or foreign characters.
The <pre> tag does not affect the way HTML behaves beyond white space collapsing. Everything that works in a <div> should work in <pre>. For instance:
&bullet; // Prints •
You're probably typing raw UTF-8 characters, thus the misprints (your doc uses ISO-8859-15, not UTF-8).
If client doesn't want neither UTF-8 nor HTML entities he simply cannot have bullets: ISO-8859-15 has around 191 printable characters that include *, º or · but not anything that's technically a bullet. If you want to type raw characters you must choose one that belongs to ISO-8859-15.
IMHO, the requirement is kind of weird. XML and HTML are different things. It's possible that you're simply not escaping text properly when you generate your XML. All decent XML libraries take care of that automatically but if you're using simple string contacatenation you should be doing it yourself. E.g.:
<this-is-xml>
<![CDATA[
<p>This is no longer XML</p>
]]>
</this-is-xml>

Does HTML5 requires spaces between attributes that are of quoted values?

HTML does normally allow to have no spaces between attributes when attributes have values and those values are quoted.
Example (Reference/Source):
In HTML-documents no White Spaces between Attributes are needed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>no attribute space</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p class="CLASS"title='TITLE'></p>
</body>
</html>
See the third-last line:
<p class="CLASS"title='TITLE'></p>
^^
Now using such HTML chunk changing the doctype to HTML 5 (<!DOCTYPE HTML>), makes the experimental W3C HTML 5 conformance checker give an error exactly there telling me:
Validation Output: 1 Error
Error Line 9, Column 22: No space between attributes.
<p class="CLASS"title='TITLE'></p>
^
So I thought that HTML 5 is backwards compatible to how browsers deal with HTML in reality and browsers AFAIK deal with this well. So I'm a bit puzzeled at least. I also have problems to decipher the (somewhat needlessly) compilcated HTML 5 specs to be precise at this point because what I did find (W3C again, see http://www.w3.org/TR/html-markup/syntax.html#syntax-attributes) it's not saying that this is (may nor must) be an error.
You are reading a discontinued, non-normative reference. If you look at the definition of the start tag in the specification (which is normative) it says:
Then, the start tag may have a number of attributes, the syntax for which is described below. Attributes must be separated from each other by one or more space characters.
So I thought that HTML 5 is backwards compatible to how browsers deal with HTML in reality and browsers AFAIK deal with this well.
Being compatible with real world markup is a design goal, but lots of things have been obsoleted and leaving out the space between attributes is something that almost never occurs intentionally.
Section 4.3, "Elements" of the document you link in the question says:
Optionally, one or more attributes, each of which must be preceded by
one or more space characters.
Usin the W3C Official HTML Validator, having no spaces between attributes are checked as errors if you use the HTML5 Doctype:
<!DOCTYPE html>
The output message is the following:
Line 9, Column 23: No space between attributes.

Breaking multiple values in an attribute into multiple lines?

Let me explain by example:
<html lang="en-US" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
...
</html>
As you can see, the prefix attribute in the html tag has multiple definitions. How do I break them into multiple lines? (Considering that a line break is equivalent a space when minified back into a single line... it's kinda tough.)
Is this considered normal?
<html lang="en-US" prefix="
og: http://ogp.me/ns#
fb: http://ogp.me/ns/fb#
article: http://ogp.me/ns/article#
">
EDIT: Facebook does it like this: https://developers.facebook.com/docs/payments/product/
<html lang="en-US" prefix=
"og: http://ogp.me/ns#
fb: http://ogp.me/ns/fb#
article: http://ogp.me/ns/article#">
The attribute values are different. Each whitespace character is stored in the DOM. Whether the difference matters depends on the definition of the attribute. Many attributes, such as class, are defined as taking a set of whitespace-separated tokens as value, and for them, the amount and type of whitespace characters between tokens, or before the first token and after the last token, does not matter.
The prefix attribute is not present in HTML specifications or drafts. The relevant specification is RDFa Core 1.1, which defines the prefix attribute as
“a white space separated list of prefix-name IRI pairs” and contains examples like
<html
xmlns="http://www.w3.org/1999/xhtml"
prefix="foaf: http://xmlns.com/foaf/0.1/
dc: http://purl.org/dc/terms/"
>
So for the prefix attribute, formatting as in the question is acceptable. (Whether it is “normal” in a sense other than “conforming” is a matter of opinion.)
I don't think it's all that "normal". In general, like the comments to your question suggest, it's technically possible but you're opening your page up to (unnecessary) potential parsing errors.
Look to the HTML WG's example regarding using newlines in the title attribute as a concrete example of this.
Furthermore, I was unable to find/remember a single case where I'd seen this used on purpose, with the exception of SVG (but that's not technically HTML).
However, if you run this sample through the W3C's validator, it'll pass with no errors or warnings in regards to multi-line attributes:
<!DOCTYPE html>
<html lang="en-US" prefix="
og: http://ogp.me/ns#
fb: http://ogp.me/ns/fb#
article: http://ogp.me/ns/article#
">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Generally, it's better to be safe than sorry. Since I couldn't find any examples to the contrary in this case, I'd venture to say that other developers would agree (Do by all means correct me if I'm wrong).

jspx: output DOCTYPE with only a PUBLIC identifier

I want to output the following DOCTYPE specifier with jspx:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
This seems to be impossible. What I tried is:
<jsp:output doctype-root-element="HTML" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
...which results in:
org.apache.jasper.JasperException: /WEB-INF/layouts/fittopage.jspx(3,100) <jsp:output>: 'doctype-root-element' and 'doctype-system' attributes must appear together
(I'm using Tomcat 6.0). I don't want to include a SYSTEM identifier containing the DTD's URI (in this case doctype-system="http://www.w3.org/TR/html4/loose.dtd") because that reproducibly causes browsers (Firefox and Chrome) to render the page differently (or not at all).
Re: Garret Wilson's question: "So how do I output <!DOCTYPE html> for HTML 5 with the JSP document (JSPX) syntax? Is it even possible?"
This can be done with jsp:text. (It cannot be done with jsp:output).
The current version of JSP specification is JavaServer Pages Specification Version 2.3. (Implemented in current Java EE 8 (Tomcat 9.x) as well as previous Java EE 7 (Tomcat 8.x)). Quoting from chapter JSP.5.6 "<jsp:output\>":
The doctype-root-element, doctype-system and doctype-public properties allow the page author to specify that a DOCTYPE be automatically generated in the XML prolog of the output. Without these properties, the DOCTYPE would need to be output manually via a <jsp:text> element before the root element of the JSP document, which is inconvenient.
A DOCTYPE must be automatically output if and only if the doctype-system
element appears in the translation unit as part of a <jsp:output> action. The doctype-root-element must appear and must only appear if the doctype-system property appears, or a translation error must occur. The doctype-public property is optional, but must not appear unless the doctype-system property appears, or a translation error must occur.
Until somebody asks the specification committee for more flexibility here, and a new version of specification comes out, this is all that we have.
An example using jsp:text:
<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<jsp:directive.page contentType="text/html" />
<jsp:text><!DOCTYPE html></jsp:text>
<html lang="en">
<head>
<meta charset="${pageContext.response.characterEncoding}"/>
<title>Hello world</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
</jsp:root>

html:title in pure XML document

I decided to use pure XML+CSS instead of (X)HTML for my webpage.
I use and with no problems.
However I can't specify webpage's title.
Document looks like this:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/css" href="style.css"?>
<html:html xmlns:html="http://www.w3.org/1999/xhtml">
<html:head>
<html:title>FooBar</html:title>
</html:head>
<site>
<header>foo</header>
<article>
<title>foo</title>
<p>foo bar <html:a href="#">foobar</html:a></p>
</article>
</site>
</html:html>
But it doesn't work.
Update: it works on Chrome and Internet Explorer. Doesn't work on Firefox.
Why don't you leave xhtml as it is and use another namespace with an alias for your custom tags?
Edit
I mean something like:-
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:a="urn:mycrop.com:mystuff">
<head>
<title>FooBar</title>
</head>
<a:site>
<a:header>foo</a:header>
<a:article>
<a:title>foo</a:title>
<p>foo bar foobar</p>
</a:article>
</a:site>
</html>
Of course this will fail if the browser strictly expects XHTML but that was true of your originally anyway.
From your comments elsewhere in this question it looks like you've gone for XSLT as a stylesheet anyway. That being the case you needn't bother with including any of the HTML stuff anyway, just have the source XML contain the data you want to display.
OK, I used XSLT to do this. Works like a charm on every browser.