Skip to content

Static HTML Template helper#14623

Merged
jridgewell merged 11 commits intoampproject:masterfrom
jridgewell:static-template
Apr 18, 2018
Merged

Static HTML Template helper#14623
jridgewell merged 11 commits intoampproject:masterfrom
jridgewell:static-template

Conversation

@jridgewell
Copy link
Copy Markdown
Contributor

@jridgewell jridgewell commented Apr 13, 2018

Create static HTML DOM trees using a HTML like syntax. Leading/trailing text (including whitespace) is dropped, and only a single root element in the template is supported.

This is only available for static trees. Dynamic variables in the string are not permitted, and will result in linting errors and runtime errors.

Valid uses are:

// Yes, the variable name must be "html".
const html = htmlFor(doc || el);

const div = html`<div><p>Hello World</p></div>`;

Or, you can use it directly as the template literal tag:

const div = htmlFor(doc || el)`<div><p>Hello World</p></div>`;

Closes #14529, jump starts #13906.
/cc @alanorozco, @kristoferbaxter

Copy link
Copy Markdown
Contributor

@kristoferbaxter kristoferbaxter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few questions, I don't understand why known arguments at tagged template literal creation time cannot be included.

Other than that, LGTM

this.win.document.documentElement.classList.add('i-amphtml-ios-embed');
const documentElement = this.win.document.documentElement;
const topClasses = documentElement.className;
documentElement.className = 'i-amphtml-ios-embed';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clean up.

I'm curious why className was set to an empty value and the classList was added to afterward in the previous code. Is there a subtle behaviour specific to iOS?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<img class="i-amphtml-intrinsic-sizer" />
</i-amphtml-sizer>`;
const intrinsicSizer = sizer.firstElementChild;
intrinsicSizer.setAttribute('src',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I am missing something. Why can't this attribute be included in the template literal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The src requires dynamic height and width, which aren't statically analyzable.

preload.setAttribute('rel', 'preload');
const preload = htmlFor(this.document_)`
<link rel="preload" referrerpolicy="origin" />`;
preload.setAttribute('href', url);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here, is there a reason the url cannot be included here in the tagged template literal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The url is not statically analyzable, so there's not guarantee that it's not malicious.

@alanorozco
Copy link
Copy Markdown
Member

/cc @newmuis @Enriqe @gmajoulet for amp-story.

IMO this should be used instead of SimpleTemplate where the templates are static.

Dynamic templating still unsolved.

@newmuis
Copy link
Copy Markdown
Contributor

newmuis commented Apr 16, 2018

@alanorozco Thanks to our naive localization approach, pretty much none of our templates are static anymore (translated strings get swapped in at runtime) ☹️

@jridgewell
Copy link
Copy Markdown
Contributor Author

(translated strings get swapped in at runtime)

Using the html helper is still a better approach here:

diff --git a/extensions/amp-story/0.1/amp-story.js b/extensions/amp-story/0.1/amp-story.js
--- a/extensions/amp-story/0.1/amp-story.js
+++ b/extensions/amp-story/0.1/amp-story.js
  * Container for "pill-style" share widget, rendered on desktop.
  * @private @const {!./simple-template.ElementDef}
  */
-const SHARE_WIDGET_PILL_CONTAINER = {
-  tag: 'div',
-  attrs: dict({'class': 'i-amphtml-story-share-pill'}),
-  children: [
-    {
-      tag: 'span',
-      attrs: dict({'class': 'i-amphtml-story-share-pill-label'}),
-      localizedStringId:
-          LocalizedStringId.AMP_STORY_SYSTEM_LAYER_SHARE_WIDGET_LABEL,
-    },
-  ],
-};
+const shareWidgetPillContainer = html`
+    <div class="i-amphtml-story-share-pill">
+      <span class="i-amphtml-story-share-pill-label"></span>
+    </div>`
+shareWidgetPillContainer.querySelector('i-amphtml-story-share-pill-label')
+    .innerText = LocalizedStringId.AMP_STORY_SYSTEM_LAYER_SHARE_WIDGET_LABEL;

@jridgewell
Copy link
Copy Markdown
Contributor Author

jridgewell commented Apr 17, 2018

Size Build Dist
Pre: 334.31KB 76.6KB
Post: 335.54KB 76.84KB
After #14657 and #14658 76.72KB

@googlebot
Copy link
Copy Markdown

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

jridgewell and others added 8 commits April 17, 2018 19:34
Create static HTML DOM trees using a HTML like syntax.

This is **only** available for static trees. **Dynamic variables in the string are not permitted**.
@jridgewell jridgewell merged commit 333a47c into ampproject:master Apr 18, 2018
@jridgewell jridgewell deleted the static-template branch April 18, 2018 00:57
glevitzky pushed a commit that referenced this pull request Apr 27, 2018
* Static HTML Template helper

Create static HTML DOM trees using a HTML like syntax.

This is **only** available for static trees. **Dynamic variables in the string are not permitted**.

* Typecast

* Update fixed position tests

* Do not create HTML elements using html helper

* Insensitive lint

* lint

* Add htmlRefs function

* Bump max filesize

* Tests

* Revert changes to eslint-plugin-amphtml-internal version

* Lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants