Skip to content

Commit 8cf75d3

Browse files
committed
Coreify specific layout.js, CE, and BE functinos
1 parent 2ee75c4 commit 8cf75d3

File tree

5 files changed

+94
-48
lines changed

5 files changed

+94
-48
lines changed

src/base-element.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import {ActionTrust, DEFAULT_ACTION} from './core/constants/action-constants';
1818
import {Layout, LayoutPriority} from './layout';
1919
import {Services} from './services';
20+
import {applyFillContent, dispatchCustomEvent} from './core/dom';
2021
import {devAssert, user, userAssert} from './log';
21-
import {dispatchCustomEvent} from './core/dom';
2222
import {getData, listen, loadPromise} from './event-helper';
2323
import {getMode} from './mode';
2424
import {isArray} from './core/types';
@@ -869,22 +869,16 @@ export class BaseElement {
869869
}
870870

871871
/**
872-
* Configures the supplied element to have a "fill content" layout. The
873-
* exact interpretation of "fill content" depends on the element's layout.
874-
*
875-
* If `opt_replacedContent` is specified, it indicates whether the "replaced
876-
* content" styling should be applied. Replaced content is not allowed to
877-
* have its own paddings or border.
872+
* See src/core/dom.js.applyFillContent for full description.
873+
* TODO: migrate usages to the fn in dom.js.
874+
* @deprecated
878875
*
879876
* @param {!Element} element
880877
* @param {boolean=} opt_replacedContent
881878
* @public @final
882879
*/
883880
applyFillContent(element, opt_replacedContent) {
884-
element.classList.add('i-amphtml-fill-content');
885-
if (opt_replacedContent) {
886-
element.classList.add('i-amphtml-replaced-content');
887-
}
881+
applyFillContent(element, opt_replacedContent);
888882
}
889883

890884
/**

src/core/dom/index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
import {assertElement} from '../assert/base';
18+
import {childNodes, matches} from './query';
1719
import {dict} from '../types/object';
18-
import {matches} from './query';
20+
import {isInternalOrServiceNode} from '../layout';
1921
import {toWin} from '../window';
2022

2123
const HTML_ESCAPE_CHARS = {
@@ -536,3 +538,37 @@ export function dispatchCustomEvent(node, name, opt_data, opt_options) {
536538
export function containsNotSelf(parent, child) {
537539
return child !== parent && parent.contains(child);
538540
}
541+
542+
/**
543+
* Configures the supplied element to have a "fill content" layout. The
544+
* exact interpretation of "fill content" depends on the element's layout.
545+
*
546+
* If `opt_replacedContent` is specified, it indicates whether the "replaced
547+
* content" styling should be applied. Replaced content is not allowed to
548+
* have its own paddings or border.
549+
*
550+
* @param {!Element} element
551+
* @param {boolean=} opt_replacedContent
552+
* @public @final
553+
*/
554+
export function applyFillContent(element, opt_replacedContent) {
555+
element.classList.add('i-amphtml-fill-content');
556+
if (opt_replacedContent) {
557+
element.classList.add('i-amphtml-replaced-content');
558+
}
559+
}
560+
561+
/**
562+
* Returns the original nodes of the custom element without any service
563+
* nodes that could have been added for markup. These nodes can include
564+
* Text, Comment and other child nodes.
565+
*
566+
* @param {!Element} element
567+
* @return {!Array<!Node>}
568+
*/
569+
export function getRealChildNodes(element) {
570+
return childNodes(
571+
element,
572+
(node) => !isInternalOrServiceNode(assertElement(node))
573+
);
574+
}

src/core/layout.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Whether the tag is an internal (service) AMP tag.
19+
* @param {!Element|string} tag
20+
* @return {boolean}
21+
*/
22+
function isInternalElement(tag) {
23+
const tagName = typeof tag == 'string' ? tag : tag.tagName;
24+
return !!(tagName && tagName.toLowerCase().startsWith('i-'));
25+
}
26+
27+
/**
28+
* Returns "true" for internal AMP nodes or for placeholder elements.
29+
* @param {!Element} node
30+
* @return {boolean}
31+
*/
32+
export function isInternalOrServiceNode(node) {
33+
if (isInternalElement(node)) {
34+
return true;
35+
}
36+
if (
37+
node.tagName &&
38+
(node.hasAttribute('placeholder') ||
39+
node.hasAttribute('fallback') ||
40+
node.hasAttribute('overflow'))
41+
) {
42+
return true;
43+
}
44+
return false;
45+
}

src/custom-element.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
Layout,
2424
LayoutPriority,
2525
applyStaticLayout,
26-
isInternalElement,
2726
isLoadingAllowed,
2827
} from './layout';
2928
import {MediaQueryProps} from './core/dom/media-query-props';
@@ -47,6 +46,7 @@ import {getIntersectionChangeEntry} from './utils/intersection-observer-3p-host'
4746
import {getMode} from './mode';
4847
import {getSchedulerForDoc} from './service/scheduler';
4948
import {isExperimentOn} from './experiments';
49+
import {isInternalOrServiceNode} from './core/layout';
5050
import {rethrowAsync} from './core/error';
5151
import {setStyle} from './core/dom/style';
5252
import {shouldBlockOnConsentByMeta} from './consent';
@@ -1897,14 +1897,15 @@ function createBaseCustomElementClass(win, elementConnectedCallback) {
18971897
}
18981898

18991899
/**
1900-
* Returns the original nodes of the custom element without any service
1901-
* nodes that could have been added for markup. These nodes can include
1902-
* Text, Comment and other child nodes.
1903-
* @return {!Array<!Node>}
1900+
* See dom.getRealChildNodes for the full description.
1901+
* TODO: migrate to the fn in dom.js.
1902+
* @deprecated
19041903
* @package @final
1904+
*
1905+
* @return {!Array<!Node>}
19051906
*/
19061907
getRealChildNodes() {
1907-
return query.childNodes(this, (node) => !isInternalOrServiceNode(node));
1908+
return dom.getRealChildNodes(this);
19081909
}
19091910

19101911
/**
@@ -2171,26 +2172,6 @@ function assertNotTemplate(element) {
21712172
devAssert(!element.isInTemplate_, 'Must never be called in template');
21722173
}
21732174

2174-
/**
2175-
* Returns "true" for internal AMP nodes or for placeholder elements.
2176-
* @param {!Node} node
2177-
* @return {boolean}
2178-
*/
2179-
function isInternalOrServiceNode(node) {
2180-
if (isInternalElement(node)) {
2181-
return true;
2182-
}
2183-
if (
2184-
node.tagName &&
2185-
(node.hasAttribute('placeholder') ||
2186-
node.hasAttribute('fallback') ||
2187-
node.hasAttribute('overflow'))
2188-
) {
2189-
return true;
2190-
}
2191-
return false;
2192-
}
2193-
21942175
/**
21952176
* Creates a new custom element class prototype.
21962177
*

src/layout.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ export function isLayoutSizeFixed(layout) {
173173
return layout == Layout.FIXED || layout == Layout.FIXED_HEIGHT;
174174
}
175175

176-
/**
177-
* Whether the tag is an internal (service) AMP tag.
178-
* @param {!Node|string} tag
179-
* @return {boolean}
180-
*/
181-
export function isInternalElement(tag) {
182-
const tagName = typeof tag == 'string' ? tag : tag.tagName;
183-
return tagName && tagName.toLowerCase().startsWith('i-');
184-
}
185-
186176
/**
187177
* Parses the CSS length value. If no units specified, the assumed value is
188178
* "px". Returns undefined in case of parsing error.

0 commit comments

Comments
 (0)