Skip to content
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

Refactor: move getRealChildNodes and getRealChildren to core #34813

Merged
merged 6 commits into from
Jun 15, 2021
Merged
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
3 changes: 2 additions & 1 deletion builtins/amp-layout/amp-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {BaseElement} from '../../src/base-element';
import {Layout, applyFillContent, isLayoutSizeDefined} from '#core/dom/layout';
import {realChildNodes} from '#core/dom/query';
import {registerElement} from '#service/custom-element-registry';

class AmpLayout extends BaseElement {
Expand All @@ -36,7 +37,7 @@ class AmpLayout extends BaseElement {
}
const container = this.win.document.createElement('div');
applyFillContent(container);
this.getRealChildNodes().forEach((child) => {
realChildNodes(this.element).forEach((child) => {
container.appendChild(child);
});
this.element.appendChild(container);
Expand Down
2 changes: 1 addition & 1 deletion docs/building-an-amp-extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ The AMP runtime will not manage scheduling layouting for elements that have
owners.

```javascript
this.cells_ = this.getRealChildren();
this.cells_ = realChildElements(this.element);

this.cells_.forEach((cell) => {
Services.ownersForDoc(this.element).setOwner(cell, this.element);
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-accordion/0.1/amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {Layout} from '#core/dom/layout';
import {Services} from '#service';
import {bezierCurve} from '#core/data-structures/curve';
import {clamp} from '#core/math';
import {closest} from '#core/dom/query';
import {closest, realChildElements} from '#core/dom/query';
import {createCustomEvent} from '../../../src/event-helper';
import {dev, devAssert, user, userAssert} from '../../../src/log';
import {dict} from '#core/types/object';
Expand Down Expand Up @@ -89,7 +89,7 @@ class AmpAccordion extends AMP.BaseElement {
this.sessionId_ = this.getSessionStorageKey_();
this.currentState_ = this.getSessionState_();

this.sections_ = this.getRealChildren();
this.sections_ = realChildElements(this.element);
this.sections_.forEach((section, index) => {
userAssert(
section.tagName.toLowerCase() == 'section',
Expand Down
7 changes: 5 additions & 2 deletions extensions/amp-audio/0.1/amp-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
} from '../../../src/mediasession-helper';
import {Layout, applyFillContent, isLayoutSizeFixed} from '#core/dom/layout';
import {assertHttpsUrl} from '../../../src/url';
import {closestAncestorElementBySelector} from '#core/dom/query';
import {
closestAncestorElementBySelector,
realChildNodes,
} from '#core/dom/query';
import {dev, user} from '../../../src/log';
import {getMode} from '../../../src/mode';
import {listen} from '../../../src/event-helper';
Expand Down Expand Up @@ -135,7 +138,7 @@ export class AmpAudio extends AMP.BaseElement {
);

applyFillContent(audio);
this.getRealChildNodes().forEach((child) => {
realChildNodes(this.element).forEach((child) => {
if (child.getAttribute && child.getAttribute('src')) {
assertHttpsUrl(child.getAttribute('src'), dev().assertElement(child));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import '../amp-call-tracking';
import {Services} from '#service';
import {clearResponseCacheForTesting} from '../amp-call-tracking';
import {realChildElements} from '#core/dom/query';

describes.realWin(
'amp-call-tracking',
Expand Down Expand Up @@ -77,7 +78,7 @@ describes.realWin(
}

function expectHyperlinkToBe(callTrackingEl, href, textContent) {
const hyperlink = callTrackingEl.getRealChildren()[0];
const hyperlink = realChildElements(callTrackingEl)[0];

expect(hyperlink.getAttribute('href')).to.equal(href);
expect(hyperlink.textContent).to.equal(textContent);
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-carousel/0.1/scrollable-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
observeWithSharedInOb,
unobserveWithSharedInOb,
} from '../../../src/viewport-observer';
import {realChildElements} from '#core/dom/query';

/** @const {string} */
const TAG = 'amp-scrollable-carousel';
Expand Down Expand Up @@ -59,7 +60,7 @@ export class AmpScrollableCarousel extends BaseCarousel {

/** @override */
buildCarousel() {
this.cells_ = this.getRealChildren();
this.cells_ = realChildElements(this.element);

this.container_ = this.element.ownerDocument.createElement('div');
this.container_.classList.add('i-amphtml-scrollable-carousel-container');
Expand Down
7 changes: 5 additions & 2 deletions extensions/amp-carousel/0.1/slidescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {BaseSlides} from './base-slides';
import {Keys} from '#core/constants/key-codes';
import {Services} from '#service';
import {bezierCurve} from '#core/data-structures/curve';
import {closestAncestorElementBySelector} from '#core/dom/query';
import {
closestAncestorElementBySelector,
realChildElements,
} from '#core/dom/query';
import {createCustomEvent, listen} from '../../../src/event-helper';
import {dev, user} from '../../../src/log';
import {dict} from '#core/types/object';
Expand Down Expand Up @@ -174,7 +177,7 @@ export class AmpSlideScroll extends BaseSlides {

this.element.classList.add('i-amphtml-slidescroll');

this.slides_ = this.getRealChildren();
this.slides_ = realChildElements(this.element);

this.noOfSlides_ = this.slides_.length;

Expand Down
7 changes: 5 additions & 2 deletions extensions/amp-carousel/0.2/amp-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {Carousel} from '../../amp-base-carousel/0.1/carousel.js';
import {CarouselEvents} from '../../amp-base-carousel/0.1/carousel-events';
import {ChildLayoutManager} from '../../amp-base-carousel/0.1/child-layout-manager';
import {Services} from '#service';
import {closestAncestorElementBySelector} from '#core/dom/query';
import {
closestAncestorElementBySelector,
realChildElements,
} from '#core/dom/query';
import {computedStyle} from '#core/dom/style';
import {createCustomEvent, getDetail, listen} from '../../../src/event-helper';
import {dev, devAssert, userAssert} from '../../../src/log';
Expand Down Expand Up @@ -142,7 +145,7 @@ class AmpCarousel extends AMP.BaseElement {
this.action_ = Services.actionServiceForDoc(this.element);

const {element, win} = this;
const slides = this.getRealChildren();
const slides = realChildElements(this.element);

element.appendChild(this.renderContainerDom_());
this.scrollContainer_ = this.element.querySelector(
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-consent/0.1/amp-consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {dict, hasOwn} from '#core/types/object';
import {getData} from '../../../src/event-helper';
import {getServicePromiseForDoc} from '../../../src/service-helpers';
import {isArray, isEnumValue, isObject} from '#core/types';
import {realChildElements} from '#core/dom/query';

import {isExperimentOn} from '#experiments';

Expand Down Expand Up @@ -223,7 +224,7 @@ export class AmpConsent extends AMP.BaseElement {
/** @type {string} */ (this.consentId_)
);

const children = this.getRealChildren();
const children = realChildElements(this.element);
for (let i = 0; i < children.length; i++) {
const child = children[i];
toggle(child, false);
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-fit-text/0.1/amp-fit-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isLayoutSizeDefined,
} from '#core/dom/layout';
import {px, setStyle, setStyles} from '#core/dom/style';
import {realChildNodes} from '#core/dom/query';
import {throttle} from '#core/types/function';

const TAG = 'amp-fit-text';
Expand Down Expand Up @@ -90,7 +91,7 @@ class AmpFitText extends AMP.BaseElement {
lineHeight: `${LINE_HEIGHT_EM_}em`,
});

this.getRealChildNodes().forEach((node) => {
realChildNodes(this.element).forEach((node) => {
this.contentWrapper_.appendChild(node);
});
this.updateMeasurerContent_();
Expand Down
5 changes: 3 additions & 2 deletions extensions/amp-fx-flying-carpet/0.1/amp-fx-flying-carpet.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {CommonSignals} from '#core/constants/common-signals';
import {Layout} from '#core/dom/layout';
import {Services} from '#service';
import {dev, userAssert} from '../../../src/log';
import {realChildElements, realChildNodes} from '#core/dom/query';
import {setStyle} from '#core/dom/style';

const TAG = 'amp-fx-flying-carpet';
Expand Down Expand Up @@ -75,10 +76,10 @@ export class AmpFlyingCarpet extends AMP.BaseElement {
const doc = this.element.ownerDocument;
const container = doc.createElement('div');

this.children_ = this.getRealChildren();
this.children_ = realChildElements(this.element);
this.container_ = container;

const childNodes = this.getRealChildNodes();
const childNodes = realChildNodes(this.element);
this.totalChildren_ = this.visibileChildren_(childNodes).length;

const owners = Services.ownersForDoc(this.element);
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-image-slider/0.1/amp-image-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
observeWithSharedInOb,
unobserveWithSharedInOb,
} from '../../../src/viewport-observer';
import {realChildElements} from '#core/dom/query';
import {setStyles} from '#core/dom/style';

export class AmpImageSlider extends AMP.BaseElement {
Expand Down Expand Up @@ -110,7 +111,7 @@ export class AmpImageSlider extends AMP.BaseElement {

/** @override */
buildCallback() {
const children = this.getRealChildren();
const children = realChildElements(this.element);

for (let i = 0; i < children.length; i++) {
const child = children[i];
Expand Down
8 changes: 6 additions & 2 deletions extensions/amp-image-viewer/0.1/amp-image-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {Services} from '#service';
import {WindowInterface} from '#core/window/interface';
import {bezierCurve} from '#core/data-structures/curve';
import {boundValue, distance, magnitude} from '#core/math';
import {closestAncestorElementBySelector, elementByTag} from '#core/dom/query';
import {
closestAncestorElementBySelector,
elementByTag,
realChildElements,
} from '#core/dom/query';
import {continueMotion} from '../../../src/motion';
import {createCustomEvent} from '../../../src/event-helper';
import {dev, userAssert} from '../../../src/log';
Expand Down Expand Up @@ -133,7 +137,7 @@ export class AmpImageViewer extends AMP.BaseElement {
/** @override */
buildCallback() {
this.element.classList.add('i-amphtml-image-viewer');
const children = this.getRealChildren();
const children = realChildElements(this.element);

userAssert(
children.length == 1,
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-lightbox/0.1/amp-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {dict, hasOwn} from '#core/types/object';
import {getMode} from '../../../src/mode';
import {htmlFor} from '#core/dom/static-template';
import {isInFie} from '../../../src/iframe-helper';
import {realChildElements} from '#core/dom/query';
import {toArray} from '#core/types/array';
import {tryFocus} from '#core/dom';
import {unmountAll} from '../../../src/utils/resource-container-helper';
Expand Down Expand Up @@ -233,7 +234,7 @@ class AmpLightbox extends AMP.BaseElement {

this.isScrollable_ = element.hasAttribute('scrollable');

const children = this.getRealChildren();
const children = realChildElements(this.element);

this.container_ = element.ownerDocument.createElement('div');
if (!this.isScrollable_) {
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-pan-zoom/0.1/amp-pan-zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
unobserveContentSize,
} from '#core/dom/size-observer';
import {px, scale, setStyles, translate} from '#core/dom/style';
import {realChildElements} from '#core/dom/query';

const PAN_ZOOM_CURVE_ = bezierCurve(0.4, 0, 0.2, 1.4);
const TAG = 'amp-pan-zoom';
Expand Down Expand Up @@ -166,7 +167,7 @@ export class AmpPanZoom extends AMP.BaseElement {
/** @override */
buildCallback() {
this.action_ = Services.actionServiceForDoc(this.element);
const children = this.getRealChildren();
const children = realChildElements(this.element);

userAssert(
children.length == 1,
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-script/0.1/amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {dict, map} from '#core/types/object';
import {getElementServiceForDoc} from '../../../src/element-service';
import {getMode} from '../../../src/mode';
import {getService, registerServiceBuilder} from '../../../src/service-helpers';
import {realChildElements} from '#core/dom/query';
import {rewriteAttributeValue} from '../../../src/url-rewrite';
import {tryParseJson} from '#core/types/object/json';
import {urls} from '../../../src/config';
Expand Down Expand Up @@ -236,7 +237,7 @@ export class AmpScript extends AMP.BaseElement {
container = this.win.document.createElement('div');
applyFillContent(container, /* replacedContent */ true);
// Reparent all real children to the container.
const realChildren = this.getRealChildren();
const realChildren = realChildElements(this.element);
for (let i = 0; i < realChildren.length; i++) {
container.appendChild(realChildren[i]);
}
Expand Down
9 changes: 6 additions & 3 deletions extensions/amp-sidebar/0.1/amp-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {Keys} from '#core/constants/key-codes';
import {Services} from '#service';
import {SwipeDef, SwipeXRecognizer} from '../../../src/gesture-recognizers';
import {Toolbar} from './toolbar';
import {closestAncestorElementBySelector} from '#core/dom/query';
import {
closestAncestorElementBySelector,
realChildElements,
} from '#core/dom/query';
import {createCustomEvent} from '../../../src/event-helper';
import {debounce} from '#core/types/function';
import {descendsFromStory} from '../../../src/utils/story';
Expand Down Expand Up @@ -452,7 +455,7 @@ export class AmpSidebar extends AMP.BaseElement {
*/
updateForOpened_(trust) {
// On open sidebar
const children = this.getRealChildren();
const children = realChildElements(this.element);
const owners = Services.ownersForDoc(this.element);
owners.scheduleLayout(this.element, children);
owners.scheduleResume(this.element, children);
Expand Down Expand Up @@ -502,7 +505,7 @@ export class AmpSidebar extends AMP.BaseElement {
toggle(this.getMaskElement_(), /* display */ false);
Services.ownersForDoc(this.element).schedulePause(
this.element,
this.getRealChildren()
realChildElements(this.element)
);
this.triggerEvent_(SidebarEvents.CLOSE, trust);

Expand Down
9 changes: 6 additions & 3 deletions extensions/amp-sidebar/0.2/amp-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {Keys} from '#core/constants/key-codes';
import {Services} from '#service';
import {SwipeDef, SwipeXRecognizer} from '../../../src/gesture-recognizers';
import {Toolbar} from './toolbar';
import {closestAncestorElementBySelector} from '#core/dom/query';
import {
closestAncestorElementBySelector,
realChildElements,
} from '#core/dom/query';
import {createCustomEvent} from '../../../src/event-helper';
import {debounce} from '#core/types/function';
import {descendsFromStory} from '../../../src/utils/story';
Expand Down Expand Up @@ -450,7 +453,7 @@ export class AmpSidebar extends AMP.BaseElement {
*/
updateForOpened_(trust) {
// On open sidebar
const children = this.getRealChildren();
const children = realChildElements(this.element);
const owners = Services.ownersForDoc(this.element);
owners.scheduleLayout(this.element, children);
owners.scheduleResume(this.element, children);
Expand Down Expand Up @@ -501,7 +504,7 @@ export class AmpSidebar extends AMP.BaseElement {
toggle(this.getMaskElement_(), /* display */ false);
Services.ownersForDoc(this.element).schedulePause(
this.element,
this.getRealChildren()
realChildElements(this.element)
);
// TODO(#25080): update history manipulation based on resolution of this issue.
if (this.historyId_ != -1) {
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-sidebar/1.0/base-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {PreactBaseElement} from '#preact/base-element';
import {Sidebar} from './component';
import {dict} from '#core/types/object';
import {pauseAll} from '../../../src/utils/resource-container-helper';
import {realChildNodes} from '#core/dom/query';
import {toggle} from '#core/dom/style';
import {toggleAttribute} from '#core/dom';
import {useToolbarHook} from './sidebar-toolbar-hook';
Expand Down Expand Up @@ -50,7 +51,7 @@ export class BaseElement extends PreactBaseElement {

/** @override */
updatePropsForRendering(props) {
this.getRealChildNodes().map((child) => {
realChildNodes(this.element).map((child) => {
if (
child.nodeName === 'NAV' &&
child.hasAttribute('toolbar') &&
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-sticky-ad/1.0/amp-sticky-ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
toggle,
} from '#core/dom/style';
import {dev, user, userAssert} from '../../../src/log';
import {realChildElements} from '#core/dom/query';
import {removeElement} from '#core/dom';
import {whenUpgradedToCustomElement} from '../../../src/amp-element-helpers';

Expand Down Expand Up @@ -59,7 +60,7 @@ class AmpStickyAd extends AMP.BaseElement {
this.viewport_ = this.getViewport();
this.element.classList.add('i-amphtml-sticky-ad-layout');

const children = this.getRealChildren();
const children = realChildElements(this.element);
userAssert(
children.length == 1 && children[0].tagName == 'AMP-AD',
'amp-sticky-ad must have a single amp-ad child'
Expand Down
Loading